spring cloud config 配置中心快速实现过程解析

spring-cloud-config 配置中心实现

Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为server端和client端。

server端为分布式配置中心,是一个独立的微服务应用;client端为分布式系统中的基础设置或微服务应用,通过指定配置中心来管理相关的配置。

Spring Cloud Config 构建的配置中心,除了适用于 Spring 构建的应用外,也可以在任何其他语言构建的应用中使用。
Spring Cloud Config 默认采用 Git 存储配置信息,支持对配置信息的版本管理。

本示例主要内容

  • 配置中心演示client端和server端实现
  • 配置文件放在git(因github有时候不太稳定,我放到了国内服务器)
  • 版本切换(test、pro、dev)

Spring Cloud Config 特点

  • 提供server端和client端支持(Spring Cloud Config Server和Spring Cloud Config Client);
  • 集中式管理分布式环境下的应用配置;
  • 基于Spring环境,实现了与Spring应用无缝集成;
  • 可用于任何语言开发的程序;
  • 默认实现基于Git仓库(也支持SVN),从而可以进行配置的版本管理;同时也支持配置从本地文件或数据库读取。

代码构建

server端实现

1.pom.xml添加maven依赖

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

2.application.yml配置

server:
 port: 8001
spring:
 application:
  name: cloud-config-server
 cloud:
  config:
   server:
    git:
     uri: https://gitee.com/tqlin/spring-boot-demo.git #因为github有时候不稳定,我这里改到了码云仓
     searchPaths: /cloud-config/config-repo/      #配置文件目录
     force-pull: true

3.CloudConfigServerApplication.java启动类

@EnableConfigServer
@SpringBootApplication
public class CloudConfigServerApplication {

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

client端实现

1.pom.xml添加maven依赖

  <dependencies>
    <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>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

2.bootstrap.properties配置文件

spring.cloud.config.name=easy-config
spring.cloud.config.profile=test
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=master
  • spring.application.name:对应{application}部分
  • spring.cloud.config.profile:对应{profile}部分
  • spring.cloud.config.label:对应git的分支。如果配置中心使用的是本地存储,则该参数无用
  • spring.cloud.config.uri:配置中心的具体地址(sever端地址)
  • spring.cloud.config.discovery.service-id:指定配置中心的service-id,便于扩展为高可用配置集群。

特别注意:Spring Cloud 构建于 Spring Boot 之上,在 Spring Boot 中有两种上下文,一种是 bootstrap, 另外一种是 application, bootstrap 是应用程序的父上下文,也就是说 bootstrap 加载优先于 applicaton。bootstrap 主要用于从额外的资源来加载配置信息,还可以在本地外部配置文件中解密属性。

这两个上下文共用一个环境,它是任何Spring应用程序的外部属性的来源。bootstrap 里面的属性会优先加载,它们默认也不能被本地相同配置覆盖。

3.application.properties配置文件

spring.application.name=cloud-config-client
server.port=8002

运行示例

1.首先在码云上面创建一个文件夹config-repo用来存放配置文件,我们创建以下三个配置文件:

 // 开发环境
 easy-config-dev.properties  内容为:easy.hello=dev config
 // 测试环境
 easy-config-test.properties  内容为:easy.hello=test config
 // 生产环境
 easy-config-pro.properties  内容为:easy.hello=pro config

根据上面构建的代码指定的项目地址为:https://gitee.com/tqlin/spring-boot-demo.git 目录为: /cloud-config/config-repo/

2.分别运行server端和client端

找到CloudConfigServerApplication.java、CloudConfigClientApplication.java分别运行

3.测试server端

直接访问:http://localhost:8001/easy-config/dev

我们看到成功返回了开发配置文件信息

{
name: "easy-config",
profiles: [
"dev"
],
label: null,
version: "6053b4c1c2343ac27e822b2a9b60c6343be72f96",
state: null,
propertySources: [
{
name: "https://gitee.com/tqlin/spring-boot-demo.git/cloud-config/config-repo/easy-config-dev.properties",
source: {
easy.hello: "dev config"
}
}
]
}

访问:http://localhost:8001/easy-config/test、http://localhost:8001/easy-config/pro,相应的会返回测试及正式环境的配置

仓库中的配置文件会被转换成web接口,访问可以参照以下的规则:

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

以easy-config-dev.properties为例子,它的application是easy-config,profile是dev。client会根据填写的参数来选择读取对应的配置。

4.测试client端

访问:http://localhost:8002/hello 我们发现界面成功返回了 test config,说明测试配置文件client端读取成功了

我们修改bootstrap.properties配置的spring.cloud.config.profile的值为dev,重启client端,访问:http://localhost:8002/hello 这时候界面返回 dev config,表示开发配置访问成功。

资料

Spring Cloud Config 示例源码

官网文档

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

(0)

相关推荐

  • Spring Cloud Config实现分布式配置中心

    在分布式系统中,配置文件散落在每个项目中,难于集中管理,抑或修改了配置需要重启才能生效.下面我们使用 Spring Cloud Config 来解决这个痛点. Config Server 我们把 config-server 作为 Config Server,只需要加入依赖: <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-ser

  • SpringCloud之分布式配置中心Spring Cloud Config高可用配置实例代码

    一.简介 当要将配置中心部署到生产环境中时,与服务注册中心一样,我们也希望它是一个高可用的应用.Spring Cloud Config实现服务端的高可用非常简单,主要有以下两种方式. 传统模式:不需要为这些服务端做任何额外的配置,只需要遵守一个配置规则,将所有的Config Server都指向同一个Git仓库,这样所有的配置内容就通过统一的共享文件系统来维护.而客户端在指定Config Server位置时,只需要配置Config Server上层的负载均衡设备地址即可, 就如下图所示的结构. 服

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

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

  • spring cloud config分布式配置中心的高可用问题

    在前面的文章中,我们实现了配置文件统一管理的功能,但是我们可以发现,我们仅仅只用了一个server,如果当这个server挂掉的话,整个配置中心就会不可用,下面,我们就来解决配置中心的高可用问题. 下面我们通过整合Eureka来实现配置中心的高可用,因为作为架构内的配置管理,本身其实也是可以看作架构中的一个微服务,我们可以把config server也注册为服务,这样所有客户端就能以服务的方式进行访问.通过这种方法,只需要启动多个指向同一Gitlab仓库位置的config server端就能实现

  • 详解SpringCloud Config配置中心

    一.创建Config配置中心项目 1.添加依赖 <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-config-server</artifactId> </dependency> 2.启动类,需要添加@EnableConfigServer import org.springframework.boot.SpringApp

  • 利用Spring Cloud Config结合Bus实现分布式配置中心的步骤

    概述 假设现在有个需求: 我们的应用部署在10台机器上,当我们调整完某个配置参数时,无需重启机器,10台机器自动能获取到最新的配置. 如何来实现呢?有很多种,比如: 1.将配置放置到一个数据库里面,应用每次读取配置都是直接从DB读取.这样的话,我们只需要做一个DB变更,把最新的配置信息更新到数据库即可.这样无论多少台应用,由于都从同一个DB获取配置信息,自然都能拿到最新的配置. 2.每台机器提供一个更新配置信息的updateConfig接口,当需要修改配置时,挨个调用服务器的updateConf

  • spring-cloud入门之spring-cloud-config(配置中心)

    前言 在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件:spring-cloud-config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中. 本节主要演示怎么用Git仓库作为配置源. 开源地址:https://github.com/bigbeef 创建配置项目 在github中创建一个项目,专门用来保存我们所有项目的配置文件,项目是我的项目结构 配置项目地址:https://github.com/bigbeef/

  • spring cloud config 配置中心快速实现过程解析

    spring-cloud-config 配置中心实现 Spring Cloud Config 用于为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,分为server端和client端. server端为分布式配置中心,是一个独立的微服务应用:client端为分布式系统中的基础设置或微服务应用,通过指定配置中心来管理相关的配置. Spring Cloud Config 构建的配置中心,除了适用于 Spring 构建的应用外,也可以在任何其他语言构建的应用中使用. Spring Clou

  • Spring Cloud之配置中心的搭建

    Spring Cloud是现在流行的分布式服务框架,它提供了很多有用的组件.比如:配置中心.Eureka服务发现.消息总线.熔断机制等. 配置中心在Spring Cloud的众多组件中是比较基础的,它提供了配置文件的统一管理,可以很轻松的切换不通的环境. 它的具体结构如下: 存储配置文件的文件系统(通常使用git) 配置中心服务端(从文件系统获取最新的配置文件,为客户端提供配置信息) 配置客户端(从配置中心获取配置信息) Spring Cloud是建立在Spring Boot基础上的,Sprin

  • 详解Spring Cloud Config采用Git存储时两种常用的配置策略

    由于Spring Cloud Config默认采用了Git存储,相信很多团队在使用Spring Cloud的配置中心时也会采用这样的策略.即便大家都使用了Git存储,可能还有各种不同的配置方式,本文就来介绍一下两种常用的配置策略. 第一种:多个项目公用一个Git仓库,用不同的目录区分项目 主要的配置项如下: spring.cloud.config.server.git.uri=https://github.com/dyc87112/config-repo.git spring.cloud.con

  • Spring Cloud Alibaba Nacos Config配置中心实现

    什么是 Nacos Config 在分布式系统中,由于服务数量巨多,为了方便服务 配置文件统一管理,实时更新,所以需要分布式配置中心组件. Spring Cloud Alibaba Nacos Config 是 Spring Cloud Config 的替代方案. Nacos Config 的存储配置功能为分布式系统中的外部化配置提供服务器端和客户端支持,可以在 Nacos 中集中管理 Spring Cloud 应用的外部属性配置. 引入依赖 在 pom.xml 中添加 spring-cloud

  • Spring Cloud Config分布式配置中心使用介绍详解

    目录 1.分布式配置中心应用场景 2.Spring Cloud Config 2.1.Config简介 2.2.Config分布式配置应用 2.3.构建Config Server统一配置中心 2.4.构建Client客户端(在已有简历微服务基础上) 1.分布式配置中心应用场景 往往,我们使用配置文件管理⼀些配置信息,比如application.yml 单体应用架构:配置信息的管理.维护并不会显得特别麻烦,手动操作就可以,因为就一个工程: 微服务架构:因为我们的分布式集群环境中可能有很多个微服务,

随机推荐