Git源码: https://github.com/chenhang666/SpringCloud
1、概述
SpringCloud Config为微服务架构中的微服务提供几种化的外部配置支持,配置服务器为各个不同微服务应用的所有环节提供了一个配置中心化的外部配置。
分布式系统面临的配置问题
- 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较少,因此系统中会出现大量的服务。
- 由于每个服务都需要必要的配置信息才能运行,所以一套集中式的、动态的配置管理设施是必不可少的。
- 服务端也称为分布式配置中心,它是一个独立的微服务应用,用来连接配置服务器并为客户端提供获取配置信息、加密、解密信息等访问接口。
-
客户端则是通过制定的配置中心来管理应用资源,以及与业务相关的配置内容,并在启动的时候从配置中心获取和加载配置信息,配置服务器默认采用git来存储配置信息,这样就有助于对环境配置进行版本管理,并且可以通过git客户端工具来方便的管理和访问配置内容。
-
SpringCloud Config默认使用Git来存储配置文件
2、Config服务端与Github通信
Github上新建microservicecloud-config项目,在本地将其clone下来,新建一个application.yml文件(保存格式为UTF-8),提交到Github
spring:
profiles:
active:
- dev
---
spring:
profiles: dev #开发环境
application:
name: microservicecloud-config-dev
---
spring:
profiles: test #测试环境
application:
name: microservicecloud-config-test
项目中新建一个模块microservicecloud-config-3344
application.yml
server:
port: 3344
spring:
application:
name: microservicecloud-config
cloud:
config:
server:
git:
#uri: git@github.com:chenhang666/microservicecloud-config.git
uri: https://github.com/chenhang666/microservicecloud-config.git
#路径对应Github的路径,建议选http,ssh好像会报错
主启动类
package com.chen.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer //开启配置中心服务端
public class Config_3344_SpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(Config_3344_SpringCloudApp.class, args);
}
}
访问路径:http://localhost:3344/application-dev.yml
3、Config客户端通过Config服务端获取Github上的配置
在本地git库microservicecloud-config下,新建一个microservicecloud-config-client.yml文件(保存格式为UTF-8),提交到Github
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/
创建ConfigClient模块
<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.chen.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-config-client-3355</artifactId>
<dependencies>
<!-- SpringCloud Config客户端 -->
<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>
<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>
<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>
新建bootstrap.yml
#application.yml是用户级的资源配置项
#bootstrap.yml是系统级
spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀
profile: dev #本次访问的配置项
label: master
uri: http://localhost:3344
#本微服务启动后先去找3344服务,通过SoringCloudConfig获取Github的服务地址
新建application.yml
spring:
application:
name: microservicecloud-config-client
创建测试类ConfigClientRest
package com.chen.springcloud.rest;
import org.springframework.beans.factory.annotation.Value;
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("/config")
public String getConfig(){
String str = "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
System.out.println("******str: " + str);
return "applicationName: " + applicationName + "\t eurekaServers:" + eurekaServers + "\t port: " + port;
}
}
启动ConfigServer与ConfigClient,bootstrap.yml中的profile属性决定启用的配置,当前profile为dev,启动后的端口为8201。
ConfigClient项目启动时会通过ConfigServer 获取github访问地址。
测试地址:http://localhost:8201/config