SpringCloudConfig 使用git搭建配置中心

发布于:2024-03-29 ⋅ 阅读:(13) ⋅ 点赞:(0)

一 SpringCloudConfig 配置搭建步骤

1.引入 依赖pom文件

引入 spring-cloud-config-server 是因为已经配置了注册中心

<dependencies>
        <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.cloud</groupId>
            <artifactId>spring-cloud-netflix-eureka-client</artifactId>
        </dependency>
    </dependencies>

2. 添加.propertis 配置文件

启动Spring Cloud Config服务器,它会从Git仓库中读取配置并通过HTTP接口暴露。
以下是配置application.properties

server.port=8050
spring.application.name=config-server

# 注册到服务注册中心
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

#####################读取git远程配置中心文件位置################################
# github的仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/yunzhongfan/zhangcloudconfig.git
# 搜索 Github/Gitee 上名为 config-server 仓库的目录
spring.cloud.config.server.git.searchPaths=repo
# github的分支,默认是master
spring.cloud.config.label=master
# git 仓库登录用户名
spring.cloud.config.server.git.username=xxxx@qq.com
# git 仓库登录密码
spring.cloud.config.server.git.password=yyyyyy
#启动时直接从远程 Git 仓库获取配置 (GitHub/Gitee)
spring.cloud.config.server.git.lone-on-start=true




#######################读取本地配置文件配置###################################
#表示配置读取选择本地的地址
# spring.profiles.active=native
#本地配置文件位置
#spring.cloud.config.server.native.search-locations=G:\\env-properties\\spring-config

3.在git 仓库 添加配置文件

确保你的Git仓库中有一个config目录,并且配置文件遵循{application}-{profile}.properties或{application}-{profile}.yml的命名规则。

在这里插入图片描述

启动Spring Cloud Config服务器的主类可能看起来像这样:

4.开启服务注册发现和配置服务注解

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }

}

5. 测试配置中心的配置文件

启动后,你可以通过访问如下URL来获取配置信息:

http://localhost:port/application-name/profile/label

其中application-name是配置文件的应用名,profile是环境名(如dev, test, prod),label是Git的分支名。如果在application.properties中设置了默认的label,则可以省略。

请确保你的开发环境安装了Git,并且网络可以访问GitHub或其他Git仓库的URL。

http://localhost:8050/config-server/test/master
在这里插入图片描述

http://localhost:8050/config-server/dev/master

在这里插入图片描述

二 SpringCloudConfig 配置常见属性

Spring Cloud Config 是一个用于集中管理应用程序配置的框架,它将配置存储在一个外部系统(如Git仓库)中,并使用Spring Enviroment和Vault支持密码等敏感信息的管理。

Spring Cloud Config 常见的配置属性如下:

spring.cloud.config.server.git.uri: 配置Git仓库的地址。

spring.cloud.config.server.git.username: 配置Git仓库的用户名(如有权限限制)。

spring.cloud.config.server.git.password: 配置Git仓库的密码(如有权限限制)。

spring.cloud.config.server.git.cloneOnStart: 配置是否在启动时克隆仓库,默认为true。

spring.cloud.config.server.git.skipSslValidation: 配置是否跳过Git仓库SSL验证。

spring.cloud.config.server.git.timeout: 配置Git仓库连接的超时时间。

spring.cloud.config.server.git.defaultLabel: 配置默认的Git分支,默认为master。

spring.cloud.config.server.git.searchPaths: 配置Git仓库中查找配置文件的路径。

spring.cloud.config.server.port: 配置Config Server的端口。

spring.cloud.config.server.prefix: 配置Config Server的URL前缀。
属性名 说明 默认值
spring.cloud.config.allow-override 标记以指示可以使用{@link #isSystemPropertiesOverride()systemPropertiesOverride}。设置为false以防止用户意外更改默认值 true
spring.cloud.config.authorization 客户端使用的授权令牌连接到服务器
spring.cloud.config.discovery.enabled 标记以指示启用配置服务器发现 false
spring.cloud.config.discovery.service-id 服务ID来定位配置服务器 configserver
spring.cloud.config.enabled 标记说远程配置启用 true
spring.cloud.config.fail-fast 标记表示无法连接到服务器是致命的 false
spring.cloud.config.label 用于拉取远程配置属性的标签名称
spring.cloud.config.name 用于获取远程属性的应用程序名称
spring.cloud.config.override-none 标志表示当{@link #setAllowOverride(boolean)allowOverride}为true时,外部属性应该采用最低优先级,并且不覆盖任何现有的属性源(包括本地配置文件) false
spring.cloud.config.override-system-properties 标记以指示外部属性应覆盖系统属性 true
spring.cloud.config.password 联系远程服务器时使用的密码
spring.cloud.config.profile 获取远程配置时使用的默认配置文件(逗号分隔) default
spring.cloud.config.retry.initial-interval 初始重试间隔(以毫秒为单位) 1000
spring.cloud.config.retry.max-attempts 最大尝试次数 6
spring.cloud.config.retry.max-interval 退避的最大间隔 2000
spring.cloud.config.retry.multiplier 下一个间隔的乘数 1.1
spring.cloud.config.server.bootstrap 表示配置服务器应使用远程存储库中的属性初始化其自己的环境 false
spring.cloud.config.server.default-application-name application 传入请求没有特定的默认应用程序名称 application
spring.cloud.config.server.default-label 传入请求没有特定标签时的默认存储库标签
spring.cloud.config.server.default-profile 传入请求没有特定的默认应用程序配置文件时 default
spring.cloud.config.server.encrypt.enabled 在发送给客户端之前启用对环境属性的解密 true
spring.cloud.config.server.git.force-pull 标记表示存储库应该强制拉。如果真的丢弃任何本地更改并从远程存储库获取
spring.cloud.config.server.git.password 使用远程存储库验证密码
spring.cloud.config.server.git.repos 存储库标识符映射到位置和其他属性
spring.cloud.config.server.git.search-paths 在本地工作副本中使用的搜索路径
spring.cloud.config.server.git.timeout 用于获取HTTP或SSH连接的超时(以秒为单位) 5
spring.cloud.config.server.git.uri 远程存储库的URI
spring.cloud.config.server.git.username 用于远程存储库的身份验证用户名
spring.cloud.config.server.native.fail-on-error 标识以确定在解密期间如何处理异常 false
spring.cloud.config.server.native.search-locations 搜索配置文件的位置 []
spring.cloud.config.server.native.version 为本地存储库报告的版本字符串
spring.cloud.config.server.overrides 无条件发送给所有客户的资源的额外地图
spring.cloud.config.server.prefix 配置资源路径的前缀
spring.cloud.config.server.strip-document-from-yaml 标记为指示作为文本或集合(而不是映射)的YAML文档应以“本机”形式返回 true
spring.cloud.config.uri 远程服务器的URI http:// localhost:8888
spring.cloud.config.username 联系远程服务器时使用的用户名
本文含有隐藏内容,请 开通VIP 后查看