背景
spring boot项目不再读取自己的配置文件,而是统一去配置中心读取属于自己的配置文件
配置中心服务端
IDEA上面创建一个空的maven工程,命名为configuration-service
。
创建目录
mkdir -p src/main/java/hello
配置服务器pom.xml
4.0.0 io.spring configuration-service 0.0.1-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE UTF-8 1.8 org.springframework.cloud spring-cloud-config-server org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Finchley.SR2 pom import
这个Maven配置主要是引入Spring Cloud的配置中心服务器的配置。
创建服务器端主类ConfigServiceApplication.java
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.cloud.config.server.EnableConfigServer;@EnableConfigServer@SpringBootApplicationpublic class ConfigServiceApplication { public static void main(String[] args) { SpringApplication.run(ConfigServiceApplication.class, args); }}
这里主要是启用该服务为配置中心服务。
创建服务器端主配置application.properties
server.port=8888spring.cloud.config.server.git.uri=${HOME}/Downloads/config
这里配置配置中心服务端口为8888,以及读取的配置文件位置目录配置。
配置中心客户端
在IDEA上面创建Maven空工程,命名为configuration-client
。
创建目录
mkdir -p src/main/java/hello
添加客户端pom.xml
4.0.0 io.spring configuration-client 1.0-SNAPSHOT jar org.springframework.boot spring-boot-starter-parent 2.0.6.RELEASE UTF-8 1.8 org.springframework.cloud spring-cloud-starter-config org.springframework.boot spring-boot-starter-actuator org.springframework.boot spring-boot-starter-web org.springframework.boot spring-boot-starter-test test org.springframework.cloud spring-cloud-dependencies Finchley.SR2 pom import
添加客户端主类ConfigClientApplication
package hello;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplicationpublic class ConfigClientApplication { public static void main(String[] args) { SpringApplication.run(ConfigClientApplication.class, args); }}
这里是spring boot的正常配置。
添加客户端资源主类MessageRestController
package hello;import org.springframework.beans.factory.annotation.Value;import org.springframework.cloud.context.config.annotation.RefreshScope;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;@RefreshScope@RestControllerpublic class MessageRestController { @Value("${message:Hello default}") private String message; @RequestMapping("/message") String getMessage() { return this.message; }}
这里暴露一个请求,这个请求是从配置文件中读取自己的配置参数message
。
客户端application.properties配置
server.port=8082spring.application.name=a-bootiful-clientmanagement.endpoints.web.exposure.include=*
这里主要就是设置当前服务端口和服务名,已经开放相关运维接口,这些运维接口主要就是localhost:8082/actuator/refresh
的post请求,用于刷新配置中心的配置参数的。
客户端bootstrap.properties针对配置中心的配置
spring.application.name=a-bootiful-client# N.B. this is the default:spring.cloud.config.uri=http://localhost:8888
这里主要是配置当前服务名称,以及配置中心的服务地址。注意这里服务名,必须与配置中心读取的文件名保持一致,即配置中心的配置文件名必须为a-bootiful-client.properties
才能够读取到文件。
配置中心的配置文件管理
配置中心主要使用git来管理配置文件,也就是说每次对配置文件的修改,必须使用git来提交后,配置中心才会认可。
创建配置文件
cd ~/Downloads/configgit initvi a-bootiful-client.propertiesgit add a-bootiful-client.propertiesgit commit -m "创建a-bootiful-client.properties文件"
a-bootiful-client.properties内容如下:
message=zyl
客户端测试是否生效
修改配置文件
vi a-bootiful-client.propertiesgit commit -am "修改配置a-bootiful-client.properties文件"
a-bootiful-client.properties内容如下:
message=zyl2
客户端测试是否生效
这个适合就需要先调用刷新接口,再进行测试。
再调用测试接口:
总结
上面就是对Spring Cloud的配置中心这个部门的基本使用,这里还是涉及到配置文件更新后,使用git版本控制工具提交后,各个节点怎么批量更新的问题。
源代码在这里: