一、通过客户端刷新,使配置文件生效
1、创建远程仓库,获取远程仓库地址(省略),application.yml
2、创建服务端工程
3、添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
4、加注解
@EnableDiscoveryClient
@EnableConfigServer //激活对配置中心的支持
@SpringBootApplication
public class SpringCloudConfigServerMqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigServerMqApplication.class, args);
}
}
5、修改配置application.properties
spring.application.name=spring-cloud-config-server
server.port=8500
spring.cloud.config.server.git.uri=https://***.git
spring.cloud.config.server.git.username=***
spring.cloud.config.server.git.password=***
eureka.client.serviceUrl.defaultZone=http://**:***/eureka/
6、请求方式
1、application-{profiles}.yml请求
http://192.168.0.131:8500/application-local.yml
http://192.168.0.131:8500/application-test.yml
http://192.168.0.131:8500/application-prod.yml
2、application/{profiles}/{lable}
http://192.168.0.131:8500/application/local/master
http://192.168.0.131:8500/application/test/master
http://192.168.0.131:8500/application/prod/master
3、{lable}/application-{profiles}.yml
http://192.168.0.131:8500/master/application-local.yml
http://192.168.0.131:8500/master/application-test.yml
http://192.168.0.131:8500/master/application-prod.yml
7、创建客户端工程
8、添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
9、加注解
@SpringBootApplication
@EnableDiscoveryClient
public class SpringCloudProviderDynastyMqApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudProviderDynastyMqApplication.class, args);
}
}
10、修改配置bootstrap.yml
spring:
application:
name: dynasty
cloud:
config:
uri: http://192.168.0.131:8500
profile: prod
label: master
management:
endpoints:
web:
exposure:
include: refresh,health,info
11、发送请求
12、修改远程仓库的值,使用postman刷新
此种刷新方式的缺点在于:配置文件有变化时,需要客户端主动刷新,当客户端有很多时,刷新不是很方便,,希望有一种方式可以通过服务端刷新
二、使用kafka通过刷新服务端来使配置生效
1、服务端修改
1.1新增依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- kafka依赖 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
1.2修改配置
eureka:
client:
serviceUrl:
defaultZone: http://ip:port/eureka/
server:
port: 8500
spring:
application:
name: spring-cloud-config-server-mq
cloud:
config:
server:
git:
uri: https://giturl.git
username: username
password: password
bus:
enabled: true
stream:
kafka:
binder:
brokers: 192.168.0.128:9092,192.168.0.129:9092,192.168.0.130:9092
management:
endpoints:
web:
exposure:
include: "*"
1.3启动项目
2、客户端修改
2.1添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>
2.2修改配置
spring:
application:
name: dynasty
cloud:
config:
uri: http://192.168.0.131:8500
profile: prod
label: master
bus:
enabled: true
kafka:
bootstrap-servers: 192.168.0.128:9092,192.168.0.129:9092,192.168.0.130:9092
2.3启动项目
3、请求示例
3.1git配置
3.2请求
3.3修改git配置
3.4调用刷新接口
3.5再次请求