SpringCloud系列--7.集群配置中心SpringCloudConfig
SpringCloud Config 为分布式系统提供了配置服务器和配置客户端。我们将配置文件存放到外部系统(Git,SVN等),Spring Cloud Config的服务器与客户端回到这些外部系统中读取、使用这些配置
配置服务器主要有以下功能:
- 提供访问配置的服务接口
- 对属性进行加密和解密
- 可以简单地嵌入Spring Boot的应用中.
配置客户端主要有以下功能
- 绑定配置服务器,使用远程的属性来初始化Spring容器。
- 对属性进行加密和解密
- 属性改变时,可以对他们进行重新加载
- 提供了与配置相关的几个管理端点
- 在初始化引导程序的上下文时,进行绑定配置服务器和属性解密等工作,当然也可以实现其他工作。
应用结构如下图:
容器初始化时会先建立一个"引导上下文(BootStrap Context)",再创建主应用的上下文。引导上下文读取bootstrap.yml(或.properties)文件,主应用程序上下文通常读取application.yml(或.properties)文件, 因为application.yml的配置会在bootstrap.yml后加载,所以如果两个文件中同时存在某属性,则application.yml的配置会覆盖bootstrap.yml的配置。
spring-cloud-config-server提供了四种配置: spring.profiles.active配置如下:
- git: 默认值,表示去git仓库读取配置文件
- subversion: 表示去svn仓库读取配置文件
- native: 本地的文件系统中读取配置文件
- vault: 去Vault中读取配置文件,Vault是一款资源控制工具,可对资源实现安全访问
服务端配置:
如果使用git协议的话,需要将ssh生成的公钥添加到仓库中
生成方式如下:
ssh-keygen -m PEM -t rsa -b 4096 -C "wujingjian@finupgroup.com"
或者使用https://gitlab.xxxxx协议(测试发现https的没问题, http的会连接不上,以后还续继续研究)
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: git@gitlab.xxxxxxx.xx:wujingjian/configserver.git
search-paths: config-repo
default-label: master
force-pull: true
启动类开启注解@EnableConfigServer
package org.crazyit.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigApplication.class, args);
}
}
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.crazyit.cloud</groupId>
<artifactId>spring-config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Dalston.SR1</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
<version>1.9.0</version>
</dependency>-->
<!--<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
<version>1.5.3.RELEASE</version>
</dependency>-->
</dependencies>
</project>
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
where the "application" is injected as the spring.config.name
in the SpringApplication
(i.e. what is normally "application" in a regular Spring Boot app), "profile" is an active profile (or comma-separated list of properties), and "label" is an optional git label (defaults to "master".)
#多目录配置的话: search-paths: '{application}'
spring:
application:
name: configserver
cloud:
config:
server:
git:
uri: git@gitlab.puhuitech.cn:wujingjian/configserver.git
search-paths: '{application}'
default-label: master
这样访问
http://localhost:8888/testdir/config
就会找
git@gitlab.puhuitech.cn:wujingjian/configserver/testdir/application-config.properties 是可以的。
同时:
git@gitlab.puhuitech.cn:wujingjian/configserver/testdir/testdir-config.properties
也是可以的。
标题:SpringCloud系列--7.集群配置中心SpringCloudConfig
作者:码农路上
地址:http://wujingjian.club/articles/2020/03/27/1585309716774.html