700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > spring cloud bus_Spring Cloud学习笔记--消息总线(Bus)

spring cloud bus_Spring Cloud学习笔记--消息总线(Bus)

时间:2023-05-02 05:23:42

相关推荐

spring cloud bus_Spring Cloud学习笔记--消息总线(Bus)

Spring Cloud Bus links nodes of a distributed system with a lightweight message broker. This can then be used to broadcast state changes (e.g. configuration changes) or other management instructions. [1]

简单来说,Spring Cloud Bus通过一个轻量级消息代理连接到分布式系统的节点。

上一篇文件,我介绍了Spring Cloud Config

五雨:Spring Cloud学习笔记--配置中心(Config)​

这里有一个问题,如果客户机在集群中过多了化,Github上一但更新了配置文件,下面每一台服务都进行手动刷新也是不现实的,这里就可以使用Spring Cloud Bus进行广播。

架构图如下:

这里我们以RabbitMQ为例子进行集成。

RabbitMQ如何部署请自行百度。

这是我的RabbitMQ版本:

注册中心A:

pom文件:

<

yml文件:

server:#注册中心A使用的端口号port: 30001eureka:instance:#注册中心A主机地址hostname: eurekaserver1client:#本服务为注册中心,所以不需要向注册中心注册自己register-with-eureka: false#本服务为注册中心,不需要进行检索服务fetch-registry: falseservice-url:defaultZone: http://eurekaserver2:30002/eureka/spring:application:name: eurekaserver1

启动类中增加EnableEurekaServer注解。

注册中心B:

pom文件:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-server</artifactId></dependency>

yml文件:

server:#注册中心B使用的端口号port: 30002eureka:instance:#注册中心B主机地址hostname: eurekaserver2client:#本服务为注册中心,所以不需要向注册中心注册自己register-with-eureka: false#本服务为注册中心,不需要进行检索服务fetch-registry: falseservice-url:defaultZone: http://eurekaserver1:30001/eureka/spring:application:name: eurekaserver2

启动类:

配置中心(Config Server):

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency>

bootstrap.yml:

server:#当前Config服务的端口号配置port: 65010spring:application:name: configserverrabbitmq:host: 106.12.128.158port: 5672username: usernamepassword: passwordcloud:config:server:git:uri: /plsjava/plstestsearch-paths: testplsconfigusername: usernamepassword: passwordbus:enabled: truetrace:enabled: trueeureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/management:endpoints:web:exposure:include: "*"

启动类增加EnableEurekaClient注解和EnableConfigServer注解

TestModel:

public class TestModel {private String id;private String url;private String msg;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public String getMsg() {return msg;}public void setMsg(String msg) {this.msg = msg;}}

Controller:

@EnableEurekaClient@RefreshScope@RestControllerpublic class ConfigServerController {//描述eureka客户端信息的类@Autowiredprivate DiscoveryClient eurekaClient;@RequestMapping(value="/ServerTest", method= RequestMethod.GET, produces= MediaType.APPLICATION_JSON_VALUE)public TestModel getTest(HttpServletRequest request){TestModel tm = new TestModel();tm.setUrl(request.getRequestURL().toString());tm.setMsg("请求访问成功");return tm;}}

我们先看一下配置中心:

可见配置中心已经搭建成功。

模拟配置客户端A:

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>

bootstrap.yml

spring:application:#对应配置中心所获取的配置文件的{application}name: ctestrabbitmq:host: 106.12.128.158port: 5672username: usernamepassword: passwordcloud:config:#配置中心的URL#uri: http://localhost:65010/#指定的环境名profile: dev#指定的分支名#label: master#文件前缀名称#name: ctestdiscovery:enabled: trueservice-id: configserverbus:enabled: truetrace:enabled: trueeureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/management:endpoints:web:exposure:include: "*"

application.yml

server:port: 65012

Controller:

@EnableEurekaClient@RestController@RefreshScope@RequestMapping("clientconfig")public class ClientConfigController {@Value("${from}")private String fromString;@GetMapping("/from")public String getEv() {return fromString;}}

模拟配置客户端B:

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-bus-amqp</artifactId></dependency>

bootstrap.yml:

spring:application:#对应配置中心所获取的配置文件的{application}name: ctestrabbitmq:host: 106.12.128.158port: 5672username: usernamepassword: passwordcloud:config:#配置中心的URL#uri: http://localhost:65010/#指定的环境名profile: dev#指定的分支名#label: master#文件前缀名称#name: ctestdiscovery:enabled: trueservice-id: configserverbus:enabled: truetrace:enabled: trueeureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/management:endpoints:web:exposure:include: "*"

application.yml

server:port: 65013

Controller:

@EnableEurekaClient@RestController@RefreshScope@RequestMapping("clientconfig")public class ClientConfigController {@Value("${from}")private String fromString;@GetMapping("/from")public String getEv() {return fromString;}}

好了,运行一下看看吧

这个时候,我修改配置文件并上传到github上

然后通过postman向配置中心的/actuator/bus-refresh接口发起post请求

再次访问模拟配置客户端A和B提供的接口

发现获得的配置文件都已经成功修改掉了。

Spring Cloud Bus成功的起到了作用。

参考

^Spring Cloud Bus(官方文档)https://cloud.spring.io/spring-cloud-static/spring-cloud-bus/1.3.1.RELEASE/

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