在回来的路上看到一个个的都抱着花,吃了一路的狗粮,本来想着去旁边的工业园里跑跑步呢,想一想仍是算了,人家过七夕,俺们过巴西。上一博客学习了Spring Cloud Config使用git做为配置中心,本篇学习下使用svn做为配置中心。html
1、Server 端java
1.准备配置文件git
这里在本地电脑安装了下svn server,并在https://cuiyw/svn/config-repo/config目录下提交了上一博客的3个配置文件。web
2.建立Spring Cloud Config SVN Serverspring
这里建立了Spring Cloud Config Server项目,并在项目中引入spring-cloud-config-server、svnkit,具体pom.xml以下。apache
<?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 http://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>2.1.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <groupId>com.example</groupId> <artifactId>SpringCloudConfigSVNServer</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>war</packaging> <name>SpringCloudConfigSVNServer</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> <spring-cloud.version>Greenwich.SR2</spring-cloud.version> </properties> <dependencies> <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> <!-- https://mvnrepository.com/artifact/org.tmatesoft.svnkit/svnkit --> <dependency> <groupId>org.tmatesoft.svnkit</groupId> <artifactId>svnkit</artifactId> <version>1.9.3</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </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>
3.配置文件浏览器
在application.properties中设置以下配置。须要配置spring.cloud.config.server.default-label= 不然会报No label found for: trunk错误。tomcat
server.port=8003
spring.application.name=spring-cloud-config-server
spring.cloud.config.server.svn.uri=https://cuiyw/svn/config-repo/config/
spring.cloud.config.server.svn.search-paths=
spring.cloud.config.server.svn.username=cuiyw
spring.cloud.config.server.svn.password=123456
spring.profiles.active=subversion
spring.cloud.config.server.default-label=
4.启动类配置app
仍是在main方法类中设置@EnableConfigServer注解。maven
5.测试
启动以后在浏览器中输入http://localhost:8003/neo-config-dev.properties,则显示该属性文件中的内容。
2、Client端
客户若是只是改动spring.cloud.config.uri=http://localhost:8003/,而后修改配置文件的值。将原来dev属性文件的neo.hello=i am dev改成neo.hello=i am dev new,其实仍是不起做用的。Spring Cloud Config分服务端和客户端,服务端负责将git(svn)中存储的配置文件发布成REST接口,客户端能够从服务端REST接口获取配置。但客户端并不能主动感知到配置的变化,从而主动去获取新的配置。客户端如何去主动获取新的配置信息呢,springcloud已经给咱们提供了解决方案,每一个客户端经过POST方法触发各自的/refresh。
1.引入依赖
这里还须要在Client中引入依赖spring-boot-starter-actuator。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
2.开启更新机制
须要给加载变量的类上面加载@RefreshScope,在客户端执行/refresh的时候就会更新此类下面的变量值
package com.example.demo; 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 HelloController { @Value("${neo.hello}") private String hello; @RequestMapping("/hello") public String from() { return this.hello; } }
3.开启暴露端点
management.endpoints.web.exposure.include=*
4.测试
以post请求的方式来访问http://localhost:8002/actuator/refresh就会更新修改后的配置文件。
而后在浏览器中输入http://localhost:8002/hello时则会显示svn最新的属性值。
参考:
https://blog.csdn.net/qq_27385301/article/details/82899303
http://www.ityouknow.com/springcloud/2017/05/23/springcloud-config-svn-refresh.html