spring cloud config 整合svn

weixin_34041003發表於2018-05-03

配置檔案

新建一個資料夾,建立application-test.properties檔案,內容是:

name=default
test=www

提交到svn庫

server端

  1. 新建spring boot專案, 命名為configServer
  2. 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>
  1. 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
  1. ServerApplication
@SpringBootApplication
@EnableConfigServer
public class ServerApplication {
    public static void main(String[] args) {
        new SpringApplicationBuilder(ServerApplication.class).web(true).run(args);
    }
}
  1. 啟動專案,訪問 http://localhost:8888/application/test, 注意命名,application是application-test.properties的字首,test是字尾

  2. 請求結果

{"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端

  1. pom.xml 新增依賴包
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
  1. 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
  1. ClientApplication
@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {
        SpringApplication.run(ClientApplication.class, args);
    }

}
  1. 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;
    }

}
  1. 啟動專案,訪問http://localhost:9999/name
default

訪問http://localhost:9999/test

www

更新配置檔案

  1. 修改 application-test.properties內容
name=creditease
test=creditease

提交svn

  1. configServer
    不需要更改

  2. 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();
            }
        }
    }
  1. 重啟後呼叫http://localhost:9999/refresh1
  2. 重新呼叫http://localhost:9999/namehttp://localhost:9999/test

總結

因為沒有搞通自動配置,在配置hook檔案時在命令列可以實現重新整理,但是在hook檔案沒有觸發,再研究。

相關文章