SpringCloud微服务实战-SpringCloudConfig配置中心
- Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。
- 客户端和服务器上的概念都与Spring Environment和PropertySource抽象相同,因此它们非常适合Spring应用程序,但可以与任何语言运行的应用程序一起使用。
- 当应用程序通过从开发环境到测试环境和生产环境的部署管道时,您可以管理这些环境之间的配置,并确保应用程序在迁移时需要运行所需的一切。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,并且可以通过各种工具来访问内容。
10.1 基本使用
<?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>spring-cloud-microservice-study</artifactId>
<groupId>com.clsaa.learn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.coding.net/eggyer/learn-spring-cloud-config.git
- 编写启动类(@EnableConfigServer)
package com.clsaa.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
/**
* Created by eggyer on 2017/3/13.
*/
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApp {
public static void main(String[] args) {
SpringApplication.run(ConfigServerApp.class,args);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
我们可以通过http://localhost:8080/application-application.yml获取数据
10.2 应用和数据获取映射规则
- application:spring.application.name
- label:git上的分支名
- profile:文件名
获取git上的资源信息遵循如下规则 |
---|
/{application}/{profile}[/{label}] |
/{application}-{profile}.yml |
/{label}/{application}-{profile}.yml |
/{application}-{profile}.properties |
/{label}/{application}-{profile}.properties |
{
"name": "foobar-dev.yml",
"profiles": [
"master"
],
"label": "master",
"version": "5fd6ff5dc6ffab7b8aec5e4f6828218d5028f907",
"state": null,
"propertySources": [
{
"name": "https://git.coding.net/eggyer/learn-spring-cloud-config.git/application.yml",
"source": {
"document": "profile:profile-default"
}
}
]
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
10.3 SpringCloudConfig-Client
spring:
cloud:
config:
uri: http:
profile: dev #对应业务名称的profile
label: master #当配置文件在git上时为分支名
application:
name: foobar #对应文件的业务名称
server:
port: 8081
package com.clsaa.springcloud.config;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Created by eggyer on 2017/3/13.
*/
@SpringBootApplication
public class ConfigClientApp {
public static void main(String[] args) {
SpringApplication.run(ConfigClientApp.class,args);
}
}
package com.clsaa.springcloud.config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
/**
* Created by eggyer on 2017/3/13.
*/
@RestController
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/profile")
public String getProfile(){
return profile;
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
10.4 git仓库配置详解
- 分别创建git仓库special和simple,在两个仓库下加入配置文件application.yml,内容分别为profile: special;profile: simple
-
在configserver配置文件编写
-
使用一个微服务一个配置
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.coding.net/eggyer/{application}
使用一种环境一种配置
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.coding.net/eggyer/{application}-{profile}
模式匹配
- 在special下添加两个配置文件special-dev.yml,special-test.yml
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.coding.net/eggyer/learn-spring-cloud-config.git
repos:
simple: https://git.coding.net/eggyer/simple
special:
pattern: special*/dev*,*special*/test* #/用来隔开application和environment(开发环境)
uri: https://git.coding.net/eggyer/special
10.5 加解密
- The server also exposes /encrypt and /decrypt endpoints (on the assumption that these will be secured and only accessed by authorized agents). If you are editing a remote config file you can use the Config Server to encrypt values by POSTing to the /encrypt endpoint, e.g.
$ curl localhost:8888/encrypt -d mysecret
682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
The inverse operation is also available via /decrypt (provided the server is configured with a symmetric key or a full key pair):
$ curl localhost:8888/decrypt -d 682bc583f4641835fa2db009355293665d2647dade3375c0ee201de2a49f7bda
mysecret
10.6 用户认证
server部分
<?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>spring-cloud-microservice-study</artifactId>
<groupId>com.clsaa.learn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>microservice-config-server</artifactId>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.coding.net/eggyer/learn-spring-cloud-config.git
security:
basic:
enabled: true
user:
name: user
password: password
client部分
- 在bootstrap文件中加入
- 在uri下可以加入username和password属性,优先级比uri中高
- 那么为什么springcloud会提供两种方式?
- 生产环境中configserver需要高可用(可能有多个)
- 上面方式中如果需要负载均衡需要部署nginx等组件
- 节点信息被硬编码在配置文件中
- 没有充分发挥服务发现的优势所在
- 那么我们需要让configclient也具有服务发现的功能,让其通过eureka自动的寻找到configserver
spring:
cloud:
config:
uri: http:
profile: dev #对应业务名称的profile
label: master #当配置文件在git上时为分支名
application:
name: foobar #对应文件的业务名称
10.7 通过Eureka发现configserver
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
配置服务与注册中心联合使用
在生产环境中,我们可能会将Config Server 与 Eureka等注册中心联合使用(注意:目前Spring Cloud只支持与Eureka及Consul联合使用,不支持与Zookeeper联合使用),下面讲解如何将Config Server与 Eureka 联合使用。
准备工作
- 启动服务
microservice-discovery-eureka
; - 和上文一样,准备好几个配置文件,命名规范为
项目名称-环境名称.properties
,本文使用的名称是microservice-config-client-eureka-dev.properties
。
代码示例
服务器端代码示例:
首先新建一个Maven项目,在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>
<artifactId>microservice-config-server-eureka</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>spring-cloud-microservice-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
启动类:ConfigServerEurekaApplication.java
@SpringBootApplication
@EnableConfigServer
@EnableDiscoveryClient
public class ConfigServerEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigServerEurekaApplication.class, args);
}
}
配置文件:application.yml
server:
port: 8050
spring:
application:
name: microservice-config-server-eureka
cloud:
config:
server:
git:
uri: https:
search-paths: config-repo
username:
password:
eureka:
client:
serviceUrl:
defaultZone: http:
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
客户端示例
创建一个Maven项目,在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>
<artifactId>microservice-config-client-eureka</artifactId>
<packaging>jar</packaging>
<parent>
<groupId>com.itmuch.cloud</groupId>
<artifactId>spring-cloud-microservice-study</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
</project>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
启动类:ConfigClientEurekaApplication.java
@SpringBootApplication
@EnableDiscoveryClient
public class ConfigClientEurekaApplication {
public static void main(String[] args) {
SpringApplication.run(ConfigClientEurekaApplication.class, args);
}
}
编写测试Controller
/**
* 这边的@RefreshScope注解不能少,否则即使调用/refresh,配置也不会刷新
* @author eacdy
*/
@RestController
@RefreshScope
public class ConfigClientController {
@Value("${profile}")
private String profile;
@GetMapping("/hello")
public String hello() {
return this.profile;
}
}
配置文件:application.yml
server:
port: 8051
配置文件:bootstrap.yml
spring:
application:
name: microservice-config-client-eureka
cloud:
config:
profile: dev
label: master
discovery:
enabled: true # 默认false,设为true表示使用注册中心中的configserver配置而不自己配置configserver的uri
serviceId: microservice-config-server-eureka # 指定config server在服务发现中的serviceId,默认为:configserver
eureka:
client:
serviceUrl:
defaultZone: http:
# 参考文档:https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#discovery-first-bootstrap
从示例代码我们发现,想要将Config Server 与 注册中心联合使用,只需要在客户端侧配置spring.cloud.config.discovery.enabled=true
和 spring.cloud.config.discovery.serviceId
两个配置项即可。Eureka的配置前文有讲到过,如有疑问,详见服务发现的相关章节。
注意:当服务发现是Eureka
及 Consul
时,Config Server支持与之联合使用;如果是Zookeeper
做服务发现,目前不支持与之联合使用。
注意点:
- client需要添加以下依赖,否则访问/refresh将会得到404:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- client的controller需要添加@RefreshScope注解,否则配置无法刷新。
- 本文的
bootstrap.yml
文件中的内容不能放到application.yml
中,否则config部分无法被加载,因为config部分的配置先于application.yml
被加载,而bootstrap.yml
中的配置会先于application.yml
加载, - Config Server也可以支持本地存储或svn而不使用git,相对较为简单,故而本文不作赘述,有兴趣的可以自行阅读Spring Cloud的文档。
参考文档:
Config Server与注册中心联合使用:https://github.com/spring-cloud/spring-cloud-config/blob/master/docs/src/main/asciidoc/spring-cloud-config.adoc#discovery-first-bootstrap
Config Server的高可用: https://github.com/spring-cloud/spring-cloud-config/issues/87