1.config server 的搭建
1.1导入依赖
< 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>
1.2yml文件的编写–连接到git上,从git上拉取共同的配置(我这里连接的是gitee)
server :
port : 3344
spring :
application :
name : cloud- config- server
cloud :
config :
server :
git :
uri : --- --- --- --- --- -
username : --- --- --- --- -
password : --- --- --- --- ---
basedir : D: \basedirs
eureka :
client :
service-url :
defaultZone : http: //localhost: 7001/eureka
1.3 启动类添加注解
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main ( String[ ] args) {
SpringApplication. run ( ConfigApplication. class , args) ;
}
}
2.config client 的搭建
2.1导入依赖
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-actuator</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-config-client</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-netflix-eureka-client</ artifactId>
</ dependency>
< dependency>
< groupId> org.springframework.boot</ groupId>
< artifactId> spring-boot-starter-web</ artifactId>
</ dependency>
2.2编写bootstrap.yml文件(由于配置文件去sever端拉取信息,所以yml文件需要优先加载,才能满足后面的程序如果需要用到配置,才不会报错)
spring :
cloud :
config :
discovery :
enabled : true
service-id : cloud- config- server
profile : dev
label : master
application :
name : customer
server :
port : 3355
eureka :
client :
service-url :
defaultZone : http: //localhost: 7001/eureka
2.3 为了进行测试我在gitee上编写了yml文件的信息如下
2.4 编写controller进行测试(引入gitee拉取的信息)
@RestController
@RequestMapping ( "/customer" )
@RefreshScope
public class ConfigController {
@Value ( "${config.info}" )
private String configInfo;
@GetMapping ( "/info" )
public String getConfiginfo ( ) {
return configInfo;
}
}
2.5 直接访问(http://localhost:3355/customer/info)拉取信息
2.6 为了实现gitee上代码进行修改,client端能及时不用重启服务进行拉取最新配置
bootstrap.yml文件添加监控端点
management :
endpoints :
web :
exposure :
include : "*"
controller层添加注解
修改配置后利用post请求,进行client端的手动刷新(http://localhost:3355/actuator/refresh)
自动刷新的配置那就要找bus了。
1.1在config server端添加bus依赖(借助rabbitmq实现消息的通知)
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-bus-amqp</ artifactId>
</ dependency>
1.2 yml文件添加端口暴露,连接rabbitmq(进行总线的刷新),我这里使用的是docker启动的
management :
endpoints :
web :
exposure :
include : "bus-refresh"
spring :
rabbitmq :
port : 5672
host : 192.168.43.158
virtual-host : /test
username : test
password : test
2.1 config client端进行配置
< dependency>
< groupId> org.springframework.cloud</ groupId>
< artifactId> spring-cloud-starter-bus-amqp</ artifactId>
</ dependency>
同理连接rabbitmq就行,所有的config端进行信息的总线刷新都得添加上诉依赖,连接rabbitmq
修改gitee上的信息后,只需要刷新config server,client的所有都能接受到最新的刷新消息
利用post请进行总线的刷新(这里的bus-refresh是对外暴露的刷新地址)
> http://localhost:3344/actuator/bus-refresh