SpringCloud-config,bus
config配置中心管理微服务的配置文件优点:集中管理配置文件不同生产环境不同配置,动态化配置的更新配置信息改变时 不需要重启即可更新配置服务config使用config云端仓库此处使用gitee码云创建云端仓库 SpringCloud-config本地编辑配置文件config-dev.yml并提交(到本地仓库)推送到云仓库config server服务端搭建config-server模块导入co
·
config配置中心,bus消息总线,bus+config刷新所有服务配置bus-refresh
config配置中心
管理微服务的配置文件
优点:
集中管理配置文件
不同生产环境不同配置,动态化配置的更新
配置信息改变时 不需要重启即可更新配置服务
config使用
- config云端仓库
此处使用gitee码云
创建云端仓库 SpringCloud-config
本地编辑配置文件config-dev.yml并提交(到本地仓库)
推送到云仓库
config server服务端
-
搭建config-server模块
-
导入config-server依赖
<!--config Server-->
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
- 编写配置,设置远程仓库的地址
server:
port: 9527
spring:
application:
name: config-server
cloud:
config:
server:
#git的远程仓库地址
git:
uri: https://gitee.com/yhlovezq/spring-cloud-config.git
label: master # 分支配置
- 创建引导类
@SpringBootApplication
@EnableConfigServer // 启用config server功能
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class,args);
}
}
- 测试访问配置文件
http://localhost:9527/master/config-dev.yml
config client客户端
- 导入start-config依赖
<!--config client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
- 配置config server的地址,要读取的配置文件的名称等信息
创建bootstrap.yml文件,存放系统级配置,优先级高于application.yml
#该配置文件优先级高于application
#配置从config-Server获取配置文件的名称等信息
spring:
cloud:
config:
#配置config-server的地址
uri: http://localhost:9527
#配置获得的配置文件的信息
name: config # 文件名config-dev.yml
profile: dev # 指定-后边的名称 dev
label: master #分支
- 获取配置文件的配置值
在Controller中使用@Value注解
@RestController
@RequestMapping("/product")
public class IProductController {
@Value("${username}")
private String name;
@Autowired
private IProductService iProductService;
/*@RequestMapping("/findOne")
public Product findById(int id){
return iProductService.findById(id);
}*/
@RequestMapping("/findOne/{id}")
public Product findById(@PathVariable("id") int id){
Product byId = iProductService.findById(id);
byId.setTitle(name);//将配置文件的键值对返回
return byId;
}
}
- 启动测试
- 启动客户端刷新
- 客户端引入actuator监控依赖
<!--actuator SpringCloud的监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 获取配置信息的类上添加@RefreshScope注解
IProductController.java - 暴漏refresh端点,bootstrap.yaml中添加配置
#配置从config-Server获取配置文件的名称等信息
spring:
cloud:
config:
#config集成Eureka后开启注册发现
discovery:
enabled: true
service-id: config-server # configServer服务命名称
#配置config-server的地址
# uri: http://localhost:9527
#配置获得的配置文件的信息
name: config # 文件名config-dev.yml
profile: dev # 指定-后边的名称 dev
label: master #分支
# 开启自动刷新配置
#用于暴露结点
management:
endpoints:
web:
exposure:
include: '*' # *不安全或者refresh
- 使用curl工具或者postman发送post请求
curl -X http://localhost:8000/actuator/refresh
http://localhost:8000/actuator/refresh
注:云端修改后就得发送一次请求
- configServer集成Eureka
将config-server注册到注册中心
- 添加eureka-client依赖
<!--config集成Eureka-->
<!-- eureka-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
- 添加配置连接eureka
eureka:
instance:
hostname: localhost # 主机名
client:
service-url:
#defaultZone: http://eureka-server-2:8762/eureka,http://eureka-server-1:8761/eureka #注册到多节点服务端时
defaultZone: http://localhost:8761/eureka # eureka服务端地址,将来客户端使用该地址和eureka进行通信
- 修改客户端配置
#该配置文件优先级高于application
#配置从config-Server获取配置文件的名称等信息
spring:
cloud:
config:
#config集成Eureka后开启注册发现
discovery:
enabled: true
service-id: config-server # configServer服务命名称
#配置config-server的地址
# uri: http://localhost:9527
#配置获得的配置文件的信息
name: config # 文件名config-dev.yml
profile: dev # 指定-后边的名称 dev
label: master #分支
# 开启自动刷新配置
#用于暴露结点
management:
endpoints:
web:
exposure:
include: '*' # *不安全或者refresh
问题:每次要实现配置数据更新,每个服务都要发送一条post指令,麻烦
bus消息总线
微服务间通信消息的组件
搭建:
- 在config-server和config-client中引入bus依赖:bus-amqp
<!-- bus -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<!--SpringCloud自带监控,需要暴漏bus-refresh断点-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- 分别在config-server和config-client(bootstrap.yml)中配置文件中配置RabbitMQ连接信息
spring:
#配置rabbitmq信息
rabbitmq:
host: localhost
port: 5672
username: yh
password: yh
virtual-host: /yh
- 在config-server中设置暴露监控断点bus-refresh(需要添加SpringCloud的actuator依赖)
# 开启自动刷新配置
#用于暴露结点
management:
endpoints:
web:
exposure:
include: 'bus-refresh' # *不安全或者refresh
- 启动测试
云仓库修改配置,config-server服务端发送post请求
http://localhost:9527/actuator/bus-refresh
刷新client客户端,读取最新配置
更多推荐
已为社区贡献1条内容
所有评论(0)