Spring-Cloud之Config配置中心-7
一、我们前面基本上都是讲解的Spring Cloud Netflix的组件,下面我们会重点说Spring Cloud Config分布式配置中心。为什么需要这个组件来管理配置呢?在分布式应用开发过程中我们的大小服务会特别多,如果一个个去配置的话会带来更多的工作量,所以Spring Cloud 催生一种集中配置的方式,这种方式就是Config组件。这个主键主要介绍一下几个部分。
1)Config Server从本地读取配置。
2)Config Server从远程仓库Git上面读取配置。
3)搭建高可用的Config Server集群。
4)使用Spring Cloud Bus刷新配置。
二、Config Server从本地读取配置。
1、Config Server编写。
1)首先在Springboot和SpringCloud的基础上加入一下依赖。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
2)编写启动项
package com.cetc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer; @SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3)编写配置application.yaml
server:
port: 8683
# port: 8684
spring:
application:
name: config-server
cloud:
config:
server:
native:
search-locations: classpath:/shared # 指定本地配置目录
profiles:
active: native # 本地读取配置
说明:这里profiles.active这里一定要配置native从本地读取。spring.cloud.server.native.search-locations为指定扫描的路径。
4)编写需要配置的客户端配置文件config-client.yaml

server:
port: 8685
version: 1.0
说明:config-client必须和服务名称一样。如何存在不同环境,比如:dev,test,prod等环境。可以通过spring.profiles.active来激活指定文件。
服务端的配置就完成了。那么下面编写客户端。
2、编写Config Client客户端。
1)加入相关配置pom.xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
2)编写启动项和测试接口。
package com.cetc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
package com.cetc.web.rest; import org.springframework.beans.factory.annotation.Value;import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/test")
public class TestResource { @Value("${version}")
private String version; @GetMapping("/getVersion")
public String getVersion() {
return version;
}
}
3)重点来了,这里我们不编写application.yaml类似的文件了,这里我们使用bootstrap.yaml。为什么是bootstrap.yaml,因为bootstrap相对于application有优先执行权。
spring:
application:
name: config-client
cloud:
config:
uri: http://127.0.0.1:8683
fail-fast: true
说明:这里的spring.cloud.config的uri为服务配置地址。fail-fast为快速失败配置。
3、测试。启动Config Server、Config Client端口为8683,8685。
Config Client启动过程节点。


测试接口:

三、Config Server从Git仓库获取配置文件。可以自己使用github,也可以自己打架gitlab服务。我这里是自己搭建的gitlab。
1)上面的工程基本上都是一样的,需要修改的地方就是Config Server 的application.yaml配置文件,修改如下:
server:
port: 8683
# port: 8684
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: http://127.0.0.1/lilin/SpringCloudConfig.git
search-paths: shared
username: lilin
password:
label: master
说明:一般配置文件都是私有的,所以一般都是使用账号登录,如果需要公开的可以使用github等方式。label:为那个分支。search-paths为搜索路径。
2)在Git上面编写config-client.yaml配置文件,为了方便我把之前的传上Git了。

3)启动Config Server和Config Client,端口8683,8685。
Config Server的展示如下:

客户端和从本地获取没有什么区别。
测试接口如下:

四、构建高可用的Config Server集群。为了方便管理,配置采用注册方式,所以需要加入Eureka服务注册中心。这里我们还是使用以前的注册中心Eureka Server端口。结构图如下

1、改造Config Server
1)在上面工程中,加入Eureka Client依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2)在上面工程中,加入启动配置
package com.cetc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
3)在上面工程基础上,修改配置。
server:
port: 8683
# port: 8684
spring:
application:
name: config-server
cloud:
config:
server:
git:
uri: http://127.0.0.1/lilin/SpringCloudConfig.git
search-paths: shared
username: lilin
password:
label: master
eureka:
client:
service-url:
defaultZone:
http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式
2、改造Config Client.
1)在上面工程基础上,加入Euraka Client依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2)在上面工程基础上,修改启动项。
package com.cetc; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; @SpringBootApplication
@EnableEurekaClient
public class ConfigClientApplication { public static void main(String[] args) {
SpringApplication.run(ConfigClientApplication.class, args);
}
}
3)修改bootstrap.yaml配置文件。
spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-server
eureka:
client:
service-url:
defaultZone:
http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式
3、启动Eureka Server、2个Config Server、Config Client。端口8670,8683/8684、8685。

可以得知,启动一切正常。
测试接口:

五、使用Spring Cloud bus刷新配置。

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-bus-amqp</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
(2)修改配置文件bootstrap.yaml。
spring:
application:
name: config-client
cloud:
config:
fail-fast: true
discovery:
enabled: true
service-id: config-server
rabbitmq:
host: 127.0.0.1
port: 5672
username: admin
password: admin
eureka:
client:
service-url:
defaultZone:
http://127.0.0.1:8670/eureka/ # 实际开发中建议使用域名的方式
management:
endpoints:
web:
exposure:
include: ["*"]
说明:目前Spring 2.0版本是通过actuator的接口进行外部访问的所以在使用的时候,一定要配置management.endpoints.web.exposure.include。
(3)在需要刷新配置的地方加入@RefreshScope注解,注意这个注解只针对于当前类生效。我们这里是获取的版本号所以在TestResource类加上。
package com.cetc.web.rest; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; @RestController
@RequestMapping("/api/test")
@RefreshScope
public class TestResource { @Value("${version}")
private String version; @GetMapping("/getVersion")
public String getVersion() {
return version;
}
}
(4)测试。启动Eureka Server,2个Config Server,2个Config Client端口分别为8670,8683/8684,8685/8686。

访问两个接口为:

(5)刷新配置。我们修改Git的上面config-client.yaml的版本号为2.0。

通过PostMan发送POST请求http://127.0.0.1:8685/actuator/bus-refresh,进行刷新。

发送请求测试,等待刷新:

从上面可以看出,我想一个接口发送了刷新请求,结果全部实例都进行了刷新,这样的结果好处就是。一次更改,全部可刷新。
说明:通过对启动接口:

我们可以得知,刷新是可以带参数的。而这个参数的意思就是刷新特定实例参数。
比如:http://127.0.0.1:8685/actuator/bus-refresh?destination=config-client:**
六、总结:
1)Spring Cloud Config简单来说就是对配置的简单化,让配置集中化。
2)Spring Cloud Config通过Eureka可以实现集群化,达到高可用。
3)通过对客户端进行改造加入Bus总线,可以实现一次更改,多个服务实例的更新。避免了多个服务重启的问题。
4)Spring Cloud Config的配置获取方式有:本地(native)、Git、SVN、vault等方式。
七、源码地址:https://github.com/lilin409546297/spring-cloud/tree/master/config
Spring-Cloud之Config配置中心-7的更多相关文章
- Spring Cloud 2-Config 分布式配置中心(七)
Spring Cloud Config 1.github配置 2.服务端配置 pom.xml application.xml Application.java 3.配置和命名 1. 配置加载顺序 ...
- Spring Cloud Consul使用——配置中心
1.pom.xml <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www ...
- spring cloud学习(六) 配置中心-自动更新
上一篇学习了spring cloud config的基本使用,但发现有个问题,就是每次更改配置后,都需要重启服务才能更新配置,这样肯定是不行的.在上网查资料了解后,spring cloud支持通过AM ...
- spring cloud学习(五) 配置中心
Spring Cloud Config为服务端和客户端提供了分布式系统的外部化配置支持.配置服务中心采用Git的方式存储配置文件,因此我们很容易部署修改,有助于对环境配置进行版本管理. 一.配置中心 ...
- Spring Cloud (6) 分布式配置中心-高可用
高可用 现在已经可以从配置中心读取配置文件了,当微服务很多时都从配置中心读取配置文件,这时可以将配置中心做成一个微服务,将其集群化,从而达到高可用. 改造config-server 加入eureka ...
- Spring Cloud (5) 分布式配置中心
Spring Cloud Config 在分布式系统中,由于服务数量很多,为了方便服务配置文件统一管理,实时更新,所以需要分布式配置中心组件.在Spring Cloud中,使用Spring Cloud ...
- spring cloud 集成分布式配置中心 apollo(单机部署apollo)
一.什么是apollo? Apollo(阿波罗)是携程框架部门研发的分布式配置中心,能够集中化管理应用不同环境.不同集群的配置,配置修改后能够实时推送到应用端,并且具备规范的权限.流程治理等特性,适用 ...
- Spring Cloud Gateway 结合配置中心限流
前言 上篇文章我讲过复杂的限流场景可以通过扩展RedisRateLimiter来实现自己的限流策略. 假设你领导给你安排了一个任务,具体需求如下: 针对具体的接口做限流 不同接口限流的力度可以不同 可 ...
- Spring Cloud之分布式配置中心
用服务的方式来实现 ConfigAppApplication.java package com.packtpub.ConfigApp; import org.springframework.boot. ...
- Spring Cloud Alibaba nacos 配置中心使用
背景 上一文我们讲到了如何去搭建注册中心,这一次我们讲述如何使用nacos作为注册中心 spring-cloud-alibaba-basis 创建基础依赖 首先我们创建一个spring-cloud-a ...
随机推荐
- js逆向分析之acorn和escodegen的使用
替换之前的d形如 d("77696669") 执行代码 const fs = require('fs'); const acorn = require('acorn'); cons ...
- 冰多多团队-第七次scrum例会
冰多多团队-第七次Scrum会议 工作情况 团队成员 已完成任务 待完成任务 zpj 接入IAT模块 debug, IAT 牛雅哲 调研科大讯飞SDK中其他模块,寻找符合我们的需求的部分,将接口更换成 ...
- ubuntu16 安装 qBittorrent ( Linux下BT下载图形工具 )
qBittorrent 是QT编写的一款BT下载软件,支持FreeBSD, Linux, macOS, OS/2, Windows: 官网:https://www.qbittorrent.org/ 在 ...
- Spring Web Flux 相关概念
Reactive Streams.Reactor 和 Web Flux 上面介绍了反应式编程的一些概念,以及 Reactor 和 Web Flux.可能读者看到这里有些乱.这里介绍一下三者的关系.其实 ...
- 使用CompletableFuture实现业务服务的异步调用实战代码
假如我有一个订单相关的统计接口,需要返回3样数据:今日订单数.今日交易额.总交易额. 一般的我们的做法是串行调用3个函数,把调用返回的结果返回给调用者,这3次调用时串行执行的,如果每个调用耗时1秒的话 ...
- Java学习-059-Jsoup爬虫获取中国所有的三级行政区划数据(四),生成相应的 JSON 数据并输出
还是直接上马,对应的源码如下所示: 生成的三级行政区划部分 JSON 数据如下所示:
- 如何学会快速调用API
作为一名苦逼的程序员,在实现各种需求的过程中,总是需要调用各种各样的API,也就是要读各种文档.我们所要做的是弄清楚这些API的参数然后调用即可.说起来非常简单,是实际上却不是这样.现在火爆的机器学习 ...
- PHP获取指定分钟数的下一个整数倍
2019-6-11 11:51:03 星期二 情景: 要定时发送邮件, 邮件数据入表时就记录下其待发送时间, 然后crontab是每分钟扫描邮件表, 找出当时那一分钟需要发送的邮件 举例: 假如有一种 ...
- 分布式事务二TCC
分布式事务解决方案之TCC 4.1.什么是TCC事务 TCC是Try.Confirm.Cancel三个词语的缩写,TCC要求每个分支事务实现三个操作:预处理Try.确认Confirm.撤销Cancel ...
- Nginx添加静态页面
1.编辑配置文件 sudo vim /etc/nginx/nginx.conf 在http {}中添加如下 server { listen 80; server_name localhost; loc ...