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. 添加bootstrap.yml 配置文件

启动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

在这里插入图片描述

Config支持我们使用的请求的参数规则为:

/ { 应用名 } / { 环境名 } [ / { 分支名 } ] eg: http://localhost:8050/config-server/dev/master
/ { 应用名 } - { 环境名 }.yml
/ { 应用名 } - { 环境名 }.properties eg: http://localhost:8050/config-server-dev.properties
/ { 分支名 } / { 应用名 } - { 环境名 }.yml
/ { 分支名 } / { 应用名 } - { 环境名 }.properties eg:http://localhost:8050/master/config-server-dev.properties

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

二、springcloud Client 客户端搭建

Config Server 是一个可横向扩展、集中式的配置服务器,它用于集中管理应用程序各个环境下的配置,默认使用 Git存储配置内容(也可使用Subversion、本地文件系统或 Vault存储配置,限于篇幅,本书不做讨论),因此可以很方便地实现对配置的版本控制与内容审计

在这里插入图片描述

1.引入相关的jar包

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

    
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.zhang.buiness</groupId>
            <artifactId>amaster-config-pub</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>

2.搭建bootstrap.yml 配置文件,配置springcloud 相关信息

server:
    port: 9006
eureka:
    client:
        service-url:
            # 注册中心地址
            defaultZone: http://localhost:8761/eureka/
spring:
    cloud:
        config:
            discovery:
                #表示使用服务发现组件Erurker中的ConfigServer,而不自己指定Config Server#的uri,默认false
                enabled: true
                #指定ConfigServer在服务发现中的serviceId,默认是configserver
                serviceId: config-server
            # github的分支,默认是master
            label: master
            name: config-server
            #git仓库中配置文件的profile
            profile: dev
            uri: http://127.0.0.1:8050


3.建立application.yml 配置文件 应用程序级别的胚子文件

server:
  port: 9006

spring:
  application:
    name: amaster-config-env-server

在这里插入图片描述

4.建立启动类

@EnableDiscoveryClient
@SpringBootApplication
@EnableEurekaClient
public class AmasterConfigEnvServerApplication {

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

}

5. 建立测试代码从配置中心拉取配置值


@RestController
@RequestMapping("/env")
public class EnvWebController {

    @Autowired
    private EnvServiceImpl envService;


    @RequestMapping("/get/env/all/v1")
    private List<String> getEnvConfig(){
      return  envService.getEnvConfig();

    }
}


@Service
public class EnvServiceImpl {

    @Value("${word}")
    private String word;
    public List<String> getEnvConfig() {
        return Arrays.asList(word);
    }
}

6.启动测试类

6.1.启动注册中心、和配置中心服务端

6.2 启动cloudConfig 客户端

启动测试类后,在启动日志中会看到对应的配置中心地址
在这里插入图片描述
启动成功后会在注册中心中看到配置中心和配置中心客户端
在这里插入图片描述
访问测试配置文件的值,可以看到成功读取配置中心git中的值
在这里插入图片描述
在这里插入图片描述

读取配置中心常见错误,读取不到配置配置中心报,
Spring Cloud 服务启动连接 报:Fetching config from server at : http://localhost:8888

配置文件的优先级

通过错误日志,发现服务还是取的8888的配置中心的文件,这就涉及到了一个配置文件的优先级,在spring Cloud 中,有个 “启动上下文”,用来加载ConfigServer里面的配置,默认的加载顺序为:本地bootstrap.配置 --> 链接 configServer,加载远程配置 --> 加载本地application. 配置

三 、springcloud 配置文件刷新

3.1 基于spring-boot-starter-actuator 的配置文件手动刷新功能

1.添加依赖包

 <!--springcloud 使用/refresh端点手动刷新项目依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

2.在对应需要刷新的属性上增加@RefreshScope 刷新依赖

@Service
@RefreshScope
public class EnvServiceImpl {

    @Value("${word}")
    private String word;
    public List<String> getEnvConfig() {
        return Arrays.asList(word);
    }
}

  1. 修改git配置文件仓库的配置
    在这里插入图片描述

  2. 执行刷新配置的命令

>curl -X POST http://localhost:9006/actuator/refresh

在这里插入图片描述
如果有配置更新则显示更新的配置key
在这里插入图片描述

5.测试
刷新前

在这里插入图片描述
显示更新后的属性
在这里插入图片描述

3.2 基于springcloudConfig Bus 自动刷新配置

四、SpringCloud config Server 的登录用户认证

Spring Cloud Confg的用户认证
在前文的示例中,ConfgServer是允许匿名访问的。为了防止配置内容的外泄,应该保护ConfgServer 的安全。有多种方式做到这一点,例如通过物理网络安全,或者为ConfgServer添加用户认证等。

在未配置SpringCloud config Server安全认证登录之前,可以直接通过
http://localhost:8050/config-server/test/master 等查看配置中心的配置文件,SpringCloud config Client 也可以不经过认证直接访问配置中心。因此可以通过security 进行登录访问的安全认证设置

4.1 config Server 端配置 用户认证配置

添加依赖

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

2.配置文件开启basic安全认证

########开始springcloud Server的 配置文件访问的安全认证#######
#开启基于HTTP basic的认证

spring.security.basic.enabled=true
spring.security.user.name=admin
spring.security.user.password=12345678

3.测试访问
直接访问 http://localhost:8050/config-server/test/master ,会跳到登录窗口,登录成功后可以看到访问的内容
在这里插入图片描述

4.2 SpringCloudConfig Client端登录访问的用户认证

在Config server端开启用户认证之后,Config Client端虽然可以启动的事时候连接配置中心显示如下
在这里插入图片描述
在Config Client 端需要加入以下用户认证配置
spring.cloud.config.discovery.username 和 spring.cloud.config.discovery.password 用户认证属性才可以访问到配置文件

spring:
    cloud:
        config:
            discovery:
                #表示使用服务发现组件Erurker中的ConfigServer,而不自己指定Config Server#的uri,默认false
                enabled: true
                #指定ConfigServer在服务发现中的serviceId,默认是configserver
                serviceId: config-server
            # github的分支,默认是master
            label: master
            # github的分支,默认是master
            name: config-server
            #git仓库中配置文件的profile
            profile: @profiles.active@
            # 配置中心地址 不使用注册中心的时候配置使用
            #uri: http://127.0.0.1:8050
            #uri: http://admin:12345678@127.0.0.1:8050
            # Config Client 用户认证 用户名 ,在Config server 端开启用户认证后需要加入
            username: admin
            # Config Client 用户认证 用户名  ,在Config server 端开启用户认证后需要加入
            password: 12345678

五、SpringCloud Server的高可用

5.1 Git 仓库的高可用

由于配置内容都存储在 Git 仓库中,所以要想实现 ConfgServer 的高可用,必须有一个高可用的Git 仓库。有两种方式可以实现Git 仓库的高可用。

  • 使用第三方 Git 仓库:这种方式非常简单,可使用例如 GitHub、BitBucket、git@osc、Coding等提供的仓库托管服务,这些服务本身就已实现了高可用。
  • 自建Git 仓库管理系统:使用第三方服务的方式虽然省去了很多烦恼,但是很多场景下,倾向于自建 Git仓库管理系统。此时就需要保证自建Git的高可用。以 GitLab为例,读者可参照官方文档搭建高可用的 GitLab:https://about.gitlab.com/high-availability/

5.2 基于SpringCloudConfig Bus的高可用

  • RabbitMQ的高可用
    还记得前文使用 Spring Cloud Bus实现了配置的自动刷新吗?由于 Spring Cloud Bus依赖RabbitMQ(当然也可使用其他 MQ),所以 RabbitMQ 的高可用也是必不可少的。搭建高可用RabbitMQ的资料详见:https://www.rabbitmg.com/ha.html。由于比较简单,笔者不做赘述。当然,也可使用云平台的提供的RabbitMQ服务

5.3 基于SpringCloud Server 的高可用

5.3.1 Config Server 未注册到Eureka Server 上

在这里插入图片描述

5.3.1 Config Server 注册到 Eureka Server 上

在这里插入图片描述

六 、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联系远程服务器时使用的用户名
  • 3
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: SpringCloudConfig是一个用于管理应用配置的分布式配置中心,可以帮助你从多个环境中获取和管理配置。它可以从Git存储库,本地文件系统或远程配置服务器加载配置数据,并以服务端和客户端的形式将配置数据公开给应用程序。 ### 回答2: Spring Cloud Config 是一个分布式配置管理解决方案,可以将所有微服务的配置统一管理,并通过配置中心动态更新配置,实现了可持续集成和快速迭代的需求。Spring Cloud Config 提供了一个基于 Git 仓库配置中心,可以将微服务的配置文件存储在中心仓库中,并通过访问该仓库获取配置信息。 Spring Cloud Config 的架构包括三个主要组件:Config ServerConfig Client 和 Git 仓库Config Server配置中心的核心组件,是一个独立的微服务,负责管理配置信息并提供 REST 接口给其他微服务获取配置Config Client 是每个微服务中的库,用于获取从 Config Server 中获取配置信息,并将其应用到当前微服务中。Git 仓库用于存储配置文件,Config Server 可以从该仓库中读取配置文件,并将其暴露给 Config Client。 使用 Spring Cloud Config 可以带来以下优势: 1. 集中管理:所有微服务的配置信息都可以集中管理,方便维护和更新。 2. 动态更新:当配置发生变化时,无需重启微服务,Config Client 可以主动获取最新配置,并应用到当前微服务中。 3. 版本控制:通过将配置文件存储在 Git 仓库中,可以实现对配置文件的版本控制,方便查看历史修改记录以及回滚操作。 4. 高可用性:使用 Config Server 集群可以实现配置中心的高可用性,提供更好的服务可靠性。 总之,Spring Cloud Config 提供了一种灵活、可靠的配置管理解决方案,可以帮助开发团队更好地管理微服务的配置,提高开发效率和系统可靠性。 ### 回答3: Spring Cloud Config 是一个分布式配置管理工具,它提供了集中式的配置管理,能够方便地管理各种应用的配置信息。 Spring Cloud Config 分为服务端和客户端两个部分。服务端负责管理各个应用的配置信息,而客户端则将配置信息集成到对应的应用中。通过集中管理配置信息,可以实现应用的动态更新和灵活部署。 Spring Cloud Config 支持多种存储方式,包括本地文件系统、Git 仓库和远程配置中心等。可以根据需求选择不同的存储方式,并通过简单的配置即可集成到应用中。 使用 Spring Cloud Config,可以实现配置文件的版本控制、变更管理和安全控制。配置文件的版本控制可以方便地回滚到之前的配置信息,变更管理可以记录配置文件的修改历史,而安全控制可以保护配置文件的访问权限。 通过 Spring Cloud Config,可以将配置信息与应用代码分离,实现应用的解耦和可扩展性。配置信息的集中管理可以减少配置文件的冗余,提高配置的复用性。 另外,Spring Cloud Config 还提供了高可用的配置服务,可以通过集群部署来实现配置服务的负载均衡和容错能力。 总之,Spring Cloud Config 配置中心为分布式系统的配置管理提供了可靠和高效的解决方案,可以帮助开发者实现应用的动态配置和快速部署。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值