资讯专栏INFORMATION COLUMN

分布式配置中心:Spring Cloud Config

ckllj / 1114人阅读

摘要:根据自己维护的仓库信息和客户端传递过来的配置定位信息去查找配置信息。通过命令将找到的配置信息下载到的文件系统中。该配置内容的优先级高于包内部的配置内容,在包中重复的内容将不会被加载。在中配置注册中心地址。

构建配置中心

创建一个基础Spring Boot工程,命名为config-server,并在pom.xml中引入以下依赖:

        
            org.springframework.cloud
            spring-cloud-config-server
        

在程序主类中添加@EnableConfigServer注解

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

在application.properties文件中添加配置服务的基本信息和git仓库的相关信息

spring.application.name=config-server
server.port=7001
#git路径,精确到具体仓库位置
spring.cloud.config.server.git.uri=https://gitee.com/dongspace/config-file/
#git uri下的相对搜索位置,可以配置多个,以","分隔
spring.cloud.config.server.git.search-paths=config-client,demo
spring.cloud.config.server.git.username=***
spring.cloud.config.server.git.password=***
Git配置仓库

新建一个仓库,命名为config-file

新建两个文件夹,分别命名为config-client和demo

在config-client下新建配置文件application.properties、application-stg1.properties,并添加如下配置

from=master
#stg1中的配置
from=master-stg1

在demo文件夹下新建dongspace.properties、dongspace-stg1.properties,并添加如下配置

from=dong-master
#stg1中的配置
from=dong-master-stg1

配置规则详解

配置信息的URL与配置文件的映射关系如下:

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

/{application}/{profile}/[/{label}]

上面的URL对应{application}-{profile}.properties,其中{application}对应配置文件名,{label}对应代码分支,默认为default
以上通过浏览器访问的路径对应为:

http://localhost:7001/application-stg1.properties

http://localhost:7001/master/application-stg1.properties

http://localhost:7001/application/default/master

客户端配置

创建一个基础Spring Boot工程,命名为config-client,并在pom.xml中引入以下依赖:

        
            org.springframework.cloud
            spring-cloud-starter-config
        

在bootstrap.properties中加入以下配置:

server.port=7002
spring.application.name=config-client
spring.cloud.config.profile=stg1
spring.cloud.config.label=master
spring.cloud.config.uri=http://localhost:7001/

创建一个测试类

@RestController
public class TestConfigController {

    @Value("${from}")
    private String from;
    
    @RequestMapping("/testConfig")
    public Object testConfig() {
        return from;
    }
}
服务端的占位符配置

实现多个服务对应一个仓库中的不同目录

spring.cloud.config.server.git.uri=https://gitee.com/dongspace/config-file/
#{application}对应访问服务的应用名
spring.cloud.config.server.git.search-paths={application}

实现一个服务对应目录下的一个仓库

spring.cloud.config.server.git.uri=https://gitee.com/dongspace/{application}-conf
基础架构

客户端应用从config-server中获取配置信息遵从下面的执行流程:

应用启动时,根据bootstrap.properties中配置的{application}、{profile}、{label},向Config Server请求获取配置信息。

Config Server根据自己维护的Git仓库信息和客户端传递过来的配置定位信息去查找配置信息。

通过git clone命令将找到的配置信息下载到Config Server的文件系统中。

Config Server创建Spring的ApplicationContext实例,并从Git本地仓库中加载配置文件,返回给客户端使用。

客户端应用获得外部配置信息后,加载到客户端的ApplicationContext实例。该配置内容的优先级高于Jar包内部的配置内容,在jar包中重复的内容将不会被加载。

健康监测

当使用占位符配置URI时,Config Server会默认加载application为app的仓库,根据之前的配置规则,服务端的健康检测器会不断检查https://gitee.com/dongspace/a... 仓库是否可以连通,这时访问配置中心的/health端点时返回的服务状态为"DOWN",当服务化配置中心时,将影响它的可用性判断,此时有两种解决方案:1.创建名为app-config的仓库;2.改变健康监测的配置如下

spring.cloud.config.server.health.repositories.check.name=app2
spring.cloud.config.server.health.repositories.check.label=master
spring.cloud.config.server.health.repositories.check.profiles=default
服务化配置中心 config-server改造

在pom.xml文件中添加eureka依赖,以加入Eureka的服务治理体系。


    org.springframework.cloud
    spring-cloud-starter-eureka

在入口类上添加@EnableDiscoveryClient注解,表示这是一个Eureka客户端。

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

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

在application.properties中配置eureka注册中心地址。

spring.application.name=config-server
server.port=7001
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/

#spring.cloud.config.server.git.uri=https://gitee.com/dongspace/{application}-conf
spring.cloud.config.server.git.uri=https://gitee.com/dongspace/config-file/
#git uri下的相对搜索位置,可以配置多个
spring.cloud.config.server.git.search-paths={application}
spring.cloud.config.server.git.username=416974870@qq.com
spring.cloud.config.server.git.password=a1991122911
# 重新设定git仓库连接性检测的地址,需新建命名为app2的仓库
#spring.cloud.config.server.health.repositories.check.name=app2
#spring.cloud.config.server.health.repositories.check.label=master
#spring.cloud.config.server.health.repositories.check.profiles=default
config-client改造

前两步同config-server的改造,最后修改bootstrap.properties配置文件

server.port=7002
spring.application.name=config-client

eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
spring.cloud.config.profile=stg1
spring.cloud.config.label=master
# 开启通过服务名来访问配置中心
spring.cloud.config.discovery.enabled=true
# 配置中心服务名为config-server
spring.cloud.config.discovery.service-id=config-server
#spring.cloud.config.uri=http://localhost:7001/
测试

分别启动eureka-server、config-server、config-client,访问http://localhost:1111,可以看到两个应用已经成功注册:

测试config-client的/testConfig接口,结果如下:

失败快速响应

config-client在启动时,如果访问不到config-server会报错,如果不做任务额外配置,客户端的响应较慢,在报错时已经打印出很多日志。
添加如下配置,可以让客户端在启动时优先检查配置中心的连接情况:

spring.cloud.config.fail-fast=true
重试机制

config-client启动无法连接配置中心时,我们希望客户端能重试连接几次,这样可以避免因网络抖动而导致应用无法启动的情况。
首先确保客户端的快速响应机制打开,再添加如下重试配置:

# 配置重试次数,默认为6
spring.cloud.config.retry.max-attempts=6
# 间隔乘数,默认1.1,如第一次间隔为1000ms,则第二次间隔为1100ms
spring.cloud.config.retry.multiplier=1.1
# 初始重试间隔时间,默认1000ms
spring.cloud.config.retry.initial-interval=1000
# 最大间隔时间,默认2000ms
spring.cloud.config.retry.max-interval=2000
动态刷新配置 其他配置 安全保护

在Config Server中引入Spring Security即可实现配置中心的密码访问。
首先在pom.xml文件中添加依赖:


    org.springframework.boot
    spring-boot-starter-security

然后在application.properties中配置用户名密码:

security.user.name=admin
security.user.password=admin

最后在配置中心客户端配置用户名和密码:

security.user.name=admin
security.user.password=admin
加密与解密 出现的问题

一、初始提交码云时报push to origin/master was rejected错误

解决方案如下:

1.切换到自己项目所在的目录,右键选择GIT BASH Here,Idea中可使用Alt+F12

2.在terminl窗口中依次输入命令:

git pull

git pull origin master

git pull origin master --allow-unrelated-histories

3.在idea中重新push自己的项目,成功!!!

参考资料

1.《Spring Cloud微服务实战》

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/73637.html

相关文章

  • 史上最简单的SpringCloud教程 | 第六篇: 布式配置中心(Spring Cloud Co

    摘要:程序的入口类打开网址访问,网页显示这就说明,从获取了的属性,而是从仓库读取的如图本文源码下载四参考资料优秀文章推荐史上最简单的教程终章史上最简单的教程第一篇服务的注册与发现史上最简单的教程第七篇高可用的分布式配置中心 转载请标明出处: http://blog.csdn.net/forezp/a...本文出自方志朋的博客在上一篇文章讲述zuul的时候,已经提到过,使用配置服务来保存各个服...

    SQC 评论0 收藏0
  • 2021 年最新基于 Spring Cloud 的微服务架构分析

    摘要:是一个相对比较新的微服务框架,年才推出的版本虽然时间最短但是相比等框架提供的全套的分布式系统解决方案。提供线程池不同的服务走不同的线程池,实现了不同服务调用的隔离,避免了服务器雪崩的问题。通过互相注册的方式来进行消息同步和保证高可用。 Spring Cloud 是一个相对比较新的微服务框架,...

    cikenerd 评论0 收藏0
  • Spring Cloud Consul 之Greenwich版本全攻略

    摘要:在我们的文档中,我们使用来表明就选举和事务的顺序达成一致。提供成员关系,故障检测和事件广播。这是一个允许请求的请求响应机制。这包括服务发现,还包括丰富的运行状况检查,锁定,键值,多数据中心联合,事件系统和。 转载请标明出处: http://blog.csdn.net/forezp/a...本文出自方志朋的博客 什么是Consul Consul是HashiCorp公司推出的开源软件,使...

    qingshanli1988 评论0 收藏0
  • SpringCloud打造微服务平台--概览

    摘要:授权框架使第三方应用程序来获取对服务的有限访问机会。无论是通过编排资源所有者和服务之间的交互批准的资源所有者,或通过允许第三方应用程序来获取自己的访问权限。 SpringCloud打造微服务平台--概览 简述 SpringCloud是什么 Spring Boot和SpringCloud是什么关系 Spring Boot是Spring的一套快速WEB开发的脚手架,可建立独立的Sprin...

    siberiawolf 评论0 收藏0

发表评论

0条评论

最新活动
阅读需要支付1元查看
<