欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 财经 > 产业 > SpringCloud之配置中心svn示例和refresh

SpringCloud之配置中心svn示例和refresh

2024/10/24 22:30:29 来源:https://blog.csdn.net/qq_30614345/article/details/136217985  浏览:    关键词:SpringCloud之配置中心svn示例和refresh

SpringCloud之配置中心svn示例和refresh

上一篇配置中心git示例留了一个小问题,当重新修改配置文件提交后,客户端获取的仍然是修改前的信息,这个

问题我们先放下,待会再讲。国内很多公司都使用的svn来做代码的版本控制,我们先介绍以下如何使svn+Spring

Cloud Config来做配置中心。

1、svn版本

同样先示例server端的代码,基本步骤一样。

1.1 添加依赖

<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>spring-cloud-config-server</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-cloud-config-server</name><description>spring-cloud-config-server</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.RELEASE</spring-cloud.version></properties><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><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>

需要多引入svnkitr包

1.2 配置文件

server:port: 8001
spring:cloud:config:server:svn:uri: http://192.168.0.6/svn/repo/config-repousername: usernamepassword: passworddefault-label: trunkprofiles:active: subversionapplication:name: spring-cloud-config-server

和git版本稍有区别,需要显示声明subversion。

1.3 启动类

启动类没有变化,添加@EnableConfigServer激活对配置中心的支持

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

1.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端会自动读取最新提交的内容。

2、客户端

2.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><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>spring-cloud-config-client</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-cloud-config-client</name><description>spring-cloud-config-client</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.RELEASE</spring-cloud.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></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>

2.2 配置文件

application.properties

spring.application.name=spring-cloud-config-client
server.port=8002

bootstrap.properties

spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=trunk

2.3 控制器

package com.example.web;import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
class HelloController {@Value("${neo.hello}")private String hello;@RequestMapping("/hello")public String from() {return this.hello;}
}

2.4 启动类

package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringCloudConfigClientApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudConfigClientApplication.class, args);}}

客户端测试:客户端直接使用上一篇示例项目spring-cloud-config-client来测试,配置基本不用变动。启动

项目后访问:http://localhost:8002/hello,返回:hello im dev update说明已经正确的从server端获

取到了参数。同样修改svn配置并提交,再次访问http://localhost:8002/hello依然获取的是旧的信息,和git

版本的问题一样。

3、refresh

现在来解决上一篇的遗留问题,这个问题在svn版本中依然存在。Spring Cloud Config分服务端和客户端,服务端

负责将git(svn)中存储的配置文件发布成REST接口,客户端可以从服务端REST接口获取配置。但客户端并不能

主动感知到配置的变化,从而主动去获取新的配置。客户端如何去主动获取新的配置信息呢,springcloud已经给

我们提供了解决方案,每个客户端通过POST方法触发各自的/refresh

3.1 添加依赖

<?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>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>1.5.3.RELEASE</version><relativePath/></parent><groupId>com.example</groupId><artifactId>spring-cloud-config-client-refresh</artifactId><version>0.0.1-SNAPSHOT</version><name>spring-cloud-config-client-refresh</name><description>spring-cloud-config-client-refresh</description><properties><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding><project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding><java.version>1.8</java.version><spring-cloud.version>Dalston.RELEASE</spring-cloud.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-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></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-boot-starter-actuator包,spring-boot-starter-actuator是一套监控的功能,可以监控

程序在运行时状态,其中就包括/refresh的功能。

3.2 开启更新机制

需要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值。

package com.example.web;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
// 使用该注解的类,会在接到SpringCloud配置中心配置刷新的时候,自动将新的配置更新到该类对应的字段中。
class HelloController {@Value("${neo.hello}")private String hello;@RequestMapping("/hello")public String from() {return this.hello;}
}
package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class SpringCloudConfigClientRefreshApplication {public static void main(String[] args) {SpringApplication.run(SpringCloudConfigClientRefreshApplication.class, args);}}
spring.application.name=spring-cloud-config-client
server.port=8002
management.security.enabled=false
spring.cloud.config.name=neo-config
spring.cloud.config.profile=dev
spring.cloud.config.uri=http://localhost:8001/
spring.cloud.config.label=trunk

3.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 :触发后回调的URL

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

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

  • events :触发的事件列表。

events事件类型描述
push仓库有push时触发,默认事件
create当有分支或标签被创建时触发
delete当有分支或标签被删除时触发

svn也有类似的hook机制,每次提交后会触发post-commit脚本,我们可以在这里写一些post请求

这样我们就可以利用hook的机制去触发客户端的更新,但是当客户端越来越多的时候hook支持的已经不够优雅,

另外每次增加客户端都需要改动hook也是不现实的。其实Spring Cloud给了我们更好解决方案,后面文章来介

绍。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com