分布式配置中心:Spring Cloud Config

栏目: Java · 发布时间: 5年前

内容简介:配置信息的URL与配置文件的映射关系如下:上面的URL对应{application}-{profile}.properties,其中{application}对应配置文件名,{label}对应代码分支,默认为default以上通过浏览器访问的路径对应为:

构建配置中心

  • 创建一个基础Spring Boot工程,命名为config-server,并在pom.xml中引入以下依赖:
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
  • 在程序主类中添加@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

以上通过浏览器访问的路径对应为:

客户端配置

  • 创建一个基础Spring Boot工程,命名为config-client,并在pom.xml中引入以下依赖:
<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
  • 在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

基础架构

分布式配置中心:Spring Cloud Config

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

  1. 应用启动时,根据bootstrap.properties中配置的{application}、{profile}、{label},向Config Server请求获取配置信息。
  2. Config Server根据自己维护的Git仓库信息和客户端传递过来的配置定位信息去查找配置信息。
  3. 通过git clone命令将找到的配置信息下载到Config Server的文件系统中。
  4. Config Server创建Spring的ApplicationContext实例,并从Git本地仓库中加载配置文件,返回给客户端使用。
  5. 客户端应用获得外部配置信息后,加载到客户端的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

其他配置

安全保护

首先在服务端pom.xml中添加Spring Security依赖:

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

出现的问题

初始提交码云时报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自己的项目,成功!!!


以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

数据结构、算法与应用(原书第2版)

数据结构、算法与应用(原书第2版)

Sartaj Sahni / 王立柱、刘志红 / 机械工业出版社 / 2015-4 / 79.00元

《数据结构、算法与应用——C++语言描述》是享有盛誉的数据结构教科书的第2版。它完整地包含了基本数据结构的内容,是CS2课程的理想用书。作者Sartaj Sahni通过循循善诱的讲解、直观具体的讨论和基于现实的应用,让读者轻松、愉快地学习。新版书着重利用标准模板库(STL),把书中开发的数据结构和算法与相应的STL实现方法相互关联。本书还增加了很多新的实例和练习题。 书中的应用实例是它的特色......一起来看看 《数据结构、算法与应用(原书第2版)》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

正则表达式在线测试
正则表达式在线测试

正则表达式在线测试