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

 更新时间:2018年01月19日 13:45:01   作者:牛奋lch  
这篇文章主要介绍了详解spring cloud config整合gitlab搭建分布式的配置中心,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

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

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

一、配置Server

1、添加依赖

<dependency> 
      <groupId>org.springframework.cloud</groupId> 
      <artifactId>spring-cloud-config-server</artifactId> 
    </dependency> 

2、在Application主类开启支持

@EnableConfigServer 

3、配置application.yml文件

server: 
 port: 8888 
spring: 
 application: 
  name: config 
 cloud: 
  config: 
   server: 
    git: 
     uri: https://gitlab.xxx.com/xxxxx/xxxxx.git   # 配置gitlab仓库的地址,注意,此处必须以.git结尾 
     search-paths: /config-repo # gitlab仓库地址下的相对地址,可以配置多个,用,分割。 
     username: your username                       # gitlab仓库的账号 
     password: your password                       # gitlab仓库的密码 

注意:如果配置文件放置在Git存储库的根目录下,则无需使用searchPaths参数,本例中的配置文件在config-repo目录中,因此使用searchPaths参数提示Config服务器搜索config-repo子目录

4、启动server,并在浏览器输入http://localhost:8888/configserver/dev/master

{ 
 
  "name": "configserver", 
  "profiles": [ 
    "dev" 
  ], 
  "label": "master", 
  "version": "073cda9ce85a3eed00e406f4ebcc4651ee4d9b19", 
  "state": null, 
  "propertySources": [ 
    { 
      "name": "https://gitlab.xxx.com/xxxxx/xxxxx/project/config-repo/configserver.properties", 
      "source": { 
        "name": "chhliuxyh", 
        "hello": "i'm the king of the world!!!", 
        "profile": "profile-default" 
      } 
    } 
  ] 
 
} 

可以看到server端已经可以从gitlab上读取到配置文件了。可以通过如下表单中的方式访问gitlab上的资源

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

例如在浏览器中输入:http://localhost:8888/configserver-dev.yml,结果如下:

hello: i'm the king of the world!!! 
name: chhliuxyh 
profile: profile-default 

二、配置客户端

1、添加pom依赖

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

2、配置bootstrap.yml文件

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

server: 
 port: 8889 
spring: 
 application: 
  name: configserver  # 必须与配置文件的前缀一致,例如此处我们的配置文件名是configserver-dev.properties,则此处需配置成configserver 
 cloud: 
  config: 
   uri: http://localhost:8888/ //配置spring cloud config服务端的url 
   profile: dev           # 指定profile 
   label: master           # 指定gitlab仓库的分支 

3、验证客户端

在客户端新增一个Controller

package com.chhliu.springcloud.config;  
import org.springframework.beans.factory.annotation.Value; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 
import org.springframework.cloud.context.config.annotation.RefreshScope; 
import org.springframework.web.bind.annotation.GetMapping; 
import org.springframework.web.bind.annotation.RestController;  
@SpringBootApplication 
@RestController 
@RefreshScope //注解@RefreshScope指示Config客户端在服务器配置改变时,也刷新注入的属性值 
public class SpringcloudConfigClientApplication { 
 
  public static void main(String[] args) { 
    SpringApplication.run(SpringcloudConfigClientApplication.class, args); 
  } 
 
  @Value("${hello}") // 读取gitlab配置文件中的属性,如果我们读取到了值,说明客户端是OK的 
  private String profile; 
 
  @GetMapping("/hello") 
  public String hello() { 
    return this.profile; 
  } 
} 

在浏览器中访问:http://localhost:8889/hello,结果如下:

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

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Spring Security使用权限注解实现精确控制

    Spring Security使用权限注解实现精确控制

    在现代的应用系统中,权限管理是确保系统安全性的重要环节,Spring Security作为Java世界最为普及的安全框架,提供了强大而灵活的权限控制功能,这篇文章将深入探讨Spring Security使用权限注解实现精确控制,需要的朋友可以参考下
    2024-12-12
  • Spring Cloud学习教程之DiscoveryClient的深入探究

    Spring Cloud学习教程之DiscoveryClient的深入探究

    这篇文章主要给大家介绍了关于Spring Cloud学习教程之DiscoveryClient的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧。
    2018-04-04
  • Java遍历起止日期中间的所有日期操作

    Java遍历起止日期中间的所有日期操作

    这篇文章主要介绍了Java遍历起止日期中间的所有日期操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-09-09
  • SpringBoot多环境配置方式的新手教程

    SpringBoot多环境配置方式的新手教程

    我们平时做项目的时候,一般都会分几套环境,每一套环境的配置都是不一样的,所以这篇文章就来为大家详细介绍一下SpringBoot多环境配置方式,希望对大家有所帮助
    2023-11-11
  • Java秒杀系统:web层详解

    Java秒杀系统:web层详解

    本文主要介绍了如何设计一个秒杀系统的web层相关知识。具有很好的参考价值。下面跟着小编一起来看下吧,希望能够给你带来帮助
    2021-10-10
  • Java 常见异常(Runtime Exception )详细介绍并总结

    Java 常见异常(Runtime Exception )详细介绍并总结

    这篇文章主要介绍了Java 常见异常(Runtime Exception )详细介绍并相关资料,大家在开发Java 应用软件的时候经常会遇到各种异常这里帮大家整理了一部分,并解释如何解决,需要的朋友可以参考下
    2016-10-10
  • Flink入门级应用域名处理示例

    Flink入门级应用域名处理示例

    这篇文章主要介绍了一个比较简单的入门级Flink应用,代码很容易写,主要用到的算子有FlatMap、KeyBy、Reduce,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-03-03
  • Java中的IO流原理和流的分类详解

    Java中的IO流原理和流的分类详解

    这篇文章主要介绍了Java中的IO流原理和流的分类详解,Java io流是Java编程语言中用于输入和输出操作的一种机制。它提供了一组类和接口,用于处理不同类型的数据流,包括文件、网络连接、内存等,需要的朋友可以参考下
    2023-10-10
  • Java Vector实现班级信息管理系统

    Java Vector实现班级信息管理系统

    这篇文章主要为大家详细介绍了Java Vector实现班级信息管理系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2022-02-02
  • Java实现简单的分页功能

    Java实现简单的分页功能

    这篇文章主要为大家详细介绍了Java实现简单的分页功能,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08

最新评论