server 端
1、添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
2、配置文件
# git配置样例
spring:
application:
name: config-server # 模块名称
cloud:
config:
server:
git:
uri: https://github.com/ # 配置git仓库的地址
search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
username: # git仓库的账号
password: # git仓库的密码
# 本地配置样例
spring:
application:
name: config-server # 模块名称
profiles:
active:native
cloud:
config:
server:
native:
searchLocations: file:/opt/xxx # 配置文件目录
# SVN配置样例
spring:
cloud:
config:
server:
svn:
uri: http://192.168.0.6/svn/repo/config-repo
username: username
password: password
default-label: trunk
profiles:
active: subversion
3、启动
//启动类添加@EnableConfigServer,激活对配置中心的支持
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
client 端
1、添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加刷新依赖 http://localhost:8002/refresh -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
2、配置文件
server:
port:8002
# 本地样例
spring:
application:
name:config-client
cloud:
config:
fail-fast:true
discovery:
service-id:config
enabled:true
password:
username:
# git
spring:
cloud:
config:
name: #对应{application}部分
profile: #对应{profile}部分
uri: #配置中心的具体地址
label: #对应git的分支
解决高耦合
服务端改造
1、依赖添加
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
2、配置文件
#添加eureka配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址
3、启动类
//EnableDiscoveryClient 激活对注册中心的支持
@EnableDiscoveryClient
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
客户端改造
1、添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2、配置文件
spring.application.name=config-client
server.port=8002
spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.label=master
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/ #指向注册中心的地址
3、启动类
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
刷新配置
1、利用github的webhook触发机制实现
2、利用spring cloud bus 消息总线机制,操作案例如下:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
#客户端模式
## 刷新时,关闭安全验证
management.security.enabled=false
## 开启消息跟踪
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=192.168.9.89
spring.rabbitmq.port=5672
spring.rabbitmq.username=admin
spring.rabbitmq.password=123456
#服务端模式
server:
port: 8001
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: https://github.com/ # 配置git仓库的地址
search-paths: config-repo # git仓库地址下的相对地址,可以配置多个,用,分割。
username: username # git仓库的账号
password: password # git仓库的密码
rabbitmq:
host: 192.168.0.6
port: 5672
username: admin
password: 123456
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址
management:
security:
enabled: false
局部更新:/bus/refresh?destination=customers:8000
总线事件跟踪,只需设置spring.cloud.bus.trace.enabled=true
,这样在/bus/refresh
端点被请求后,访问/trace就可以了
保护敏感配置信息
1、下载并安装加密所需的Oracle JCE jar(只能到官网下载)
,下载后替换$JAVA_HOME/jre/lib/security目录中的local_policy.jar和US_export_policy.jar.
2、创建加密密钥,设置环境变量ENCRYPT_KEY
3、使用POST请求/encrypt 加密,/decrypt解密,如果进行了加解密需要在属性值前加上{cipher},例如 server.port = {cipher}8888
4、设置服务端不能解密 : spring.cloud.config.server.encrypt.enabled = false
5、在客户端添加环境变量ENCRYPT_KEY及添加pom依赖spring-security-rsa