spring-cloud-config service使用svn做爲遠程的例子

首先是新建項目config-server,在pom文件,加入對應的dependencyweb

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.service.config</groupId>
  <artifactId>service-config</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>service-config Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
  </parent>

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

  <dependencies>
    <dependency>
      <groupId>org.springframework.cloud</groupId>
      <artifactId>spring-cloud-config-server</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
    <dependency>
      <groupId>org.tmatesoft.svnkit</groupId>
      <artifactId>svnkit</artifactId>
    </dependency>
  </dependencies>

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

而後新建Application啓動類,記住要新建包放進去,不然會報錯spring

加上@EnableConfigServer註解apache

/**
 * Created by hpx on 2017/5/12.
 */
@SpringBootApplication
@EnableConfigServer
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }
}

接着就是配置文件:application.yml瀏覽器

spring:
  application:
    name: service-config
  profiles:
    active: subversion
  cloud:
    config:
      server:
        svn:
          uri: https://**.***.***.177/svn/cloud
          default-label: trunk
          username:  username
          password:  password
server:
  port: 8888

接下來看我svn結構app

而後啓動Applicationmaven

打開瀏覽器,輸入http://localhost:8888/service-config/trunk,看到以下內容svn

換個姿式:http://localhost:8888/service-config.ymlspring-boot

約定以下:ui

application:配置url

profile:環境..好比說你看到我有一個-dev文件否?

label:分支(不知道這麼理解對不對)

試試:http://localhost:8888/service-config/dev/trunk

-----------------------------------------------------------------------------------------------------------

接着新建config-client,pom添加以下...

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.service.client</groupId>
  <artifactId>service-client</artifactId>
  <packaging>war</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>service-client Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.5.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
  </parent>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-dependencies</artifactId>
        <version>Brixton.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>

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
  </dependencies>

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

仍是咱們的application.yml(ps:若是出現注入值失敗的狀況,能夠試下把applicaton rename to bootstarp)

spring:
  application:
    name: service-config
  cloud:
    config:
      label: trunk
      profile: dev
      uri:  http://localhost:8888
server:
  port: 8882

仍是Application,標準能啓動就行,而後咱們寫一個Controller

/**
 * Created by hpx on 2017/5/12.
 */
@RestController
@RefreshScope
public class TestController {
    @Value("${myconfig.name}")
    private String name;

    @RequestMapping("/name")
    public String getName() {
        return name;
    }
}

瀏覽器輸入:http://localhost:8882/name

獲得結果~Game Over

相關文章
相關標籤/搜索