暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

SpringCloud详细教程 | 第七篇:分布式配置中心(Spring Cloud Config) (Greenwich版本)

小东IT技术分享 2019-04-05
309

Spring Cloud Config为分布式系统中的外部化配置提供服务器端和客户端支持。在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件。在Spring Cloud中,有分布式配置中心组件spring cloud config ,它支持配置服务放在配置服务的内存中(即本地),也支持放在远程Git仓库中同时也可以存放在Mysql数据库。在spring cloud config 组件中,分两个角色,一是config server,二是config client

本文将介绍三种实现方式 Git 本地 JDBC

一 简介

  1. 概念理解

来源官方文档 https://cloud.spring.io/spring-cloud-config/spring-cloud-config.html#redisbackend

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

2.功能介绍

Spring Cloud Config Server功能:

  • 用于外部配置的HTTP,基于资源的API(名称 - 值对或等效的YAML内容)

  • 加密和解密属性值(对称或非对称)

  • 使用可轻松嵌入Spring Boot应用程序 @EnableConfigServer Config Client功能(适用于Spring应用程序):

  • 绑定到Config Server并Environment使用远程属性源初始化Spring

  • 加密和解密属性值(对称或非对称)

二. 构建config server 服务端

引入依赖

  1. <dependency>

  2. <groupId>org.springframework.cloud</groupId>

  3. <artifactId>spring-cloud-config-server</artifactId>

  4. </dependency>

改造后的pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5. <parent>

  6. <groupId>com.li</groupId>

  7. <artifactId>SpringCloudLearn</artifactId>

  8. <version>1.0-SNAPSHOT</version>

  9. <relativePath>../</relativePath>

  10. </parent>

  11. <artifactId>config-server</artifactId>

  12. <version>0.0.1-SNAPSHOT</version>

  13. <name>config-server</name>

  14. <description>分布式配置中心服务端</description>


  15. <dependencies>

  16. <dependency>

  17. <groupId>org.springframework.cloud</groupId>

  18. <artifactId>spring-cloud-config-server</artifactId>

  19. </dependency>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

  23. </dependency>


  24. </dependencies>


  25. <dependencyManagement>

  26. <dependencies>

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-dependencies</artifactId>

  30. <version>${spring-cloud.version}</version>

  31. <type>pom</type>

  32. <scope>import</scope>

  33. </dependency>

  34. </dependencies>

  35. </dependencyManagement>


  36. <build>

  37. <plugins>

  38. <plugin>

  39. <groupId>org.springframework.boot</groupId>

  40. <artifactId>spring-boot-maven-plugin</artifactId>

  41. </plugin>

  42. </plugins>

  43. </build>


  44. </project>

在程序入口类加上@EnableEurekaClient向服务中心注册和@EnableConfigServer注解开启配置服务器的功能

  1. package com.li.configserver;


  2. import org.springframework.boot.SpringApplication;

  3. import org.springframework.boot.autoconfigure.SpringBootApplication;

  4. import org.springframework.cloud.config.server.EnableConfigServer;

  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;


  6. @EnableConfigServer

  7. @EnableEurekaClient

  8. @SpringBootApplication

  9. public class ConfigServerApplication {


  10. public static void main(String[] args) {

  11. SpringApplication.run(ConfigServerApplication.class, args);

  12. }


  13. }

application.properties配置

  1. server.port=8769

  2. #服务注册中心实例的主机名

  3. eureka.instance.hostname=localhost

  4. #服务注册中心端口号

  5. eureka.port=8761

  6. #在此指定服务注册中心地址

  7. eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/


  8. # git方式

  9. # github地址

  10. spring.cloud.config.server.git.uri=https://github.com/LiHaodong888/SpringCloudLearn

  11. # 仓库路径 spring cloud会先在searchPaths中寻找配置文件

  12. spring.cloud.config.server.git.searchPaths=config-repo

  13. # 仓库的分支

  14. spring.cloud.config.label=master

  15. #git仓库账号

  16. spring.cloud.config.server.git.username=xxxx

  17. #git仓库密码

  18. spring.cloud.config.server.git.password=xxxx

如果Git仓库为公开仓库,可以不填写用户名和密码,如果是私有仓库需要填写,我这边是公开仓库

config-repo文件夹下有config-dev.properties配置文件内容属性为

  1. my.name=lhd

HTTP服务具有以下形式的资源:

  1. /{application}/{profile}[/{label}]

  2. /{application}-{profile}.yml

  3. /{label}/{application}-{profile}.yml

  4. /{application}-{profile}.properties

  5. /{label}/{application}-{profile}.properties

启动程序 打开浏览器访问 http://localhost:8769/config/dev/master

config是我的配置文件前缀 dev表示开发环境 master表示分支 到这里配置文件服务端就搭建好了 下面搭建客户端

三. 构建config client 客户端

改造后的pom.xml文件

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  4. <modelVersion>4.0.0</modelVersion>

  5. <parent>

  6. <groupId>com.li</groupId>

  7. <artifactId>SpringCloudLearn</artifactId>

  8. <version>1.0-SNAPSHOT</version>

  9. <relativePath>../</relativePath>

  10. </parent>

  11. <artifactId>config-server</artifactId>

  12. <version>0.0.1-SNAPSHOT</version>

  13. <name>config-server</name>

  14. <description>分布式配置中心服务端</description>


  15. <dependencies>

  16. <dependency>

  17. <groupId>org.springframework.cloud</groupId>

  18. <artifactId>spring-cloud-config-server</artifactId>

  19. </dependency>

  20. <dependency>

  21. <groupId>org.springframework.cloud</groupId>

  22. <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>

  23. </dependency>


  24. </dependencies>


  25. <dependencyManagement>

  26. <dependencies>

  27. <dependency>

  28. <groupId>org.springframework.cloud</groupId>

  29. <artifactId>spring-cloud-dependencies</artifactId>

  30. <version>${spring-cloud.version}</version>

  31. <type>pom</type>

  32. <scope>import</scope>

  33. </dependency>

  34. </dependencies>

  35. </dependencyManagement>


  36. <build>

  37. <plugins>

  38. <plugin>

  39. <groupId>org.springframework.boot</groupId>

  40. <artifactId>spring-boot-maven-plugin</artifactId>

  41. </plugin>

  42. </plugins>

  43. </build>


  44. </project>

bootstrap.properties配置文件

  1. spring.application.name=config-client

  2. server.port=8770


  3. #服务注册中心实例的主机名

  4. eureka.instance.hostname=localhost

  5. #服务注册中心端口号

  6. eureka.port=8761

  7. #在此指定服务注册中心地址

  8. eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/


  9. # 配置文件前缀 config-dev.properties

  10. spring.cloud.config.name=config

  11. # 指明远程仓库的分支

  12. spring.cloud.config.label=master

  13. # dev开发环境配置文件 test测试环境 pro正式环境

  14. spring.cloud.config.profile=dev

  15. # 配置服务中心的网址

  16. spring.cloud.config.uri=http://localhost:8769/

  17. spring.cloud.config.discovery.enabled=true

  18. # 配置中心服务端服务名

  19. spring.cloud.config.discovery.serviceId=config-server

重点说下

当你的配置中心的 server.port 不是 8888 的时候,服务就起不来了,从日志中可以发现,服务启动的时候,Fetching config from server at: http://localhost:8888,说明其他服务还是去 8888 这个默认端口取配置中心的文件,而不去你在 application.properties 文件中配置的配置中心读取配置文件 当你的服务配置文件使用 application.properties 文件时,服务启动还没到加载 application.properties 文件那一步,所以并不会去你配置的注册中心里的配置中心读取所需要的配置信息,因为application.properties 的优先级不高,而此时又需要一些配置信息(例如数据库的信息),系统才能继续往下执行启动,这个时候就需要使用 bootstrap.properties 引导配置文件,使用这个配置文件时,服务在启动的时候就会先加载 bootstrap.properties 配置文件,这样就会去你配置的注册中心里的配置中心读取配置文件信息,然后加载信息进行启动。 优先级bootstrap>application

在程序的入口类,写一个接口,返回从配置中心读取的my.name变量的值,代码如下:

  1. package com.li.configclient;


  2. import org.springframework.beans.factory.annotation.Value;

  3. import org.springframework.boot.SpringApplication;

  4. import org.springframework.boot.autoconfigure.SpringBootApplication;

  5. import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

  6. import org.springframework.web.bind.annotation.RequestMapping;

  7. import org.springframework.web.bind.annotation.RestController;


  8. @RestController

  9. @EnableEurekaClient

  10. @SpringBootApplication

  11. public class ConfigClientApplication {


  12. public static void main(String[] args) {

  13. SpringApplication.run(ConfigClientApplication.class, args);

  14. }


  15. @Value("${my.name}")

  16. String name;


  17. @RequestMapping("/hello")

  18. public String hello(){

  19. return name;

  20. }

  21. }

启动程序 打开浏览器访问 http://localhost:8770/hello

说明配置中心成功

四. 本地配置

修改application.properties配置文件

  1. server.port=8769

  2. spring.application.name=config-server

  3. #服务注册中心实例的主机名

  4. eureka.instance.hostname=localhost

  5. #服务注册中心端口号

  6. eureka.port=8761

  7. #在此指定服务注册中心地址

  8. eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/



  9. # 本地配置

  10. #设置为本地启动的方式,而不是通过git

  11. spring.profiles.active= native

  12. #配置文件所在目录,classpath(类路径)和(系统文件路径)两种方式配置

  13. spring.cloud.config.server.native.search-locations=classpath:/config


  14. # 版本号

  15. spring.cloud.config.server.native.version=1.0.0

在resources下新建config文件夹并新建config-dev.properties文件

启动程序 打开浏览器输入 http://localhost:8769/config/dev/ 测试

说明配置成功

重启config-client服务 看是是否可以获取

控制台打印结果 说明是可以的 访问 http://localhost:8770/hello

本地配置成功

四. JDBC配置

Spring Cloud Config Server支持JDBC(关系数据库)作为配置属性的后端。您可以通过添加spring-jdbc到类路径并使用jdbc配置文件或添加类型的bean 来启用此功能JdbcEnvironmentRepository。如果在类路径中包含正确的依赖项(有关详细信息,请参阅用户指南),Spring Boot会配置数据源。

数据库需要有一个叫做表PROPERTIES一个名为列APPLICATION,PROFILE以及LABEL(与通常的Environment意思),再加上KEY和VALUE在键和值对Properties风格。所有字段都是Java中的String类型,因此您可以根据VARCHAR需要设置它们。属性值的行为方式与它们来自命名的Spring Boot属性文件{application}-{profile}.properties(包括所有加密和解密)的行为方式相同,后者将作为后处理步骤(即不直接在存储库实现中)应用。

引入数据库依赖

  1. <!--mysql数据库驱动-->

  2. <dependency>

  3. <groupId>mysql</groupId>

  4. <artifactId>mysql-connector-java</artifactId>

  5. </dependency>

  6. <dependency>

  7. <groupId>org.springframework.boot</groupId>

  8. <artifactId>spring-boot-starter-jdbc</artifactId>

  9. </dependency>

修改application.properties配置文件

  1. server.port=8769

  2. spring.application.name=config-server

  3. #服务注册中心实例的主机名

  4. eureka.instance.hostname=localhost

  5. #服务注册中心端口号

  6. eureka.port=8761

  7. #在此指定服务注册中心地址

  8. eureka.client.service-url.defaultZone=http://${eureka.instance.hostname}:${eureka.port}/eureka/


  9. # JDBC配置

  10. #数据库相关信息

  11. spring.datasource.url=jdbc:mysql://127.0.0.1:3306/config-jdbc?useUnicode=true&characterEncoding=utf8&characterSetResults=utf8

  12. spring.datasource.username=root

  13. spring.datasource.password=root

  14. spring.datasource.driver-class-name=com.mysql.jdbc.Driver

  15. #指明为jdbc

  16. spring.profiles.active=jdbc

  17. #读取的配置的分支,需要在数据库中数据对应

  18. spring.cloud.config.label=master

  19. spring.cloud.config.server.jdbc.order=0

  20. #查询数据库的sql语句,该语句的字段必须与数据库的表字段一致

  21. spring.cloud.config.server.jdbc.sql=SELECT key1, value1 from config_properties where APPLICATION=? and PROFILE=? and LABEL=?

初始化数据库

  1. CREATE TABLE `config_properties` (

  2. `id` bigint(20) NOT NULL AUTO_INCREMENT,

  3. `key1` varchar(50) COLLATE utf8_bin NOT NULL,

  4. `value1` varchar(500) COLLATE utf8_bin DEFAULT NULL,

  5. `application` varchar(50) COLLATE utf8_bin NOT NULL,

  6. `profile` varchar(50) COLLATE utf8_bin NOT NULL,

  7. `label` varchar(50) COLLATE utf8_bin DEFAULT NULL,

  8. PRIMARY KEY (`id`)

  9. ) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_bin

其中key1字段为配置的key,value1字段为配置的值,application字段对应于应用名,profile对应于环境,label对应于读取的分支,一般为master。

插入数据config-client 的1条数据,my.name配置,具体数据库脚本如下:

  1. insert into `config_properties` (`id`, `key1`, `value1`, `application`, `profile`, `label`) values('1','my.name','lhd','config','dev','master');

启动程序测试 打开浏览器访问 http://localhost:8769/config/dev/master

说明配置成功

重启config-client服务测试

控制台打印

浏览器访问 http://localhost:8770/hello

说明数据库配置中心成功

三种方式实现配置中心你学会了吗?

redis的 后面在研究 下面介绍如何不重启服务即可实现实时更新配置

源码下载: https://github.com/LiHaodong888/SpringCloudLearn

参考资料: 

https://blog.csdn.net/forezp/article/details/87866560


往期文章回顾

SpringCloud详细教程 | 第六篇:Gateway之路由器和过滤器、熔断、降级、限流(Greenwich版本)

SpringCloud详细教程 | 第五篇:路由器和过滤器Zuul(Greenwich版本)

SpringCloud详细教程 | 第四篇:断路器客户端Hystrix(Greenwich版本)

SpringCloud详细教程 | 第三篇: 声明性REST客户端Feign(Greenwich版本)

SpringCloud详细教程 | 第二篇: 客户端负载平衡器Ribbon(Greenwich版本)

SpringCloud详细教程 | 第一篇: 服务的注册与发现Eureka(Greenwich版本)




文章转载自小东IT技术分享,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论