微服务-配置中心Spring Cloud Config

1. 为什么需要配置中心

微服务要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大量的服务。由于每个服务都需要必要的配置信息才能运行,每一个微服务自己带着一个application.yml,成千上百个配置文件的管理就比较头疼了. 所以一套集中式的、动态的配置管理设施是必不可少的。SpringCloud提供了ConfigServer来解决这个问题.

2. 是什么

官网资料→
SpringCloud Config为微服务架构中的微服务提供集中化的外部配置支持,配置服务器为各个不同微服务应用的所有环境提供了一个中心化的外部配置。SpringCloud Config分为服务端和客户端两部分。

服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器(git或svn)并为客户端提供获取配置信息,加密/解密信息等访问接口.

客户端则是通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息。配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
在这里插入图片描述

SpringCloud Config功能

  • 集中管理配置文件
  • 不同环境不同配置,动态化的配置更新,分环境部署比如 dev/test/prod/release
  • 运行期间动态调整配置,不再需要在每个服务部署的机器上编写配置文件,服务会向配置中心统一拉取配置自己的信息
  • 当配置发生变动时,服务不需要重启即可感知到配置的变化并应用新的配置
  • 将配置信息以REST接口的形式暴露, 使用curl访问刷新均可(post请求)

3. Config服务端配置

3.1 与gitHub整合配置

用你自己的账号在GitHub或码云上新建一个名为springcloud-config的新Repository.
在这里插入图片描述
将刚刚创建的远程配置仓库clone到本地. git clone https://gitee.com/wang-qz/springcloud-config.git
注意配置文件的编码, utf-8.
在这里插入图片描述
远程配置内容
在这里插入图片描述

3.2 创建配置中心服务端

新增配置中心服务端模块[cloud-config-center-3344].

3.2.1 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>atguigu-cloud-2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-center-3344</artifactId>

    <dependencies>
        <!--spring-cloud-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>

        <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>
</project>

3.2.2 编写配置

application.yml

server:
  port: 3344
spring:
  application:
    name: cloud-config-center
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/wang-qz/springcloud-config.git # 远程配置仓库
          search-paths: springcloud-config #搜索目录
          default-label: master # 读取分支
# eureka配置
eureka:
  instance:
    hostname: localhost
    instance-id: cloud-config-center-3344
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://www.eureka01.com:7001/eureka,http://www.eureka02.com:7002/eureka

3.2.3 编写启动类

com.atguigu.springcloud.ConfigCenterApplication

package com.atguigu.springcloud;

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

/**
 * 类描述:配置中心服务端启动类
 * @Author wang_qz
 * @Date 2021/11/21 21:49
 * @Version 1.0
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigCenterApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigCenterApplication.class);
    }
}

3.2.4 测试拉取远程配置信息

测试通过Config微服务是否可以从Gitee上获取配置内容, 有以下几种访问规则.

  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.yml
  • /{application}/{profile}[/{label}

访问 http://localhost:3344/master/config-dev.yml, 表示读取master分支的config-dev.yml文件.
在这里插入图片描述
访问 http://localhost:3344/config-dev.yml, 因为配置中心的配置文件中指定了默认读取分支为master, 所以访问时可以
省略label.
在这里插入图片描述
访问 http://localhost:3344/config/dev/master
在这里插入图片描述

3.3 创建Config客户端

新增Config客户端模块[cloud-config-client-3355]

3.3.1 依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <artifactId>atguigu-cloud-2020</artifactId>
        <groupId>com.atguigu.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../../pom.xml</relativePath>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>cloud-config-client-3355</artifactId>

    <dependencies>
        <!--spring-cloud-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>
        <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>
</project>

3.3.2 编写配置

bootstrap.yml

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

#eureka配置
eureka:
  instance:
    hostname: localhost
    instance-id: config-client-3355
    prefer-ip-address: true
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://www.eureka01.com:7001/eureka,http://www.eureka02.com:7002/eureka

applicaiton.yml 和 bootstrap.yml

  • applicaiton.yml是用户级的资源配置项; bootstrap.yml是系统级的,优先级更加高.
  • Spring Cloud会创建一个“Bootstrap Context”,作为Spring应用的Application Context的父上下文。初始化的时候,
    Bootstrap Context负责从外部源加载配置属性并解析配置。这两个上下文共享一个从外部获取的Environment
  • Bootstrap属性有高优先级,默认情况下,它们不会被本地配置覆盖。 Bootstrap contextApplication Context有着不同的约定,所以新增了一个bootstrap.yml文件,保证Bootstrap ContextApplication Context配置的分离。
  • 将Client模块下的application.yml文件改为bootstrap.yml,这是很关键的,因为bootstrap.yml是比application.yml先加载的。bootstrap.yml优先级高于application.yml

3.3.3 编写启动类

com.atguigu.springcloud.ConfigClientApplication

package com.atguigu.springcloud;

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

/**
 * 类描述:配置中心客户端启动类
 * @Author wang_qz
 * @Date 2021/11/21 22:08
 * @Version 1.0
 */
@SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class);
    }
}

3.3.4 编写业务类

com.atguigu.springcloud.controller.ConfigClientController

package com.atguigu.springcloud.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;

/**
 * 类描述:
 * @Author wang_qz
 * @Date 2021/11/21 22:15
 * @Version 1.0
 */
@RestController
public class ConfigClientController {
    @Value("${config.info}")
    private String configInfo;

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

}

3.3.5 测试

启动3355作为Client准备访问, http://localhost:3355/configInfo , 成功实现了客户端3355访问SpringCloud Config3344通过GitHub获取配置信息.
在这里插入图片描述
修改config-dev.yml配置并提交到GitHub中,比如加个变量age或者版本号version.
刷新3344,发现ConfigServer配置中心立刻响应.
刷新3355,发现ConfigClient客户端没有任何响应. 3355没有变化除非自己重启或者重新加载. 难到每次运维修改配置文件,客户端都需要重启?简直是噩梦般的存在. 为了避免每次更新配置都要重启客户端微服务3355. 下面修改Config Client.
引入actuator监控依赖

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

修改YML,暴露监控端口

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

业务类上添加@RefreshScope

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

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

重启config客户端后, 修改gitHub远程配置, version+1后, 访问 http://localhost:3344/master/config-dev.yml, 正常获取到修改的配置.
在这里插入图片描述
再次通过客户端访问获取远程配置 http://localhost:3355/configInfo
在这里插入图片描述
发现上面读取的配置信息还是修改之前的, 这是为什么呢?
此时, 还需要运维人员向config客户端发送一个post请求刷新配置. 使用curl或postman发送请求. 刷新缓存后读取正常.
curl -X POST http://localhost:3355/actuator/refresh

3.4 遇到的坑

上面发送post请求刷新远程配置到config客户端报 404 错误. (curl -X POST http://localhost:3355/actuator/refresh)
在这里插入图片描述
在这里插入图片描述
网上很多资料说是SpringBoot 1.x和SpringBoot 2.x版本的actuator监控的依赖版本内容改动很大导致. 我是怎么改都不行. 也不知道什么原因. 希望以后学好英语从官方资料中找到解决方案. 另外, 我使用的SpringBoot 2.2.2.RELEASE版本.
监控端点配置暴露全部, 在启动日志中显示的还是只有2个暴露端点. 没有refresh.
spring boot 中使用 actuator 404的问题
在这里插入图片描述
在这里插入图片描述

3.5 解决坑

上面发送post请求刷新远程配置到config客户端报 404 错误. (curl -X POST http://localhost:3355/actuator/refresh).
也不知道什么原因, 我隔几天重新启动应用后, 发现又正常了. 下面记录一下测试过程.
首先远程配置仓库初始版本设置为 3.
在这里插入图片描述
然后, 分别使用配置中心服务端和客户端访问该配置信息, 结果分别如下:
http://localhost:3344/config-dev.yml
在这里插入图片描述
http://localhost:3355/configInfo
在这里插入图片描述
然后修改远程配置的版本为 1.
在这里插入图片描述
先使用配置中心服务端访问. http://localhost:3344/config-dev.yml, 修改的配置立即同步到配置中心服务端了.
在这里插入图片描述
再使用配置中心客户端访问: http://localhost:3355/configInfo, 发现版本还是 3.
在这里插入图片描述
我们这里向配置中心客户端发送post请求刷新配置. curl -X POST http://localhost:3355/actuator/refresh . 发现刷新成功.
在这里插入图片描述
再次使用配置中心客户端访问 http://localhost:3355/configInfo, 发现版本已经更新为1.
在这里插入图片描述
然后, 我们来看一下配置中心客户端的启动日志, 暴露的端点个数也正常了(17个), 不再是上面的2个了.
在这里插入图片描述
通过访问/actuator, 可以查看暴露的具体端点信息.
在这里插入图片描述

个人博客

欢迎访问个人博客: https://www.crystalblog.xyz/

备用地址: https://wang-qz.gitee.io/crystal-blog/

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值