天天看點

spring cloud config server 配置中心

spring cloud config  配置中心

配置中心分為  server 服務端 client 和用戶端,

服務端搭建

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> <!-- spring boot 繼承 -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.3.5.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <groupId>com.cyy</groupId>
    <artifactId>my-spring-cloud-config-server</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <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> <!-- 添加 config-server 依賴 -->
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>

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

</project>
           

spring boot 啟動類  注意spring boot 的啟動類需要放到某一個包下,不能直接在 src 目錄下

package app;

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

/**
 * Created by Administrator on 2017/8/16.
 */
@SpringBootApplication  
@EnableConfigServer   // 配置中心服務
public class ConfigSreviceApplication {

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

properties 配置

server.port=4555 #git 倉庫位址 spring.cloud.config.server.git.uri=https://github.com/cyyinfo/spring-cloud-config-demo.git
           
#如果配置檔案不在倉庫的根目錄則需要配置此項,為配置檔案所在目錄
spring.cloud.config.server.git.search-paths=config-repository
           

到這裡一個最簡單的配置服務端搭建完成,啟動後通路

http://localhost:4555/spring-cloud-config-sevice-demo/master  可以看到結果

http://localhost:4555/[git 倉庫項目名稱]/[profile 預設master]

client  端搭建

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

    <groupId>com.cyy</groupId>
    <artifactId>my-spring-cloud-config-client</artifactId>
    <version>1.0.0-SNAPSHOT</version>

    <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.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-client</artifactId>
        </dependency>

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

</project>
           

啟動類

package app;

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 Administrator on 2017/8/16.
 */

@RestController
@SpringBootApplication
@RequestMapping("/")
public class ConfigClientApplication {

    @Value("${test.demo.name}")
    private String testDemoName;

    @RequestMapping("")
    public String index(){
        return "config clicent testDemoName="+this.testDemoName+"#";
    }

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

配置檔案  注意是  bootstrap.properties 而不是 application.properties

spring.cloud.config.env=spring-cloud-config-sevice-demo  // git 倉庫名稱
spring.cloud.config.profile=prev  // 配置氮氣環境 決定是加載git倉庫中的 application-prev.propertis haishi
spring.cloud.config.label=master  // git 分支名 
spring.cloud.config.uri=http://localhost:4555   // 配置中心項目通路位址


spring.application.name=my-config-client
server.port=4556
           

完整 demo 位址 

https://github.com/cyyinfo/spring-cloud-config-demo.git

小程式檢視更多java相關面試題

spring cloud config server 配置中心