• 实践讲解Spring配置中心config(图+文,本地文件方式)


    1 缘起

    微服务的学习过程中,发现了许多服务的配置是相同的,并且项目稳定运行期间不会轻易变更,
    于是,自己开始做实验,将这些相同的配置提取出来放在配置中心,
    各个服务需要时,通过这个配置中心获取,SpringCloud提供的配置中心组件config,
    即是提供中心化配置的组件,方便管理配置文件,
    当然,也涉及到配置文件更新的问题,
    不过,本文主要讲解如何搭建配置中心,并从配置中心获取配置文件,
    没有讲解如何刷新配置参数,会在后续的文章中分享,
    本文主要帮助读者从整体架构上理清配置中心的位置以及作用,
    并结合实践,搭建相关服务,帮助读者从工程实现上使用配置中心。

    2 架构

    配置中心的作用:为其他客户端提供公用的配置参数,
    简单的测试系统架构如下图所示,由图可知,系统有三个部分:
    注册中心、Config服务端和其他的任意客户端,
    其中,Config服务端和其他客户端均向注册中心注册,
    这样,客户端通过注册中心以及指定的Config服务端serviceId即可获取Config服务端信息,
    以获取Config服务端提供的公用配置信息。

    在这里插入图片描述
    注册中心使用Eureka,Config配置中心的服务ID为config,在其他客户端中可以通过serviceId指定config,
    获取到该Config服务端的相关信息,Config服务IP和端口,以及公用配置的数据,服务启动后的状态如下图所示。
    在这里插入图片描述

    3 Config服务端配置

    3.1 依赖

    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        <version>2.2.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-config-serverartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    3.2 参数配置

    本文的配置中心采用读取本地配置文件的方式,
    因此,核心配置的有:spring.profiles.active使用native
    spring.cloud.config配置使用navtive,具体的配置如下:
    配置文件存储在resources路径,因此,使用classpath作为前缀。

    spring:
      application:
        name: config # 配置中心服务名称
      profiles:
        active: native # 读取本地配置文件
      sleuth:
        sampler:
          probability: 1.0
        enabled: true
      cloud:
        config:
          server:
            native:
              search-locations: classpath:/config/ # 本地文件:resource目录
    server:
      port: 9002
      servlet:
        session:
          timeout: PT10S
    
    eureka: # 连接注册中心
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://localhost:8001/eureka/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    3.3 启动配置

    启动文件添加Config服务端启动注解@EnableConfigServer,
    以启用Config服务端,加载相关config配置参数,配置样例如下:

    package com.monkey.config;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    import org.springframework.cloud.config.server.EnableConfigServer;
    
    /**
     * 启动类.
     *
     * @author xindaqi
     * @date 2021-04-30 18:22
     */
    @SpringBootApplication
    @EnableConfigServer
    public class ConfigApplication {
    
    	private static final Logger logger = LoggerFactory.getLogger(ConfigApplication.class);
    
    	public static void main(String[] args) {
    		SpringApplication.run(ConfigApplication.class, args);
    		logger.info(">>>>>>>>Config启动成功");
    	}
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    3.4 资源文件

    resources路径添加待共用的配置文件:application-dev.yml,
    以对其他客户端提供数据。

    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/db_monkey_run?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=Asia/Shanghai
        username: root
        password: 123456
        type: com.alibaba.druid.pool.DruidDataSource
        druid:
          initial-size: 10
          max-active: 100
          min-idle: 10
          max-wait: 6000
          filters: stat, wall
          stat-view-servlet:
            enabled: true
            login-username: admin
            login-password: 123456
    
    server:
      port: 9125
      servlet:
        session:
          timeout: PT10S
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23

    3.5 获取配置文件

    Config服务端启动后,可以通过前缀+后缀的方式获取配置文件内容,
    其中,前缀和后缀使用中划线分割,如application-dev.yml,读取文件样例:
    http://ip:port/application/dev
    获取的文件内容如下图所示,说明配置中心服务端正常启动并可以对外提供配置数据。
    在这里插入图片描述

    4 客户端配置

    4.1 依赖

    
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-starter-netflix-eureka-clientartifactId>
        <version>2.2.3.RELEASEversion>
    dependency>
    <dependency>
        <groupId>org.springframework.cloudgroupId>
        <artifactId>spring-cloud-config-clientartifactId>
    dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    4.2 参数配置

    主要是配置配置中心参数,如开启发现配置中心,以及使用哪个配置中心以及对应的配置文件,
    详细配置如下:

    spring:
      cloud:
        config:
          discovery:
            enabled: true # 开启发现spring-config服务
            service-id: config # spring-config服务ID
          name: application # spring-config服务中的配置文件前缀
          profile: dev # spring-config服务中的配置文件后缀,完整:application-dev.yml
         
    eureka:
      client:
        fetch-registry: true
        register-with-eureka: true
        service-url:
          defaultZone: http://localhost:8001/eureka/eureka
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    4.3 读取配置文件

    通过接口读取公用的配置文件,
    这里读取数据源的地址,
    测试接口如下:

    package com.monkey.common.api;
    
    import com.monkey.common.common.response.Response;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.RestController;
    
    /**
     * 配置数据接口.
     *
     * @author xindaqi
     * @since 2022-10-28 17:46
     */
    @RestController
    @RequestMapping("/api/v1")
    public class ConfigDataApi {
    
        @Value("${spring.datasource.url}")
        String datasourceUrl;
    
        @GetMapping("/datasource/url")
        public Response<String> getDatasourceUrl() {
            return Response.success(datasourceUrl);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    测试结果如下图所示,由图可知,客户端正常获取配置中心数据。
    在这里插入图片描述

    5 小结

    (1)配置中心的作用:为其他客户端提供公用的配置参数;
    (2)通过注册中心获取配置中心信息。

  • 相关阅读:
    ARM GIC 和NVIC的区别
    【APP自动化测试必知必会】Appium之微信小程序自动化测试
    Istio Ambient Mesh 介绍
    c++ 归并排序
    Java设计模式之秒杀系统
    2022年国赛数学建模思路及参考代码提前预约
    企业电子招标采购系统源码Spring Boot + Mybatis + Redis + Layui + 前后端分离 构建企业电子招采平台之立项流程图
    设计模式-桥接模式
    直方图统计增强方法
    在FPGA上快速搭建以太网
  • 原文地址:https://blog.csdn.net/Xin_101/article/details/127584130