Springcloud config 分布式配置中心

微服务把单一服务拆成一个一个的子服务,每个服务粒度相对较小。在系统中会出现大量的服务。每个服务都需要必要的配置文件。

Springcloud config 为微服务架构中的微服务提供集中化的外部配置支持。 配置服务器为各个不同的微服务应用提供了一个中心化的配置。

作用:

​ 集中管理配置文件

​1. 在不同的环境配置。 动态的配置更新。 分环境 dev test prod

​2.在运行期间动态调整配置。 动态调整tomcat 线程池

3.当配置文件发生了改变的时候。 服务不需要重新启动

4.将配置信息以rest接口进行暴露  动态访问刷新机制

 

1.引pom.xml

<dependencies>
        <!--添加消息总线RabbitMQ支持-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</artifactId>
        </dependency>
        <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>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>

主要是spring-boot-starter-actuator监控这个依赖

 

2.新建yml配置文件

server:
  port: 3344


spring:
  application:
    name: cloud-config-server #注册到eureka
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/***/springcloud-config.git
          # 搜索目录
          search-paths:
            - springcloud-config
      label: master

  rabbitmq:
    port: 5672
    username: guest
    password: guest
    host: localhost

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

#暴露监控的端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

主要是配置 暴露监控端口

3.新建main方法

@Slf4j
@SpringBootApplication
@EnableConfigServer
public class ConfigMain3344 {
    public static void main(String[] args) {
        SpringApplication.run( ConfigMain3344.class,args);
        log.info("****************ConfigMain3344 启动 ************ ");
    }
@EnableConfigServer这个注解集成了eureka功能

分布式配置中心服务就新建好了

二。再建两个客户端服务测试

1.引pom.xml
<dependencies>
        <!--添加消息总线RabbitMQ支持-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-amqp</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>
        <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>

2.新建main方法

@SpringBootApplication
@Slf4j
@EnableEurekaClient
public class ConfigClientMain3355 {
    public static void main(String[] args) {
        SpringApplication.run( ConfigClientMain3355.class,args);
        log.info("****************ConfigClientMain3355 启动 ************ ");
    }
}

3.新建controller调用测试

@RefreshScope
@RestController
@Slf4j
public class ConfigController {
    @Value("${config.info}")
    private String configInfo;

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

重点是@RefreshScope这个注解让配置文件中自定义的变量能通过@Value注解正常注入

4.建yml配置文件

server:
  port: 3355


spring:
  application:
    name: config-client
  cloud:
    config:
      label: master # 分支名字
      name: config  # 配置文件名字
      profile: dev  # 读取后缀
      uri: http://localhost:3344 # 配置中心的地址

  rabbitmq:
    port: 5672
    username: guest
    password: guest
    host: localhost

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



#暴露监控的端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

这里YML文件名必须是bootstrap,他比application先加载,保证'Bootstrap Context'和'Application Context'配置的分离.

读取配置规则:

eq:  master config-dev.yml

/{lable}/{application}-{profile}.yml  最常用

/{application}-{profile}.yml

/{application}/{profile}/{lable}

label : 分支 branch

name 服务名字

profile: 环境 dev test prod

 

测试:当配置文件发生变化后,只能手动刷新用POST请求访问  http://localhost:3355/actuator/refresh

再访问客户端接口就能看到改变,缺点就是每个微服务都要手动刷新

 

三。动态刷新:

一次广播处处生效刷新

配置中心服务刷新一次,所有微服务的配置都刷新

SpringCloud Bus 消息总线

Springcloud bus 配合 Spring Cloud config 使用 可以实现配置的动态刷新

Spring cloud bus 目前只支持Rabbitmq 和 Kafka

Spring cloud bus 能管理和传播分布式系统的消息。 就像一个执行器 广播状态 事件推送等 。也可以当作微服务 的通信通道 。

在微服务架构种。 通常会使用轻量级消息代理来构建一个共用消息主题。

系统中的所有微服务实例都链接上来。 由于该主题产生的消息会被所有的实例监听和消费,所以被称为消息总线。

利用消息总线触发一个服务端configserver(配置中心服务) 的/bus/refresh 端点。 从而刷新客户端配置

1.pom.xml加依赖,上文中已有

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

2.配置yml,上文中已配

rabbitmq:
  port: 5672
  username: guest
  password: guest
  host: localhost



#暴露监控的端点
management:
  endpoints:
    web:
      exposure:
        include: "*"

3.测试

发送POST请求:

localhost:3344/actuator/bus-refresh   所有微服务的配置都刷新

localhost:3344/actuator/bus-refresh/{destination}  刷新指定微服务的配置

localhost:3344/actuator/bus-refresh/服务名:端口号

eq:localhost:3344/actuator/bus-refresh/config-client:3355

调用微服务具体接口可看到结果

posted @ 2020-08-25 20:24  neona  阅读(109)  评论(0编辑  收藏  举报