1、介绍

1、概述

官网:https://cloud.spring.io/spring-cloud-config/reference/html/

微服务意味着要将单体应用中的业务拆分成一个一个子服务,每个服务的粒度相对较小,因此系统中会出现大量的 服务。由于每个服务都需要必要的配置才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。Spring Cloud为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同的微服务应用的所有环境提供了一个中心化的外部配置。

2、能干什么

1、集中管理配置文件

2、不同环境不同配置,动态化的配置更新,分环境比如dev/test/prod/beta/release

3、运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心同意拉去配置自己的信息

4、当配置发生改变时,服务不需要重启即可感知到配置的变化并应用新的配置

5、将配置信息以REST接口的形式暴露
业务配置容器 业务配置中心_后端

2、配置Spring Cloud Config服务端-新建MODULE

1、POM

<!--引入Config配置中心服务端依赖-->
<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>

2、Yaml配置文件

server:
  port: 3344

spring:
  application:
    name:  cloud-config-center #注册进Eureka服务器的微服务名
  cloud:
    config:
      server:
        git:
          uri: https://xxxxx #git仓库名字
          #搜索目录
          search-paths:
            - springcloud-config
          username: xxx #公开库可以不用填写
          password: xxx #公开库可以不用填写
      #读取分支
      label: master

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

3、新建gitee仓库,并新建文件

业务配置容器 业务配置中心_spring_02

4、主启动类

添加@EnableConfigServer注解,表示是服务配置中心服务端

package org.jun;

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

/**
 * @author junfeng.lin
 * @date 2021/2/25 14:17
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenter3344Main {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenter3344Main.class,args);
    }
}

5、修改hosts文件(可选)

修改C:\Windows\System32\drivers\etc下的hosts文件

127.0.0.1 config3344.com

6、测试

先启动服务注册中心,这里我们选择eureka,再启动服务配置项目,访问http://config3344.com:3444/master/config-dev

业务配置容器 业务配置中心_客户端_03

读取配置规则
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties

其中profile指环境,如dev或prod;label指分支,如master或dev

3、配置Spring Cloud Config客户端-新建MODULE

1、POM

<!--引入Config配置中心客户端依赖-->
<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>

2、编写bootstrap.yml配置文件

application.yml是用户级的资源配置项,而bootstrap.yml是系统级的资源配置项,bootstrap.yml的优先级更高,它不会被本地配置覆盖。

SpringCloud会创建一个"Bootstrap Context",作为Spring应用的“Application Context"的父上下文。初始化的时候,“Bootstrap Context"负责从外部源加载配置属性并解析配置,这两个上下文共享一个从外部获取的"Environment”。"Bootstrap Context"和"Application Context"这两个上下文有不同的约定,所以新增一个bootstrap.yml文件,保证这两个上下文的配置分离。

编写bootstrap配置文件如下:

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
      discovery:
        enabled: true
        service-id: cloud-config-center	#配置中心的微服务名

#服务注册到eureka地址
eureka:
  client:
    service-url:
      defaultZone: http://eureka7002.com:7002/eureka

3、主启动类

package org.jun;

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

/**
 * @author junfeng.lin
 * @date 2021/2/26 11:29
 */
@SpringBootApplication
@EnableEurekaClient
public class ConfigClient3355Main {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClient3355Main.class,args);
    }
}

4、Controller

获取配置中心的配置信息

package org.jun.Controller;

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

/**
 * @author junfeng.lin
 * @date 2021/2/26 13:28
 */
@RestController
@RefreshScope
public class ConfigClientController {

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

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

}

5、测试

业务配置容器 业务配置中心_配置文件_04
业务配置容器 业务配置中心_spring_05

通过3355客户端可以通过3344服务端访问到从gitee获取的配置信息

报错:java.lang.IllegalStateException: No instances found of configserver (configserver)

原因:没有找到config配置中心实例,服务启动顺序有误

解决方案:先启动服务注册中心,再启动服务配置中心服务端,再启动服务配置中心客户端

4、Config客户端的动态刷新

为了避免每次远端更新配置信息都需要重启客户端微服务3355来加载更新的配置信息,我们需要使用动态刷新:

1、修改3355模块,在POM中引入actuator监控:

其实actuator依赖几乎除了是网关的微服务外都得加。

<dependency>
    <groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2、修改配置文件,暴露监控端口:

在bootstrap.yml中加入如下配置暴露监控断点:

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: "*"
3、@RefreshScope注解

在业务类Controller上添加@RefreshScope注解使客户端服务具有刷新功能

4、手动发送POST请求刷新客户端3355

该刷新请求必须发送后,客户端才能获得刷新后的信息,刷新客户端的请求必须是POST请求:

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