个人博客:兰陵笑笑生,欢迎浏览博客!java
上一章 SpringCloud基础教程(三)-Eureka进阶当中,咱们在对Eureka的有了基本的基础认识之上,深刻的了解Eureka高可用集群和其余的生产环境中用到的一些配置。本章将开始了解分布式环境下的配置中心。git
为何须要配置中心,在单体的应用中,配置文件就能够很好的解决配置问题。可是在微服务架构模式下,系统不可避免的被拆分了许多个微服务组件,若是仍是经过之前的方式,配置的工做量会很大。为此,一个通用的分布式的配置管理中心是必不可少的。Spring Cloud提供了另一个组件Spring Cloud Config。程序员
Spring Cloud Config提供了服务端和客户端的支持,基于Spring环境,可以无缝的集成Spring,默认的实现是基于Git仓库。固然也支持SVN,本地文件等,甚至自定义实现。web
这里咱们使用Git做为配置文件的存储仓库,首先创建Maven项目,并在Pom.xml文件中引入相关依赖。spring
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency>
建立ConfigServerApplicaition.java 类,使用@EnableConfigServer注解,表示容许该服务以HTTP形式对外提供配置管理服务:json
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication @EnableConfigServer public class ConfigServerApplicaition { public static void main(String[] args) { SpringApplication.run(ConfigServerApplicaition.class, args); } }
添加applicaiton.yml,配置以下:bootstrap
spring: application: name: single cloud: config: server: git: uri: https://gitee.com/lnxxs/springCloudConfig.git password: username: search-paths: conf label: master
在这以前,事先建立Git仓库,添加配置文件,master分支添加以下文件和内容:架构
ConfigServer.properties: k1=master-default-v1 ConfigServer-dev.properties:k1=master-dev-v1 ConfigServer-test.properties:k1=master-test-v1 ConfigServer-pro.properties:k1=master-pro-v1
在服务启动后,能够基于HTTP请求访问以下的URL进行配置信息的获取,获取的格式有以下几种:app
其中applicaiton是文件的名称。如分布式
1) http://localhost:6001/ConfigServer-dev.properties
2) http://localhost:6001/master/ConfigServer-dev.properties :加载指定分支的属性:
3) http://localhost:6001/ConfigServer/default :显示默认的配置
{ "name":"ConfigServer", "profiles":[ "default" ], "label":null, "version":"f516174f308769468f1ac683cfeffa803a63c9af", "state":null, "propertySources":[ { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties", "source":{ "k1":"master-default-v1" } } ] }
4) http://localhost:6001/ConfigServer/dev :显示默认和dev的信息
{ "name":"ConfigServer", "profiles":[ "dev" ], "label":null, "version":"f516174f308769468f1ac683cfeffa803a63c9af", "state":null, "propertySources":[ { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-dev.properties", "source":{ "k1":"master-dev-v1" } }, { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties", "source":{ "k1":"master-default-v1" } } ] }
5) http://localhost:6001/ConfigServer/test/master 显示指定的分支的test和默认的配置
{ "name":"ConfigServer", "profiles":[ "test" ], "label":"master", "version":"f516174f308769468f1ac683cfeffa803a63c9af", "state":null, "propertySources":[ { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-test.properties", "source":{ "k1":"master-test-v1" } }, { "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties", "source":{ "k1":"master-default-v1" } } ] }
若是经过以上的URL获取到Git仓库的配置信息,说明配置服务端正常。
接下来咱们建立一个SpringBoot应用做为客户端来读取Config Server中提供的配置。(咱们开发的任何一个微服务组件均可以认为是一个Config Client客户端。)
新建ConfigClientApplication,java启动类,并在项目文件pom.xml中添加依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <!--web依赖--> <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>
添加bootstrap.yml,注意这些配置必须是在bootstrap.yml中,配置才能生效,若是配置在applicaiton.yml中是没有任何效果的。
spring: application: name: server-client cloud: config: label: master name: ConfigServer profile: test uri: http://localhost:6001/
建立ConfigClientApplication.java启动类,并添加EnableAutoConfiguration注解,表示自动获取项目中的配置变量。
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication @EnableAutoConfiguration public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } }
建立控制器,测试获取配置信息:
@RestController public class ValueController { @Value("${k1}") String value; @GetMapping("/get") public String getValue(){ return value; } }
调用接口测试,正确的获取到了配置中心的信息:
在没法链接Config Server的时候,咱们有时候要求客户端须要启动失败,咱们能够经过配置bootstrap配置项:
spring: cloud: config: fail-fast: true retry: initial-interval: 1000 max-attempts: 6 max-interval: 2000 multiplier: 1.1
若是咱们要求客户端组件在Config Server不可用是,让客户端重试,那么咱们也能够经过设置:
spring: cloud: config: fail-fast: false
同时在客户端的配置文件中引入spring-retry和aop依赖:
<dependency> <groupId>org.springframework.retry</groupId> <artifactId>spring-retry</artifactId> <version>1.2.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
若是须要对Config Server的http请求进行帐号密码控制,在服务端配置:
spring: application: name: single cloud: config: server: git: uri: https://gitee.com/lnxxs/springCloudConfig.git password: username: search-paths: conf label: master username: password: uri: http://localhost:6001/ enabled: true security: user: name: user password: pwd
注意用户名、密码的属性是spring.security.user.name和spring.security.user.password
spring.cloud.config.username和spring.cloud.config.password服务端配置是没有什么做用,是客户端用来配置用的
并在服务端的pom文件中引入security依赖:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
咱们只须要将用户名和密钥复制并配置再每一个客户端中bootstrap.xml中就能够,客户端是不须要引入security依赖的。
spring: application: name: server-client cloud: config: label: master profile: test uri: http://localhost:6001/ name: ConfigServer username: user password: pwd
在配置HTTP权限的时候,若是服务端在引入security以后,没有配置用户名和密码,那么服务端项目启动时,就会随机生成一个全局的密钥。这里咱们不使用随机的密码。若是配置了用户名和密码,那么客户端必须同时配置同样的用户名和加密的密钥。
这一篇文章简单的介绍了Spring Cloud ConfIf组件的使用,并结合git仓库搭建了分布式配置中心,实现了程序和配置的隔离,解耦编码与环境之间的耦合,这是很是重要的,固然这样的配置仍是存在肯定,在实际的生成环境中,全部的服务配置都依赖于配置中心,那么若是配置中心出现宕机该怎么办,下一章咱们将继续了解分布式配置中心的集群搭建.
以上就是本期的分享,你能够关注本博客的#Spring Cloud基础教程!#
还能够关注公众号: 程序员笑笑生,关注更多精彩内容!
本文由博客一文多发平台 OpenWrite 发布!
个人博客地址兰陵笑笑生,欢迎浏览!