走进Spring Cloud之十一 SpringCloud bus 消息总线刷新配置(Greenwich版本)

走进Spring Cloud之十一 SpringCloud bus 消息总线刷新配置(Greenwich版本)

SpringCloud Bus

在微服务中,我们将使用轻量级消息代理,通过一个共用的消息主题,让系统中所有微服务都连上来,主题中的消息会被所有监听者消费,所以称为消息总线。spring cloud bus将分布式的节点用轻量的消息代理连接起来。它可以用于广播配置文件的更改或者服务之间的通讯,也可以用于监控。

改造config-client

pom.xml

修改pom.xml添加spring-cloud-starter-bus-amqp依赖

<?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">
    <parent>
        <artifactId>scexample</artifactId>
        <groupId>com.pubutech</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-client</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <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-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-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>

bootstrap.yml

增加rabbitmq的配置支持

spring:
  cloud:
    config:
      name: springcloud-config             #对应{application}部分
      profile: pro                         #对应{profile}部分
      #uri: http://localhost:8888/          #配置中心的具体地址
      label: master                        #对应git的分支。如果配置中心使用的是本地存储,则该参数无用
      discovery:
        service-id: config-server      #指定配置中心的service-id,便于扩展为高可用配置集群。
      enabled: true                        #开启Config服务发现支持
      #配置重试机制
      retry:
        initial-interval: 2000
        max-attempts: 2000
        max-interval: 2000
        multiplier: 1.2
      fail-fast: true
    bus:
      #动态刷新配置
      refresh:
        enabled: true
      #跟踪总线事件
      trace:
        enabled: true
  rabbitmq:
    host: 192.168.199.133
    port: 5672
    username: test
    password: test123
    virtual-host: /

eureka:
  client:
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

#关闭安全认证
management:
  security:
    enabled: false
  #refresh接入点显式暴露出来
  endpoints:
    web:
      exposure:
        include: refresh,health,info,bus-refresh

改造config-server

pom.xml

修改pom.xml添加spring-cloud-starter-bus-amqp依赖

<?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">
    <parent>
        <artifactId>scexample</artifactId>
        <groupId>com.pubutech</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>config-server</artifactId>
    <packaging>jar</packaging>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</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.boot</groupId>
            <artifactId>spring-boot-starter-actuator</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>

application.yml

增加rabbitmq的配置支持

server:
  port: 8888

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          uri: https://github.com/Jaysong2012/scexample     # 配置git仓库的地址
          search-paths: springcloud-config-repo                             # git仓库地址下的相对地址,可以配置多个,用,分割。
          username:                                             # git仓库的账号(私有库必填)
          password:                                             # git仓库的密码(私有库必填)
      label: master                                        #配置仓库的分支
  rabbitmq:
    host: 192.168.199.133
    port: 5672
    username: test
    password: test123

#关闭安全认证
management:
  security:
    enabled: false
  #refresh接入点显式暴露出来
  endpoints:
    web:
      exposure:
        include: refresh,health,info,bus-refresh

eureka:
  client:
    service-url:
      #设置与Eureka Server交互的地址,查询服务和注册服务都需要依赖这个地址。默认是http://localhost:8761/eureka ;多个地址可使用 , 分隔。
      defaultZone: http://localhost:8761/eureka/,http://localhost:8762/eureka/,http://localhost:8763/eureka/

启动测试

这里我们在8889 和88902个端口分别运行2个config-client
访问 http://localhost:8761/
8761

访问 http://localhost:8889/writer
writer

修改远程github仓库配置
github

通知更新

curl -v  -X POST http://localhost:8889/actuator/bus-refresh

再次访问 http://localhost:8889/writer
result

GitHub源代码

猜你喜欢

转载自blog.csdn.net/weixin_43430036/article/details/83993919