700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Spring Cloud学习:07消息总线(Spring Cloud Bus)

Spring Cloud学习:07消息总线(Spring Cloud Bus)

时间:2023-06-20 01:45:11

相关推荐

Spring Cloud学习:07消息总线(Spring Cloud Bus)

独角兽企业重金招聘Python工程师标准>>>

1 消息总线介绍

消息总线是一种通信工具,可以在机器之间互相传输消息、文件等。消息总线扮演着一种消息路由的角色,拥有一套完备的路由机制来决定消息传输方向。发送端只需要向消息总线发出消息而不用管消息被如何转发。Spring Cloud Bus 通过轻量消息代理连接各个分布的节点。管理和传播所有分布式项目中的消息,本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。

2 改造配置客户端(Config Client)

2.1基于上次工程进行改造,在config-client的pom文件中添加spring-cloud-starter-bus-amqp依赖:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="/POM/4.0.0" xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.laravelshao.springcloud</groupId><artifactId>config-client</artifactId><version>0.0.1-SNAPSHOT</version><packaging>jar</packaging><name>config-client</name><description>Demo project for Spring Boot</description><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.8.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.SR4</spring-cloud.version></properties><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-eureka</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency></dependencies><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>${spring-cloud.version}</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.2 在配置文件bootstrap.yml中添加rabbitmq相关配置信息:

server:port: 8881management:security:enabled: false # 设置是否启用安全限制spring:application:name: config-clientcloud:config:uri: http://localhost:8888/ # 配置服务中心地址label: master # 远程仓库分支profile: dev # 指定环境discovery:enabled: true # 从配置中心读取文件service-id: config-server # 配置中心服务id# rabbitmq配置rabbitmq:host: localhostport: 5672#username:#password:eureka:client:serviceUrl:defaultZone: http://localhost:8889/eureka/ # 配置eureka服务器地址

management.security.enabled:设置是否启用安全限制

注意事项:需要配置management.security.enabled: false不启用安全限制,否则在请求刷新bus时会显示:

{

"timestamp": 1512118086064,

"status": 401,

"error": "Unauthorized",

"message": "Full authentication is required to access this resource.",

"path": "/bus/refresh"

}

后台也会提示添加安全策略或者设置不启用安全限制。

Full authentication is required to access actuator endpoints. Consider adding Spring Security or set 'management.security.enabled' to false.

2.3 在启动类添加@RefreshScope注解

@SpringBootApplication@RestController@RefreshScopepublic class ConfigClientApplication {public static void main(String[] args) {SpringApplication.run(ConfigClientApplication.class, args);}@Value("${hello}")String hello;@RequestMapping("/hi")public String hi() {return hello;}}

2.4 依次启动eureka-server、config-server、config-client(启动两个实例,端口为8881、8882)

请求http://localhost:8881/hi 或 http://localhost:8882/hi,返回信息:

hello config server

2.5修改配置仓库中config-client-dev.properties中hello对应的值为hello config server change,使用POST请求http://localhost:8881/bus/refresh,会发现config-client重新读取了配置文件

再次请求http://localhost:8881/hi 或 http://localhost:8882/hi,返回更新后的信息:

hello config server change

本文源码下载地址:

/laravelshao/spring-cloud-learning/tree/master/setion07-bus

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。