Spring Cloud Config为分布式系统中的外部配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。客户端和服务器上的概念映射与Spring Environment
和PropertySource
抽象相同,因此它们与Spring应用程序非常契合,但可以与任何以任何语言运行的应用程序一起使用。随着应用程序通过从开发人员到测试和生产的部署流程,您可以管理这些环境之间的配置,并确定应用程序具有迁移时需要运行的一切。服务器存储后端的默认实现使用git,因此它轻松支持标签版本的配置环境,以及可以访问用于管理内容的各种工具。可以轻松添加替代实现,并使用Spring配置将其插入。
快速开始
一、服务端
准备工作:
a、本地编写个application.yml配置文件,文件内容为:
spring:
profiles:
active:
- dev
---
spring:
profiles: dev
application:
name: microservicecloud-config-zhanghf-dev
---
spring:
profiles: test
application:
name: microservicecloud-config-zhanghf-test
配置文件中配置两种环境,分别为开发环境和测试环境,默认为开发环境,然后再github上创建个仓库并将此配置文件推送到远程仓库中,这边我创建的仓库为 git@github.com:Heaven8/microservisecloudConfig.git。
重要的事情说三遍:文件需要为UTF-8格式、UTF-8格式、UTF-8格式。
b、本地做下域名映射,便于待会访问测试
127.0.0.1 config-4433.com
开始编码:
1、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>microservicecloud</artifactId>
<groupId>com.zhanghf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-config-4433</artifactId>
<dependencies>
<!-- springCloud Config -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- 避免Config的Git插件报错:org/eclipse/jgit/api/TransportConfigCallback -->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>4.10.0.201712302008-r</version>
</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>
2、appplication.yml配置文件
server:
port: 4433
spring:
application:
name: microservisecloudConfig
cloud:
config:
server:
git:
uri: git@github.com:Heaven8/microservisecloudConfig.git #GitHub上面的git仓库名字
设置端口为4443,spring.cloud.config.server.git.uri为github上面创建好的相应git仓库名字
3、启动类添加注解@EnableConfigServer
package com.zhanghf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* SpringCloudConfig
*/
@SpringBootApplication
@EnableConfigServer
public class Config_4433_StartSpringCloudApp
{
public static void main( String[] args )
{
SpringApplication.run(Config_4433_StartSpringCloudApp.class,args);
}
}
4、运行测试
文件的读取有一下几种方式:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中“应用程序”作为SpringApplication
中的spring.config.name
注入(即常规Spring Boot应用程序中通常为“应用程序”),“配置文件”是活动配置文件(或逗号分隔列表)的属性),“label”是可选的git标签(默认为“master”)。
下面演示yml文件的3中读取方式:
访问:http://config-4433.com:4433/application-dev.yml
访问:http://config-4433.com:4433/application/dev/master
访问:http://config-4433.com:4433/master/application-dev.yml
即完成了configServer端的配置。
二、客户端
准备工作:
a、本地编写microservicecloud-config-client.yml配置文件
spring:
profiles:
active:
- dev
---
server:
port: 8201
spring:
profiles: dev
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka7001.com:7001/eureka/
---
server:
port: 8202
spring:
profiles: test
application:
name: microservicecloud-config-client
eureka:
client:
service-url:
defaultZone: http://eureka7002.com:7002/eureka/
以UTF-8编码保存后上传到git仓库。
b、域名映射
127.0.0.1 clientConfig-5533.com
开始编码:
1、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>microservicecloud</artifactId>
<groupId>com.zhanghf</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-config-client-5533</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>
2、配置文件bootstrap.yml(这个为系统级别配置文件,优先级比application.yml的高)
spring:
cloud:
config:
name: microservicecloud-config-client #需要从github上读取的资源名称,注意没有yml后缀名
profile: test #本次访问的配置项
label: master
uri: http://config-4433.com:4433 #本微服务启动后先去找4433号服务,通过SpringCloudConfig获取GitHub的服务地址
spring.cloud.config.name为刚才编写的配置文件名称,不需要后缀名,spring.cloud.config.profile指定本次配置项,spring.cloud.config.label为配置文件所在分支,最后个uri为本微服务启动后先去找4433服务,即configserver服务,再获取github的服务地址。
3、测试类
package com.zhanghf.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;
}
}
读取到配置文件的信息后通过接口config返回配置信息。
4、主启动类
package com.zhanghf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* SpringCloudConfig
*
*/
@SpringBootApplication
public class ConfigClient_5533_StartSpringCloudApp
{
public static void main( String[] args )
{
SpringApplication.run(ConfigClient_5533_StartSpringCloudApp.class,args);
}
}
5、测试
访问http://clientconfig-5533.com:8202/config
本例的多种测试环境直接写在配置环境中,实际应用中为运维等人员直接修改github上的配置文件信息 ,不需要开发人员修改代码,即可完成配置的更高。
完整项目地址:git@github.com:Heaven8/SpringCloud.git