按我的理解,当项目越来越大时,做一个微服务配置中心统一管理各个服务的配置是相当重要的,这里我演示的是将微服务配置中心与服务中心放在一起的
在码云新建一个项目spring-cloud-config,新建文件夹 service-registry-config,在service-registry-config中新建配置文件 application-dev.yml(这里注意一下,文件名{name}-{profile}.yml/properties,如果不带profile的话,这个配置会被所有{name}-{profile}.yml共同拥有) application-dev.yml
app: info: this is registry config-1再新建一个application-pro.yml application-pro.yml
web: info: this is registry config-2在项目registry 中添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>在启动类中加注解
@SpringBootApplication @EnableEurekaServer // 启用Eureka服务 @EnableConfigServer //启动配置中心 public class RegistryApplication {bootstrap.yml
spring: application: #应用名称 (服务注册中心) name: config-server #云服务配置 cloud: config: server: prefix: /config #添加映射路径client注册路径 192.168.0.112:8761/config #git中心配置(这里我将配置文件存储在码云上) git: #配置文件所在的服务地址 uri: https://gitee.com/zhushaoyun/spring-cloud-config #配置文件所在的文件路径 search-paths: service-registry-config #公开的项目不需要设置码云用户名和密码 username: password:启动服务中心 通过连接拿到
这个结果可能是xml也可能是json根据个人设置,不报错拿到就是配置成功
在app=producer-1 与app-consumer-1中添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency>都新建bootstrap.yml配置文件
spring: #云配置 cloud: config: uri: http://${user.name}:${user.password}@192.168.0.112:8761/config #配置文件前缀 (这里配置的name+profile为我们要读的配置文件 即 application-dev) name: application profile: pro # discovery: # enabled: true #发现服务 # service-id: config-server #服务名 user: name: admin password: admin_1因为bootstrap.yml优先于application.yml加载,所以讲user自定义配置拿过来,这里http:// u s e r . n a m e : {user.name}: user.name:{user.password}@192.168.0.112:8761/config中因为注册中心中有简单验证,这里user的值也需加上,在registry中因为添加prefix: /config映射路径,这里也需加上,主要区别eureka连接,我这里用的uri连接,也可以使用discovery配置
在app-producer-1 中添加接口,对应application-dev.yml
/** * 读取配置中心的配置测试控制器 */ @RestController public class InfoResource { @Value("${app.info}") private String info; // 注入app.info的值(本地配置文件没有这个配置,该配置从配置中心读取) /** * 输出 变量值 * @return 变量值 */ @RequestMapping("/appInfo") public String info(){ return info; }在app-consumer-1中添加接口,对应application-pro.yml
@RestController public class WebInfoResource { @Value("${web.info}") private String webInfo; @RequestMapping("webInfo") public String info(){ return webInfo; } }启动
得到 “this is registry config-2”
得到 “this is registry config-1” 能正常得到则配置成功,但是这种会有一些限制,当配置更新时,git新提交,producer与consumer服务拿不到最新配置,需要重启配置中心,所以需要一种让她刷新配置的方式,这里我是用的的是RabbitMQ.
安装RabbitMQ,我使用的在别的机器上安装,也可以本地,是用的docker安装.链接: https://blog.csdn.net/weixin_44400390/article/details/96430208.
在registry中添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>bootstrap.yml
spring: application: #应用名称 (服务注册中心) name: config-server #云服务配置 cloud: config: server: prefix: /config #添加映射路径client注册路径 192.168.0.112:8761/config #git中心配置(这里我将配置文件存储在码云上) git: #配置文件所在的服务地址 uri: https://gitee.com/zhushaoyun/spring-cloud-config #配置文件所在的文件路径 search-paths: service-registry-config #公开的项目不需要设置用户名和密码 username: password: rabbitmq: host: 192.168.0.61 port: 5672 username: root password: 123456 #开启actuator/bus-refresh 接口,使用POST请求,刷新配置中心拉取最新配置, #根据springboot版本,这里可能变为management.security.enable:false,需要版本支持,路径也会不一样 /bus/refresh management: endpoints: web: exposure: include: bus-refresh user: name: admin password: admin_1启动registry
在app-producer-1,app-consumer-1中添加依赖
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-bus-amqp</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>在bootstrap.yml中添加rabbitMQ配置
rabbitmq: host: 192.168.0.61 port: 5672 username: root password: 123456在配置接口类名上加注解 @RefreshScope
/** * 读取配置中心的配置测试控制器 * @ RefreshScope 注解会在配置中心配置改变的时候 手动访问本项目的/refresh路径,就会实现自动刷新配置文件,重新加载配置文件中的数据 */ @RestController @RefreshScope public class InfoResource { @Value("${app.info}") private String info; // 注入app.info的值(本地配置文件没有这个配置,该配置从配置中心读取) /** * 输出 变量值 * @return 变量值 */ @RequestMapping("/appInfo") public String info(){ return info; } } @RestController @RefreshScope public class WebInfoResource { @Value("${web.info}") private String webInfo; @RequestMapping("webInfo") public String info(){ return webInfo; } }然后启动
得到 “this is registry config-2”
得到 “this is registry config-1”
在码云上更改;
app: info: this is registry config-1, 你吃饭了吗? web: info: this is registry config-2,还没有,你呢?提交,
得到 “this is registry config-2”
得到 “this is registry config-1” 这时是得不到最新配置的 发送 POST 请求
这里映射路径/config是不需要的 如果成功是没有返回数据的,不会报错
得到 “this is registry config-2,还没有,你呢?”
得到 “this is registry config-1, 你吃饭了吗?” 不出错即可在线拿到配置