SpringCloud -- Config、Bus解析

x33g5p2x  于2022-02-07 转载在 Spring  
字(11.0k)|赞(0)|评价(0)|浏览(224)

1、Config

1.1、概述简介

1. 分布式面临的问题

  • 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。

2. Config是什么

  • Spring Cloud Config分为服务端和客户端两部分。
  • 服务端也成为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口。
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。

3. 能干嘛

  1. 集中管理配置文件
  2. 不同环境不同配置,动态化的配置更新,分环境部署比如dev/test/prod/beta/release
  3. 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  4. 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  5. 将配置信息以REST接口的形式暴露
    官网地址

1.2、Config服务端配置与测试

1. 建Module

  • Module的名称为cloud-config-center-3344

2. 改POM

<dependencies>

        <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>
        <dependency>
            <groupId>com.xiao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. 改YML

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  profiles:
    active: dev
  cloud:
    config:
      server:
        git:
          uri:  https://gitee.com/henryxjh/springcloud-config.git # url地址
          search-paths: # 搜索路径
            - springcloud-config
      label: master  # 配置的是主分支


eureka:
  client:
    service-url:
      defaultZone:  http://localhost:7001/eureka

4. 主启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

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

5. 测试结果

(1)、其他的访问路径格式

/{application}/{profile}[/{label}]
/{application}-{profile}.yml   ## 推荐使用这个
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

1.3、Config客户端配置与测试

1. 建Module

  • Module的名称为cloud-config-client-3355

2. 改POM

<dependencies>

        <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>
        <dependency>
            <groupId>com.xiao</groupId>
            <artifactId>cloud-api-commons</artifactId>
            <version>1.0-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

3. 改YML

server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344

eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
  • 要将application.yaml文件名称改为bootstrap.yaml是很关键的,bootstrap.yaml是比application.yaml优先加载的。且它是系统级的,优先级更高。
  • SpringCloud会创建一个Bootstrap Context,作为Spring应用的Application Context的父上下文。初始化的时候,Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment

4. 主启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

5. Controller类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

6. 测试结果

  • 如图所示,我们成功的从相应的配置文件中读取到内容

1.4、Config客户端之动态刷新(手动版)

1.4.1、面临的问题

  1. Linux运维修改GitHub上的配置文件内容做调整
  2. 刷新3344,发现ConfigServer配置中心立刻响应
  3. 刷新3355,发现ConfigServer客户端没有任何响应
  4. 3355没有变化除非自己重启或者重新加载

1.4.2、动态刷新配置与测试

1. POM引入actuator监控

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

2. 在yml文件中添加以下代码

management:
  endpoints:
    web:
      exposure:
        include: "*"

3. 修改controller类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return configInfo;
    }
}

4. 需要发送一个请求刷新客户端

curl -X POST "http://localhost:3355/actuator/refresh"

5. 测试结果

(1)、访问3344端口

(2)、访问3355端口

2、Bus

2.1、概述简介

1. 是什么

  • Spring Cloud Bus是用来将分布式系统的节点与轻量级消息系统连接起来的框架,它整合了Java的事件处理机制和消息中间件的功能。Spring Cloud Bus目前支持RabbitMQKafka
  • 在微服务架构的系统中,通常会使用轻量级的消息代理来构建一个公用的消息主题,并让系统中所有微服务实例都连接上来。由于该主题中产生的消息会被所有实例监听和消费,所以称它为消息总线。在总线的各个实例,都可以方便地广播一些需要让其他连接在该主题上的实例都知道的消息。
  • 其原理是ConfigClient实例都监听MQ中同一个topic默认是SpringCloudBus。当一个服务刷新数据的时候,他会把这个消息放入到Topic中,这样其它监听到同一Topic的服务就能得到通知,然后去更新自身的配置。

2. 能干嘛

  • Spring Cloud Bus能管理和传播分布式系统间的消息,就像一个分布式执行器,可用于广播状态更改、时间推送等,也可以当作微服务间的通信通道。

2.2、全局广播形式的动态刷新

2.2.1、新添加一个客户端

1. 建Module

  • Module的名称为cloud-config-client-3366

2. 改POM

<dependencies>

    <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>
    <dependency>
        <groupId>com.atguigu.springcloud</groupId>
        <artifactId>cloud-api-commons</artifactId>
        <version>${project.version}</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

3. 改YML

server:
  port: 3366

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      name: config
      profile: dev
      uri: http://localhost:3344
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka
management:
  endpoints:
    web:
      exposure:
        include: "*"

4. 主启动

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

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

5. controller类

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${spring.application.name}")
    private String serverPort;

    @Value("${config.info}")
    private String configInfo;

    @GetMapping("/configInfo")
    public String getConfigInfo(){
        return "serverPort:"+serverPort+"\t\n\n configInfo: "+configInfo;
    }

}

2.2.2、两种导致全局动态刷新的方式

  1. 利用消息总线触发一个客户端/bus/refresh,而刷新所有客户端的配置

  1. 利用消息总线触发一个服务端ConfigServer/bus/refresh端点,而刷新所有客户端的配置(更加推荐)

(1)、第二种架构方式更合适的原因

  1. 打破了微服务的职责单一性,因为微服务本身是业务模块,它本不应该承担配置刷新职责
  2. 破坏了微服务各节点的对等性
  3. 有一定的局限性。例如,微服务在迁移时,它的网络地址常常会发生变化,此时如果想要做到自动刷新,那就会增加更多的修改。

2.2.3、添加消息总线支持的配置

1. 给cloud-config-center-3344配置中心服务端添加消息总线支持

(1)、POM

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

(2)、新添加的YML内容

rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: 123456

2. 给cloud-config-center-3355客户端添加消息总线支持

(1)、POM

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

(2)、新添加的YML内容

rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: 123456

3. 给cloud-config-center-3366客户端添加消息总线支持

(1)、POM

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

(2)、新添加的YML内容

rabbitmq:
    host: localhost
    port: 5672
    username: admin
    password: 123456

2.2.4、测试结果

  • 在发送该请求curl -X POST "http://localhost:3344/actuator/bus-refresh"之后的测试结果如下

  • 一次修改,广播通知,处处生效

2.3、定点通知形式的动态刷新

  • 2.2小节上述环境,只通知3355不通知3366的请求如下:
curl -X POST "http://localhost:3344/actuator/bus-refresh/config-client:3355"

相关文章