本篇主要介绍的是SpringCloud中的分布式配置中心(SpringCloud Config)的相关使用教程。git
Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client经过接口获取数据、并依据此数据初始化本身的应用。github
开发环境spring
注:不必定非要用上述的版本,能够根据状况进行相应的调整。须要注意的是SpringBoot2.x之后,jdk的版本必须是1.8以上!bootstrap
确认了开发环境以后,咱们再来添加相关的pom依赖。浏览器
<dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-server</artifactId> </dependency> </dependencies>
目前SpringCloud Config的使用主要是经过Git/SVN方式作一个配置中心,而后每一个服务从其中获取自身配置所需的参数。SpringCloud Config也支持本地参数配置的获取。若是使用本地存储的方式,在 application.properties
或 application.yml
文件添加 spring.profiles.active=native
配置便可,它会从项目的 resources路径下读取配置文件。若是是读取指定的配置文件,那么可使用 spring.cloud.config.server.native.searchLocations = file:D:/properties/
来读取。app
首先是服务端这块,首先建立一个注册中心,为了进行区分,建立一个springcloud-config-eureka
的项目。 代码和配置和以前的基本同样。 application.properties
配置信息:分布式
配置信息:测试
spring.application.name=springcloud-hystrix-eureka-server server.port=8005 eureka.client.register-with-eureka=false eureka.client.fetch-registry=false eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
配置说明:fetch
服务端这边只须要在SpringBoot启动类添加@EnableEurekaServer
注解就能够了,该注解表示此服务是一个服务注册中心服务。this
代码示例:
@SpringBootApplication @EnableEurekaServer public class ConfigEurekaApplication { public static void main(String[] args) { SpringApplication.run(ConfigEurekaApplication.class, args); System.out.println("config 注册中心服务启动..."); } }
建立好了注册中心以后,咱们再来建立一个配置中心,用于管理配置。 建立一个springcloud-config-server
的项目。而后在application.properties
配置文件添加以下配置:
配置信息:
spring.application.name=springcloud-config-server server.port=9005 eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/ spring.cloud.config.server.git.uri = https://github.com/xuwujing/springcloud-study/ spring.cloud.config.server.git.search-paths = /springcloud-config/config-repo spring.cloud.config.server.git.username = spring.cloud.config.server.git.password =
配置说明:
注:若是想使用本地方式读取配置信息,那么只需将spring.cloud.config.server.git
的配置改为spring.profiles.active=native
,而后在resources路径下新增一个文件便可。
这里为了进行本地配置文件测试,新建一个configtest.properties
配置文件,添加以下内容:
word=hello world
代码这块也很简单,在程序主类中,额外添加@EnableConfigServer
注解,该注解表示启用config配置中心功能。代码以下:
、、、
@EnableDiscoveryClient @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); System.out.println("配置中心服务端启动成功!"); } }
、、、
完成上述代码以后,咱们的配置中心服务端已经构建完成了。
咱们新建一个springcloud-config-client
的项目,用于作读取配置中心的配置。pom依赖仍是和配置中心同样,不过须要新增一个配置,用于指定配置的读取。 建立一个bootstrap.properties
文件,并添加以下信息:
配置信息:
spring.cloud.config.name=configtest spring.cloud.config.profile=pro spring.cloud.config.label=master spring.cloud.config.discovery.enabled=true spring.cloud.config.discovery.serviceId=springcloud-config-server eureka.client.serviceUrl.defaultZone=http://localhost:8005/eureka/
配置说明:
注:上面这些与spring-cloud相关的属性必须配置在bootstrap.properties中,config部份内容才能被正确加载。由于bootstrap.properties的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties。须要注意的是eureka.client.serviceUrl.defaultZone
要配置在bootstrap.properties,否则客户端是没法获取配置中心参数的,会启动失败!
application.properties配置
spring.application.name=springcloud-config-client server.port=9006
配置说明:
程序主类代码,和以前的基本一致。代码以下:
代码示例:
@EnableDiscoveryClient @SpringBootApplication public class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); System.out.println("配置中心客户端启动成功!"); } }
为了方便查询,在控制中进行参数的获取,并返回。@Value
注解是默认是从application.properties
配置文件获取参数,可是这里咱们在客户端并无进行配置,该配置在配置中心服务端,咱们只需指定好了配置文件以后便可进行使用。
代码示例:
@RestController public class ClientController { @Value("${word}") private String word; @RequestMapping("/hello") public String index(@RequestParam String name) { return name+","+this.word; } }
到此,客户端项目也就构建完成了。
完成如上的工程开发以后,咱们来进行测试。
首先咱们把springcloud-config-server
项目的application.properties
配置文件添加spring.profiles.active=native
配置,注释掉spring.cloud.config.server.git
相关的配置,而后在src/main/resources目录下新建一个configtest.properties
文件,而后在里面添加一个配置 word=hello world
。 添加完成以后,咱们依次启动springcloud-config-eureka
、springcloud-config-server
、springcloud-config-client
这三个项目。启动成功以前,先看来看看配置中心服务端的配置文件获取,在浏览器输入:
查看该文件的配置信息。
注:配置文件的名称是configtest.properties
,可是若是直接该名称的话是获取不到的,由于在配置文件名须要经过-
来进行获取,若是配置文件名称没有-
,那么添加了-
以后,会自动进行匹配搜索。
springcloud config 的URL与配置文件的映射关系以下:
/{application}/{profile}[/{label}] /{application}-{profile}.yml /{label}/{application}-{profile}.yml /{application}-{profile}.properties /{label}/{application}-{profile}.properties
上面的url会映射{application}-{profile}.properties对应的配置文件,{label}对应git上不一样的分支,默认为master。
界面返回:
word: hello world
而后调用客户端的接口,查看是否可以获取配置信息。在浏览器上输入:
界面返回:
pancm,hello world
示例图:
在完成本地测试以后,咱们把这个spring.profiles.active=native
配置注释掉,解除spring.cloud.config.server.git
相关的注释(帐号和密码要填写真实的),而后在git仓库上创建一个config-repo 文件夹,新建configtest-pro.properties
、configtest-dev.properties
两个配置,这两个的配置分别是 word=hello world!!
和 word=hello world!
, 而后和configtest.properties
配置文件一块儿上传到config-repo 文件夹中。
首先在浏览器输入:
浏览器返回:
word: hello world!
而后再浏览器输入:
浏览器返回:
word: hello world!!
上传了configtest.properties
文件,可是这个文件名称没有-
,咱们想获取其中参数的信息的话,能够在而后-
随意添加一个参数,它会自动进行匹配,在浏览器输入:
浏览器返回:
word: hello world
而后进行客户端接口调用测试,在浏览器输入:
浏览器返回:
pancm,Hello World!!
因为这里我配置的前缀是 pro ,因此读取的是 configtest-pro.properties 文件的数据,想要获取其余的配置,修改spring.cloud.config.profile
配置便可。
示例图:
基于SpringBoot2.x、SpringCloud的Finchley版本开发的地址:https://github.com/xuwujing/springcloud-study
基于SpringBoot1.x、SpringCloud 的Dalston版本开发的地址: https://github.com/xuwujing/springcloud-study-old
若是感受项目不错,但愿能给个star,谢谢!
<iframe frameborder="no" border="0" marginwidth="0" marginheight="0" width=330 height=86 src="//music.163.com/outchain/player?type=2&id=1344874921&auto=0&height=66"></iframe>
原创不易,若是感受不错,但愿留言推荐!您的支持是我写做的最大动力! 版权声明: 做者:虚无境 博客园出处:http://www.cnblogs.com/xuwujing CSDN出处:http://blog.csdn.net/qazwsxpcm 我的博客出处:http://www.panchengming.com