第十二章 Spring Cloud Config 统一配置中心详解

目录

一、配置问题分析及解决方案

1、问题分析

2、解决方案

二、Spring Cloud Config 介绍

1、Spring Cloud Config特性

2、Spring Cloud Config作用    

3、Spring Cloud Config 组件

   统一配置中心服务端

   统一配置中心客户端

4、Spring Cloud Config 工作流程

三、 配置中心使用

1 搭建远程 git 仓库

1.1 新建远程仓储

1.2.创建远程仓储管理的配置文件 

2搭建统一配置服务中心服务端

2.1创建项目,引入依赖

2.2 创建启动类 添加注解@EnableConfigServer

2.3 配置 config Server 管理的远端仓储信息

2.4 ConfigServer 获取远端配置信息测试

3、搭建统一配置服务中心客户端

3.1 创建项目,引入依赖

3.2 订单服务配置 

3.3 创建订单服务controller

3.4 创建启动类

3.5 启动测试


一、配置问题分析及解决方案

1、问题分析

        通过上图可知,每个微服务都有一个配置文件,目前只是11个微服务,就需要11个配置文件,若有上百个微服务呢?常规配置管理解决方案缺如下:

  • 硬编码(需要修改代码、繁琐、风险大)
  • properties 或者 yml(集群环境下需要替换和重启)
  • xml(重新打包和重启) 

2、解决方案

        使用Spring Cloud Config集中式配置管理中心,用来实现微服务系统中服务配置的统一管理。

        组件:统一配置中心服务端集中管理配置文件、统一配置中心客户端就是各微服务。

二、Spring Cloud Config 介绍

1、Spring Cloud Config特性

  • 提供服务端和客户端支持(Spring Cloud Config Server 和 Spring Cloud Config Client)
  • 集中式管理分布式环境下的应用部署
  • 属性值的加密和解密(对称加密和非对称加密)
  • 基于 Spring 环境,无缝与 Spring 应用集成
  • 可用于任何语言开发的程序
  • 默认实现基于 Git ,可以进行版本管理

2、Spring Cloud Config作用    

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

3、Spring Cloud Config 组件

        Spring Cloud Config 在微服务分布式系统中,采用Server 服务端「Client 客户端」的组件方式来提供可扩展的配置服务。

   统一配置中心服务端

  • 是一个独立的微服务应用
  • 集中管理配置文件
  • 提供配置文件的存储
  • 以接口的形式将配置文件的内容提供出去; 

   统一配置中心客户端

  • 是各个微服务
  • 在启动的时候通过接口从配置中心获取和加载获取数据(配置信息)
  • 并依据此配置信息初始化自己的应用。

4、Spring Cloud Config 工作流程

       工作流程:微服务即config client 到 config Server 获取配置文件,config Server 到远端仓库获取配置。

       详细说明:统一配置中心服务端 config Server 也是一个微服务,这个微服务将来可能是个小集群,如果把所有配置都放到 config Server,一旦有版本改动,则整个小集群都需要改。因此,可以将所有微服务的配置统一放到 git 远程仓储上进行版本管理。config Server 只需要配置 git 远程仓储地址,在 config Server 启动时候,config Server 会到 git 远程仓储上进行拉取配置到本地仓储,启动后如果远程仓储的配置有改动,则 config Server 会自动检测到远程仓储的配置改动,进行自动拉取最新配置。其他微服务即config client 可以通过  config Server  获取所需配置信息。
因此需要搭建 git 远程仓储环境。

       注意: 统一配置中心服务端 config Server 读取远程仓储的配置的时候是有一定的规则,因此在远程仓储中的配置文件命名也要有一定的规则。

  • 远程仓储配置文件命名规则

       {application}-{profile}.yml/{application}-{profile}.properties

       如:order-dev.yml、order-test.yml、order-prod.yml

  • config Server 读取远程仓储的配置规则:    

       其中 label 代表的是分支例如master分支,profile代表的是环境。

       如:http://localhost:7001/master/order-dev.yml
       如:http://localhost:7001/master/order-test.yml
       如:http://localhost:7001/order/dev/master
       如:http://localhost:7001/order/test/master

三、 配置中心使用

       配置中心使用步骤

  • 搭建远程 git 仓储
  • 搭建统一配置服务中心服务端
  • 搭建统一配置服务中心客户端

1 搭建远程 git 仓库

1.1 新建远程仓储

1.2.创建远程仓储管理的配置文件 

       在远程仓储 master 分支上对订单服务的进行配置,环境分别是dev/test/prod。文件名分别为order-dev.yml、 order-test.yml、 order-prod.yml,订单服务的端口号分别为:9000、9100、9200,配置分别如下:

server:
  port: 9000
spring:
  application:
    name: order-service 

# 配置eureka客户端信息
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/
  instance:
    prefer-ip-address: true
    # instance-id: order-service
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

2搭建统一配置服务中心服务端

2.1创建项目,引入依赖

       搭建统一配置服务中心服务端 config server,创建项目,引入 统一服务配置中心 config 依赖。

<?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>springcloudbase</artifactId>
        <groupId>com.hwadee.springcloud2022</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>configServer7009</artifactId>

    <dependencies>
        <!-- 添加统一服务配置中心 config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!--Eureka Client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

</project>

2.2 创建启动类 添加注解@EnableConfigServer

       创建启动类,添加 注解@EnableConfigServer 启动 config服务端 应用。

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

@SpringBootApplication
@EnableEurekaClient// 启动 eureka 客户端
@EnableConfigServer// 启动 config 服务端
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

2.3 配置 config Server 管理的远端仓储信息

       配置config Server服务端 要管理的远程仓库地址。

server:
  port: 7009
spring:
  application:
    name: config-service # 为当前商品服务命名
  cloud:
    config:
      server:
        git:
          #  username: xiashanzhu
          #  password: aa@86886830622
          uri: https://gitee.com/xiashanzhu/config-repo #要读取的远程仓库的配置文件的地址。
          default-label: master # 指定分支,不指定则默认master
eureka:
  client:
    service-url: # 配置服务注册地址,与 eureka-server 中暴露地址保持一致
      defaultZone: http://localhost:8000/eureka
  instance:
    prefer-ip-address: true  # 是否使用 IP 地址注册,默认 false
    # instance-id: product-service  # 实例 id,服务的唯一标识
    instance-id: ${spring.cloud.client.ip-address}:${server.port} # 如果想在控制页面看到服务地址与端口,可以将 instance-id 这样配置
    lease-renewal-interval-in-seconds: 5  # 发送心跳的间隔,单位秒,默认 30
    lease-expiration-duration-in-seconds: 10 # 续约到期时间,单位秒,默认90

2.4 ConfigServer 获取远端配置信息测试

           分别启动 注册中心 和 统一配置中心服务端 config server,根据前面介绍的配置读取的规则分别进行测试。

  • /label/{application}-{profile}.yml 方式测试

       测试:http://localhost:7001/master/order-dev.yml
       测试:http://localhost:7001/master/order-test.yml

  • /{application}/{profile}/label 方式测试

       测试:http://localhost:7001/order/dev/master
       测试:http://localhost:7001/order/test/master

 注意:
          
/{application}-{profile}.yml默认访问的是master分支下的配置文件。例如访问

          http://localhost:7009/order/dev  和 http://localhost:7009/order/dev/master 结果相同:

小结

       在配置服务中心服务端访问远程仓储的配置文件例如http://localhost:7009/order/dev/master时, 配置服务中心服务端会自动的进行拉取远程仓储到本地,例如:C:/Users/HP/AppData/Local/Temp/config-repo-xxxx/ 目录中。

3、搭建统一配置服务中心客户端

3.1 创建项目,引入依赖

       搭建统一配置服务中心客户端 config client,创建订单服务项目,引入 统一服务配置中心 spring-cloud-starter-config 依赖。就等于开启了 统一配置服务中心的客户端。

<?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>springcloudbase</artifactId>
        <groupId>com.hwadee.springcloud2022</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.hwadee.springcloud</groupId>
    <artifactId>orderServer9000</artifactId>

    <dependencies>
        <!-- 统一配置服务中心客户端依赖 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <!--动态健康监控 可以用于动态感知配置变化-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <!--web依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!-- 管理公共api -->
        <dependency>
            <groupId>com.hwadee.springcloud</groupId>
            <artifactId>springcloud-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <!--Eureka Client-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <!-- 方便创建类的gettter setter -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>

</project>

3.2 订单服务配置 

  • 远程仓储创建订单服务配置 

       在远程仓储 master 分支上对订单服务的进行配置,环境分别是dev/test/prod。文件名分别为order-dev.yml、 order-test.yml、 order-prod.yml,订单服务的端口号分别为:9000、9100、9200,配置分别如下:。

  • 本地创建订单服务配置 

       订单服务客户端在启动的时候需要加载配置文件,但此时配置文件不在本地,而是在远程仓储上面,此时运行会报错。因此在运行的时候需要一个本地配置文件bootstrap.yml/properties,在bootstrap.yml/properties 中告知客户端配置文件需要从config Server 服务端上获取。使用配置文件bootstrap原因在于微服务启动时候 bootstrap 配置文件加载顺序优先级最高。

       在 bootstrap.yml/properties 中获取 config Server 服务端上的配置文件的配置方式有两种方式,硬编码方式和服务名方式。

       硬编码方式(不推荐):

       硬编码方式,即将 config Server 服务端 的地址硬编码在bootstrap中。但是不推荐使用,因为 config Server 服务端 也是集群模式,如果其中硬编码的服务挂掉,则会导致其他服务失败。配置方式如下:

spring:
  cloud:
    config:
      label: master # 指定分支
      name: order # 指定应用名称
      profile: dev # 指定激活环境
      uri: http://localhost:7009 #硬编码 指定访问 config server 远程仓储的地址的ip和端口

       服务名方式:

       使用 config Server 服务端 的服务名方式。因为 config Server 服务端 也是集群模式,使用服务名,订单服务 会先到注册中心找到 config Server 服务端 该服务名的地址,然后在这些地址中在选择一个地址,进行远端的配置文件信息获取。但注册中心的配置信息需要写在 bootstrap中,而不是配置在远程仓储中。

       注册中心信息修改为:

        bootstrap.yml配置信息修改为:

spring:
  cloud:
    config:
      discovery:
        service-id: CONFIG-SERVICE  #告诉当前客户端 统一配置中心的服务端服务id
        enabled: true #开启客户端,根据服务id到注册中心获取配置信息
      label: master # 指定分支
      name: order # 指定应用名称
      profile: dev # 指定激活环境

# 配置eureka客户端信息
eureka:
  client:
    service-url:
      defaultZone: http://localhost:8000/eureka/

3.3 创建订单服务controller

import com.hwadee.springcloud.entity.Product;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Value("${env}")
    private String env;
    @Value("${port}")
    private String port;
    @Value("${info}")
    private String info;

    @RequestMapping(value = "/getConfig")
    public Product getConfigInfo() {
        Product product = new Product();
        product.setName(env +"环境 端口:"+ port +" "+ info);
        return product;
    }

}

3.4 创建启动类

      创建启动类,在启动类中加入注解 @EnableEurekaClient 将来注入到服务中心。

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


@SpringBootApplication
@EnableEurekaClient// 启动 eureka 客户端
public class OrderServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServerApplication.class, args);
    }
}

3.5 启动测试

     启动 注册中心 、 配置管理中心服务端、订单服务。分别对硬编码和服务名方式测试。

  • 配置文件硬编码方式测试:

        重新启动订单服务,输入地址 http://localhost:9000/order/getConfig  查看结果

  • 配置文件服务名方式测试:

       重新启动订单服务,输入地址 http://localhost:9000/order/getConfig  查看结果

第十一章:GetAway服务网关详解

第十三章:Spring Cloud Config 统一配置中心详解-客户端动态刷新

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值