Spring cloud 配置中心+消息总线 实现分布式配置不重启刷新

Spring cloud config

统一配置中心,管理所有微服务的配置,具体功能和使用方法请参考《Spring cloud 架构解析和框架搭建》https://blog.csdn.net/leonhongliang806/article/details/98170113,这里不重复说明。

本章节主要介绍如何搭建本地的配置中心(配置文件不存放在git上)

架构

在这里插入图片描述

  1. 所有的配置文件存放在config server所在的主机本地目录

  2. Config server加载配置文件

  3. Config server 在eureka中注册,可实现高可用

  4. Service 在eureka中注册,访问config server获取配置内容

实现

Eureka

搭建启动eureka server,具体参考《Spring cloud架构解析和框架搭建》,这里不赘述

Config

配置文件

主机下创建配置文件目录,并存放配置文件,如:

clientservice.yml

此配置文件用于某个service应用的配置

server:

  port:
9001

spring:

  profiles:
test

  application:

    name:
zmpdata-client-service

eureka:

  client:

    service-url:

      defaultZone: http://eureka7001.com:7001/eureka

  instance:

    instance-id: zmpdata-client-service-9001 #服务访问实例名

    prefer-ip-address: true #访问实例以 ip 显示

#业务配置

test: 111



---



server:

  port:
9001

spring:

  profiles:
pro

  application:

   
name: zmpdata-client-service

eureka:

  client:

    service-url:

      defaultZone: http://eureka7001.com:7001/eureka

  instance:

    instance-id: zmpdata-client-service-9001 #服务访问实例名

    prefer-ip-address: true #访问实例以 ip 显示

#业务配置

test: 222

配置文件中可以区分为多个profile,以—划分,例子中为test环境的配置和product环境的配置

Profiles:区分配置文件环境

application.yml

config server的配置文件

server:

  port: 6688



#注册到eureka

eureka:

  client:

    service-url:

      defaultZone: http://eureka7001.com:7001/eureka

  instance:

    instance-id: zmpdata-config-6688 #服务访问实例名

    prefer-ip-address: true
#访问实例以 ip 显示



spring:

  application:

    name:
zmpdata-config

  cloud:

    config:

      server:

        native:

          search-locations:
C:/iwhalecloud/dev/idea_workspace/ldt3-zmpdata-springcloud/config-home/etc

  profiles:

    active:
native  # 配置使用本地储存

注册到eureka中,服务名为zmpdata-config

注意与存放git的不同配置:

Active需要配置为native表示使用本地存储,非git

Search-locations 配置为本地存放配置文件的路径

Application

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import
org.springframework.cloud.client.discovery.EnableDiscoveryClient;

import
org.springframework.cloud.config.server.EnableConfigServer;

import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

 

@SpringBootApplication

@EnableConfigServer

@EnableEurekaClient

public class Config6688_App {

    public static void main(String[] args) {

	springApplication. run (Config6688_App.class, args);

    }

}

启用注解

启动config server

查看是否已经注册到eureka

在这里插入图片描述
查看是否可以获取到本地配置
在这里插入图片描述
在这里插入图片描述

Service

bootstrap.yml

该服务应用的配置文件,这里使用bootstrap是因为bootstrap.yml比application.yml的优先级更高,所以外部环境的基本配置放到bootstrap中,需要经常变更的或业务配置放到clientservice.yml中

spring:

  cloud:

    config:

      #直接通过uri访问

      #uri: http://localhost:6688



      #通过eureka访问

      discovery:

        enabled: true                   #开启配置服务发现

        serviceId: zmpdata-config       #配置中心服务名

      #寻找配置文件(,)隔开是多个配置文件

      name: clientservice

      profile: test

      #profile: pro

      enabled: true

eureka:

  client:

    service-url:

      defaultZone: http://eureka7001.com:7001/eureka

如果config server没有注册到eureka中可以直接通过uri访问config
server

Name:配置该应用需要的配置文件名

Profile:配置文件的profile

实现

在接口实现中取配置文件中的字段并展示
在这里插入图片描述

启动service

Bootstrap里配置的profile是test,查看调用

在这里插入图片描述

结论

到目前为止实现了分布式服务统一配置中心的功能,但是如果要修改配置,需要修改本地的配置文件clientservice.yml,然后重启service才能生效,下面介绍如何结合spring cloud bus实现不重启配置刷新

Spring cloud bus

Spring cloud bus通过轻量消息代理连接各个分布的节点。这会用在广播状态的变化(例如配置变化)或者其他的消息指令。Spring bus的一个核心思想是通过分布式的启动器对spring boot应用进行扩展,也可以用来建立一个多个应用之间的通信频道。

Spring cloud bus可以将它理解为管理和传播所有分布式项目中的消息,其实本质是利用了MQ的广播机制在分布式的系统中传播消息,目前常用的有Kafka和RabbitMQ。利用bus的机制可以做很多的事情,其中配置中心客户端刷新就是典型的应用场景之一。

架构

在这里插入图片描述

  1. 修改配置文件

  2. Post请求bus/refresh通知config server配置更新(spring boot1.x 调用bus/refresh,springboot2.x 调用actuator/bus-refresh)

  3. Config server发送更新消息给bus

  4. Bus通过MQ 将消息广播给每个服务

  5. Service收到MQ的消息,请求config server获取更新的配置

  6. Config service获取最新配置给service

实现

RabbitMQ

安装

docker
pull rabbitmq:3.7.7-management

docker run -d --name RabbitMQ -p
5672:5672 -p 15672:15672 -e RABBITMQ_DEFAULT_USER=admin -e
RABBITMQ_DEFAULT_PASS=123 rabbitmq:3.7.7-management

启动

访问:

http://ip:15672/

在这里插入图片描述

改造config

pom.xml

增加依赖

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-bus-amqp</artifactId>

</dependency>

Bus-amqp 使用amqp消息和MQ通信

application.yml

spring:

  rabbitmq:

    host: 172.16.23.126

    port: 5672

    username: admin

    password: 123



#暴露接口用于更新配置(springboot2.x)

#management:

  #endpoints:

    #web:

      #exposure:

        #include: "refresh"

#动态刷新配置 ---需要忽略权限拦截(springboot1.x)

management:

  security:

    enabled: false

增加rabbitmq的配置

Springboot 版本1.x 关闭安全验证

Springboot 版本 2.x 暴露post的刷新接口

重启config

declaring queue for inbound: springCloudBus.anonymous.oivT-ChITjKcIE9i6sMWgw, bound
to: springCloudBus

started
inbound.springCloudBus.anonymous.oivT-ChITjKcIE9i6sMWgw

Adding
{message-handler:inbound.springCloudBus.default} as a subscriber to the ‘bridge.springCloudBus’
channel

MQ中增加消息队列

改造service

pom.xml

增加依赖

<dependency>

    <groupId>org.springframework.cloud</groupId>

    <artifactId>spring-cloud-starter-bus-amqp</artifactId>

</dependency>

bootstrap.yml

spring:

  rabbitmq:

    host: 172.16.23.126

    port: 5672

    username: admin

    password: 123



#动态刷新配置 ---需要忽略权限拦截(springboot1.x)

management:

  security:

    enabled: false

增加rabbitmq配置

实现

在需要用到刷新配置项的地方增加注解@RefreshScope

// 需要动态刷新配置,加上该注解

@RefreshScope

public class TransactionServiceImpl implements TransactionService {



    @Value("${test}")

    private String test;



    @Override public String getTran() {

        return test;

    }

重启service

declaring queue for inbound: springCloudBus.anonymous.4p1kfURgSfK7DsNO6H4LBg, bound
to: springCloudBus

started inbound.springCloudBus.anonymous.4p1kfURgSfK7DsNO6H4LBg

Adding
{message-handler:inbound.springCloudBus.default} as a subscriber to the
‘bridge.springCloudBus’ channel

MQ中增加消息队列

配置刷新

第一次调用service
在这里插入图片描述

修改配置文件

配置项test改为111333

在这里插入图片描述

Post调用/bus/refresh

(springboot2.x 调用actuator/bus-refresh)

在这里插入图片描述

查看service的日志,test配置项已经刷新:

DiscoveryClient_ZMPDATA-CLIENT-SERVICE/zmpdata-client-service-9001:
registering service…

Received remote refresh
request. Keys refreshed [test]

第二次调用service
在这里插入图片描述

结论

实现不重启服务配置动态刷新

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值