在回來的路上看到一個個的都抱着花,吃了一路的狗糧,本來想着去旁邊的工業園裏跑跑步呢,想一想仍是算了,人家過七夕,俺們過巴西。上一博客學習了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