Greenwich.SR2版本的Spring Cloud Config+BUS实例
Spring Cloud Config统一的配置中心同注册中心Eureka一样,也分服务端和客户端。服务端用来保存配置信息,客户端用来读取。它的优势是基于Git仓库,支持多环境、多分支配置、动态刷新。我们把服务网关Zuul(参见Greenwich.SR2版本的Spring Cloud Zuul实例)看成Config的客户端,它的路由配置(zuul.routes开头配置项)我们从Git上配置,如此一来我们直接修改本地Git上的路由配置再push到远程仓库后,调用/bus/refresh即可生效,达到动态路由的目的。同理,其他微服务组件也可以作为Config客户端读取业务配置。
至于如何实现配置信息支持动态更新,那就需要另外一个组件的出手了:Spring Cloud BUS,消息总线。原理是Git上配置更新后,通过调用/bus/refresh接口,基于消息中间件推送给所有Config客户端。这里的消息中间件用Kafka。Config服务端也支持集群,为了让Config客户端调用Config服务端集群方便,我们集成Eureka来发现Config服务端。
先看Config服务端,三板斧:
1、pom:
<?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> <groupId>com.example</groupId>
<artifactId>configuration-service</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</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-stream-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</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>
2、application:
#本机端口
server.port=8766 #本机服务名
spring.application.name=config-server #注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/ #Git仓库地址
spring.cloud.config.server.git.uri=https://github.com/wuxun1997/spring-cloud-demo #Git仓库路径下搜索路径
spring.cloud.config.server.git.search-paths=config #支持动态刷新
spring.cloud.bus.refresh.enabled=true
management.endpoints.web.exposure.include=bus-refresh #配置BUS使用的消息中间件Kafka
spring.kafka.bootstrap-servers=127.0.0.1:9092
3、主类通过@EnableConfigServer启动配置中心:
package hello; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.config.server.EnableConfigServer; @EnableConfigServer
@EnableDiscoveryClient
@SpringBootApplication
public class ConfigServiceApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServiceApplication.class, args);
}
}
启动Config服务端前需要启动本地Kafka,否则它会一直提示连接失败。如果连不上你的git仓库,在application.properties中加上用户名、密码配置:
#Git仓库用户、密码
spring.cloud.config.server.git.username=你的Git仓库用户名XXX
spring.cloud.config.server.git.password=你的Git仓库密码XXX
现在我们在git仓库创建两个配置文件:
hello.properties:

config/configclient-test.properties:

我们可以直接对Config服务端请求:

接着我们新建项目来看看Config客户端。再来三板斧:
1、pom:
<?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> <groupId>com.example</groupId>
<artifactId>configuration-client</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.8</java.version>
</properties> <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> <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-stream-kafka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-bus</artifactId>
</dependency>
</dependencies> <dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>Greenwich.SR2</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>
2、bootstrap:
#本机端口
server.port=8778 #本机服务名
spring.application.name=configClient #注册中心地址
eureka.client.service-url.defaultZone=http://localhost:8888/eureka/ #支持注册中心访问Config服务端
spring.cloud.config.discovery.enabled=true #Config服务端服务名
spring.cloud.config.discovery.service-id=config-server #git仓库配置文件分支(默认即为master)
spring.cloud.config.label=master #git仓库配置文件环境信息
spring.cloud.config.profile=test
3、主类通过@RefreshScope支持动态获取Config最新配置:
package hello; import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @EnableDiscoveryClient
@SpringBootApplication
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
} @RefreshScope
@RestController
class MessageRestController { @Value("${message:Hello default}")
private String message; @RequestMapping("/message")
String getMessage() {
return this.message;
}
}
现在我们先通过Config客户端获取到Git仓库的master分支下config目录下的configClient(默认拿Config客户端的服务名作为properties文件名)-test(环境)的配置项:

接下来我们动态刷新,直接去Git仓库上编辑message的值好了:

提交后我们直接看Config服务端,也自动更新了:

但是这是客户端并未更新,需要我们调用/bus-refresh接口去刷新一下服务端:

这会儿再调用客户端就能获取最新配置了:

Greenwich.SR2版本的Spring Cloud Config+BUS实例的更多相关文章
- Greenwich.SR2版本的Spring Cloud Zuul实例
网关作为对外服务,在微服务架构中是一个很重要的组件,主要体现在动态路由和接入鉴权这两个功能上.现在我们通过Spring Cloud Zuul来实现对之前a-feign-client(参见Greenwi ...
- Greenwich.SR2版本的Spring Cloud Feign实例
前面我们了解了Spring Cloud Ribbon和Hystrix,在使用上它们基本上会成队出现,那么是不是可以把它们组合起来使用?而且我们发现,在服务消费方a-beautiful-client里通 ...
- Greenwich.SR2版本的Spring Cloud Hystrix实例
之前我们在eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例)中,服务消费方a-beautiful-client调用服务提供方a-bootiful-clien ...
- Greenwich.SR2版本的Spring Cloud Ribbon实例
上次我们了解了eureka(参见Greenwich.SR2版本的Spring Cloud Eureka实例),里面的服务消费方(服务实例a-beautiful-client)我们其实已经用到了ribb ...
- Greenwich.SR2版本的Spring Cloud Eureka实例
作为微服务架构中最为核心和基础的服务治理,注册中心提供了微服务实例的自动化注册与发现.而作为一个服务注册中心,eureka的作用与传统的zk.etcd的作用是一样的,同样也支持高可用(集群).不同之处 ...
- Greenwich.SR2版本的Spring Cloud Zipkin实例
调用链跟踪是微服务架构中的基础能力,Spring Cloud Zipkin+Sleuth为我们提供了该能力.首先我们先建立Zipkin服务端,它需要集成Eureka,用于发现服务提供方和消费方,进行数 ...
- 0.9.0.RELEASE版本的spring cloud alibaba sentinel实例
sentinel即哨兵,相比hystrix断路器而言,它的功能更丰富.hystrix仅支持熔断,当服务消费方调用提供方发现异常后,进入熔断:sentinel不仅支持异常熔断,也支持响应超时熔断,另外还 ...
- 关于spring cloud “Finchley.RC2”版本在spring cloud config中的ArrayIndexOutOfBoundsException
原文 https://www.cnblogs.com/Little-tree/p/9166382.html 在学spring cloud config的时候遇到一个ArrayIndexOutOfBou ...
- 0.9.0.RELEASE版本的spring cloud alibaba nacos实例
简而言之,nacos与eureka的不同之处有三:后台老板.部署方式.功能.nacos是阿里的,eureka是奈飞的:nacos有自己的安装包,需要独立部署,eureka仅作为一个服务组件,引入jar ...
随机推荐
- Vue 前后端分离系统中遇到跨域问题
https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Access_control_CORS I Your application is running ...
- POI中的CellType类型以及值的对应关系
POI 中的CellType类型以及值的对应关系 CellType 类型 值 CELL_TYPE_NUMERIC 数值型 0 CELL_TYPE_STRING 字符串型 1 CELL_TYPE_FOR ...
- k8s的高可用
一.高可用原理 配置一台新的master节点,然后在每台node节点上安装nginx,nginx通过内部的负载均衡将node节点上需要通过访问master,kube-apiserver组件的请求, ...
- SVG的几个标签元素
defs svg允许我们定义以后需要重复使用的图形元素.建议把所有需要再次使用的元素定义在defs元素里面.这样做可以增加svg内容的易读性和可访问性.在defs元素定义的元素不会直接呈现.你可以在你 ...
- ajax向服务器发出get和post请求
假设有个网站A,它有一个简单的输入用户名的页面,界面上有两个输入框,第一个输入框包含在一个form表单里用来实现form提交,第二个输入框是单独的.没有包含在form里,下面就用这两个输入框来学习下j ...
- CF480E Parking Lot(two-pointers + 单调队列优化)
题面 动态加障碍物,同时查询最大子正方形. n,m≤2000n,m\leq2000n,m≤2000 题解 加障碍不好做,直接离线后反着做,每次就是清除一个障碍物. 显然倒着做答案是递增的,而且答案的值 ...
- JSP中9大内置对象类型
JSP中九大内置对象为: request 请求对象 类型 javax.servlet.ServletRequest 作用域 Requ ...
- url的主要功能是什么
URL是Uniform Resource Loctor的缩写 URL作用:通过URL可以到达任何一个地方寻找需要的东西,比如文件.数据库.图像.新闻组等等,可以这样说,URL是Internet上的地址 ...
- 主席树K-th Number
/*K-th NumberTime Limit: 20000MS Memory Limit: 65536KTotal Submissions: 44535 Accepted: 14779Case Ti ...
- 怎么联系$zcy$呢?
\(QQ:2939533969\) \(luogu:\)little_sun 窝经常以little_sun,little_sun0331,zcy05331的昵称混迹于各大网站 窝的CSDN blog ...