亲宝软件园·资讯

展开

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

Coding不良人 人气:0
想了解spring cloud config和bus组件实现自动刷新功能的相关内容吗,Coding不良人在本文为您仔细讲解spring cloud config自动刷新的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:spring,cloud,config自动刷新,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会有一条临时队列

image-20211030161511816

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

image-20211030161433784

运行:

写了一个测试方法

image-20211030161748091

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

image-20211030161827684

没问题

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

向config服务端发送post请求

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

具体路径根据项目来

在gitee的config管理中,点击webhooks

image-20211030171402520

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

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

加载全部内容

相关教程
猜你喜欢
用户评论