spring cloud config server 官方文档在
http://cloud.spring.io/spring-cloud-config/single/spring-cloud-config.html
也有中文版的。
1. Quick Start
1.1.建立一个config sever项目
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.cloud.shj</groupId>
<artifactId>config-server</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>SpringCloud-Config</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
<repositories>
<repository>
<id>spring-releases</id>
<url>https://repo.spring.io/libs-release</url>
</repository>
</repositories>
</project>
pom.xml
server:
port: 7010 #端口
spring:
application:
name: server-config
cloud:
config:
server:
git:
uri: https://github.com/shaweiwei/myspringcloudconfig/ #Spring Cloud Config服务器从git存储库中提取远程客户端的配置(必须提供)
search-paths: myconfigpath #搜索文件目录
label: master
主启动类
package com.example.springcloudconfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigApplication {
public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigApplication.class, args);
}
}
注意:
1.config server的git服务器,如果是公共的,用户名和密码就不用设置,如果是私有的,需要设置用户名和密码。
访问地址https://github.com/shaweiwei/myspringcloudconfig/可以直进入下面的页面。
打开文件夹“myconfigpath",可以看到很多properties文件
1.2.尝试一个客户端
C:\Users\lunmei>curl localhost:7010/config-client-dev.yml
democonfigclient:
message: hello spring io
myww: myww version 2
http服务具有以下服务的资源
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
什么意思?比如,对于上面的文件config-client-dev.properties,打开后内容是
application:config-client
profile:dev
label:master
C:\Users\lunmei>curl localhost:7010/config-client-dev.yml
democonfigclient:
message: hello spring io
myww: myww version 2
C:\Users\lunmei>curl localhost:7010/config-client-dev.properties
democonfigclient.message: hello spring io
myww: myww version 2
C:\Users\lunmei>curl localhost:7010/master/config-client-dev.properties
democonfigclient.message: hello spring io
myww: myww version 2
C:\Users\lunmei>curl localhost:7010/master/config-client-dev.yml
democonfigclient:
message: hello spring io
myww: myww version 2
类似的
C:\Users\lunmei>curl localhost:7010/service-config/dev/master
{"name":"service-config","profiles":["dev"],"label":"master","version":"38bd83c975164ec6f1ce10343fe346c0022db3fa","state":null,"propertySources":[{"name":"https://github.com/shaweiwei/myspringcloudconfig//myconfigpath/service-config-dev.properties","source":{"ip":"192.168.30.51","port":"7777"}}]}
1.3.config client端写法
pom.xml
<?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">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>config-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>config-client</name>
<description>Demo project for Spring Boot</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.5.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Finchley.SR1</spring-cloud.version>
</properties>
<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.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
bootstrap.properties
# 和git里的文件名对应
spring.application.name=config-client
server.port=7021
# 指明配置服务中心的网址
spring.cloud.config.uri= http://localhost:7010/
# 远程仓库的分支
spring.cloud.config.label=master
# dev 开发环境配置文件 | test 测试环境 | pro 正式环境
# 和git里的文件名对应
spring.cloud.config.profile=dev
启动类
package com.example.configclient;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class ConfigClientApplication {
@Value("${myww}") // git配置文件里的key
String foo;
@RequestMapping("/config")
public String config(){
return foo;
}
@RequestMapping("/a")
public String a(){
return "hello";
}
public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
1.4.config server继承EnvironmentRepository
config client 不变,config server写法变了。使用自定义的内容。
注意官方文档中有一段说明:
环境库
您要在哪里存储配置服务器的配置数据?管理此行为的策略是EnvironmentRepository,服务于Environment对象。这个Environment是Spring Environment(包括propertySources作为主要功能)的域的浅层副本。Environment资源由三个变量参数化:
{application}, which maps to spring.application.name on the client side.
{profile}, which maps to spring.profiles.active on the client (comma-separated list).
{label}, which is a server side feature labelling a "versioned" set of config files.存储库实现通常表现得像一个Spring Boot应用程序从“spring.config.name”等于{application}参数加载配置文件,“spring.profiles.active”等于{profiles}参数。配置文件的优先级规则也与常规启动应用程序中的相同:活动配置文件优先于默认配置,如果存在多个配置文件,则最后一个配置文件(例如向Map添加条目))。
示例:客户端应用程序具有此引导配置:
bootstrap.ymlspring:
application:
name: foo
profiles:
active: dev,mysql
pom.xml
server:
port: 7010
spring:
application:
name: server-config
profiles:
active: mybatis #生效版本profile
#cloud:
#config:
#server:
#git:
#uri: https://github.com/shaweiwei/myspringcloudconfig/
#search-paths: myconfigpath
#label: master
增加配置文件
package com.example.springcloudconfig.config;
import org.springframework.cloud.config.environment.Environment;
import org.springframework.cloud.config.environment.PropertySource;
import org.springframework.cloud.config.server.environment.EnvironmentRepository;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import java.util.HashMap;
import java.util.Map;
@Configuration
@Profile("mybatis")
public class MybatisEnvironmentRepository implements EnvironmentRepository {
/**
*
* @param application
* @param profile
* @param label
* @return
*/
@Override
public Environment findOne(String application, String profile, String label) {
//map的内容可以从数据库或其他地方获取。
Map<String,String> map = new HashMap<String,String>();
map.put("key1","key");
map.put("key2","key");
map.put("key3","key");
if(application.equals("config-client")){//当spring.application.name=config-client时候
map.put("myww","look at here");
}
Environment environment = new Environment(application,new String[]{profile},label,"1.0",null);
environment.add(new PropertySource("application.properties",map));
return environment;//new Environment(application,profile);
}
}
运行后发现仍然生效
C:\Users\lunmei>curl http://localhost:7010/master/config-client-dev.yml
application: config-client
key1: key
key2: key
key3: key
myww: look at here
C:\Users\lunmei>curl http://localhost:7010/master/other-dev.yml
application: config-client
key1: key
key2: key
key3: key
而运行了客户端后,也能看到效果
C:\Users\lunmei>curl http://localhost:7021/config
look at here