Spring|Spring Cloud Config 使用本地配置文件方式

一、简介 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。
在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中。
在spring cloud config 组件中,分两个角色,一是config server,二是config client。
二、配置 2.1 Spring Cloud Config Server项目
1 pom.xml中导入Config Server需要的包

org.springframework.cloudspring-cloud-config-server

2 在Application类中添加@EnableConfigServer注解
package com.sunbufu; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer@SpringBootApplicationpublic class ConfigServerApplication {public static void main(String[] args) {SpringApplication.run(ConfigServerApplication.class, args); }}

3 修改配置文件application.yml,指定本地客户端配置文件的路径
spring:profiles:active: nativecloud:config:server:native:searchLocations: F:/conf

4 准备客户端配置文件
Spring|Spring Cloud Config 使用本地配置文件方式
文章图片

client-dev.yml文件的内容:
server:#设置成0,表示任意未被占用的端口port: 8081nickName: world

2.2 Spring Cloud Config Client项目
1 pom.xml中导入Config Client需要的包(注意,此处跟Config Server的配置不同)
org.springframework.cloudspring-cloud-starter-config

2 在src/main/resources中,新建bootstrap.yml文件
bootstrap文件会在application文件之前加载,一般是不会变的。
spring:application:name: clientcloud:config:uri: http://127.0.0.1:8888profile: devlabel: master

资源文件映射如下:
/{application}/{profile}[/{label}]/{application}-{profile}.yml/{label}/{application}-{profile}.yml/{application}-{profile}.properties/{label}/{application}-{profile}.properties

3 新建HelloController用来显示读取到的配置
package com.sunbufu.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestControllerpublic class HelloController {@Value("${nickName}")private String nickName; @RequestMapping("/hello")public String hello() {return "hello " + nickName; }}

Spring|Spring Cloud Config 使用本地配置文件方式
文章图片

三、总结 源码地址 :https://github.com/sunbufu/sunbufu-cloud
总觉的使用svn或者git不如直接修改配置文件方便,特此记录下来。
spring cloud config本地读取配置文件 1、创建maven项目,引入spring boot 起步依赖
4.0.0org.springframework.bootspring-boot-starter-parent2.1.7.RELEASE com.wxzcloud-config-demo30.0.1-SNAPSHOTcloud-config-demo3Demo project for Spring Boot1.8org.springframework.bootspring-boot-starterorg.springframework.bootspring-boot-starter-testtestorg.springframework.cloudspring-cloud-dependencies${spring-cloud.version}pomimportorg.springframework.bootspring-boot-maven-plugin

新建config-server 模块,引入依赖
4.0.0com.wxzcloud-config-demo30.0.1-SNAPSHOT com.wxzconfig-server0.0.1-SNAPSHOTconfig-serverDemo project for Spring Boot1.8org.springframework.bootspring-boot-starterorg.springframework.cloudspring-cloud-config-server2.1.2.RELEASEorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-maven-plugin

2、配置文件:
spring:cloud:config:server:native:search-locations: classpath:/sharedprofiles:active: nativeapplication:name: config-serverserver:port: 8769

在resources下新建目录shared,里面新建文件config-client-dev
server:port: 8762foo: foo version 1

在启动类添加
@EnableConfigServer

新建config-client模块
4.0.0com.wxzcloud-config-demo30.0.1-SNAPSHOT com.wxzconfig-client0.0.1-SNAPSHOTconfig-clientDemo project for Spring Boot1.8org.springframework.bootspring-boot-starter-weborg.springframework.cloudspring-cloud-starter-config2.1.2.RELEASEorg.springframework.bootspring-boot-starter-testtestorg.springframework.bootspring-boot-maven-plugin

新建配置文件bootstrap.yml(bootstrap比application具有优先的读取顺序)
spring:cloud:config:uri: http://localhost:8769fail-fast: trueapplication:name: config-clientprofiles:active: dev

新建一个controller进行测试:
package com.wxz.configclient.controller; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; /** * @author Wangxingze * @date 2019-08-26 12:58 */@RestControllerpublic class Test {@Value("${foo}")public Stringfoo; @GetMapping("/t")public String t(){return foo; }}

依次启动server client,启动时可以看到读取了配置文件和启动黛安克,访问:
Spring|Spring Cloud Config 使用本地配置文件方式
文章图片

注意:spring boot 和cloud的版本以及config依赖的版本
org.springframework.bootspring-boot-starter-parent2.1.7.RELEASE

我这里好像没有指定spring cloud 的版本呀
config的相关依赖使用:2.1.2.RELEASE
【Spring|Spring Cloud Config 使用本地配置文件方式】以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

    推荐阅读