? 小声哔哔
上个月根据大佬‘纯洁的微笑’做了Spring Cloud系列的总结,由于项目比较近搁置了一段时间,只写了点零碎知识点,今天忙里偷点闲,再整点,把远程配置中心给完结了~
上篇Spring Cloud系列博客留下个问题:
springboot项目只有在启动的时候才会获取配置文件的值,修改git仓库配置文件信息后,client端并没有再次去获取配置。
终结之前➡
emmmm...先抛开client端无法在仓库文件修改配置后获取最近配置这个问题不说,首先得秉承着spring cloud服务治理的理念,要将server端当做一个服务注册到eureka中,client端直接去eureka中去获取配置中心server端的服务。
把配置中心服务端和客户端都注册到eureka上,交给老大管理即可。
?开始吧~
在此不再赘述新建项目啥的了,在上篇Spring Cloud系列博客建立项目基础上修改就好了:
➡server端修改:
1.pom加入eureka依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2.配置文件加上支持eureka配置
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址
3.启动类添加@EnableDiscoveryClient
注解激活对注册中心的支持
至此server端修改搞定~启动eureka和server端可以看到server端已经注册到eureka上了~
➡client端修改:
1.pom同样加入eureka依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
2.配置文件修改:
去掉:spring.cloud.config.url(直接指向配置中心server的地址)
增加 :spring.cloud.config.discovery.enable=true(开启配置中心服务发现的支持)
eureka.client.serviceUrl.defaultzone:http://localhost:8000/eureka (注册中心的支持)
完整配置:
spring:
application:
name: spring-cloud-config-client
cloud:
config:
name: neo-config #对应{application}部分
profile: dev #对应{profile}部分
# uri: http://localhost:8001/ #去掉配置中心的具体地址
label: master #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
discovery:
service-id: spring-cloud-config-server #指定server端的name,也就是server
enabled: true #开启Config服务发现支持
server:
port: 8002
eureka:
client:
serviceUrl:
defaultZone: http://localhost:8000/eureka/ ## 注册中心eurka地址
# 特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.yml中,
# config部分内容才能被正确加载。
# 因为config的相关配置会先于application.properties,
# 而bootstrap.properties的加载也是先于application.properties。
3.启动类同样添加@EnableDiscoverConfig注解,激活对配置中心的支持
//开启注册中心支持
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
启动client端,可以看到也注册到了eureka注册中心:
访问client端测试,发现可以正常访问git中心配置:
ok~这样就解决了如果服务端更换地址,客户端也要跟着更换spring.cloud.config.url配置,现在都交给eureka注册管理,解决这一问题~
但是依然无法解决修改git仓库配置文件信息后,client端并没有再次去获取配置这个问题。
下面使用Spring Cloud Bus正式终结此贴~
开始终结➡
➡扒一扒bus是啥?
Spring cloud bus即常说的消息总线。它可以管理和传播所有分布式项目中的消息,其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用bus的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一。
简单理解就是bus接收git配置代码提交的消息,再通知各个客户端去更新配置。
图示:
bus工作流程:
1、提交代码触发post给客户端A发送bus/refresh
2、客户端A接收到请求从Server端更新配置并且发送给Spring Cloud Bus
3、Spring Cloud bus接到消息并通知给其它客户端
4、其它客户端接收到通知,请求Server端获取最新配置
5、全部客户端均获取到最新的配置
➡修改client端
1.pom添加amqp依赖支持消息总线(rabbitmq方式)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2.配置文件新增rabbitmq配置
## 刷新时,关闭安全验证
management.security.enabled=false
#注意
#springboot2.0之后刷新改变安全验证修改为这个
#management.endpoints.web.exposure.include="*"
## 开启消息跟踪
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=xxx
spring.rabbitmq.password=xxx
至此client修改完成。
测试:修改client端prot端口启动三个客户端模拟测试
可以看到eureka上完成client服务注册:
依次访问localhost:8009,localhost:8010,localhost:8011,得到远程配置:
说明客户端都已经读取到了server端的内容。
现在更新一下git远程仓库的配置:neo.hello=hello my name is dev server,现在访问localhost:8009依然是neo.hello=hello my name is dev,没有改变,现在要在cmd模拟一下webhook刷新。
PS:
webhook介绍:
WebHook是当某个事件发生时,通过发送http post请求的方式来通知信息接收方。Webhook来监测你在Git上的各种事件,最常见的莫过于push事件。如果你设置了一个监测push事件的Webhook,那么每当你的这个项目有了任何提交,这个Webhook都会被触发,这时就会发送一个HTTP POST请求到你配置好的地址。
webhook添加:
现在打开cmd窗口执行:
curl -X POST http://localhost:8009/bus/refresh
执行完成后,依次访问localhost:8009,localhost:8010,localhost:8011,得到远程配置:
说明三个客户端均已经拿到了最新配置文件的信息,这样我们就实现了远程仓库更改配置,客户端可以拿到最新的配置了。
end?no~~~
以上虽然达到了利用消息总线触发一个客户端bus/refresh,而刷新所有客户端的配置的目的,但是client端本身是业务模块,不该让它来作为webhook的配置来刷新配置。
现在要让server-config端来做这个事儿:
bus流程变为:
1、提交代码触发post请求给bus/refresh
2、server端接收到请求并发送给Spring Cloud Bus
3、Spring Cloud bus接到消息并通知给其它客户端
4、其它客户端接收到通知,请求Server端获取最新配置
5、全部客户端均获取到最新的配置
所以现在要修改server端,让其支持bus/refresh
1.pom添加amqp依赖支持消息总线(rabbitmq方式)
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
2.配置文件新增rabbitmq配置
## 刷新时,关闭安全验证
management.security.enabled=false
#注意
#springboot2.0之后刷新改变安全验证修改为这个
#management.endpoints.web.exposure.include="*"
## 开启消息跟踪
spring.cloud.bus.trace.enabled=true
spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=xxx
spring.rabbitmq.password=xxx
至此server端修改完成。
测试:这次模拟git远程配置修改后webhook请求server端刷新
先修改远程git配置:
neo.hello=hello my name is dev server负责刷新
curl -X POST http://localhost:8001/bus/refresh
执行成功后,依次访问localhost:8009,localhost:8010,localhost:8011,得到远程配置:
可以看到client客户端已经获取到了最新配置~只是我搞了中文乱码了而已~~~ = =!
累shi了。。。终于搞定了,暂时完结。感谢各路大神资料!