SpringCloud Config 分布式配置中心
1、概述
分布式系统面临问题,配置问题
集中式的、动态的配置管理必不可少
Spring colud Config为微服务架构中心的微服务提供集中式的外部配置支持,配置服务器为各个不同的微服务应用提供一个中心化的外部配置
Spring colud Config分为服务端和客户端两部分
- 服务端
也成为分布式配置中心,他是一个独立的微服务应用,用来连接配置服务器并未客户端提供获取配置等访问接口
- 客户端
通过指定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取配置,配置服务器默认采用git来存储配置信息。这样有助于对环境配置的版本管理,并且可以通过git客户端访问配置内容
2、功能
-
集中管理配置文件
-
不同环境不同配置,动态化的配置更新,分环境部署dev/test/prod/beta/release
-
当配置发生变化是,服务不需要重启
-
运行期间动态调整配置
-
将配置信息以rest接口形式暴露
-
与github、svn整合
3、SpringCloud Config 服务端配置
1、在github上新建springcloud-study-config的repository
https://github.com/bailijun007/springcloud-study-config
2、在本地硬盘中新建git仓库并clone
3、在本地git仓库中新建一个application.yml,提交到github
格式必须是UTF-8的形式保存,否则会有乱码
spring:
profiles:
active: dev
---
spring:
profiles: dev #开发环境
application:
name: springcloud-study-config-dev
---
spring:
profiles: test #测试环境
application:
name: springcloud-study-config-test
#请保存为UTF-8格式
4、新建module,springcloud-study-config-3344
- 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">
<parent>
<artifactId>springcloud-study</artifactId>
<groupId>com.blj</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-study-config-3344</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!--zuul依赖-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Hystrix容错-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
- yml文件
server:
port: 3344
spring:
application:
name: springcloud-study-config
cloud:
config:
server:
git:
uri: https://github.com/bailijun007/springcloud-study-config.git
#username: #账号
#password: #密码
- 主启动类
package com.blj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class Config3344App {
public static void main(String[] args) {
SpringApplication.run(Config3344App.class,args);
}
}
修改host文件增加映射
- 修改host文件增加映射
127.0.0.1 config-3344.com - 启动服务,访问yml
http://config-3344.com:3344/application-dev.yml
http://config-3344.com:3344/application/dev/master
4.SpringCloud Config 客户端配置与测试
1、在本地git仓库中新建一个springcolud-study-config.yml
spring:
profiles:
active: dev
---
server:
port: 8201
spring:
profiles: dev #开发环境
application:
name: springcloud-study-config-client
eureka:
client:
service-url:
defaultZone: eureka-dev.com:7001/eureka
---
server:
port: 8202
spring:
profiles: test #开发环境
application:
name: springcloud-study-config-client
eureka:
client:
service-url:
defaultZone: eureka-test.com:7001/eureka
#请保存为UTF-8格式
2.新建springcloud-study-config-client-3355模块
- 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">
<parent>
<artifactId>springcloud-study</artifactId>
<groupId>com.blj</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-study-config-client-3355</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--Hystrix容错-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-hystrix</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-config-client -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</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>
</dependency>
<!--热部署 修改后立即生效-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
</project>
- 配置文件
application.yml是用户级的资源配置项
bootstrap.yml是系统级的,优先级更高
Springcolud 会创建一个BootStrap Context,作为spring应用的Application Context的父上下文。初始化的时候,BootStrap Context负责从外部资源加载配置属性并解析配置。这两个上下文共享一个外部获取的Environment。BootStrap 属性有高优先级,默认情况下,他们不会被本地配置覆盖。BootStrap Context和Application Context有着不同的约定。
所以新增一个bootstrap.xml,保证BootStrap Context和Application Context的配置分离。
- application.yml
spring:
application:
name: springcloud-study-config-client
- bootstrap.yml
spring:
cloud:
config:
name: springcolud-study-config-client #需要从github上服务的资源名称,注意没有yml后缀
profile: dev #对应spring.profiles.active
label: master
uri: http://config-3344.com:3344 #本次服务启动后,先去找3344服务,通过SpringCloudConfig获取github上的配置
- host文件配置
127.0.0.1 client-config.com
- 主启动类
package com.blj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
@SpringBootApplication
public class ConfigClient3355App {
public static void main(String[] args) {
SpringApplication.run(ConfigClient3355App.class,args);
}
}
- 新建rest类,验证是否能从Github上读取配置
package com.blj.springcloud.rest;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ConfigClientRest {
@Value("${spring.application.name}")
private String applicationName;
@Value("${eureka.client.service-url.defaultZone}")
private String eurekaServers;
@Value("${server.port}")
private String port;
@RequestMapping(value = "/config")
public String getConfig(){
System.out.println("applicationName:"+this.applicationName
+"eurekaServers:"+this.eurekaServers
+"port:"+this.port);
return "applicationName:"+this.applicationName
+"eurekaServers:"+this.eurekaServers
+"port:"+this.port;
}
}
测试
- 启动config配置中心3344微服务并自测
http://config-3344.com:3344/springcolud-study-config-client-dev.yml
- 启动3355作为client准备访问
- bootstrap.yml中profile值是什么,决定github上读取什么。
如果目前是profile:dev,而dev在github上默认端口是:8201,则访问:
http://client-config.com:8201/config
如果目前是profile:test,而dev在github上默认端口是:8202,则访问:
http://client-config.com:8202/config