700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > spring cloud config动态刷新_Spring Cloud学习笔记--配置中心(Config)

spring cloud config动态刷新_Spring Cloud学习笔记--配置中心(Config)

时间:2020-02-19 19:56:22

相关推荐

spring cloud config动态刷新_Spring Cloud学习笔记--配置中心(Config)

Spring Cloud Config provides server-side and client-side support for externalized configuration in a distributed system. With the Config Server, you have a central place to manage external properties for applications across all environments. [1]

简单来说,Spring Cloud Config在分布式环境下提供了一种动态获取配置文件的一种方式。

它可以用来管理整个环境中的配置文件。

我们这里拿Git作为演示。

1、简单搭建

1)、创建配置中心,代码如下:

pom文件:

<

bootstrap.yml:

server:#当前Config服务的端口号配置port: 65010spring:application:name: configservercloud:config:server:git:uri: /wuyusrc/DemoTestsearch-paths: configtestusername: usernamepassword: password

其中:url是指git地址,search-paths是指匹配查询的路径名,username是git上的用户名,password是指git上的密码。

启动程序增加EnableConfigServer注解:

在github上,我们上传几个测试文件

启动配置中心服务,测试一下。

Config支持请求的参数格式为:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties[1]

其中application为应用名,对应测试例子中的ctest,profile为环境名,对应测试例子中的dev,label为分支名,分支名默认是master。

2)、创建Config Client,代码如下:

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency>

bootstrap.yml文件:

server:port: 65011spring:application:#本程序的服务名name: configclientcloud:config:#配置中心的URLuri: http://localhost:65010/#指定的环境名profile: dev#指定的分支名#label: master#文件前缀名称name: ctest

创建一个Controller对外提供服务:

@RestController@RefreshScope@RequestMapping("clientconfig")public class ClientConfigController {@Value("${from}")private String fromString;@GetMapping("/from")public String getEv() {return fromString;}}

启动Config Client测试一下吧。

可以看出来,client已经成功获取到配置文件了。

2、高可用

1)、改造一下配置中心

pom文件:

<dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-config-server</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency>

bootstrap.yml:

server:#当前Config服务的端口号配置port: 65010spring:application:name: configservercloud:config:server:git:uri: /plsjava/plstestsearch-paths: testplsconfigusername: usernamepassword: passwordeureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/

启动文件增加EnableEurekaClient注解和EnableConfigServer注解。

2)、改造一下Config Client。

pom文件:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-config</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-netflix-eureka-client</artifactId></dependency>

bootstrap.yml:

spring:application:#对应配置中心所获取的配置文件的{application}name: ctestcloud:config:#配置中心的URL#uri: http://localhost:65010/#指定的环境名profile: dev#指定的分支名#label: master#文件前缀名称#name: ctestdiscovery:enabled: trueservice-id: configservereureka:client:service-url:defaultZone: http://eurekaserver1:30001/eureka/

application.yml:

server:port: 65012

Controller:

@EnableEurekaClient@RestController@RefreshScope@RequestMapping("clientconfig")public class ClientConfigController {@Value("${from}")private String fromString;@GetMapping("/from")public String getEv() {return fromString;}}

启动注册中心,配置文件和Config Client。

注册中心显示如下:

请求服务显示如下:

可以看出来,已经成功获取了配置文件。

架构图应该是这样:

好了,Config这样就可以搭建起来了。

参考

^abSpring Cloud Config(官方文档)https://cloud.spring.io/spring-cloud-config/reference/html/

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。