为什么要使用配置中心?
基于spring-cloud(一),我们发现:每个项目都散落着各种配置文件,某一个基础服务信息变更,都会引起一系列的更新和重启,配置中心是我们的解决办法。
比如我们需要改一下EureKa的ip :
能变成这种方式,
而${fdfs-url}我们能通过config向github获取
我们先在github上写一些配置
将来这些url能够被使用了sping-config组件的微服务自动调用到!
server端
①创建一个 spring-cloud-config微服务
②引入依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
③配置文件 application.yml
spring:
application:
name: spring-cloud-config-server
cloud:
config:
server:
git:
uri: https://github.com/CV-Engineer/mySpringcloud.git #这里填写你要拉取的配置的路径,在图一的蓝色框框中
password: 你的github密码
username: 你的github用户名
#暴露端口号
server:
port: 8083
#调用注册中心
eureka:
client:
service-url: {"defaultZone":"http://localhost:8000/eureka/"}
④启动spring-cloud-config微服务
编写启动类:需要添加@EnableConfigServer这个注解
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerDemoApplication.class, args);
}
}
然后可以在浏览器访问spring-cloud-config微服务来获得git的代码,只是这时候填写的url要注意:
填写:http://localhost:8083/config-dev.properties
结果:
填写:http://localhost:8083/config/dev/
返回一个dom树
仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:
- /{application}/{profile}[/{label}]
- /{application}-{profile}.yml
- /{label}/{application}-{profile}.yml
- /{application}-{profile}.properties
- /{label}/{application}-{profile}.properties
修改config-dev.properties
的配置从fdfs-url=192.168.3.235:7777
变为fdfs-url=192.168.3.235:777777
刷新一下浏览器,发现变化了,说明config是自动获取的。
然后现在就说明config组件正常工作啦,然后我们就可以去spring-cloud(一) 里的需要这些配置的微服务获得注册中心的配置
client端
这里我们拿spring-cloud(二)的customer来调用
①添加依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
需要config服务的 都需要这个依赖 所以可以放在parent的pom文件里
②配置文件
需要配置两个配置文件,application.properties和bootstrap.properties
application.properties如下:
#起名
spring.application.name=spring-cloud-consumer
server.port=8082
feign.hystrix.enabled=true
bootstrap.properties如下:
spring:
cloud:
config:
profile: dev #生产环境,到时候切换成test 就是获取config-test.properties这个配置文件的代码
name: config #配置文件前缀
# label: master #属于哪个分支
discovery:
enabled: true
service-id: SPRING-CLOUD-CONFIG-SERVER #调用config的组件名称
eureka:
client:
service-url: {"defaultZone":"http://localhost:8000/eureka/"}
我们可以看看git的配置文件名:
③编写controller获得配置
@RestController
public class ConsumerController {
@Value("${fdfs-url}")
private String fdfsURL;
@RequestMapping("/testRest3")
public String testRest3(){
System.out.println("ConsumerController.testRest333");
return "读取到配置文件的值----》"+fdfsURL;
}
}
④WEB测试
启动项目后访问http://localhost:8082/testRest3l 浏览器显示
读取到配置文件的值----》http://192.168.3.235:777777
然后我们把 profile
改成test
重启项目刷新页面,浏览器显示
读取到配置文件的值----》http://10.77.233.12:9999/
即可完成生产环境的切换!
通知配置
当git代码发送变化时,config微服务能自动刷新,但是调用了config配置的client微服务并不知情
比如我们把config-dev.properties
里的配置从fdfs-url=192.168.3.235:777777
改成fdfs-url=192.168.3.235:66666
server能够马上知道:
但是client还不知情:
手动刷新配置
①引入依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
②开启更新机制
client的controller:使用@RefreshScope 这个注解
@RestController
@RefreshScope
public class ConsumerController {
。。。中间代码不变。。。
}
④server配置文件
management:
endpoints:
web:
exposure:
include: "*"
⑤client配置文件
management:
endpoints:
web:
exposure:
include: refresh
⑥在PostMan中以Post请求发送一条url
client的ip:端口号加上/actuator/refresh
⑦然后去刷新一下client就行了
但是每次手动刷新客户端也很麻烦,有没有什么办法只要提交代码就自动调用客户端来更新呢,github的webhook是一个好的办法。
rabbitMq实现消息总线
比如多个client 都调用了config里的同一个配置文件,现在这个配置文件发生了变化,那么我们还一个个的调用http://localhost:xxxx/actuator/refresh 这个请求去逐一通知?有没有什么办法当我们执行了一次http://localhost:xxxx/actuator/refresh,其他也顺便通知一下其他client呢?
我们将使用中到Spring Cloud Bus 消息总线
原理图:
通知一个客户端,所有客户端都知道了,需要通过队列MQ来实现,这里我们使用和boot集成最好的RabbitMQ
步骤
①依赖
②MQ配置文件 在config server 和 client都加上
spring:
rabbitmq:
host: 192.168.0.6
port: 5672
username: admin
password: 123456
③修改client的配置 :include: bus-refresh
management:
endpoints:
web:
exposure:
include: bus-refresh
④然后修改git 配置
⑤对其中一个client进行通知
⑥然后访问其他的client看看是否被通知成功