- Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。使用Config Server,您可以在所有环境中管理应用程序的外部属性。
- 客户端和服务器上的概念都与Spring Environment和PropertySource抽象相同,因此它们非常适合Spring应用程序,但可以与任何语言运行的应用程序一起使用。
- 当应用程序通过从开发环境到测试环境和生产环境的部署管道时,您可以管理这些环境之间的配置,并确保应用程序在迁移时需要运行所需的一切。服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标记版本,并且可以通过各种工具来访问内容。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
application.yml配置
- uri可以是一个git地址具体规则稍后介绍
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);
}
}
我们可以通过
http://localhost:8080/application-application.yml
获取数据
应用和数据获取映射规则
- 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 |
特别注意:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部分内容才能被正确加载。因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。
- 当我们用这种url路径访问时会得到json数据
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 注意https://git.coding.net/eggyer/learn-spring-cloud-config.git/application.yml只是一个标识符表明文件的相对路径,而无法直接访问.
- bootstrap.x里面的配置 —>链接config server 加载远程配置 —>加载application.x里的配置,所以我们要使用bootstrap.yml
- 本地同名配置会被远程的覆盖
-
spring官方建议我们在bootstrap中放置不更改的属性.
-
bootstrap.yml
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- application.yml
- 1
- 2
- APP
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- controller
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 分别创建git仓库special和simple,在两个仓库下加入配置文件application.yml,内容分别为profile: special;profile: simple
-
在configserver配置文件编写
-
使用一个微服务一个配置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 访问http://localhost:8080/master/simple-default.yml显示:profile: simple
- 可以发现我们可以用仓库名放入URL中(这样我们可以让每一个微服务单独使用一个git仓库,更容易进行权限管理)
- 个人建议一个配置环境一个文件
1、使用一种环境一种配置
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
2、模式匹配
- 在special下添加两个配置文件special-dev.yml,special-test.yml
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 然而会发现在访问http://localhost:8080/master/special-default.yml显示profile:profile-default,即 https://git.coding.net/eggyer/learn-spring-cloud-config.git默认配置文件的内容
- 原因是pattern: special*/dev*,special/test*,只匹配dev结尾和test结尾的内容.不建议使用这种方式,建议使用通配符的方式.
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.oschina.net/it-much/config-repo-51cto-video # 公用
search-paths:
- foo # foo路径
- bar # bar路径
4、账号密码
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.oschina.net/it-much/config-repo-51cto-video
username:
password:
三、传输加解密
加密技术主要为两种,一种是对称加密,一种是非对称加密
- 1
安装方式:可以参考里面的README,其实也很简单:把jdk下面 /jre/lib/security 目录下面的两个jar替换了。
server:
port: 8080
spring:
cloud:
config:
server:
git:
uri: https://git.oschina.net/it-much/config-repo-51cto-video
username:
password:
encrypt:
key: foo
这样加解密就实现了
用户认证
server部分
- pom文件
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
client部分
- 在bootstrap文件中加入
- 在uri下可以加入username和password属性,优先级比uri中高
- 那么为什么springcloud会提供两种方式?
- 生产环境中configserver需要高可用(可能有多个)
- 上面方式中如果需要负载均衡需要部署nginx等组件
- 节点信息被硬编码在配置文件中
- 没有充分发挥服务发现的优势所在
- 那么我们需要让configclient也具有服务发现的功能,让其通过eureka自动的寻找到configserver
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
10.7 通过Eureka发现configserver
- 添加pom文件
配置服务与注册中心联合使用
在生产环境中,我们可能会将Config Server 与 Eureka等注册中心联合使用(注意:目前Spring Cloud只支持与Eureka及Consul联合使用,不支持与Zookeeper联合使用),下面讲解如何将Config Server与 Eureka 联合使用。
准备工作
- 启动服务
microservice-discovery-eureka
; - 和上文一样,准备好几个配置文件,命名规范为
项目名称-环境名称.properties
,本文使用的名称是microservice-config-client-eureka-dev.properties
。
代码示例
服务器端代码示例:
首先新建一个Maven项目,在pom.xml
中添加如下内容:
- 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
配置文件:application.yml
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
客户端示例
创建一个Maven项目,在pom.xml
中添加如下内容:
- 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
- 1
- 2
- 3
- 4
- 5
- 6
- 7
编写测试Controller
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
配置文件:application.yml
- 1
- 2
配置文件:bootstrap.yml
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
从示例代码我们发现,想要将Config Server 与 注册中心联合使用,只需要在客户端侧配置spring.cloud.config.discovery.enabled=true
和 spring.cloud.config.discovery.serviceId
两个配置项即可。Eureka的配置前文有讲到过,如有疑问,详见服务发现的相关章节。
注意:当服务发现是Eureka
及 Consul
时,Config Server支持与之联合使用;如果是Zookeeper
做服务发现,目前不支持与之联合使用。
注意点:
- client需要添加以下依赖,否则访问/refresh将会得到404:
- 1
- 2
- 3
- 4
- client的controller需要添加@RefreshScope注解,否则配置无法刷新。
- 本文的
bootstrap.yml
文件中的内容不能放到application.yml
中,否则config部分无法被加载,因为config部分的配置先于application.yml
被加载,而bootstrap.yml
中的配置会先于application.yml
加载, - Config Server也可以支持本地存储或svn而不使用git,相对较为简单,故而本文不作赘述,有兴趣的可以自行阅读Spring Cloud的文档。