美文网首页Java 杂谈计算机杂谈
2019-08-27/spring cloud bus 自动刷新

2019-08-27/spring cloud bus 自动刷新

作者: 呼噜噜睡 | 来源:发表于2019-08-27 23:08 被阅读0次

    以spring cloud Greenwich.SR2和spring boot 2.1.5.RELEASE 版本举例。假设本地已经安装了RabitMQ。
    config server的pom:

         <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>
    

    congfig server的application.yml:

    server:
      port: 8888
    
    eureka:
      client:
        service-url:
          # 将自己注册到eureka server的地址
          defaultZone: http://localhost:8761/eureka/
    
    spring:
      application:
        name: config
      cloud:
        config:
          server:
            git:
              uri: https://github.com/fantasyWJP/wxdc_config_study_original.git
              username: xxx
              password: xxx
        bus:
          refresh:
            enabled: true
    
    management:
      endpoints:
        web:
          exposure:
            include: bus-refresh
    

    其余节点的pom:

             <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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-config-client</artifactId>
            </dependency>
    
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-bus-amqp</artifactId>
            </dependency>
    

    其余节点的bootstrap.yml配置:

    server:
      port: 8763
    
    eureka:
      client:
        service-url:
          # 将自己注册到eureka server的地址
          defaultZone: http://localhost:8761/eureka/
    
    spring:
      application:
        # 应用的名字
        name: order
      cloud:
        config:
          discovery:
            enabled: true
            service-id: config
          profile: dev
    

    如果是远端的RabbitMQ,各个节点,包括config server都要加入以下配置:

    spring:
      rabbitmq:
        host: xxx
        port: 5672
        username: xxx
        password: xxx
    

    各个节点的环境变量或者配置类使用了注解@RefreshScope
    刷新节点,使用postman发送post请求:
    http://127.0.0.1:8888/actuator/bus-refresh

    相关文章

      网友评论

        本文标题:2019-08-27/spring cloud bus 自动刷新

        本文链接:https://www.haomeiwen.com/subject/ohfuectx.html