编程开发的时候有没有觉得很多配置文件需要维护,比如,修改了数据库连接,所有用到该数据库的服务配置都得替换,是不是超级的麻烦呢

下面,给大家介绍一下Spring提供的配置自动化组件-spring cloud config

config也就是配置的意思,springcloud也是想尽办法帮助大家节约开发和发布成本,这也是为什么大家喜欢用他的原因,并且这个配置中心也是极其的简单。

首先,所有的配置文件有两部分组成,第一:文件名 第二:所属的环境(即本地环境,测试环境或者线上环境,通常用dev,sit或者uat表示)

存储配置文件需要有个存储的地方,目前用的比较多的是数据库和远程仓库git

这次我已git为例,首先看下目录结构

spring容器基本概念和使用(SpringCloud之自动化配置-config)(1)

是不是非常的简单,一个目录下,就只有配置文件,文件格式命名: {profile}-{env}.properties

这里我推荐用properties后缀(因为我用yml文件试了很多次,都没有读取到数据,有机会再试试吧,大家知道的话也可以下方评论留言哈!)

该配置文件目录建好后,上传提交到git

spring容器基本概念和使用(SpringCloud之自动化配置-config)(2)

git地址为:

http://chengjq@192.168.129.200:8089/r/java/springConfig.git

新建配置中心服务config-server

还是先来看下目录结构,仍然是最简单的ConfigServerApplication.java , application.yml , pom.xml三个文件

spring容器基本概念和使用(SpringCloud之自动化配置-config)(3)

1.pom.xml

<dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- spring boot test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

config-server主要依赖于spring-cloud-starter-eureka-server,spring-cloud-starter-config这两个包

2.application.yml

eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ server: port: 8888 spring: cloud: config: server: git: uri: http://chengjq@192.168.129.200:8089/r/java/springConfig.git searchPaths: /** username: chengjq password: 123456 label: master application: name: config-server

spring.cloud.config.server.git.uri:git的地址,即上面的 http://chengjq@192.168.129.200:8089/r/java/springConfig.git

searchpath:目录 /** 搜索所有

username:git的用户名

password:git的密码

当然,公开的git可以不填username和password

application.name:该应用的名字

3.ConfigServerApplication.java

@EnableEurekaServer @EnableConfigServer @SpringBootApplication public class ConfigServerApplication { public static void main(String[] args) { SpringApplication.run(ConfigServerApplication.class, args); } }

@EnableConfigServer:作为配置中心的标志

启动该项目,发现在eureka中已经注册成功,端口为配置文件中定义的8888

spring容器基本概念和使用(SpringCloud之自动化配置-config)(4)

ok,至此,配置中心已经搭建成功

下面,我们来写个例子,看下是否可以从git的配置文件中读取到该属性

配置客户端 config-client

首先,还是看下目录结构

spring容器基本概念和使用(SpringCloud之自动化配置-config)(5)

结构还是很简单,就这三个文件,ConfigClientApplication.java , application.yml , pom.xml

1.pom.xml

<dependencies> <!--eureka server --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <!-- spring boot test--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>Dalston.RC1</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>

想要使用配置中心,还是得有依赖:spring-cloud-starter-config

2. application.yml

eureka: client: serviceUrl: defaultZone: http://localhost:8761/eureka/ spring: cloud: config: name: ceshi profile: dev label: master discovery: enabled: true service-id: config-server # uri: http://localhost:8888/ application: name: config-client server: port: 8089 management: security: enabled: false

这块多了些参数

spring.cloud.config.name:文件名(我的文件名叫ceshi)

profile:适用的环境(我的是开发环境 简称dev)

label:分支(目前是master,即为主干)

uri:定义的config服务地址(我这里的是http://localhost:8888/,即为配置中心的地址)

discovery.enabled:服务发现(true or false )

discovery.service-id:服务名

正常情况下,uri和discovery.enabled,discovery.service-id任选其一就行,如果git会经常换地址,可以使用服务名

3.ConfigClientApplication.java

@SpringBootApplication @EnableEurekaClient @RestController @RefreshScope public class ConfigClientApplication { @Value("${name}") String name; @Value("${age}") String age; public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); } @RequestMapping(value = "/hi") public String hi(){ return "我是" name ",今年" age "岁"; } }

启动该客户端,看到应用已被注册成功

浏览器输入:http://localhost:8089/hi.发现获取到了数据

spring容器基本概念和使用(SpringCloud之自动化配置-config)(6)

至此,已经可以从配置中心获取到数据了

但是,大家会发现,如果我去修改配置文件信息,并且提交到git后,没法实施刷新页面信息,无法做到自动化配置

当然,springcloud还是提供了解决方案

看config-client中的配置文件,发现有个依赖:spring-boot-starter-actuator 该以来就是提供实时刷新,在需要获取配置数据的类上加上@RefreshScope

spring容器基本概念和使用(SpringCloud之自动化配置-config)(7)

然后在postman或者其他api接口调用工具上使用刷新接口即可,注意是post请求,这应该算是半自动化,如果想实现全自动化,可以实施监控git的push操作。

spring容器基本概念和使用(SpringCloud之自动化配置-config)(8)

再次去刷新地址,发现数据修改了

至此,半自动化的spring cloud config配置已完成,如有问题,请下方评论,谢谢!

,