SpringCloud入门(7)Bus

257 阅读4分钟

上一节中的Config虽然解决了配置文件繁杂的问题,但是有一个弊端,那就是当我们服务端代码修改后,也就是gitee上的东西修改后需要再次启动服务才能够使修改生效。

为了解决这个问题,SpringCloud推出了Bus,学过计网的同学有印象就会知道总线这个东西,就类似广播一样,本篇文章也是SpringCloud入门系列的终章。

引入Bus

建立一个空父bus模块,修改父模块的pom文件,导入对应的依赖以供其子模块使用。

<dependencies>
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
</dependencies>

看到spring-cloud-starter-bus-amqp 就知道需要rabbitmq,在练手的情况一般我都利用wsl2使用docker,下载rabbitmq镜像并且开启,大家可以各显神通,反正能下载就行了。

判断是否下载成功只需要访问http://localhost:15672 ,出现以下界面就是成功

用户名和密码都是guest,登录进去后能看到以下界面

在bus父模块下首先建立一个bus-config-client9601 子模块。

配置文件

从服务端获取内容,所以得设置服务端的相关内容,也就是spring.cloud.config.xxx ,还需要设置bus,rabbitmq,actuator,eureka等相关内容,最终的配置文件如下:

server.port: 9601
spring:
  application:
    name: bus-config-client
  cloud:
    config:
      profile: dev
      label: master
      uri: http://localhost:8101	#服务端地址
    bus:				#开启bus
      enabled: true
      trace:
        enabled: true
  rabbitmq:				#设置rabbitmq的相关信息
    host: localhost
    port: 5672
    username: guest
    password: guest
management:				#开启actuator
  endpoints:
    web:
      exposure:
        include: bus-refresh	#开启自动刷新的api

eureka:					#常规设置eureka
  client:
    fetch-registry: true
    register-with-eureka: true
    service-url:
      defaultZone: http://localhost:8001/eureka/
  instance:
    instance-id: bus-config-client9601

除此之外的配置文件都是通过conifg从服务端中直接获取

但是因为我们又是另外一个项目,所以需要在gitee的仓库中创建一个bus-config-client-dev.properties 文件。

再次强调,配置文件名:{spring.application.name}-{profile}

主启动类

主启动类还是比较简单

@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope			//开启自动刷新的注解
public class BusConfigClient9601 {
    @Value("${name}")
    private String name;

    public static void main(String[] args) {
        SpringApplication.run(BusConfigClient9601.class, args);
    }
	
    //模拟业务类
    @GetMapping("/get")
    public String getName() {
        return "hello, " + name;
    }
}

测试

首先先简单测试一下,开启EurekaServer8001 服务、ConfigServer8101 服务、BusConfigClient9601[9602(通过修改启动类配置文件)] 服务

访问localhost:9601/get ,然后修改gitee中的配置文件,接着发送post请求

http://localhost:9601/actuator/bus-refresh ,再次访问get接口。

在idea的控制面板界面也能够清晰地看到重新读取了文件

进阶刷新

刚才的刷新其实是刷新了所有一个主线上的客户端,不过由于项目比较少的缘故无法体现,为此新建一个BusClient9701 的子模块。

主启动类和配置文件没啥好说的,基本是拷贝BusConfigClient9601 模块的

server.port: 9701
spring:
  application:
    name: bus-client
  cloud:
    config:
      profile: dev
      label: master
      uri: http://localhost:8101
    bus:
      enabled: true
      trace:
        enabled: true
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
management:
  endpoints:
    web:
      exposure:
        include: bus-refresh

#eureka:
#  client:
#    fetch-registry: true
#    register-with-eureka: true
#    service-url:
#      defaultZone: http://localhost:8001/eureka/
#  instance:
#    instance-id: bus-client9701

这里把eureka相关的给注释掉是为了说明config的刷新和eureka无关

@SpringBootApplication
@EnableEurekaClient
@RestController
@RefreshScope				//一样需要这个
public class BusClient9701 {
    @Value("${name}")
    private String name;

    public static void main(String[] args) {
        SpringApplication.run(BusClient9701.class, args);
    }

    @GetMapping("/get")
    public String getName() {
        return "hello, " + name;
    }
}

可以通过http://[ip地址:port端口号]/actuator/bus-refresh/[spring.application.nema微服务名称] 来刷新指定的微服务

只要是总线上的微服务都可以刷新,也就是说ip:port 并没有限制

保持上面开启的微服务不动,新开一个BusClient9701 服务 ,最终开启的服务如下

由于修改配置文件再访问端口看结果的gif图比较大,这里就看idea控制台的结果即可。

控制台中有关BusClient9701的输出可能会出现报错或者警告,那是因为没有注册进Eureka,但是引入了Eureka。

刷新所有总线上的微服务,发送http://localhost:9701/actuator/bus-refresh post请求 。可以看到所有的微服务都再次获取服务端的资源。

刷新总线上指定名称的微服务 ,发送http://localhost:9701/actuator/bus-refresh/bus-config-client post请求来刷新名字为bus-config-client的微服务。可以看到只有bus-config-client 的微服务才再次获取服务端的资源。

刷新某个微服务上的具体端口 ,发送http://localhost:9701/actuator/bus-refresh/bus-config-client:9602 post请求来刷新名字为bus-config-client的微服务中端口为9602的服务。可以看到只有名字为bus-config-client 端口为9602的微服务才再次获取服务端的资源。

再次强调,以上刷新的网址,只要是总线上的微服务都可以刷新,ip:port 并没有限制

http://localhost:9701/actuator/bus-refresh/bus-config-client:9602http://localhost:9601/actuator/bus-refresh/bus-config-client:9602 或者http://localhost:9602/actuator/bus-refresh/bus-config-client:9602 的效果是一样的。

SpringCloud入门系列到这里也告一段落了,创作不易,如果对你有帮助,欢迎点赞,收藏和分享啦!