Skip to content

spring cloud 学习笔记0x8 #25

@hugeterry

Description

@hugeterry

消息总线

  • 启动rabbitmq(需安装rabbitmq)

  • 改造config-client,添加spring-cloud-starter-bus-amqp(消息总线)和spring-boot-starter-actuator(健康监控)的起步依赖

      <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>
  • 新增application.properties,加入以下配置

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

    其中加上rabbitmq的配置,包括地址、端口、用户名、密码。加上springcloud bus的两个配置,并通过management.endpoints.web.exposure.include对bus-refresh端口进行监控跟踪

    web端点默认的监控端点只有health和info。

  • 对application启动类增加@EnableEurekaClient,@EnableDiscoveryClient声明为eureka client,并增加@RefreshScope,打开动态刷新配置

    @SpringBootApplication
    @EnableEurekaClient
    @EnableDiscoveryClient
    @RestController
    @RefreshScope
    public class ScconfigClientApplication {
    
        public static void main(String[] args) {
            SpringApplication.run(ScconfigClientApplication.class,   args);
        }
    
        @Value("${name}")
        String name;
    
        @RequestMapping(value = "/c")
        public String c() {
            return "info:" + name;
        }
    }
  • 依次启动以下服务

    • scconfig-eureka-server 8558
    • scconfig-server 8555
    • scconfig-client 8551
    • scconfig-client 8552
  • 访问http://localhost:8551/c , http://localhost:8552/c 可以看到返回结果

    info:hugeterry version 1

  • 修改仓库为hugeterry version 2,访问http://localhost:8551/actuator/bus-refresh 刷新数据

  • 再次访问http://localhost:8551/c , http://localhost:8552/c 可以看到返回结果

    info:hugeterry version 2

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions