柏竹 柏竹
首页
后端
前端
  • 应用推荐
关于
友链
  • 分类
  • 标签
  • 归档

柏竹

奋斗柏竹
首页
后端
前端
  • 应用推荐
关于
友链
  • 分类
  • 标签
  • 归档
  • Java基础

  • JavaWeb

  • 拓展技术

  • 框架技术

  • 数据库

  • 数据结构

  • Spring

  • SpringMVC

  • SpringBoot

  • SpringClound

    • SpringCloud认知
    • SpringCloud Eureka注册中心
    • SpringCloud Ribbon负载均衡
    • SpringCloud Hystrix熔断器
    • SpringCloud Feign
    • SpringCloud Gateway网关
    • SpringCloud Config配置中心
      • 搭建远端仓库
        • 配置文件命名规则
      • 搭建配置中心
      • 服务获取配置
    • SpringCloud Bus服务总线
  • Ruoyi-Vue-Plus

  • 后端
  • SpringClound
柏竹
2021-11-02
目录

SpringCloud Config配置中心

# Config 配置中心

Spring Cloud Config 为微服务提供了集中式的配置方案,它主要通过远端仓库进行获取相关配置文件

配置中心也是微服务,需要注册到 Eureka 服务注册中心

主要功能:

  • 提供 配置服务支持
  • 集中管理各个环境的配置文件
  • 仓库 版本管理模式

# 搭建远端仓库

远端仓库搭建于 gitee,把之前配置好的 application.yml 文件信息,推送到远端仓库中,进行服务!

  • 由于GitHub国内访问较慢,因此应用 gitee 示例
  • 对 Git 不了解? 点击 Git 应用 了解 (opens new window)

示例

  1. 创建仓库

    如果创建仓库为 公开 ,配置中心 无需配置密码

  2. 创建两个文件夹 API、config

    待会测试不同子目录访问

  3. 根据以往配置有 user-server、consumer、gateway 。分别将这些服务的配置存储到仓库的不同目录中,并重新命名 (命名很重要)

    • ==/user-dev.yml== (仓库根目录)
    • ==API/gateway-dev.yml==
    • ==config/consumer-dev.yml==

# 配置文件命名规则

一般情况配置中心会通过远端的配置文件名称进行锁定应用,正因如此 配置文件的命名会直接关系到 服务的配置是否生效

命名方式:

  • =={application}-{profile}.yml==
  • =={application}-{profile}.properties==

application :服务名称

profile :区分 环境 。分别有 dev (开发环境) ; test (测试环境) ; pro (生产环境)

这是 user-server 服务的配置 重命为 user-dev.yml ,其他服务也如此

其实命名参数的内容可以自定义,后面会根据这些参数 application ; profile ; 分支 进行锁定配置文件 (它们之间要有 - 分割,且顺序严格)

# 搭建配置中心

Config 配置中心 是与远端仓库交互的微服务(由上图可以看到),前提也需注册到Eureka上进行服务 。主要用于配置 连接远端仓库、访问目录、账号密码 等相关 连接远端的配置

示例

基于以上篇章的基础进行测试

  1. 创建 新Maven工程(无骨架) 我创建的工程名称 config-server

  2. config-server 配置依赖 pom.xml

    <!-- eureka -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>
    <!-- config -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    
  3. config-server 启动类

    // Eureka客户端
    @EnableDiscoveryClient
    @SpringBootApplication
    // 启用配置服务
    @EnableConfigServer
    public class ConfigApplication {
        public static void main(String[] args) {
            SpringApplication.run(ConfigApplication.class, args);
        }
    }
    
  4. config-server 配置文件 application.yml

    server :
        port : 14000
    eureka :
        client :
            service-url :
                defaultZone : http://localhost:10086/eureka
    spring :
        application :
            name : config-server
        cloud:
            config :
                server :
                    git :
                        # 仓库 url
                        uri : https://gitee.com/xxxx/my-config-test.git
                        # 需要访问目录 (默认根路径开始) 
                        search-paths : 'API,config'
                        # 以下配置在 仓库为私有的前提配置;公开无需配置
                        # 用户名
                        username : {gitee账号}
                        # 密码
                        password : {gitee密码}
    
                # 读取分支 (默认master)
    #            label : test
    

    注意:

    • 公开仓库无需配置账号密码
    • 指定子目录的配置需要 ' 符号括住;多个子目录需要 , 分隔符进行分隔
  5. 测试

    依次启动 Eureka 、config-server 两个服务,分别访问一下地址:

    • http://localhost:14000/user-dev.yml (opens new window)
    • http://localhost:14000/gateway-dev.yml (opens new window)
    • http://localhost:14000/consumer-dev.yml (opens new window)
    • 不用指定子路径可直接访问到 远端仓库的配置资源
    • 访问 gateway 的资源时可能会出现被过滤器过滤的问题,因此需要 关闭跨域相关配置 或 赋予地址权限(不过 不关闭跨域也不会影响配置信息的读取,也就浏览器看不到而已)

# 服务获取配置

上面已经 搭建了 仓库、配置中心 ,因此可直接舍弃 user-server、consumer、gateway 三个微服务的配置,重新配置,主要访问远端的配置信息

示例

说明: 有三个微服务,我围绕 user-server 微服务进行配置,其他也如此,就不赘述了

  1. user-server 添加依赖

    <!-- config -->
    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-config</artifactId>
    </dependency>
    
  2. user-server 删除 原有的 application.yml , 替换成 bootstrap.yml

    eureka :
        client :
            service-url :
                defaultZone : http://127.0.0.1:10086/eureka
    spring :
        cloud : 
            config : 
                # 以下 指定 命名格式 进行查找
                # 与仓库中配置文件的 application 保持一致
                name : user
                # 与仓库中的配置文件的 profile 保持一致
                profile : dev
                # 指定配置所属分支
                label : master
                discovery:
                    # 使用配置中心
                    enabled : true
                    # 配置中心服务名
                    service-id : config-server
    
  3. 测试

    1. 依次启动 Eureka 、config-server 、user-server 三个服务
    2. 访问 http://localhost:9091/user/1 (opens new window) (返回数据表示成功)

说明:

  • bootstrap.yml 也是默认配置文件 且比 application.yml 加载早
  • 锁定仓库中的配置有:name (服务名称) ; profile (环境) ; label (分支)
  • bootstrap.yml 相当于项目启动的引导文件

bootstrap.yml 与 application.yml 的区别

bootstrap.yml application.yml
配置级别 系统 应用
加载时段 较早 较迟
配置参数 较少变动 较多变动


仓库代码 : https://gitee.com/Bozhu12/spring-cloud-examples.git (opens new window)

官方文档 : Spring Cloud Config (opens new window)

#SpringClound#Java

← SpringCloud Gateway网关 SpringCloud Bus服务总线→

最近更新
01
HTTPS自动续签
10-21
02
博客搭建-简化版(脚本)
10-20
03
ruoyi-vue-plus-部署篇
07-13
更多文章>
Theme by Vdoing | Copyright © 2019-2024 | 桂ICP备2022009417号-1
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式