< 返回技术文档列表

spring cloud config和bus组件怎么实现自动刷新功能

发布时间:2021-11-07 01:00:03⊙投诉举报

本篇内容主要讲解“spring cloud config和bus组件怎么实现自动刷新功能”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“spring cloud config和bus组件怎么实现自动刷新功能”吧!

1.启动RabbitMQ

2.修改config服务端 依赖:(boot和cloud版本不要太高,不然刷新配置的请求不能用)

<dependencies>
    <!--consul-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>
    <!--健康检查-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
    <!--config-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <!--bus-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>
    <!--web-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

配置:

spring:
  cloud:
    consul:
      host: localhost
      port: 8500
    config:
      server:
        git:
          uri: https://gitee.com/xiang_pei/config.git
          default-label: master
  application:
    name: CONFIGSERVER
  rabbitmq:
    host: localhost
    port: 5672
    username: guest
    password: guest
    virtual-host: /

#暴露所有端点,便于发送更新配置请求
management:
  endpoints:
    web:
      exposure:
        include: "*"

启动:(启动类加@EnableConfigServer)mq会有一条临时队列

spring cloud config和bus组件怎么实现自动刷新功能

3.修改微服务(即客户端) 依赖:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-consul-discovery</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!--config client的依赖-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    <!--重新导入bootstrap-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bootstrap</artifactId>
    </dependency>
    <!--bus-->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-bus-amqp</artifactId>
    </dependency>

</dependencies>

配置:不变(rabbitmq的配置应该写到gitee上)bootstrap.properties

spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
spring.application.name=CONFIGCLIENT

spring.cloud.config.discovery.service-id=CONFIGSERVER
spring.cloud.config.discovery.enabled=true


spring.cloud.config.name=config
spring.cloud.config.label=master
spring.cloud.config.profile=dev

spring cloud config和bus组件怎么实现自动刷新功能

运行:

写了一个测试方法

spring cloud config和bus组件怎么实现自动刷新功能

name属性是gitee上的配置文件的name

spring cloud config和bus组件怎么实现自动刷新功能

没问题

4.利用webhooks实现自动刷新配置

向config服务端发送post请求

http://localhost:8080/actuator/bus-refresh

具体路径根据项目来

在gitee的config管理中,点击webhooks

spring cloud config和bus组件怎么实现自动刷新功能

url就填上面那个,因为需要公网才能测试,所以暂时不演示了。

设置完,改gitee的配置就会自动刷新了

到此,相信大家对“spring cloud config和bus组件怎么实现自动刷新功能”有了更深的了解,不妨来实际操作一番吧!这里是血鸟云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!


/template/Home/Zkeys/PC/Static