1. 程式人生 > >spring cloud config 用svn做配置檔案倉庫

spring cloud config 用svn做配置檔案倉庫

剛接觸spring cloud 的 ;做了一個svn做配置檔案倉庫demo;

建立一個spring boot工程作為 spring  cloud config  server ;下面是pom.xml檔案;

<?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>1.2.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.honkib.springcloud</groupId>
    <artifactId>spring-cloud-config-server</artifactId>
    <version>1.0-SNAPSHOT</version>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-config</artifactId>
                <version>1.0.4.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>

        <!--表示為web工程-->
        <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.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        
         <!--config server need read svn-->
        <dependency>
            <groupId>org.tmatesoft.svnkit</groupId>
            <artifactId>svnkit</artifactId>
            <version>1.8.10</version>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

configserver程式結構:

然後建立一個application.java 類

package com.hobkib.cloud.demo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liaokailin on 16/4/28.
 */

@Configuration
@EnableAutoConfiguration
@EnableConfigServer
public class Application {

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

其中@EnableConfigServer 為作為配置檔案服務的註解;

appliction.yml配置檔案:

server:
  port: 8888

spring:
  cloud:
    config:
      enabled: true
      server:
        svn:
          uri: http://192.168.0.66/svn/cloudconfig/
          username: chenhua
          password: 123456
        #git:
        #  uri: https://github.com/pcf-guides/configuration-server-config-repo
        default-label: config
  profiles:
    active: subversion

logging:
  levels:
    org.springframework.boot.env.PropertySourcesLoader: TRACE
    org.springframework.cloud.config.server: DEBUG


建立一個svn倉庫作為spring cloud 的配置檔案庫;


這張圖片中配置檔案的結構寫錯了;

配置檔案的結構應該是在客戶端的 配置檔案appliction.preperties檔案中的兩個欄位;(spring.cloud.config.name  和  spring.cloud.config.profile)

       要符合{spring.cloud.config.name}-{spring.cloud.config.profile}.preperties 或者字尾為.yml的配置檔案;

建立svn庫後一定要建立一個資料夾;(必須的),不知道是是不是讀取庫的問題;預設情況下,他會去這你提供的svn庫uri中的trunk資料夾下讀取(可以配置這個資料夾的名稱。配置成空預設去trunk資料夾查詢配置檔案),配置檔案不能放到svn的根目錄下(預設去trunk的資料夾下找配置檔案);

客戶端工程:

客戶端的工程結構:

客戶端的配置檔案:

<?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>

    <groupId>com.honkib.springcloud</groupId>
    <artifactId>spring-cloud-config-client</artifactId>
    <version>1.0-SNAPSHOT</version>


    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.3.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    


    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-starter-parent</artifactId>
                <version>1.0.1.RELEASE</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</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>
        
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
        
    </dependencies>
    <repositories>
        <repository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-milestones</id>
            <name>Spring Milestones</name>
            <url>http://repo.spring.io/milestone</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </pluginRepository>
    </pluginRepositories>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

appliction.java
package com.hobkib.cloud.democlient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by liaokailin on 16/4/28.
 */
@SpringBootApplication
@RestController
public class Application {
    @Value("${mysqldb.datasource.url:World}")
    String bar;

    @RequestMapping("/")
    String hello() {
        return "Hello " + bar + "!";
    }

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

bootstrap.preperties 配置檔案:
server.port=8081
spring.cloud.config.uri=http://localhost:${config.port:8888}
spring.cloud.config.name=cloud-config
spring.cloud.config.profile=${config.profile:dev}

spring.application.name=cloud-simple-service


這樣程式就已經完成了;

接下來就是執行程式了;

先啟動configserver 程式;

然後再啟動client工程;

成功了的話;我們應該能在client程式的日誌中看到這麼一條日誌:

2017-02-16 15:32:54.209  INFO 9836 --- [           main] b.c.PropertySourceBootstrapConfiguration : Located property source: CompositePropertySource [name='configService', propertySources=[MapPropertySource [name='http://192.168.0.66/svn/cloudconfig/config/cloud-config-dev.properties']]]

這就表明已經讀取到日誌了;

接下來只要在@Value("${key}")就可以獲取到配置檔案的值了;

或者好用env.getPreperty來獲取配置檔案資訊;

注:很重要的一點;不知道的是什麼問題;configserver服務端一定要用yml配置檔案,使用preperties配置檔案啟動不成功;不識別;

還有就是注意svn的檔案目錄結構;配置檔名稱結構