SpringCloudConfig-手动重新获取配置文件内容(单个服务通知+通过消息队列广播)

时间:2020-07-12
本文章向大家介绍SpringCloudConfig-手动重新获取配置文件内容(单个服务通知+通过消息队列广播),主要包括SpringCloudConfig-手动重新获取配置文件内容(单个服务通知+通过消息队列广播)使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一、SpringCloudConfig定义

spring cloud config是一个基于http协议的远程配置实现方式。通过统一的配置管理服务器进行配置管理,客户端通过https协议主动的拉取服务的的配置信息,完成配置获取。

二、为什么要使用监听的形式重新获取配置文件内容,而不是重启服务

1、无需重启服务,方便管理

三、使用(注册中心内容不再展示)

1、单个服务通知的形式来更新配置文件内容

  (1)、远程gitHub内容(config-client.yml)

spring:
  profiles:
    active: dev
---
server:
  port: 8081
spring:
  profiles: dev
  application:
    name: demo-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
    # 是否将自己注册为服务
    register-with-eureka: true
    # 需要检索检索服务
    fetch-registry: true
name: test
---
server:
  port: 8082
spring:
  profiles: test
  application:
    name: demo-client
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
    # 是否将自己注册为服务
    register-with-eureka: true
    # 需要检索检索服务
    fetch-registry: true
name: test

(2)、ConfigServer的配置

  1、pom.xml的引用如下:

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

  

  2、直接上配置文件内容(application.yml)

spring:
  application:
    name: demo-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/SweetPiglet/spring-cloud-config.git
          username: your username
          password: your password
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8080/eureka/
    # 是否注册为服务
    register-with-eureka: true
    # 是否进行扫描
    fetch-registry: true

(3)、ConfigClient配置内容

  1、pom.xml的配置如下:

                <!--引入监控-->
          <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
                <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>                

  2、配置文件的配置(bootstrap.yml)

# 引入ConfigServer的内容
spring:
  cloud:
    config:
      uri: http://localhost:9000/
      name: config-client
      profile: dev
      label: master
# 暴露监控点 management: endpoints: web: exposure: include: "*"

  3、controller层的内容

/**
 * wsq
 */
@RestController
@RefreshScope
public class TestController {
    # 需要刷新什么属性,就写什么属性
    @Value("${name}")
    private String name;
    @GetMapping("/test")
    public String test(){
        return name;
    }
}

(4)、运行程序

首先启动eurekaserver,之后启动configserver,最后启动configclient。

之后进行接口的访问:

http://localhost:9000/config-client-dev.yml

http://localhost:8081/test

之后修改配置文件的内容,将name原来的test改成test1

然后在window黑窗口中输入:

curl -X POST '''http://localhost:8081/actuator/refresh'

之后再次访问连接,查看变化,发现没有重启服务依然可以达到刷新配置文件的效果

2、监听+消息队列来实现配置的更新

(1)、出现原因

如果运维工程师修改了很多个配置文件,也就是说需要访问每一个服务,当然可以写一个脚本,但是呢,如果有服务的增加就需要改动脚本,那么有没有一种不需要进行配置的形式呢

(2)、作用

运维工程师直接通知一下configserver,configserver通过消息队列的topic形式,发一个通知,只要是订阅了该通知的,都可以收到通知,进行相应配置的更新

(3)、做法

  1、首先,rabbitmq和erlang的安装,不多说自行百度

  2、基于上面的代码,进行微调即可

  configserver需要添加的内容如下:

  pom.yml

           <!--监听-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

  

  application.yml

# 连接rabbitmq
spring:  
    rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
# 配置监听
management:
  endpoints:
    web:
      exposure:
        include: 'bus-refresh'

  

  configclient的变更如下:

  pom.xml

            <!--rabbitmq-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>

 

 bootstarp.yml

# 引入rabbitmq
spring:  
    rabbitmq:
        host: 127.0.0.1
        port: 5672
        username: guest
        password: guest
# 暴露监控点
management:
  endpoints:
    web:
      exposure:
        include: "*"

(4)测试

首先启动eurekaserver,之后启动configserver,最后启动configclient。

之后进行接口的访问:

http://localhost:9000/config-client-dev.yml

http://localhost:8081/test

之后修改配置文件的内容,将name原来的test改成test1

然后在window黑窗口中输入:

curl -X POST '''http://localhost:9000/actuator/bus-refresh'

之后再次访问连接,查看变化,发现没有重启服务依然可以达到刷新配置文件的效果

完工

三、最后提一下定点通知:

curl -X POST '''http://localhost:9000/actuator/bus-refresh/config-client:8081'

也就是后面加上服务名加端口的形式来进行服务的定点刷新

原文地址:https://www.cnblogs.com/mcjhcnblogs/p/13286944.html