Spring Cloud 配置中心之Spring Cloud Config

一、概述

  • 官网
  • SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置
  • 简单来说,就是将配置文件的内容放到配置中心集中管理。
  • 分为服务端和客户端两部分。
  • 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息,加密/解密信息等访问接口
  • 客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息
  • 配置服务器的配置信息默认采用git存储,有利于对环境配置进行版本管理,并且可以通过git客户端工具来方便地管理和访问配置内容。
  • 可以分环境配置,不同的部署环境不同的配置文件。
  • 可以动态地调整配置,无需重启即可感知配置的修改。(需要发送一个刷新请求给客户端)

二、Config 服务端配置与测试

1. 创建git仓库

  • 可以创建GitHub或者Gitee仓库,在上面上传配置文件。

image.png

  • 配置文件可以上传yml。文件名前缀表示真正的name,后缀如dev表示部署环境。可以根据不同的部署环境选择不同的后缀。
  • 配置文件内容依据实际情况即可。格式就是yml的格式。

2. 导包

  • 最重要的当然是spring-cloud-config-server。也需要将注册中心eureka客户端的包导入。
<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>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. 配置文件

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/waynedu/springcloud-config.git # 也就是仓库下载的那个地址
        # 搜索目录
          search-paths:
            - springcloud-config # 写仓库的名字即可
      # 读取分支
      label: master

# 服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka
复制代码

image.png

  • 至此,可以将配置中心的配置导入到配置文件中,就像使用本地的配置一样。
  • 服务已经默认注册到注册中心了。虽然没有显式说明。

4. 主启动类

  • 值得注意的是,需要写一个@EnableConfigServer注解,表示这是个配置服务器。
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterMain3344 {
  public static void main(String[] args) {
    SpringApplication.run(ConfigCenterMain3344.class, args);
  }
}
复制代码

5. 读取配置内容

  • 可以在浏览器上,发送请求获取配置文件信息。
  • /{label}/{application}-{profile}.yml,比如说http://localhost:3344/master/config-dev.yml
  • /{application}-{profile}.yml,比如说http://localhost:3344/config-dev.yml,默认是master
  • /{application}/{profile}[/{label}],比如说http://localhost:3344/config/dev/master
  • label:分支(branch)
  • name :服务名
  • profiles:环境(dev/test/prod)

三、Config 客户端配置与测试

1. 导包

  • 同样的,需要导入spring-cloud-starter-config,与服务端导的包基本没什么区别。
<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>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. 配置文件

  • 首先,需要说明bootstrap.yml是系统级的,application.yml是用户级的配置文件,bootstrap.yml的配置不会被本地覆盖,具有更高优先级。bootstrap.ymlapplication.yml更先加载。
  • 因此,导入外部配置文件,最好在bootstrap.xml上导入
server:
  port: 3355

spring:
  application:
    name: config-client
  cloud:
    # Config客户端配置
    config:
      label: master # 分支名称
      name: config # 配置文件名称
      profile: dev # 读取后缀名称   上述3个综合:master分支上config-dev.yml的配置文件被读取http://config-3344.com:3344/master/config-dev.yml
      uri: http://localhost:3344 # 配置中心地址

# 服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://localhost:7001/eureka

# 暴露监控端点,这是动态刷新的关键
management:
  endpoints:
    web:
      exposure:
        include: "*"
复制代码

3. 主启动类

@EnableEurekaClient
@SpringBootApplication
public class ConfigClientMain8001 {
  public static void main(String[] args) {
    SpringApplication.run(ConfigClientMain8001.class, args);
  }
}
复制代码

4. 业务类

  • 尝试从配置文件中获取信息
@RestController
@Slf4j
@RefreshScope  // 允许刷新配置内容
public class ConfigController {
  @Value("${config.version}")
  Integer version;
  @Value("${config.info}")
  String info;

  @GetMapping("/config")
  public String config() {
    String result = "version: " + version + ", info: " + info;
    log.info(result);
    return result;
  }
}
复制代码

5. 动态刷新

  • 当git上面的配置文件内容发生变化的时候,服务端可以很快获取到,但是客户端还是无法感知。需要发送一条指令动态地刷新。curl -X POST "http://localhost:3355/actuator/refresh"
  • 这样,实现了不用重启也可以刷新客户端的配置内容。
  • 但也有一些问题,这非常麻烦。如果客户端数量很多,每个都需要手动刷新,就会带来麻烦。通过消息总线的发布订阅则可以很容易解决这个问题。
© 版权声明
THE END
喜欢就支持一下吧
点赞0 分享