把对应的配置放到git仓库,数据库,本地文件都可以,我通常都是gitee上,仔细查看下面的其他事项
config server
1、gitee创建文件
2、pom添加配置
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
3、启动类注解
@EnableConfigServer
// 注册服务到eureka
@EnableDiscoveryClient
4、添加配置
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://gitee.com/xxxxxxxxxxxx.git
force-pull: true # 强制拉去
# search-paths: # 文件路径
# username:
# password:
server:
port: 25000
# 高可用通过eureka
eureka:
client:
serviceUrl:
defaultZone: http://localhost:15001/eureka,http://localhost:15002/eureka
5、访问
http://localhost:25000/config-dev.properties
这里访问路径有很多种方式,在源码EnvironmentController中查看
config client
1、pom
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<!-- 刷新配置使用 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 拉取eureka中config server服务 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2、启动类
@EnableDiscoveryClient
3、配置
spring:
application:
name: config-client
cloud:
config:
# uri: http://localhost:25000
discovery:
enabled: true
service-id: config-server
profile: prod
label: master
name: config
fail-fast: true
cache:
enabled: false
server:
port: 25100
myWords: ${words}
management:
security:
enabled: false
endpoints:
web:
exposure:
include: "*"
endpoint:
health:
show-details: always
eureka:
client:
serviceUrl:
defaultZone: http://localhost:15001/eureka,http://localhost:15002/eureka
4、控制器
package com.xl.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/refresh")
@RefreshScope
public class RefreshController {
@Value("${name}")
private String name;
@Value("${desc}")
private String desc;
@GetMapping("/name")
public String getName() {
return name;
}
@GetMapping("/desc")
public String getDesc() {
return desc;
}
}
其他事项
刷新配置:client http://localhost:25100/actuator/refresh
对称加密:server
- 查看是否可以加密:http://localhost:25000/encrypt/status
- 加密:http://localhost:25000/encrypt, post , body , raw ,text,帅气如我
- 解密:http://localhost:25000/decrypt, post , body , raw ,text,密文“帅气如我”
- 将密文上传到gitee上,desc: ‘{cipher}c39e’,主要需要加上单引号和{cipher},否则无效
在验证是否可以加密,需要在config server中bootstap.yml中添加配置encrypt🔑 任意字符,这个如果加载application.properties中无效,调用/encrypt/status报错,是因为加载顺序的原因,在加载主配置文件时,可能会因为还没有解密相关配置而导致配置加载错误,而bootstrap文件的先加载特性可以避免这种情况,确保加密相关的配置能够独立于其他可能依赖加密后配置的设置而被正确加载。
特别注意一点,加密,和jce有关,小于jdk1.8.0_161,需要下载替换