spring cloud config整合gitlab搭建分布式的配置中心

来源:互联网 发布:python编写上位机 编辑:程序博客网 时间:2024/05/01 18:38

在之前的项目中,我们都是将配置文件放在各自的服务中,但是这样做有一个缺点,一旦配置修改了,那么我们就必须停机,然后修改配置文件后再进行上线,服务少的话,这样做还无可厚非,但是如果是成百上千的服务了,这个时候,就需要用到分布式的配置管理了。而spring cloud config正是用来解决这个问题而生的。下面就结合gitlab来实现分布式配置中心的搭建。spring cloud config配置中心由server端和client端组成,

前提:在gitlab中的工程下新建一个配置文件configserver-dev.properties

一、配置Server

1、添加依赖

[html] view plain copy
  1. <dependency>  
  2.             <groupId>org.springframework.cloud</groupId>  
  3.             <artifactId>spring-cloud-config-server</artifactId>  
  4.         </dependency>  
2、在Application主类开启支持

[java] view plain copy
  1. @EnableConfigServer  
3、配置application.yml文件

[html] view plain copy
  1. server:  
  2.   port: 8888  
  3. spring:  
  4.   application:  
  5.    name: config  
  6.  cloud:  
  7.     config:  
  8.       server:  
  9.         git:  
  10.           uri: https://gitlab.xxx.com/xxxxx/xxxxx.git     # 配置gitlab仓库的地址,注意,此处必须以.git结尾  
  11.           search-paths: /config-repo # gitlab仓库地址下的相对地址,可以配置多个,用,分割。  
  12.           username: your username                                             # gitlab仓库的账号  
  13.           password: your password                                             # gitlab仓库的密码  
注意:如果配置文件放置在Git存储库的根目录下,则无需使用searchPaths参数,本例中的配置文件在config-repo目录中,因此使用searchPaths参数提示Config服务器搜索config-repo子目录
4、启动server,并在浏览器输入http://localhost:8888/configserver/dev/master

[java] view plain copy
  1. {  
  2.   
  3.     "name""configserver",  
  4.     "profiles": [  
  5.         "dev"  
  6.     ],  
  7.     "label""master",  
  8.     "version""073cda9ce85a3eed00e406f4ebcc4651ee4d9b19",  
  9.     "state"null,  
  10.     "propertySources": [  
  11.         {  
  12.             "name""https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties",  
  13.             "source": {  
  14.                 "name""chhliuxyh",  
  15.                 "hello""i'm the king of the world!!!",  
  16.                 "profile""profile-default"  
  17.             }  
  18.         }  
  19.     ]  
  20.   
  21. }  
可以看到server端已经可以从gitlab上读取到配置文件了。可以通过如下表单中的方式访问gitlab上的资源

[java] view plain copy
  1. /{application}/{profile}[/{label}]  
  2. /{application}-{profile}.yml  
  3. /{label}/{application}-{profile}.yml  
  4. /{application}-{profile}.properties  
  5. /{label}/{application}-{profile}.properties  
例如在浏览器中输入:http://localhost:8888/configserver-dev.yml,结果如下:

[java] view plain copy
  1. hello: i'm the king of the world!!!  
  2. name: chhliuxyh  
  3. profile: profile-default  
二、配置客户端

1、添加pom依赖

[html] view plain copy
  1. <dependency>  
  2.             <groupId>org.springframework.cloud</groupId>  
  3.             <artifactId>spring-cloud-starter-config</artifactId>  
  4.         </dependency>  
  5.         <dependency>  
  6.             <groupId>org.springframework.boot</groupId>  
  7.             <artifactId>spring-boot-starter-web</artifactId>  
  8.         </dependency>  
2、配置bootstrap.yml文件

注意:此处的配置文件需要放在bootstrap.properties或者是bootstrap.yml文件中,因为config的相关配置会先于application.properties,而bootstrap.properties的加载也是先于application.properties

[java] view plain copy
  1. server:  
  2.   port: 8889  
  3. spring:  
  4.   application:  
  5.     name: configserver    # 必须与配置文件的前缀一致,例如此处我们的配置文件名是configserver-dev.properties,则此处需配置成configserver  
  6.   cloud:  
  7.     config:  
  8.       uri: http://localhost:8888/ //配置spring cloud config服务端的url  
  9.       profile: dev                      # 指定profile  
  10.       label: master                     # 指定gitlab仓库的分支  
3、验证客户端

在客户端新增一个Controller

[java] view plain copy
  1. package com.chhliu.springcloud.config;  
  2.   
  3. import org.springframework.beans.factory.annotation.Value;  
  4. import org.springframework.boot.SpringApplication;  
  5. import org.springframework.boot.autoconfigure.SpringBootApplication;  
  6. import org.springframework.cloud.context.config.annotation.RefreshScope;  
  7. import org.springframework.web.bind.annotation.GetMapping;  
  8. import org.springframework.web.bind.annotation.RestController;  
  9.   
  10. @SpringBootApplication  
  11. @RestController  
  12. @RefreshScope //注解@RefreshScope指示Config客户端在服务器配置改变时,也刷新注入的属性值  
  13. public class SpringcloudConfigClientApplication {  
  14.   
  15.     public static void main(String[] args) {  
  16.         SpringApplication.run(SpringcloudConfigClientApplication.class, args);  
  17.     }  
  18.   
  19.     @Value("${hello}"// 读取gitlab配置文件中的属性,如果我们读取到了值,说明客户端是OK的  
  20.     private String profile;  
  21.   
  22.     @GetMapping("/hello")  
  23.     public String hello() {  
  24.         return this.profile;  
  25.     }  
  26. }  
在浏览器中访问:http://localhost:8889/hello,结果如下:

[java] view plain copy
  1. i'm the king of the world!!!  
说明客户端已经可以从服务端获取到值了。
三、动态刷新

无需重新启动客户端,即可更新Spring Cloud Config管理的配置

1、更新gitlab仓库中configserver-dev.properties配置文件中hello对应的属性值

2、访问http://localhost:8888/configserver/dev/master,发现server端内容已经更新

3、对Conf客户端发一个POST请求http://localhost:8889/refresh,返回200 OK。再次访问http://localhost:8889/hello,可见在并未重启客户端服务的情况下,读到的属性值已经动态更新

PS:要想实现动态刷新,需要在pom文件中添加以下starter

[html] view plain copy
  1. <dependency>  
  2.             <groupId>org.springframework.boot</groupId>  
  3.             <artifactId>spring-boot-starter-actuator</artifactId>  
  4.         </dependency>  

0
阅读全文
0 0
原创粉丝点击