spring cloud config 整合svn
配置檔案
新建一個資料夾,建立application-test.properties檔案,內容是:
name=default
test=www
提交到svn庫
server端
- 新建spring boot專案, 命名為configServer
- pom.xml 引入兩個包
<!-- 服務端包 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-config-server</artifactId>
</dependency>
<!-- svn依賴包 -->
<dependency>
<groupId>org.tmatesoft.svnkit</groupId>
<artifactId>svnkit</artifactId>
</dependency>
- application.yml
server:
port: 8888
spring:
cloud:
config:
enabled: true
server:
svn:
uri: http://localhost:81/svn/StartKit/cloudConfig
username: mi #svn使用者名稱
password: mi #svn密碼
default-label: config #讀取目錄,預設是trunk
profiles:
active: subversion
logging:
levels:
org.springframework.boot.env.PropertySourcesLoader: TRACE
org.springframework.cloud.config.server: DEBUG
- ServerApplication
@SpringBootApplication
@EnableConfigServer
public class ServerApplication {
public static void main(String[] args) {
new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
}
}
啟動專案,訪問 http://localhost:8888/application/test, 注意命名,application是application-test.properties的字首,test是字尾
請求結果
{"name":"application","profiles":["test"],"label":null,"version":"34","state":null,"propertySources":[{"name":"http://localhost:81/svn/StartKit/cloudConfig/config/application-test.properties","source":{"name":"default","test":"www"}},{"name":"http://localhost:81/svn/StartKit/cloudConfig/config/application.properties","source":{"name":"default","test":"test"}}]}
client端
- pom.xml 新增依賴包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
- bootstrap.properties
server.port=9999
spring.application.name=config-client
#配置規則為{spring.cloud.config.name}-{spring.cloud.config.profile}.properties
#指定配置檔案字首
spring.cloud.config.name=application
#指定配置檔案字尾
spring.cloud.config.profile=test
#分支配置,預設master
#spring.cloud.config.label=master
#配置服務端的地址
spring.cloud.config.uri=http://localhost:8888/
management.security.enabled=false
- ClientApplication
@SpringBootApplication
public class ClientApplication {
public static void main(String[] args) {
SpringApplication.run(ClientApplication.class, args);
}
}
- controller
新建個controller
@RestController
@RefreshScope//此註解,是在訪問/refresh後服務端載入新配置,自動把新配置注入
public class ClientController {
//載入application-test.properties的name屬性注入
@Value("${name}")
private String name;
@RequestMapping("/name")
public String name(){
return name;
}
//載入application-test.properties的test屬性
//因為沒有test屬性,所以載入application.properties的test屬性注入
@Value("${test}")
private String test;
@RequestMapping("/test")
public String test(){
return test;
}
}
- 啟動專案,訪問http://localhost:9999/name
default
www
更新配置檔案
- 修改 application-test.properties內容
name=creditease
test=creditease
提交svn
configServer
不需要更改configClient 新增重新整理方法
@RequestMapping("/refresh1")
public String refresh1(){
HttpUtils.refresh();
return "success";
}
public class HttpUtils {
public static void refresh() {
HttpURLConnection connection =null;
try {
URL url = new URL("http://localhost:9999/refresh");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.connect();//連結
InputStream in=connection.getInputStream();//等待響應
in.close();
} catch (Exception e) {
e.printStackTrace();
}finally {
if(connection!=null){
connection.disconnect();
}
}
}
總結
因為沒有搞通自動配置,在配置hook檔案時在命令列可以實現重新整理,但是在hook檔案沒有觸發,再研究。
相關文章
- spring 整合 spring cloud config 的相關知識SpringCloud
- [Spring Cloud Tutorial翻譯系列二]Spring Cloud Config Server與git整合SpringCloudServerGit
- Spring Cloud(二):Spring Cloud ConfigSpringCloud
- Spring Cloud(九)高可用的分散式配置中心 Spring Cloud Config 整合 Eureka 服務SpringCloud分散式
- 非spring boot (即spring) 使用/整合 Spring cloud Config 分散式配置中心Spring BootCloud分散式
- spring cloud分散式微服務:Spring Cloud ConfigSpringCloud分散式微服務
- Spring Cloud 整合SpringCloud
- Spring Cloud Config 規範SpringCloud
- spring cloud config筆記SpringCloud筆記
- Spring Cloud Config 配置中心SpringCloud
- spring-cloud-config-serverSpringCloudServer
- spring cloud 整合 nacosSpringCloud
- Spring Cloud Config伺服器SpringCloud伺服器
- spring cloud config 原始碼分析SpringCloud原始碼
- spring-cloud-config——Quick StartSpringCloudUI
- RocketMQ 整合 Spring Cloud StreamMQSpringCloud
- Spring Cloud(八)高可用的分散式配置中心 Spring Cloud ConfigSpringCloud分散式
- Spring Cloud實戰系列(六) - 分散式配置中心Spring Cloud ConfigSpringCloud分散式
- Spring Cloud Config 實現配置中心SpringCloud
- Spring Cloud Config-Git後端SpringCloudGit後端
- Spring Cloud Config客戶端使用SpringCloud客戶端
- Spring Cloud專題之五:configSpringCloud
- Spring Cloud Config原碼篇(十)SpringCloud
- Spring Cloud Config應用篇(九)SpringCloud
- Spring Cloud Config—提供自定義RestTemplateSpringCloudREST
- Spring Cloud Config—客戶端使用SpringCloud客戶端
- Spring Boot系列(四) Spring Cloud 之 Config ClientSpring BootCloudclient
- java 教程 Spring Cloud Config環境庫JavaSpringCloud
- Spring Cloud 之 Config與動態路由.SpringCloud路由
- 為Spring Cloud Config插上管理的翅膀SpringCloud
- 介紹一下Spring Cloud ConfigSpringCloud
- spring-cloud-config-server——Environment RepositorySpringCloudServer
- spring cloud微服務分散式雲架構-Spring Cloud Config環境庫SpringCloud微服務分散式架構
- Spring Cloud 學習筆記 ——Spring Cloud Config 請求失敗重試SpringCloud筆記
- (九)整合spring cloud雲服務架構 - commonservice-config配置服務搭建SpringCloud架構
- Spring Cloud雲架構-Config Git後端SpringCloud架構Git後端
- Spring Cloud Config Client 超時與重試SpringCloudclient
- Spring cloud config 使用gitHub或者gitee連線SpringCloudGithubGitee