美文网首页
spring cloud config server配置中心搭建

spring cloud config server配置中心搭建

作者: KICHUN | 来源:发表于2019-04-03 11:22 被阅读0次

Spring Cloud Config简介

以下是官方文档介绍的翻译

Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。
使用Config Server,您可以在所有环境中管理应用程序的外部属性。
客户端和服务器上的概念与Spring Environment和PropertySource抽象相同,因此它们非常适合Spring应用程序,但可以与任何语言运行的任何应用程序一起使用。
当应用程序通过部署管道从开发到测试并进入生产时,您可以管理这些环境之间的配置,并确保应用程序具有迁移时需要运行的所有内容。
服务器存储后端的默认实现使用git,因此它可以轻松支持配置环境的标签版本,以及可用于管理内容的各种工具。
添加替代实现并使用Spring配置插入它们很容易。

说人话就是,搭建一个config server后,所有服务端所需的外部配置properties可以在配置中心进行统一管理,客户端只需要进行相关配置并引用即可

搭建config server 父工程

首先,项目使用了父工程进行依赖配置管理,父pom文件中指定了如下版本的boot-parent及cloud dependencies

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.3.RELEASE</version>
    </parent>

    <dependencyManagement>
        <dependencies>
            <!-- 注意这个,约束了子工程的cloud依赖。版本号不兼容会有各种异常 -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>Greenwich.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

注意springboot和cloud相关版本可能存在冲突导致启动过程中的各种异常,建议到官网查询当前的最新RELEASE版本或者SR版本

搭建git仓库

config server依赖git仓库进行配置的存储,所以先要创建一个git仓库,并 按照格式 存储相关配置文件,如图


image.png

配置文件中均存储一个 key 为 hello的属性
如spring-cloud-config-client-dev.yml中:

hello: hello spring-cloud-config-client-dev.yml

config server 服务搭建

  1. 引入依赖, 主要是config-server依赖
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
  1. 配置application.yml
# 服务端口
server:
  port: 6001
spring:
  application:
#    应用名
    name: spring-cloud-config-server
  cloud:
    config:
      server:
        git:
#          git仓库地址 用户名 密码 是否强行pull
          uri: https://github.com/wangqichang/springcloudconfigrepository.git
          password: 
          username: 184375760@qq.com
          force-pull: true
  1. 编写服务启动入口 , 加入注解@EnableConfigServer
/**
 * @author wangqichang
 * @date 2019/4/2
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConfigApplication.class, args);
    }
}

  1. 启动服务,测试访问
    直接访问文件,将会输出文件内容,代表config server 搭建成功


    image.png

config client搭建

  1. 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>
  1. 创建 client 配置文件 bootstarp.yml
# 服务端口
server:
  port: 6002
spring:
  application:
#    应用名称
    name: spring-cloud-config-client
  cloud:
    config:
#     分支标签
      label: master
#      config 服务地址
      uri: http://localhost:6001/
#  当前profile
  profiles:
    active: dev
  1. 创建启动类
@SpringBootApplication
public class SpringCloudConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(SpringCloudConfigClientApplication.class, args);
    }
}
  1. 创建测试controlller,使用@Value注入hello的值
@RestController
@RequestMapping("/config")
public class ConfigTestController {

    @Value("${hello}")
    private String hello;

    @GetMapping("/test")
    public String test() {
        return hello;
    }
}
  1. 访问controller的测试URL


config的简单使用介绍到此。但这个properties的值被实时更新后还不会刷新到client。有其他实现各位大佬可以自行百度。
嗯,还是觉得携程的Apollo配置中心好用

相关文章

网友评论

      本文标题:spring cloud config server配置中心搭建

      本文链接:https://www.haomeiwen.com/subject/ewnobqtx.html