Spring Cloud Config

在我们了解spring cloud config之前,我可以想想一个配置中心提供的核心功能应该有什么

  • 提供服务端和客户端支持
  • 集中管理各环境的配置文件
  • 配置文件修改之后,可以快速的生效
  • 可以进行版本管理
  • 支持大的并发查询
  • 支持各种语言

Spring Cloud Config可以完美的支持以上所有的需求。

Spring Cloud Config项目是一个解决分布式系统的配置管理方案。它包含了Client和Server两个部分,server提供配置文件的存储、以接口的形式将配置文件的内容提供出去,client通过接口获取数据、并依据此数据初始化自己的应用。Spring cloud使用git或svn存放配置文件,默认情况下使用git,我们先以git为例做一套示例。

一、创建配置中心服务

1.首先在码云上创建一个 仓库spring-cofig-sever,在仓库里新建三个文件

lf-config-dev.properties 配置env=dev

lf-config-test.properties 配置env=test

lf-config.properties 配置 lf.config=hello lf

2.创建项目,集成spring-cloud-config,   pom.xml

  

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lf.liyouyou</groupId>
<artifactId>spring-cloud-netflix-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>lf.liyouyou</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties> <dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

3、启动类开启spring-cloud-config支持

@SpringBootApplication
@EnableConfigServer
public class SpringCloudConfigApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigApplication.class, args);
} }

4、配置properties

spring.application.name=spring-cloud-config
server.port=9002
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/ #spring.cloud.config.server.git.uri=https://github.com/**/spring-cloud-netflix-config
spring.cloud.config.server.git.uri=https://gitee.com/**/spring-cofig-sever
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.username= 用户名
spring.cloud.config.server.git.password= 密码

启动配置中心,启动项目,发现spring-cloud-config服务已经注册到配置中心。

访问http://localhost:9002/spring-cloud-config-client.properties 返回配置

二、创建配置中心消费端

1.创建工程spring-cloud-config-client , 添加spring-cloud-config客户端依赖

2.git上新增对应服务配置文件spring-cloud-config-client.properties 配置:

env=test
hello=lf
lf.config=spring-cloud-config-client

3.新增controller,获取配置

package lf.liyouyou.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;
@RestController
@RefreshScope
public class ConfigController { @Value("${env}")
private String tex; @Value("${hello}")
private String hello; @Value("${lf.config}")
private String env; @RequestMapping("/hello")
public String from() {
return env+"-"+tex+"-"+this.hello;
}
}

启动类添加扫描controller

package lf.liyouyou;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan; @SpringBootApplication
@ComponentScan(basePackages = "lf.liyouyou.controller")
public class SpringCloudConfigCilentApplication { public static void main(String[] args) {
SpringApplication.run(SpringCloudConfigCilentApplication.class, args);
} }

4.配置bootstrap.properties、application.properties

bootstrap.properties

spring.cloud.config.discovery.enabled=true
spring.cloud.config.discovery.service-id=spring-cloud-config eureka.client.service-url.defaultZone=http://localhost:8000/eureka/

application.properties

spring.application.name=spring-cloud-config-client
server.port=9003 management.endpoints.web.exposure.include=refresh

启动项目,访问http://localhost:9003/hello ,返回配置。

修改git上对应配置,发现访问http://localhost:9002/spring-cloud-config-client.properties 返回已经修改的配置,而http://localhost:9003/hello则未刷新。

@RefreshScope则可以触发刷新,用postman 或者curl -X POST http://localhost:9003/actuator/refresh进行节点刷新。
再访问而http://localhost:9003/hello,获取最新提交的配置。

三、参照二步骤创建(复制)一个config客户端spring-cloud-config-client-demo工程,端口改为9004,git上新增一个spring-cloud-config-client-demo.properties文件,配置:
lf.liyouyou=liyouyou

启动,访问http://localhost:9004/hello,返回配置。

同时修改git上对应spring-cloud-config-client.properties、spring-cloud-config-client-demo.properties  两个配置,发现访问http://localhost:9002/spring-cloud-config-client-demo.properties、http://localhost:9002/spring-cloud-config-client.properties

返回已经修改的配置,而http://localhost:9004/hello、http://localhost:9003/hello则未刷新。

同样@RefreshScope则可以触发刷新,用postman 或者curl -X POST http://localhost:9003/actuator/refresh进行节点刷新。

再访问而http://localhost:9003/hello,获取最新提交的配置。http://localhost:9004/hello还是未刷新。
再http://localhost:9004/actuator/refresh进行节点刷新,http://localhost:9004/hello才能获取到最新刷新的配置

综上,基于actuator的refresh只能进行单节点刷新

四、基于消息总线bus+监控monitor+webhook进行自动多节点刷新的最终解决方案

  基于消息总线配置+webhook的组合(http://localhost:9002/actuator/bus-refresh 所有节点都刷新),由于git推送信息会推送很多无关信息,会导致json解析异常,为此spring推出了monitor去解决此问题

  效果图如下:

  

  1.改造spring-cloud-config引入对应bus  、monitor依赖

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>lf.liyouyou</groupId>
<artifactId>spring-cloud-netflix-demo</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<groupId>lf.liyouyou</groupId>
<artifactId>spring-cloud-config</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-cloud-config</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
<spring-cloud.version>Hoxton.SR8</spring-cloud.version>
</properties> <dependencies>
<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>
<!--<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-kafka</artifactId>
</dependency>-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-monitor</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>

spring-cloud-bus可以使用rabbitMQ或者kafka进行实现,这边选择使用rabbitMQ进行实现

2.配置文件修改,添加mq 、bus支持

spring.application.name=spring-cloud-config
server.port=9002
eureka.client.service-url.defaultZone=http://localhost:8000/eureka/ #spring.cloud.config.server.git.uri=https://github.com/**/spring-cloud-netflix-config
spring.cloud.config.server.git.uri=https://gitee.com/**/spring-cofig-sever
spring.cloud.config.server.git.force-pull=true
spring.cloud.config.server.git.username=**
spring.cloud.config.server.git.password=** #Config Server会默认从应用的src/main/resource目录下检索配置文件。
#spring.profiles.active=native
#也可以通过如下方式 属性来指定配置文件的位置
#spring.cloud.config.server.native.searchLocations=file:D:/properties/ spring.rabbitmq.host=192.168.**.151
spring.rabbitmq.port=5672
spring.rabbitmq.username=**
spring.rabbitmq.password=** ## 刷新时,关闭安全验证
management.security.enabled=false spring.cloud.bus.enabled=true
spring.cloud.bus.trace.enabled=true
spring.cloud.bus.refresh.enabled=true management.endpoints.web.exposure.include=*

重新启动项目。

3.改造两个config client,pom.xml,配置文件分别添加对bus的支持

pom.xml

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>

application.properties

spring.application.name=spring-cloud-config-client
server.port=9003 spring.rabbitmq.host=192.168.**.151
spring.rabbitmq.port=5672
spring.rabbitmq.username=**
spring.rabbitmq.password=** spring.cloud.bus.enabled=true
#management.endpoints.web.exposure.include=refresh
management.endpoints.web.exposure.include=*

分别启动即可。

4.检测monitor+bus全局刷新

分别访问,http://localhost:9003/hello http://localhost:9004/hello 获取到对应配置,这时修改git上

同时修改git上对应spring-cloud-config-client.properties、spring-cloud-config-client-demo.properties  两个配置,发现访问http://localhost:9002/spring-cloud-config-client-demo.properties、http://localhost:9002/spring-cloud-config-client.properties

返回已经修改的配置,而http://localhost:9004/hello、http://localhost:9003/hello则未刷新。进行monitor刷新一次spring-cloud-config配置中心服务端

再http://localhost:9004/hello、http://localhost:9003/hello ,发现都获取到了最新提交的配置。

5、再配置webhook自动进行刷新,不需要手动进行http://localhost:9003/monitor进行刷新

新增一个webhook,并配置刷新地址http://localhost:9002外网映射ip/monitor?path=*

但是要注意添加到webhook的内网地址需要用ngrok工具进行外网地址映射,不然无妨访问本地

6、添加完成进行测试

  会发现哪怕同时更改git上多个服务的配置,访问http://localhost:9004/hello、http://localhost:9003/hello都能获取到最新配置,

  证明webhook已经帮助我们自动进行自动刷新。

env=test
hello=lf
lf.config=spring-cloud-config-client

spring-cloud-netflix-config的更多相关文章

  1. Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar

    Spring Cloud Netflix多语言/非java语言支持之Spring Cloud Sidecar 前言 公司有一个调研要做,调研如何将Python语言提供的服务纳入到Spring Clou ...

  2. Spring Cloud 入门教程(十):和RabbitMQ的整合 -- 消息总线Spring Cloud Netflix Bus

    在本教程第三讲Spring Cloud 入门教程(三): 配置自动刷新中,通过POST方式向客户端发送/refresh请求, 可以让客户端获取到配置的最新变化.但试想一下, 在分布式系统中,如果存在很 ...

  3. Spring Cloud Netflix项目进入维护模式

    任何项目都有其生命周期,Spring Could Netflix也不例外,官宣已进入维护模式,如果在新项目开始考虑技术选型时要考虑到这点风险,并考虑绕道的可能性. 原创: itmuch  IT牧场 这 ...

  4. spring cloud连载第三篇之Spring Cloud Netflix

    1. Service Discovery: Eureka Server(服务发现:eureka服务器) 1.1 依赖 <dependency> <groupId>org.spr ...

  5. 基于Spring Cloud Netflix的TCC柔性事务和EDA事件驱动示例

    Solar Spring Cloud为开发者提供了快速构建分布式系统中的一些常见工具,如分布式配置中心,服务发现与注册中心,智能路由,服务熔断及降级,消息总线,分布式追踪的解决方案等. 本次实战以模拟 ...

  6. Spring Cloud Netflix之Euraka Server注册中心

    Spring Cloud简介 Spring Cloud是基于Spring Boot的一套实现微服务架构的生态组件.生态组件中包含Spring Cloud NetFlix,Spring Cloud Fe ...

  7. Spring Cloud Netflix vs Spring Cloud Alibaba

    Spring Cloud Netflixhttps://spring.io/projects/spring-cloud-netflix spring-cloud-alibaba/README-zh.m ...

  8. Spring Cloud Netflix Zuul 重试会自动跳过经常超时的服务实例的简单说明和分析

    在使用E版本的Spring Cloud Netflix Zuul内置的Ribbon重试功能时,发现Ribbon有一个非常有用的特性: 如果某个服务的某个实例经常需要重试,Ribbon则会在自己维护的一 ...

  9. Spring Cloud Netflix Eureka源码导读与原理分析

    Spring Cloud Netflix技术栈中,Eureka作为服务注册中心对整个微服务架构起着最核心的整合作用,因此对Eureka还是有很大的必要进行深入研究. 本文主要分为四部分,一是对项目构建 ...

  10. Spring Cloud Netflix概览和架构设计

    Spring Cloud简介 Spring Cloud是基于Spring Boot的一整套实现微服务的框架.他提供了微服务开发所需的配置管理.服务发现.断路器.智能路由.微代理.控制总线.全局锁.决策 ...

随机推荐

  1. 记录一下 ThreadLocal 与 WeakReference

    ThreadLocal & WeakReference Thread整体的模块图 Thread -> ThreadLocalMap 对于继承了 WeakReference Entry本身 ...

  2. WCNSS_qcom_cfg.ini WIFI配置文件参数详细解析

    STA相关的一般配置 参数 含义 最小值 最大值 默认值 gNeighborLookupThreshold 1 触发roam scan发生的条件在WCNSS_qcom_cfg.ini文件中gNeigh ...

  3. 与图论的邂逅06:dfs找环

    当我在准备做基环树的题时,经常有了正解的思路确发现不会找环,,,,,,因为我实在太蒻了. 所以我准备梳理一下找环的方法: 有向图 先维护一个栈,把遍历到的节点一个个地入栈.当我们从一个节点x回溯时无非 ...

  4. 探索微软开源Python自动化神器Playwright

    相信玩过爬虫的朋友都知道selenium,一个自动化测试的神器工具.写个Python自动化脚本解放双手基本上是常规的操作了,爬虫爬不了的,就用自动化测试凑一凑. 虽然selenium有完备的文档,但也 ...

  5. Sklearn 与 TensorFlow 机器学习实战—一个完整的机器学习项目

    本章中,你会假装作为被一家地产公司刚刚雇佣的数据科学家,完整地学习一个案例项目.下面是主要步骤: 项目概述. 获取数据. 发现并可视化数据,发现规律. 为机器学习算法准备数据. 选择模型,进行训练. ...

  6. 【Android】报错 Please ensure Hyper-V is disabled in Windows Features, or refer to the Intel HAXM 的解决方案

    参考文章 实测华为锐龙本(adm yes)安装Android avd虚拟机教程 环境 Android Studio 3.6; Windows 1909; AMD Ryzen 4800U with Ra ...

  7. uni-app开发经验分享十四:小程序超过2M限制的方法——分包加载

      起初小程序上线时,微信限制了代码包不能超过1MB,后来功能变大变成了2M了,限制大小是出于对小程序启动速度的考虑,希望用户在使用任何一款小程序时,都能获得一种"秒开"体验.但是 ...

  8. uni-app开发经验分享二: uni-app生命周期记录

    应用生命周期(仅可在App.vue中监听) 页面生命周期(在页面中添加) 当页面中需要用到下拉刷新功能时,打开pages.json,在"globalStyle"里设置"e ...

  9. 今天聊点干货—关于CSS样式来源

    样式来源 CSS样式共有5个来源,它们分别是\(\color{#FF3030}{浏览器默认样式}\).\(\color{#FF3030}{用户样式}\).\(\color{#FF3030}{链接样式} ...

  10. (Oracle)关于blob转到目标库报ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值错误解决方案

    在数据抽取时,开发需要clob类型的数据,但是目标库类型是blob类型的,于是抽取的时候报错: ORA-01461: 仅能绑定要插入 LONG 列的 LONG 值错误 可能有以下几种原因: 可能有以下 ...