SpringCloud配置中心Config

版权声明:技术交流群:758191639 作者支付宝18696232390喜欢的可以打钱! https://blog.csdn.net/u014131617/article/details/85992756

1.Spring Cloud Config 介绍

提供了一种在分布式系统中外部化配置服务器和客户端的支
持。配置服务器有一个中心位置,管理所有环境下的应用的外部属性。客户端和服
务器映射到相同Spring Eventment 和 PropertySrouce抽象的概念,所以非常适合
Spring应用,但也可以在任何语言开发的任何应用中使用。在一个应用从开发、测
试到生产的过程中,你可以分别地管理开发、测试、生产环境的配置,并且在迁移
的时候获取相应的配置来运行。
Config Server 存储后端默认使用git存储配置信息,因此可以很容易支持标记配置
环境的版本,同时可以使用一个使用广泛的工具管理配置内容。当然添加其他方式
的存储实现也是很容易的。

参考:
http://cloud.spring.io/spring-cloud-static/Brixton.SR5/#_spring_cloud_config
核心代码:
org.springframework.cloud.config.server.environment.NativeEnvir
onmentRepository ,留意其中的 findOne 方法。

准备工作
为了更贴近生产,我们首先配置Host
127.0.0.1 config-server

microservice-config-client-dev.properties
microservice-config-client.properties

其中在: microservice-config-client-dev.properties 文件中添加如下内容:

profile=dev

服务器端代码示例

创建一个Maven项目,在pom.xml文件中添加如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-config-server</artifactId>
<packaging>jar</packaging>
<parent>
	<groupId>com.itmuch.cloud</groupId>
	<artifactId>spring-cloud-microservice-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-config-server</artifactId>
	</dependency>
</dependencies>
</project>
  • 启动类:
/**
* 通过@EnableConfigServer注解激活配置服务.
* 说明:
* 在application.yml中有个git.uri的配置,目前配置的是https://github.c
om/eacdy/spring-cloud-study/
* 获取git上的资源信息遵循如下规则:
* /{application}/{profile}[/{label}]
* /{application}-{profile}.yml
* /{label}/{application}-{profile}.yml
* /{application}-{profile}.properties
* /{label}/{application}-{profile}.properties
*
* 例如本例:可使用以下路径来访问microservice-config-client-dev.prope
rties:
* http://localhost:8040/microservice-config-client-dev.properti
es
* http://localhost:8040/microservice-config-client/dev
* ...
* @author eacdy
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigServerApplication.class, args);
	}
}
  • 配置文件:application.yml
server:
	port: 8040
spring:
	application:
		name: microservice-config-server
cloud:
	config:
		server:
		 git:
			uri: https://github.com/eacdy/spring-cloud-study/
			# 配置git仓库的地址
			search-paths: config-repo
			# git仓库地址下的相对地址,可以配置多个,用,分割。
			username:
			# git仓库的账号
			password:
			# git仓库的密码

这样,一个Config Server就完成了。

在这里插入图片描述

例如本例:可使用以下路径来访问 microservice-config-clientdev.
properties :
http://localhost:8040/microservice-config-client-dev.properties
http://localhost:8040/microservice-config-client/dev
按照上文,我们成功搭建了Config Server,并测试能够正常获取到git仓库中的配置
信息。那么对于一个微服务应用,如何才能获取配置信息呢?

配置服务客户端示例

新建一个Maven项目,在pom.xml中添加如下内容:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="ht
tp://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://m
aven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
	<artifactId>microservice-config-client</artifactId>
	<packaging>jar</packaging>
<parent>
	<groupId>com.itmuch.cloud</groupId>
	<artifactId>spring-cloud-microservice-study</artifactId>
	<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-web</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.cloud</groupId>
		<artifactId>spring-cloud-starter-config</artifactId>
	</dependency>
	<dependency>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-actuator</artifactId>
	</dependency>
</dependencies>
</project>
  • 编写启动类:
@SpringBootApplication
public class ConfigClientApplication {
	public static void main(String[] args) {
		SpringApplication.run(ConfigClientApplication.class, args);
	}
}
  • 编写测试Controller
/**
* 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
* @author eacdy
*/
@RestController
@RefreshScope
public class ConfigClientController {
	@Value("${profile}")
	private String profile;
	@GetMapping("/hello")
	public String hello() {
		return this.profile;
	}
}
  • 配置文件:application.yml
server:
	port: 8041

配置文件bootstrap.yml(为什么要使用bootstrap.yml而不直接放在
application.yml中的原因见注意点)

spring:
	application:
		name: microservice-config-client # 对应microservice-config-server所获取的配置文件的{application}
cloud:
	config:
		uri: http://config-server:8040/
		profile: dev # 指定profile,对应microservice-config-server所获取的配置文件中的{profile}
		label: master # 指定git仓库的分支,对应microservice-config-server所获取的配置文件的{label}

启动,并访问:
http://localhost:8041/hello ,我们会发现此时可以显示git仓库中配置文件的内容:

dev

配置内容的热加载
如果不重启应用,能够做到配置的刷新吗?答案显然是可以的。
我们将microservice-config-client-dev.properties中值改为

profile=abcd

并提交到git仓库。
然后使用命令(本文使用的是curl,Linux和Windows都有curl工具,当然也可以借
助其他工具,例如Postman等):
curl -X POST http://localhost:8041/refresh

然后再次访问http://localhost:8041/hello ,将会看到: abcd ,说明配置已经刷
新。

猜你喜欢

转载自blog.csdn.net/u014131617/article/details/85992756