前言:
准备:git搭建(新建gateway.properties)
springboot:2.3.0
springcloud:Hoxton.SR4
服务:config-server(作为配置中心服务端)
gateway-server(客户端)
服务端搭建:
基础jar包:
application.properties
启动文件开启配置中心,引入配置@EnableConfigServer启动即可
浏览器访问localhost:8888,访问规则https://cloud.spring.io/spring-cloud-static/spring-cloud-config/2.2.2.RELEASE/reference/html/
=============================config服务端搭建完成,接下来是客户端=====================
客户端搭建
基础jar包(同服务端一样,客户端主要是)
编辑bootstrap.properties文件(我这里为了方便看,定义了两个properties文件,一个bootstrap.properties,用于读取远程配置,一个application.properties,用于本地配置,这里有个地方要注意,如果远程跟application.properties有相同的配置,因为springcloud默认情况下本地配置会覆盖远程的配置,所以如果想以远程优先级最高,则需要增加如下配置)
bootstrap.properties配置如下
spring.cloud.config.uri: http://localhost:8888
spring.application.name: gateway
spring.profiles.active: dev
spring.cloud.config.label:dev
application.properties配置如下
management.endpoints.web.exposure.include:*
然后我们编写服务层(记住一定不要忘记加如下红标标签)
接下来我们测试一下
1.启动config服务端
2.启动客户端,调用接口http://localhost:7777/get(显示knag.li说明git远程配置读取到了)
3.接下来修改远程配置
4.访问http://localhost:8888/gateway/dev/dev(远程已经改变)
5.接着访问客户端http://localhost:7777/get(发现配置的值没有改变)
6.接下来我们postman执行localhost:7777/actuator/refresh(端口为客户端端口,提示我如下两个值有了变化)
7.再次访问客户端接口http://localhost:7777/get(发现值更新了)
本次springcloud实现远程更新客户端热部署的功能就全部完成了
下一章我们继承springcloud bus通过kafka的做消息总线