config: 分布式配置中心 & bus: 消息总线


每一个应用程序在运行时都需要相应的yml配置,分布式架构下多个服务器和应用服务面临着多个配置文件,在修改和发布上难度较大,需要有一个管理中心来统一管理,优雅的解决了配置的动态变更、持久化、运维成本等问题

流程:
分布式配置中心去远程仓库将创建好的yml文件读取,application client去分布式配置中心获取配置

Spring Cloud Config: spring cloud config server和spring cloud config client
基于Http协议

hello world

一.在gitee上创建一个仓库,创建application.yml,如下配置:

spring:
  datasource:
    url: jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&serverTimezone=Asia/Shanghai
    driver-class-name: com.mysql.cj.jdbc.Driver
    username: root
    password: fsfs

二.创建一个项目config_server
1.导入依赖:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

2.yml配置:

server:
  port: 8888

# 增加分布式配置中心服务端配置。连接GIT仓库
spring:
  cloud: # spring cloud常用配置前置
    config: # 分布式配置中心配置前置
      server: # 服务端配置
        git: # git文件仓库配置
          uri: https://gitee.com/xxxx/spring_cloud_config.git # git仓库具体地址
          #username: xxxxxx # 私有仓库必须配置用户名和密码。
          #password: xxxxxx # 公开仓库可以省略用户名和密码配置。

3.启动类上加@EnableConfigServer注解

@SpringBootApplication
@EnableConfigServer
public class SpringcloudConfigApplication {
    
    
    public static void main(String[] args) {
    
    
        SpringApplication.run(SpringcloudConfigApplication.class, args);
    }
}

三.访问测试:
在浏览器地址栏输入 http://localhost:8888/name/profile/label ,基于Config Server获取需要的配置文件。
name-必要restful参数,代表配置文件主体名称,及一般为application
profile-必要restful参数,代表配置文件扩展环境名称,默认读取default环境扩展配置,即application-xx.yml中的xx
label-可选参数,代表配置文件所在GIT分支名称,默认null,相当于master分支

访问 http://localhost:8888/application/default(没有可以不写)/master(分支名称)

四.创建项目config_client获取config_server的配置信息
1.导入依赖

扫描二维码关注公众号,回复: 16176891 查看本文章
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

2.创建bootstrap.yml,不使用application.yml,防止冲突
新配置文件 bootstrap.yml | properties。是spring cloud config技术支持的新配置文件。
配置文件由config分布式配置中心客户端读取,并请求分布式配置中心服务端,查询获取配置文件之后,Spring Boot根据配置文件,初始化环境

spring:
  cloud:
    config: # spring cloud config 客户端配置
      uri: http://localhost:8888 # 分布式配置中心服务端地址。 默认http://localhost:8888
      name: application # 要读取的配置文件名,默认是spring.application.name的配置值,如果没有配置,默认application
      # profile: default # 要读取的配置文件环境是什么,默认default
      label: master # 要读取的配置文件所在分支名称。默认null。从主干分支获取文件。

四.改变远程仓库的端口server.port,
反复启动cofig_client项目,观察端口号变化

热刷新

1.添加导入依赖

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

2.Config Client新增配置内容

management:
  endpoints:
    web:
      exposure:
        include: refresh,info,health

3.Config Client修改服务实现
在使用远程配置内容的类上(案例中的Service,放在controler上无效)增加新注解:

@Service
@RefreshScope
public class ConfigClientServiceImpl implements ConfigClientService {
    
    
    @Value("${my.content}")
    private String content;
    @Override
    public String test() {
    
    
        System.out.println("content = " + content);
        return content;
    }
}

4.测试热刷新
使用PostMan工具,发送POST
访问 http://localhost:8080/actuator/refresh 访问改地址,才能刷新注入,实际已经刷新了
再次访问 http://localhost:8080/test

Spring Cloud Bus:消息总线

Spring Cloud Bus集成了市面上常见的RabbitMQ和Kafka等消息代理。其会连接微服务系统中所有拥有Bus总线机制的节点,当有数据变更的时候,会通过消息中间件使用消息广播的方式通知所有的微服务节点同步更新数据。(如:微服务配置更新等)

基于Bus消息总线实现热刷新功能,需要在所有的Eureka Client端应用中增加spring-cloud-starter-bus-amqp依赖,这个依赖是消息总线集成的RabbitMQ消息同步组件。基于消息总线的热刷新同样是通过actuator实现的,所以需要spring-boot-starter-actuator启动器依赖。
1.在Config Client中增加依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
   <groupId>org.springframework.cloud</groupId>
   <artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- 总线技术中的amqp相关依赖。 -->
<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

2.编辑配置文件: 编辑Config Client中的配置文件。

spring:
  rabbitmq:
    host: localhost
    username: guest
    password: giest
management:
  endpoints:
    enabled-by-default: true
    web:
      exposure:
        include: bus-refresh,info,health

3.测试
消息总线Bus基于Actuator对外提供了热刷新服务,服务地址是:http://localhost:8080/actuator/bus-refresh。此服务只能使用POST方式请求,可以使用PostMan测试。

猜你喜欢

转载自blog.csdn.net/m0_56182317/article/details/130245004