Spring Cloud(Finchley.RELEASE版本)微服务学习实践:7.1分布式配置中心

环境:

jdk1.8;spring boot2.0.3;spring cloud(Finchley.RELEASE版本);Maven3.3

摘要说明:

Spring Cloud Config:Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。通过配置服务器,您可以在所有环境中管理应用程序的外部属性。客户端和服务器映射上的概念与Spring环境和PropertySource抽象完全一致,因此它们非常适合Spring应用程序,但可以用于任何语言中运行的任何应用程序。当应用程序通过从dev到测试和生产的部署管道时,您可以管理这些环境之间的配置,并确保应用程序拥有在迁移时需要运行的所有内容。服务器存储后端的默认实现使用git,因此它可以轻松地支持带标签的配置环境版本,并且可以访问各种工具来管理内容。很容易添加替代实现并将它们与Spring配置插入。可参考

特征

Spring Cloud Config Server功能:

  • 用于外部配置的HTTP,基于资源的API(名称 - 值对或等效的YAML内容)
  • 加密和解密属性值(对称或不对称)
  • 使用可轻松嵌入Spring Boot应用程序 @EnableConfigServer

Config Client功能(适用于Spring应用程序):

  • 绑定到配置服务器并Environment使用远程属性来初始化Spring
  • 加密和解密属性值(对称或不对称)
总结:配置中心为每个服务统一配置;各个服务可直接从配置中心获取信息;

步骤:

1.创建configServerconfigServer配置中心)子项目

通过SPRING INITIALIZR工具选择Cloud Config:Config Server模块构建configServer子项目引入依赖(pom.xnl):

<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>
	<parent>
		<groupId>com.tit</groupId>
		<artifactId>springCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>configServer</artifactId>
	<name>configServer</name>
	<description>配置中心-服务端</description>
	<dependencies>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
</project>

2.配置configServer

使用@EnableConfigServer注解开启configServer的服务中心配置:

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerApplication {

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

配置application.properties:

这里面需要先配置一个git仓库,当然这里也可以使用其他仓库如svn;如有分支,用户,密码及目录都可以配置;此次示例仓库为public且为更路径下故不设置;

#配置服务名称及端口
spring.application.name=config-server
server.port=9001
#将服务注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/

#服务的git仓库地址
spring.cloud.config.server.git.uri=https://gitee.com/yepiaoling/spring-could-config
#配置文件所在的目录
#spring.cloud.config.server.git.search-paths=/**
#配置文件所在的分支
spring.cloud.config.label=master
#git仓库的用户名
#spring.cloud.config.username=********
#git仓库的密码
#spring.cloud.config.password=********

仓库指定目录下的配置文件格式如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中label为分支默认为 master;application为服务名称;profile为环境;

故在仓库中添加配置文件config-client.properties,指的是给config-client服务默认环境的配置文件

test=config-client-test

启动服务:

服务注册中心(eurekaServer):eureka-server(1001)

配置中心(configServer):config-server(9001)

通过访问http://localhost:9001/config-client/master;获得配置信息:

{"name":"config-client","profiles":["master"],"label":null,"version":"e68941966cfb7f870643074ade0b27a9f752510d","state":null,"propertySources":[{"name":"https://gitee.com/yepiaoling/spring-could-config/config-client.properties","source":{"test":"config-client-test"}}]}

或者访问http://localhost:9001/master/config-client.properties(http://localhost:9001/config-client.properties)直接访问配置文件的信息:

test=config-client-test

3.创建configClientconfigClient配置客户端)子项目

通过SPRING INITIALIZR工具选择Cloud Config:Config Client模块构建configClient子项目引入依赖(pom.xnl):

<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>
	<parent>
		<groupId>com.tit</groupId>
		<artifactId>springCloud</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>configClient</artifactId>
	<name>configClient</name>
	<description>服务配置中心-客户端</description>
	<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.cloud</groupId>
			<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
		</dependency>
	</dependencies>
</project>

4.配置configClient

配置application.properties:
#配置服务名称及端口
spring.application.name=config-client
server.port=10001

但想获取配置中心的信息必须配置到bootstrap.properties中:

此次使用的是服务配置来进行与服务中心的交互;也可以直接使用spring.cloud.config.uri来进行配置;

#将服务注册到注册中心
eureka.client.serviceUrl.defaultZone=http://localhost:1001/eureka/
#开启配置服务发现
spring.cloud.config.discovery.enabled=true
#配置服务实例名称
spring.cloud.config.discovery.service-id=config-server

#配置服务中心
#spring.cloud.config.uri=http://localhost:9001/

#配置文件所在分支
spring.cloud.config.label=master
#配置文件所指环境
spring.cloud.config.profile=default

开发一个测试类(TestController)进行测试:

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TestController {
	@Value("${test}")
	private String test;

	@GetMapping("/test")
	public String test() {
		return test;
	}
}

启动服务:

服务注册中心(eurekaServer):eureka-server(1001)

配置中心(configServer):config-server(9001)

配置客户端( configClient ):config-client(10001)

通过访问http://localhost:10001/test来进行验证可直接获取配置的值 :
config-client-test

5.配置手动刷新

我们接着上面将git仓库中添加 config-client-dev.properties:
test=config-dev
更新bootstrap.properties中的:
#配置文件所指环境
spring.cloud.config.profile=dev

重新启动服务:

配置客户端( configClient ):config-client(10001)
通过访问http://localhost:10001/test来进行验证可直接获取配置的值 :
config-dev

接着我们修改config-client-dev.properties中的test值为config-client-dev;重新访问http://localhost:10001/test;但值未变,这说明配置修改但客户端未进行变更;

我们在pom.xml中添加spring-boot-starter-actuator依赖:

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

更新bootstrap.properties:

management.endpoints.web.exposure.include=*
#springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置,以post请求的方式来访问http://localhost:8081/refresh 就会更新修改后的配置文件
management.security.enabled=false

TestController中添加注解@RefreshScope;

重新启动服务:

配置客户端( configClient ):config-client(10001)
通过访问http://localhost:10001/test来进行验证可直接获取配置的值:
config-client-dev

再修改config-client-dev.properties中的test值为config-client-dev1;

用post方式:http://localhost:10001/actuator/refresh进行任务更新;再访问会获得最新值;

6.源码地址

github地址:https://github.com/cc6688211/springCloud.git


猜你喜欢

转载自blog.csdn.net/u010904188/article/details/80898748