博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Spring Cloud Config Server配置中心
阅读量:6997 次
发布时间:2019-06-27

本文共 5470 字,大约阅读时间需要 18 分钟。

  hot3.png

背景

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

客户端测试是否生效

配置中心GET测试

修改配置文件

vi a-bootiful-client.propertiesgit commit -am "修改配置a-bootiful-client.properties文件"

a-bootiful-client.properties内容如下:

message=zyl2

客户端测试是否生效

这个适合就需要先调用刷新接口,再进行测试。 客户端post刷新接口

再调用测试接口:

配置中心GET2测试

总结

上面就是对Spring Cloud的配置中心这个部门的基本使用,这里还是涉及到配置文件更新后,使用git版本控制工具提交后,各个节点怎么批量更新的问题。

源代码在这里:

参考

转载于:https://my.oschina.net/fxtxz2/blog/2254127

你可能感兴趣的文章