Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh
国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使用svn+Spring Cloud Config来做配置中心。
svn版本
同样先示例server端的代码,基本步骤一样。
1、添加依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
</dependency>
</dependencies>
需要多引入svnkitr包
2、配置文件
server:
port: spring:
cloud:
config:
server:
svn:
uri: http://192.168.0.6/svn/repo/config-repo
username: username
password: password
default-label: trunk
profiles:
active: subversion
application:
name: spring-cloud-config-server
和git版本稍有区别,需要显示声明subversion.
3、启动类
启动类没有变化,添加@EnableConfigServer激活对配置中心的支持
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication { public static void main(String[] args) {
SpringApplication.run(ConfigServerApplication.class, args);
}
}
4、测试
服务端测试
访问:http://localhost:8001/neo-config-dev.properties,返回:neo.hello: hello im dev,说明服务端可以正常读取到svn代码库中的配置信息。修改配置文件neo-config-dev.properties中配置信息为:neo.hello=hello im dev update,再次在浏览器访问http://localhost:8001/neo-config-dev.properties,返回:neo.hello: hello im dev update。说明server端会自动读取最新提交的内容
客户端测试
客户端直接使用上一篇示例项目spring-cloud-config-client来测试,配置基本不用变动。启动项目后访问:http://localhost:8002/hello,返回:hello im dev update``说明已经正确的从server端获取到了参数。同样修改svn配置并提交,再次访问http://localhost:8002/hello依然获取的是旧的信息,和git版本的问题一样。
refresh
现在来解决上一篇的遗留问题,这个问题在svn版本中依然存在。Spring Cloud Config分服务端和客户端,服务端负责将git(svn)中存储的配置文件发布成REST接口,客户端可以从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置。客户端如何去主动获取新的配置信息呢,springcloud已经给我们提供了解决方案,每个客户端通过POST方法触发各自的/refresh。
修改spring-cloud-config-client项目已到达可以refresh的功能。
1、添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
增加了spring-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控程序在运行时状态,其中就包括/refresh的功能。
2、 开启更新机制
需要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值。
@RestController
@RefreshScope // 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。
class HelloController { @Value("${neo.hello}")
private String hello; @RequestMapping("/hello")
public String from() {
return this.hello;
}
}
3、测试
springboot 1.5.X 以上默认开通了安全认证,所以需要在配置文件application.properties添加以下配置
management.security.enabled=false
OK 这样就改造完了,以post请求的方式来访问http://localhost:8002/refresh 就会更新修改后的配置文件。
我们再次来测试,首先访问http://localhost:8002/hello,返回:hello im dev,我将库中的值修改为hello im dev update。在win上面打开cmd执行curl -X POST http://localhost:8002/refresh,返回["neo.hello"]说明已经更新了neo.hello的值。我们再次访问http://localhost:8002/hello,返回:hello im dev update,客户端已经得到了最新的值。
每次手动刷新客户端也很麻烦,有没有什么办法只要提交代码就自动调用客户端来更新呢,github的webhook是一个好的办法。
4、webhook
WebHook是当某个事件发生时,通过发送http post请求的方式来通知信息接收方。Webhook来监测你在Github.com上的各种事件,最常见的莫过于push事件。如果你设置了一个监测push事件的Webhook,那么每当你的这个项目有了任何提交,这个Webhook都会被触发,这时Github就会发送一个HTTP POST请求到你配置好的地址。
如此一来,你就可以通过这种方式去自动完成一些重复性工作,比如,你可以用Webhook来自动触发一些持续集成(CI)工具的运作,比如Travis CI;又或者是通过 Webhook 去部署你的线上服务器。下图就是github上面的webhook配置。

Payload URL:触发后回调的URLContent type:数据格式,两种一般使用jsonSecret:用作给POST的body加密的字符串。采用HMAC算法events:触发的事件列表。
| events事件类型 | 描述 |
|---|---|
| push | 仓库有push时触发。默认事件 |
| create | 当有分支或标签被创建时触发 |
| delete | 当有分支或标签被删除时触发 |
svn也有类似的hook机制,每次提交后会触发post-commit脚本,我们可以在这里写一些post请求
这样我们就可以利用hook的机制去触发客户端的更新,但是当客户端越来越多的时候hook支持的已经不够优雅,另外每次增加客户端都需要改动hook也是不现实的。其实Spring Cloud给了我们更好解决方案,后面文章来介绍。
Spring Cloud(七):使用SVN存储分布式配置中心文件和实现refresh的更多相关文章
- Spring Cloud(Dalston.SR5)--Config 集群配置中心
Spring Cloud Config 是一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,他分为服务端和客户端两个部分.服务端也称为分布式配置中心,是一个独立的微服务 ...
- Spring Cloud Config、Apollo、Nacos配置中心选型及对比
Spring Cloud Config.Apollo.Nacos配置中心选型及对比 1.Nacos 1.1 Nacos主要提供以下四大功能 2.Spring Cloud Config 3.Apollo ...
- Spring Cloud(十四)Config 配置中心与客户端的使用与详细
前言 在上一篇 文章 中我们直接用了本应在本文中配置的Config Server,对Config也有了一个基本的认识,即 Spring Cloud Config 是一种用来动态获取Git.SVN.本地 ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
- Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心-刷新配置
远程 SVN 服务器上面的配置修改后,需要通知客户端来改变配置,需要增加 spring-boot-starter-actuator 依赖并将 management.security.enabled 设 ...
- Spring Cloud系列(六):配置中心
在使用Spring Boot的时候,我们往往会在application.properties配置文件中写一些值,供应用使用,这样做的好处是可以在代码中引用这些值,当这些值需要作出修改的时候,可以直接修 ...
- Spring Cloud Alibaba 整合 Nacos 实现服务配置中心
在之前的文章 <Nacos 本地单机版部署步骤和使用> 中,大家应该了解了 Nacos 是什么?其中 Nacos 提供了动态配置服务功能 一.Nacos 动态配置服务是什么? 官方是这么说 ...
- Spring Cloud(Dalston.SR5)--Config 集群配置中心-加解密
实际应用中会涉及很多敏感的数据,这些数据会被加密保存到 SVN 仓库中,最常见的就是数据库密码.Spring Cloud Config 为这类敏感数据提供了加密和解密的功能,加密后的密文在传输给客户端 ...
随机推荐
- Ping用法大全
Ping是典型的网络工具.Ping可以辨别网络功能的某些状态. 这些网络功能的状态是日常网络故障诊断的基础.特别是Ping可以识别连接的二进制状态(也就是是否连通).可是,这仅仅是 ...
- windows安装dcm4chee 出错 check file system group LOSSY_STORAGE for deletion
错误情景: 解决方法: 更改服务的监听端口(参考DICOM:Ubuntu14环境下安装dcm4chee+oviyam2.1)
- 第一个Xamarin的 Android 应用程序!
你好,安卓 Xamarin的工作室 Xamarin的应用程序图标和启动屏幕 脱机使用PDF格式: 介绍与Xamarin的Android开发 示例代码: 开始使用应用程序的探险家 显示说明: Visua ...
- [Functional Programming] Working with two functors(Applicative Functors)-- Part2 --liftAN
Let's examine a pointfree way to write these applicative calls. Since we know map is equal to of/ap, ...
- ganglia组播和单播
ganglia快速开始向导(翻译自官方wiki) 发布于 2012 年 1 月 23 日 由 admin 2 comments发表评论 转自:http://cryinstall.com/?p=18 ...
- 导入exce表格中的数据l到数据库
因为我的项目是JavaWeb的,所有是通过浏览器导入数据库到服务器端的数据库,这里我们采用struts来帮助我们完成. 1:首先定义一个文件上传的jsp页面.把我们的数据先上传到服务器端. <f ...
- 【PAT】1028. List Sorting (25)
题目链接:http://pat.zju.edu.cn/contests/pat-a-practise/1028 题目描述: Excel can sort records according to an ...
- shiny安装使用入门
下载最新版R(至少3.2.5版本),在CRAN上下载: 打开R install.packages("shiny")#安装shiny包 library(shiny)#如果出现warn ...
- webpack CommonsChunkPlugin 提取公共代码
1.项目结构 2.部分代码 module.js console.log('module.js'); index文件夹下的index.js require('../module.js'); consol ...
- WebApi2 知识点总结
1.建议使用异步接口async Task<> public async Task<IHttpActionResult> Get() 如果返回的是IEnumerable请使用: ...