首页 技术 正文
技术 2022年11月15日
0 收藏 649 点赞 4,235 浏览 5157 个字

上一章节,我们讲解了分布式配置中心spring cloud config,我们把配置项存放在git或者本地,当我们修改配置时,需要重新启动服务才能生效。但是在生产上,一个服务部署了多台机器,重新启动比较麻烦且会短暂影响用户体验。spring cloud生态在发展,肯定有对应的解决之法,接下来将要讲解的Spring Cloud Bus就是为了解决这一难题而存在的。

Spring Cloud Bus(消息总线)通过一个轻量级的消息中间件可以连接分布式系统中的各个节点。使用该总线来广播某些状态的改变(比如配置信息发生变更)或其他管理指令。可以说,消息总线是spring boot应用扩展“道路”上的推进器,而且也把它用来作应用间相互通信的消息管道。

一、项目搭建:

1. 环境准备

本章还是基于上一章来实现的,上一章讲解了git和本地配置两种方式,配置刷新原理都是一样的,这次我们只讲git配置修改后进行刷新。

上一章节内容可以参考:一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)

2. 修改config-client上一章节springcloud-feign-client模块充当着我们的config-client功能,所以在springcloud-feign-client模块的pom.xml文件中加上依赖spring-cloud-starter-bus-amqp。因此需要安装rabbitMq,大家可以自己去下载安装。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion><parent>
<groupId>com.haly</groupId>
<artifactId>springcloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent><groupId>com.haly</groupId>
<artifactId>springcloud-feign-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>springcloud-ribbon-client</name>
<description>新建一个springcloud项目</description><dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-openfeign</artifactId>
</dependency>
<dependency>
<groupId>com.haly</groupId>
<artifactId>springcloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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>
</dependencies><build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build></project>

3. 在springcloud-feign-client模块的application.properties中加上RabbitMq的配置,包括RabbitMq的地址、端口,用户名、密码。并加上spring.cloud.bus的三个配置:

spring.rabbitmq.host=localhost
spring.rabbitmq.port=5672
spring.rabbitmq.username=young
spring.rabbitmq.password=youngspring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
management.endpoints.web.exposure.include=bus-refresh

4. springcloud-feign-client模块的启动类上加上注解:@RefreshScope

package com.haly;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.cloud.openfeign.EnableFeignClients;@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
@RefreshScope
public class SpringcloudFeignClientApplication {public static void main(String[] args) {
SpringApplication.run(SpringcloudFeignClientApplication.class, args);
}}

5. springcloud-feign-client模块的测试类FeignController,测试上一章节的/testconfig方法,具体内容可以参考:一起来学Spring Cloud | 第七章:分布式配置中心(Spring Cloud Config)

package com.haly.controller;import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;import com.haly.romote.FeignRemoteService;@RestController
public class FeignController { @Autowired
FeignRemoteService feignRemoteService; @Value("${configword}")
String configword; @GetMapping(value = "/getHello")
public String getHello(@RequestParam String name) {
return feignRemoteService.hello(name);
} @GetMapping(value = "/testzuul")
public String testzuul(@RequestParam String name) {
return name +",这是springcloud-feign-client的服务接口";
} @GetMapping(value = "/testconfig")
public String testconfig(@RequestParam String name) {
return name +",git配置值:" + configword ;
}
}

6. 运行项目

启动springcloud-eureka-server,启动springcloud-config-server,启动springcloud-config,最后启动springcloud-feign-client模块

为了测试配置修改,多个服务实例都能更新,就启动两个 springcloud-feign-client 实例,端口分别是9600,9601,sts启动两个实例(端口为9600时启动项目,然后将端口改成9601,再启动项目)。

浏览器输入:http://localhost:9600/testconfig?name=young码农 或者输入 http://localhost:9601/testconfig?name=young码农

页面展示结果:young码农,git配置值:NewConfig !

7. 修改git配置,重新运行项目

这时我们去代码仓库将configword的值改为“update config”,即改变配置文件configword的值。如果是传统的做法,需要重启服务,才能达到配置文件的更新。

现在,我们只需要发送post请求:http://localhost:8881/actuator/bus-refresh,你会发现springcloud-feign-client会重新读取配置文件,接着我们查看页面运行结果。

浏览器输入:http://localhost:9600/testconfig?name=young码农 或者输入 http://localhost:9601/testconfig?name=young码农

页面展示结果:young码农,git配置值:update config !

 二、总结:

使用”destination”参数,/actuator/bus-refresh接口可以指定服务,例如 “/actuator/bus-refresh?destination=client:**”, 即刷新服务名为client的所有服务。

通过上面的测试,我们可以知道当git文件更改的时候,用post 向端口为8882的config-client发送请求/bus/refresh/;此时8882端口会发送一个消息,由消息总线向其他服务传递,从而使整个微服务集群都达到更新配置文件。

引入程序猿DD 画的一张图片,简单理解一下刷新原理:

一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)

最后项目目录结构:

一起来学Spring Cloud | 第八章:消息总线(Spring Cloud Bus)

 

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:8,964
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,486
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,331
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,114
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,747
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,781