当前位置: 首页 > news >正文

SpringCloudConfig 使用git搭建配置中心

一 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-nameapplication 传入请求没有特定的默认应用程序名称 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远程服务器的URIhttp:// localhost:8888
spring.cloud.config.username联系远程服务器时使用的用户名
http://www.lryc.cn/news/326800.html

相关文章:

  • c#基础-引用类型和值类型的区别
  • 面试题-3.20
  • glibc内存管理ptmalloc - 多线程内存管理
  • 区块链食品溯源案例实现(一)
  • 4S店车辆管理系统的设计与实现|Springboot+ Mysql+Java+ B/S结构(可运行源码+数据库+设计文档)
  • SpringBoot+Prometheus+Grafana实现应用监控和报警
  • 10 - Debian如何让特定用户切换root身份
  • HPT发布HyperGAI 多模态大模型:性能领先GPT-4V,全面胜过Gemini Pro
  • 汇春科技之MDT10F684
  • 【Vue3笔记01】如何使用Vue3和Vite搭建前端项目的基础开发环境
  • 软考高级架构师:信息安全概念和例题
  • Lilishop商城(windows)本地部署【docker版】
  • # 14 React 自定义Hook详解
  • HTML静态网页成品作业(HTML+CSS+JS)——中华美食八大菜系介绍(1个页面)
  • PostgreSQL11 | Windows系统安装PostgreSQL
  • uniapp保留两位小数,整数后面加.00
  • R: 网状Meta分析进行模型构建及图形绘制
  • 数据结构——排序算法
  • MyBatis的高级特性探索
  • 未来制造:机器人行业新质生产力提升策略
  • 开发过程中PostgreSQL常用的SQL语句,持续更新ing
  • Linux screen命令教程:如何在一个终端窗口中管理多个会话(附实例详解和注意事项)
  • Android中的本地广播与全局广播
  • Debezium日常分享系列之:Debezium2.5稳定版本之MySQL连接器配置示例和Connector参数详解
  • vue3父组件给子组件传值,并在子组件接受
  • Python爬虫如何快速入门
  • 酷开科技依托酷开系统用“平台+产品+场景”塑造全屋智能生活!
  • P8649 [蓝桥杯 2017 省 B] k 倍区间:做题笔记
  • LeetCode题练习与总结:旋转图像
  • 如何在家中使用手机平板电脑 公司iStoreOS软路由实现远程桌面