前言

本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3

本文基于前面的文章eureka-server的实现。

参考

概述

在分布式系统中,由于服务数量巨多,为了方便服务配置文件统一管理,所以需要分布式配置中心组件。Spring Cloud Config为分布式系统中的外部化配置提供服务器和客户端支持。

本篇涉及项目的结构为一个Config Server单机模式和适用于链接Git仓库,一个Config Client通过server来展示配置文件数据。 同时使用两个bus来模拟配置文件刷新。

创建config-server工程

Config-server功能

  • 用于外部配置的http,基于资源的api
  • 加密和解密属性
  • 使用可嵌入spring boot的应用程序

1.1 创建配置中心server:config-server

1.2 添加config-server的pom.xml相关依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.3 添加config-server的application添加配置信息

#加载本地文件配置
spring:
application:
name: config-server
profiles:
active: native #加载本地配置
cloud:
config:
server:
native: #加载本地目录文件
search-locations: /Users/xxxx/config-server server:
port: 13081 eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

1.4 启动类ConfigServerApplication增加注解

package spring.cloud.demo.configserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
} }

至此,一个单机本地config-server就搭建完成

1.5 创建配置中心client:config-client

1.6 添加config-client的pom相关依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.7添加config-client的bootstrap.yml相关配置(注意这里不是application.yml)

bootstrap.yml

spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
fail-fast: true
uri: http://localhost:13081 #通过域名访问配置中心服务端
discovery:
enabled: true
eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

1.8新建从服务端请求的配置文件config-client-dev.yml

客户端从服务端获取资源配置的路径规则:

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

本文采用第二种规则。

配置内容:

spring:
application:
name: config-client server:
port: 52601 eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/ info: local-config-client-dev

将新建的config-client-dev.yml放到config-server配置的/Users/xxxx/config-server目录下,如果config-server没有配置目录,默认使用resources目录下。

1.9 ConfigClientApplication增加注解

package spring.cloud.demo.configclient;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
} }

2.0 创建测试类

在config-client创建测试Controller:ConfigClientController

package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* 测试是否能获取到配置信息的controller
* @auther: maomao
* @DateT: 2019-09-17
*/
@RestController
public class ConfigClientController { @Value("${info}")
private String info; @RequestMapping("/config/info")
public String info() {
return info;
} }

2.1 启动相关服务

分别启动eureka-server、config-server、config-client。访问:http://localhost:52601/config/info,显示如下

证明本地配置文件已经生效。

2.2 从远程git加载资源配置文件

修改config-server的application.yml配置文件:

##加载本地文件配置
#spring:
# application:
# name: config-server
# profiles:
# active: native #加载本地配置
# cloud:
# config:
# server:
# native: #加载本地目录文件
# search-locations: /Users/fengfujie/config-server #加载远程git仓库资源文件
spring:
application:
name: config-server
cloud:
config:
server:
git:
# 配置git仓库的地址
uri: https://github.com/fengfujie25/sping-cloud-config
# git仓库的账号
username: xxxxxx
# git仓库的密码
password: xxxxxx server:
port: 13081 eureka:
instance:
hostname: eureka1.client.com
lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

重新启动config-server服务。然后刷新http://localhost:52601/config/info地址,显示如下

证明已经成功获取了远程git资源的配置信息。

2.3 通过服务名称访问config-server

以上配置都是通过域名访问的config-server,为了保证系统的高可用,因为生产环境的配置服务中心都是集群配置,所有客户端才通过服务名称来访问。

修改config-client的bootstrap.yml

spring:
application:
name: config-client
cloud:
config:
label: master
profile: dev
fail-fast: true
#uri: http://localhost:13081 #通过域名访问配置中心服务端
discovery:
enabled: true
service-id: config-server #通过服务访问配置中心服务端 eureka:
instance:
hostname: eureka1.client.com lease-renewal-interval-in-seconds: 5
lease-expiration-duration-in-seconds: 10
client:
service-url:
defaultZone: http://eureka1.server.com:8701/eureka/,http://eureka2.server.com:8702/eureka/,http://eureka3.server.com:8703/eureka/

spring.cloud.config.discovery.service-id:通过服务名称访问配置中心服务端

重新启动config-client.并访问http://localhost:5301/config/info,显示结果同【2.2】则代表配置信息已生效,证明是通过服务名称访问的config-server.

至此,spring cloud集成config的配置就全部完成。但是存在一个问题,如果修改远程git仓库的资源配置,项目并不会刷新,所以配置信息是不生效的。

2.4 动态刷新config-server配置

动态刷新config-serve配置方式

  • 基于RabbitMQ动态刷新
  • 原生刷新(伪动态刷新)

本文采用比较简单的原生刷新方式。

2.4.1增加相关pom.xml依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
2.4.2 在ConfigClientController增加@RefreshScope注解
package spring.cloud.demo.configclient.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; /**
* @auther: maomao
* @DateT: 2019-09-17
*/
@RestController
@RefreshScope
public class ConfigClientController { @Value("${info:error}")
private String info; @RequestMapping("/config/info")
public String info() {
return info;
} }

此方式同时还要修改@Value注解内容为@Value("${info:error}"),因为刷新的时候需要配置信息有默认值,否则会报错。

2.4.3 重新启动config-client

访问http://localhost:5301/config/info,看服务是否可以正常访问。

然后可以修改git资源仓库中的配置信息。

  • 刷新http://localhost:5301/config/info,结果显示:

证明refresh已经生效。

此方式每次都需要手动刷新一下才行,比较麻烦。GitHub提供一种Webhooks方法可以实现不用每次手动刷新。

Payload URL: 触发后回调的URL

Content type: 数据格式,两种一般使用json

Secret: 用作给POST的Body加密的字符串,采用HMAC算法

Events: 触发的事件列表

事件类型 描述
Just the push event 仓库有push的时候触发,默认事件
Send me everything 派我来一切
Let me select individual events 选择个别事件

这样我们就可以利用Webhook的机制去触发客户端的更新,但是当客户端越来越多的时候,Webhook机制也不够优雅,每次增加客户端都需要改动Webhook也不现实。

其实,Spring cloud给了我们更好的解决方案-spring cloud bus。

spring cloud bus后续更新。

总结

本文简单的实现了config-server和config-client的单机和远程git仓库的配置的调用以及配置信息的简单的动态更新。

代码地址


《Srping Cloud 2.X小白教程》目录


转载请注明出处,

  • 联系方式:4272231@163.com

spring cloud 2.x版本 Config配置中心教程的更多相关文章

  1. Spring Cloud(八):配置中心(服务化与高可用)【Finchley 版】

    Spring Cloud(八):配置中心(服务化与高可用)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-26 |  本文接之前的<Spring Clou ...

  2. Spring Cloud(七):配置中心(Git 版与动态刷新)【Finchley 版】

    Spring Cloud(七):配置中心(Git 版与动态刷新)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-04-24 |  Spring Cloud Confi ...

  3. Spring Cloud(九):配置中心(消息总线)【Finchley 版】

    Spring Cloud(九):配置中心(消息总线)[Finchley 版]  发表于 2018-04-19 |  更新于 2018-05-07 |  我们在 Spring Cloud(七):配置中心 ...

  4. Spring Cloud第十一篇 | 分布式配置中心高可用

    ​ 本文是Spring Cloud专栏的第十一篇文章,了解前十篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring Cl ...

  5. spring cloud 2.x版本 Ribbon服务发现教程(内含集成Hystrix熔断机制)

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 前言 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  6. spring cloud 2.x版本 Eureka Client服务提供者教程

    本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 1 创建eureka client 1.1 新建Srping boot工程:eureka-c ...

  7. spring cloud 2.x版本 Feign服务发现教程(内含集成Hystrix熔断机制)

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server和eureka-client的实现. 参考 ...

  8. spring cloud 2.x版本 Zuul路由网关教程

    前言 本文采用Spring cloud本文为2.1.8RELEASE,version=Greenwich.SR3 本文基于前两篇文章eureka-server.eureka-client.eureka ...

  9. spring cloud 2.x版本 Gateway动态路由教程

    摘要 本文采用的Spring cloud为2.1.8RELEASE,version=Greenwich.SR3 本文基于前面的几篇Spring cloud Gateway文章的实现. 参考 Gatew ...

随机推荐

  1. 基于Python的SQL Server数据库对象同步轻量级实现

    缘由 日常工作中经常遇到类似的问题:把某个服务器上的某些指定的表同步到另外一台服务器.类似需求用SSIS或者其他ETL工作很容易实现,比如用SSIS的话就可以,但会存在相当一部分反复的手工操作.建源的 ...

  2. Security+学习笔记

    第二章 风险分析 风险管理 评估:确定并评估系统中存在的风险 分析:分析风险对系统产生的潜在影响 响应:规划如何响应风险的策略 缓解: 缓解风险对未来安全造成的不良影响 风险分析流程 资产确定 漏洞确 ...

  3. Idea中新建yml不显示叶子形状的原因

    IntelliJ IDEA 2019.2.4 x64 (版本),不显示叶子形状,导致写配置无法自动提示(自动提示请安装插件)Spring Assistant 先看一下Editor--->File ...

  4. koa2 从入门到进阶之路 (七)

    之前的文章我们介绍了一下 koa koa-static静态资源中间件,本篇文章我们来看一下 koa 中 cookie 和 session 的使用. cookie 是存储于访问者的计算机中的变量.可以让 ...

  5. C++ 课程设计——电梯调度系统

    这是我在本学期C++课程最后的课程设计报告,源代码将会上传到GitHub上. 一.背景 随着经济的不断发展,越来越多的摩天大楼拔地而起,而电梯作为高层建筑物种的运送人员货物的设备也越来越被广泛使用.电 ...

  6. maven修改运行环境配置

    maven中自带的tomcat是6版本,比如我们想用tomcat7,jdk1.8,我们就要通过配置来改变. 改变方式如下 <build> <plugins> <plugi ...

  7. (五十七)c#Winform自定义控件-传送带(工业)-HZHControls

    官网 http://www.hzhcontrols.com 前提 入行已经7,8年了,一直想做一套漂亮点的自定义控件,于是就有了本系列文章. GitHub:https://github.com/kww ...

  8. Winform中怎样在工具类中对窗体中多个控件进行操作(赋值)

    场景 需求是在窗体加载完成后掉用工具类的方法,工具类中获取窗体的多个控件对象进行赋值. 注: 博客主页: https://blog.csdn.net/badao_liumang_qizhi 关注公众号 ...

  9. SSH框架之Struts2第三篇

    1.3相关知识点 : 1.3.1 OGNL的表达式 : 1.3.1.1 什么是OGNL OGNL是Object-Graph Navigation Language的编写,它是一种功能强大的表达式语言, ...

  10. SQLi-LABS Page-1(Basic Challenges) Less5-Less10

    Less5 GET - Double Injection - Single Quotes http://10.10.202.112/sqli/Less-5?id=1 http://10.10.202. ...