最近一致在更新Spring Cloud Config的相关内容,主要也是为这篇埋个伏笔,相信很多调研过Spring Cloud Config的用户都会吐槽它的管理能力太弱。所以,就有了下面为讲推荐的这个开源项目,但愿对已经入坑Spring Cloud Config的童鞋们有所帮助!前端
在Spring Cloud的微服务架构方案中虽然提供了Spring Cloud Config来担任配置中心的角色,可是该项目的功能在配置的管理层面仍是很是欠缺的。初期咱们能够依赖选取的配置存储系统(好比:Gitlab、Github)给咱们提供的配置管理界面来操做全部的配置信息,可是这样的管理仍是很是粗粒度的,所以这个项目的目的就是解决这个问题,经过此项目,咱们将提供一套基于Spring Cloud Config配置中心的可视化管理系统。java
在该项目中,咱们对于服务治理、配置存储、可视化操做都作了抽象,只要目的就是为了尽量的兼容全部Spring Cloud Config的用户。任何Spring Cloud Config仅须要经过一些简单的配置,或者迁移工具就能将原来正在使用的配置中心统一的管理起来。mysql
项目地址git
Github: https://github.com/dyc87112/spring-cloud-config-admingithub
Gitee:https://gitee.com/didispace/spring-cloud-config-adminweb
前端Github: https://github.com/stone-jin/spring-cloud-config-admin-webredis
前端Gitee: https://gitee.com/stone-jin/spring-cloud-config-admin-webspring
若是您以为该项目对您有用,欢迎Star、Follow支持咱们!sql
本项目采用了先后端分离的架构,经过core模块抽象了前端须要的操做,再经过persistence和discovery模块隔离不一样的配置仓库和不一样的服务注册中心,从而达到前端程序不须要关心到底使用了什么存储配置以及使用了什么注册中心,这样用户能够根据本身的须要自由的组合不一样的配置存储和服务治理机制,尽量的匹配大部分Spring Cloud用户的需求。数据库
因为SCCA的架构对各个功能模块作了比较细致的拆分,因此它存在多种不一样模式的部署方式,因此它既能够为已经在使用Spring Cloud Config提供服务,也能够为从零开始使用Spring Cloud Config的用户。
在SCCA中咱们的可部署内容自底向上分为三个部分:
Spring Cloud 配置中心
:基于Spring Cloud Config构建的配置中心服务端。SCCA REST 服务端
:SCCA的核心模块,实现了SCCA配置管理的持久化内容以及全部的管理操做API。SCCA UI 服务端
:SCCA的前端模块,实现了可视化的配置管理操做界面。下面咱们来看看SCCA支持哪些多样的部署方式。
全分离模式就是将上述三个部分都以独立的进程进行部署,每个部分均可以作高可用,具体部署结构能够以下图所示:
这种模式既能够适用于已经在使用Spring Cloud Config的用户,也适用于正准备开始适用的用户。其中,位于最底层的Spring Cloud配置中心
就是一个最原始的Spring Cloud Config Server。因此,对于已经在使用Spring Cloud Config的用户只须要再部署一套SCCA REST 服务端
和SCCA UI 服务端
,并作一些配置就可使用SCCA来管理全部的配置信息了。
案例
所谓的半分离模式就是将上述的三个模块中的两个进行组合部署,以下降复杂度的部署方式。 SCCA UI模块与SCCA REST模块合并
以下图所示,咱们能够将SCCA UI服务端
与SCCA REST服务端
组合在一个程序中来部署,这样能够有效的下降全分离模式的部署复杂度,同时对于已经在使用Spring Cloud Config的用户来讲很是友好,已经部署的配置中心能够继续沿用。
案例
注意:对接不一样存储配置中心的配置参考分离部署中两个SCCA REST服务端的不一样配置内容进行调整。
最后介绍一种比较暴力的使用模式,SCCA支持将全部三个模块整合在一块儿使用和部署,在一个Spring Boot应用中同时包含:Spring Cloud 配置中心
、SCCA REST 服务端
以及SCCA UI 服务端
,具体以下所示:
案例
本章节分别对三个核心模块的构建方式以及核心的配置内容。下面全部的构建都是基于Spring Boot构建的,因此您须要对Spring Boot项目的构建有基本的认识,这里不作介绍。
在SCCA的架构中,配置中心的核心彻底采用Spring Cloud Config,因此如何构建一个配置中心彻底遵循Spring Cloud Config的使用方法。因为目前SCCA的REST模块主要实现了对Git存储和DB存储的综合管理,因此对于Spring Cloud Config的使用也只能支持这两种模式。下面分别介绍两种配置中心的搭建与配置。
这里主要介绍几种主要的而且SCCA可以比较好支持的配置模式:
第一种:多个项目使用多个不一样Git仓库存储的模式
spring.cloud.config.server.git.uri=https://github.com/dyc87112/{application}.git
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
复制代码
这种模式下不一样的项目会对应的不一样的Git仓库,若是项目中spring.application.name=user-service
,那么它的配置仓库会定位到https://github.com/dyc87112/user-service.git
仓库下的配置。配置文件按application-{profile}.properties
的格式存储,{profile}
表明环境名。
第二种:多个项目公用一个Git仓库不一样目录的存储模式
spring.cloud.config.server.git.uri=https://github.com/dyc87112/config-repo.git
spring.cloud.config.server.git.search-paths=/{application}
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
复制代码
这种模式下不一样的项目会对应到https://github.com/dyc87112/config-repo.git
仓库下的不一样目录,若是项目中spring.application.name=user-service
,那么它的配置仓库会定位到https://github.com/dyc87112/config-repo.git
仓库下的/user-service
目录。配置文件按application-{profile}.properties
的格式存储,{profile}
表明环境名。
在使用Db存储模式的时候,必须使用Spring Cloud的Edgware版本以上。好比,能够采用下面的配置:
# config server with jdbc
spring.profiles.active=jdbc
spring.cloud.config.server.jdbc.sql=SELECT `p_key`, `p_value` FROM property a, project b, env c, label d where a.project_id=b.id and a.env_id=c.id and a.label_id=d.id and b.name=? and c.name=? and d.name=?
# Datasource, share with scca-rest-server
spring.datasource.url=jdbc:mysql://localhost:3306/config-db
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
复制代码
主要分为两个部分:
spring.profiles.active
设置为jdbc,同时指定获取配置的SQL,用户直接复制采用同样的配置便可。这里须要注意的,使用的DB要与后续介绍的SCCA REST模块采用同一个DB
在构建SCCA REST服务端的时候针对对接不一样的配置存储有一些不一样的配置要求,因此下面按目前支持的存储模式作不一样的介绍。
当对接的配置中心采用Git存储的时候,须要引入如下核心依赖:
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-rest</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
<!-- scca persistence dependency -->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-persistence-git</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
复制代码
须要按以下配置:
# if config server use git, need config these properties
scca.git.username=
scca.git.password=
scca.git.repo-uri=https://github.com/dyc87112/{application}.git
scca.git.base-path=
scca.git.file-pattern=application-{profile}.properties
# Datasource
spring.datasource.url=jdbc:mysql://localhost:3306/config-db
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
复制代码
主要两部分:
scca.git.username
:访问git的用户名scca.git.password
:访问git的密码scca.git.repo-uri
:配置git仓库的地址,与配置中心的spring.cloud.config.server.git.uri
配置一致scca.git.base-path
:配置文件存储的相对路径,与配置中心的spring.cloud.config.server.git.search-paths
配置一致scca.git.file-pattern
:配置文件的命名规则当对接的配置中心采用Git存储的时候,须要引入如下核心依赖:
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-rest</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
<!-- scca persistence dependency -->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-persistence-db</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
复制代码
须要按以下配置:
# Datasource
spring.datasource.url=jdbc:mysql://localhost:3306/config-db
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
复制代码
须要注意,当配置中心采用DB存储的时候,这里的数据源须要一致
若是SCCA REST模块在访问配置中心的时候基于服务发现的话还须要引入对应的支持依赖和配置
若是使用eureak,那么须要引入以下依赖:
<!-- scca discovery dependency-->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-discovery-eureka</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
复制代码
而且在配置中加入eureka的配置,好比:
eureka.client.serviceUrl.defaultZone=http://eureka.didispace.com/eureka/
复制代码
更多相关配置请参与Spring Cloud Netflix Eureka的配置文档。
若是使用consul,那么须要引入以下依赖:
<!-- scca discovery dependency-->
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-discovery-consul</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
复制代码
而且在配置中加入consul的相关配置,好比:
spring.cloud.consul.host=localhost
spring.cloud.consul.port=8500
复制代码
更多相关配置请参与Spring Cloud Consul的配置文档。
SCCA REST模块还有一个特别的配置scca.rest.context-path=/xhr
,该配置主要用来配置全部SCCA REST模块接口的前缀,该接口主要用于与SCCA UI模块对接时候使用,两边必须对接一致才能顺利对接。
SCCA UI服务端须要引入如下核心依赖:
<dependency>
<groupId>com.didispace</groupId>
<artifactId>scca-ui</artifactId>
<version>1.0.0-RELEASE</version>
</dependency>
复制代码
另外,还须要在配置中指定具体要访问的SCCA REST模块的位置,主要有两种模式:
scca.ui.rest-server-url=http://localhost:10130
复制代码
scca.ui.rest-server-name=scca-rest-server
复制代码
除了上面的配置以后,还须要引入eureka或consul的依赖以及作对应的配置
最后,还有一个scca.ui.rest-server-context-path=/xhr
配置,用来描述要访问的SCCA REST模块接口的前缀,与SCCA REST服务端的scca.rest.context-path=/xhr
配置相对应。
案例:SCCA UI 服务端
经过以前介绍的任何一个部署方式搭建了配置中心和管理端以后,咱们就能够打开浏览器访问咱们的UI模块实现对配置中心的管理了。
访问地址为:http://localhost:10032/admin/
,ip与端口根据实际部署UI模块的状况进行调整。
在管理各个项目的配置以前,咱们须要先作一些基础配置,好比:环境的配置、环境所属的参数配置,加密相关的配置等。
环境配置主要用来维护要使用SCCA统一管理的环境以及对应的Spring Cloud Config服务端信息。
如上图所示,经过“新增环境”按钮能够添加一个部署环境。当咱们使用了Eureka、Consul等注册中心时,只须要配置注册中心的访问地址和配置中心的服务名以及配置中心访问的前缀,后续就能够方便的使用这个环境的配置中心来进行加密解密、拉取配置等一系列的操做了。
若是不采用服务发现的机制取找到配置中心,也能够将注册中心地址留空,配置中心服务名一栏直接配置访问注册中心的URL便可。
环境参数配置主要用来配置每一个环境所属的一些特有配置信息,好比:redis的地址,eureka的地址等等。这些配置信息将用户后续为各项目在各个环境配置的时候给予参考和快捷的替换操做提供元数据。
加密管理主要用来维护一些一般须要加密的Key,这样能够在后续编辑配置内容的时候,方便的进行批量加密操做。
在完成了上面的系统配置以后,用户就能够进入配置中心模块,这里会提供具体的管理配置内容的功能。目前主要有两部分组成:项目管理和配置管理。
项目管理主要用来维护须要在各个环境部署的应用的配置信息,这里能够维护这个项目须要部署在什么环境,有多少配置的版本。
这里的三个基本概念与Spring Cloud Config的几个概念的对应关系以下:
这里配置版本(label),咱们会默认采用
master
。须要同时存在多个配置版本,实现灰度配置的时候,用户也能够本身添加label。
配置管理功能是SCCA的核心,在这里用户能够方便对各个应用、各个环境、各个版本的配置进行编辑、加密等操做。同时,也提供了一些快捷的操做,好比:根据环境参数配置一键替换、根据加密Key清单实现一键加密、经过配置中心能够加载到的配置信息等(更多便捷功能持续添加中...)
本页主要提供给没有使用过Spring Cloud Config的用户阅读。若是您已经使用过Spring Cloud Config,那么客户端如何经过Spring Cloud Config的配置中心加载配置相信已经掌握,在使用本项目的时候,无非就是搭建SCCA-REST模块和SCCA-UI模块来帮助管理您目前的配置内容。
经过前面几节内容,若是您已经完成了SCCA中几个要素的搭建,下面就来看看如何建立一个Spring Boot项目并经过配置中心来加载配置信息。
1. 建立一个基本的Spring Boot项目,并在pom.xml中引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
复制代码
2. 建立应用主类
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
复制代码
3. 建立bootstrap.properties
配置文件(也可使用yaml能够)
spring.application.name=config-client
server.port=12000
spring.cloud.config.uri=http://localhost:10032/scca-config-server
spring.cloud.config.profile=stage
spring.cloud.config.label=master
复制代码
上述配置参数与scca中维护元素的对应关系以下:
1. 建立一个基本的Spring Boot项目,并在pom.xml中引入依赖
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
复制代码
上面以经过eureka作注册中心的依赖,若是用consul,只须要将
spring-cloud-starter-eureka
换成spring-cloud-starter-consul-discovery
便可。
2. 建立应用主类
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
new SpringApplicationBuilder(Application.class).web(true).run(args);
}
}
复制代码
3. 建立bootstrap.properties
配置文件(也可使用yaml能够)
spring.application.name=config-client
server.port=12000
spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.serviceId=config-server
spring.cloud.config.profile=stage
spring.cloud.config.label=master
复制代码
上述配置参数与scca中维护元素的对应关系以下:
经过上面的两种方式从配置中心拉取配置以后,在Spring Boot项目中就能够轻松的使用全部配置内容了,好比:
@RefreshScope
@RestController
public class TestController {
@Value("${a.b.c}")
private String abc;
@RequestMapping("/abc")
public String abc() {
return this.abc;
}
}
复制代码
两个主要注解的说明:
@Value("${a.b.c}")
:读取配置key为a.b.c
的value值@RefreshScope
:下面的配置信息能够经过/refresh
端点实现动态刷新若是您还不了解Spring Cloud Config,您也能够阅读下面的几篇了解一下最原始的Spring Cloud Config配置中心和客户端接入方式