欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Cloud Config 配置中心

程序员文章站 2022-07-15 09:49:13
...


1.介绍

Spring Cloud Config分为两个角色:Config Server 和 Config Client 即 配置服务的提供方 和配置服务的使用方。它支持远程访问git仓库读取配置文件(本文使用这种方式),也支持从本地读取配置文件,这样就可以统一管理配置文件,同时它也提供实时更新配置文件功能,不需要重新启动服务(在我自己学习中会发出来)。

2.使用

1) Config Server
① 先创建一个Spring Boot项目 config-server ,pom文件如下
<?xml version="1.0" encoding="UTF-8"?>
<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>com.xeepoo</groupId>
	<artifactId>config-server</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-server</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RC1</spring-cloud.version>
	</properties>

	<dependencies>
		<!-- 配置服务 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>


</project>

②在SpringBoot启动类 ConfigServerApplication 上添加注解 @EnableConfigServer

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

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

③配置文件 application.yml

#端口号
server:
  port: 8880
spring:
  cloud:
    config:
      server:
        #git
        git:
          uri: https://gitee.com/XeePoo/config-server.git
          search-paths: resp
          username: ********
          password: ********

  • searc-path是指当前配置文件在当前仓库下的路径 比如配置文件路径为仓库 /resp/application.yml 则searc-path: resp
  • username:git用户名 
  • password:密码
  • 如果为公开仓库可以不用配置用户名和密码

本文使用的仓库为私有仓库https://gitee.com/XeePoo/config-server.git 文件夹 resp内有以一配置文件config-client-dev.properties 其中有一个属性

 test=Spring Cloud Config
启动项目:

访问地址http://localhost:8880/config-client/dev/master 结果:

{"name":"config-client",
"profiles":["dev"],
"label":master,
"version":"8a60374cecde1e78461cad66b2caf7486c61bd25",
"state":null,
"propertySources":[{"name":"https://gitee.com/XeePoo/config-server.git/resp/config-client-dev.properties",
"source":{"test":"Spring Cloud Config"}}]}

证明可以从远程服务中心获取配置,并且已经获取到了 properties 可以看到(其中label为仓库分支) 访问映射对应为:
  • /{name}/{profile}[/{label}]

其它映射可以从启动信息中看到等等

{[/{name}-{profiles}.properties],methods=[GET]}
{[/{name}-{profiles}.yml || /{name}-{profiles}.yaml],methods=[GET]}
{[/{label}/{name}-{profiles}.properties],methods=[GET]}
{[/{label}/{name}-{profiles}.json],methods=[GET]}
{[/{name}/{profiles:.*[^-].*}],methods=[GET]}
{[/{label}/{name}-{profiles}.yml || /{label}/{name}-{profiles}.yaml],methods=[GET]} 
{[/{name}/{profiles}/{label:.*}],methods=[GET]}
{[/{name}-{profiles}.json],methods=[GET]}
{[/{name}/{profile}/{label}/**],methods=[GET]
{[/{name}/{profile}/{label}/**],methods=[GET]}等等...

2) Config Client

Spring Boot项目 config-client,pom文件如下

<?xml version="1.0" encoding="UTF-8"?>
<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>com.xeepoo</groupId>
	<artifactId>config-client</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>config-client</name>
	<description>Demo project for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.1.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.RC1</spring-cloud.version>
	</properties>

	<dependencies>
		<!-- 配置服务 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-config</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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

	<repositories>
		<repository>
			<id>spring-milestones</id>
			<name>Spring Milestones</name>
			<url>https://repo.spring.io/milestone</url>
			<snapshots>
				<enabled>false</enabled>
			</snapshots>
		</repository>
	</repositories>


</project>

②在SpringBoot启动类 ConfigClientApplication

@SpringBootApplication
public class ConfigClientApplication {

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

新建一个Controller 类 TestController

@RestController
public class TestController {
    @Value("${test}")
    String test;
    @Autowired
    Environment env;

    @RequestMapping("hello1")
    public String hello1(){
        return "hello1 "+test;
    }

    @RequestMapping("hello2")
    public String hello2(){
        return "hello2 "+env.getProperty("test");
    }
}
其中: /hello1 以注入方式获取 /hello2  从环境变量中读取

 ③配置文件 bootstrap.yml(Spring Boot 中application.yml与bootstrap.yml的区别)

server:
  port: 8881
spring:
  application:
    name: config-client
  cloud:
    config:
      profile: dev
      label: master
      fail-fast: true #没有发现 config server 会快速报错停止
      uri: http://localhost:8880

这里可以结合config server的映射地址 http://localhost:8880/config-client/dev/master  理解 

  • /{name}/{profile}[/{label}]

④启动 config-client 访问地址 http://localhost:8881/hello1与 http://localhost:8881/hello2可以看到

hello1 Spring Cloud Config
hello2 Spring Cloud Config

可以证明都两种方式都获取到了远程git仓库中 属性test的值

Spring Cloud Config 配置中心

下一篇文章会 将结合Eureka注册中心 做成高可用分布式配置中心