springCloud學習-分佈式配置中心(Spring Cloud Config)

一、簡介git

  Spring Cloud Config :分佈式配置中心,方便服務配置文件統一管理,它支持配置服務放在配置服務的內存中(即本地),也支持放在遠程Git倉庫中。在spring cloud config 組件中,分兩個角色,一是config server,二是config client。github

二、config server從本地讀取配置文件web

  2.一、在父工程的pom.xml中的 <modules>節點下 添加以下代碼spring

     <module>config-server</module>
        <module>config-client</module>

  2.二、建立config-server工程,pom.xml文件以下apache

<?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.lishun</groupId>
    <artifactId>config-server</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-server</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.lishun</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


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


</project>

  2.三、在程序的入口Application類加上@EnableConfigServer註解開啓配置服務器的功能,代碼以下:bootstrap

@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

  2.四、application.properties配置文件,由於配置文件內容較多,感受yml格式配置起來不太直觀,因此使用.properties格式服務器

spring.application.name=config-server
server.port=8888

spring.cloud.config.server.native.search-locations= classpath:/shared
spring.profiles.active=native

  spring.profiles.active = native指定從本地讀取配置,讀取的路徑爲classpath下的shared目錄  app

  2.五、在工程的 Resources 目錄下建個 shared 文件夾,用於存放本地配置文。在 shared 目錄下,新建 config-client-dev.properties文件,內容以下maven

id  = config-test-native
name = study-cloud 

三、構建config-client分佈式

  3.一、建立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>

    <groupId>com.lishun</groupId>
    <artifactId>config-client</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <name>config-client</name>
    <description>Demo project for Spring Boot</description>

    <parent>
        <groupId>com.lishun</groupId>
        <artifactId>cloud</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>


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


</project>

  3.二、配置文件bootstrap.properties。

spring.application.name=config-client
spring.cloud.config.uri= http://localhost:8888/
spring.cloud.config.fail-fast=true
spring.profiles.active=dev
server.port=8881

  注意這裏是bootstrap.properties,而不是application.properties。bootstrap相對於application具備優先的執行順序,在配置文件中spring.application.name指明瞭服務名稱,spring.cloud.config.uri指定讀取配置文件的路徑,

  若是沒有讀取成功,執行快速失敗(fail-fast),spring.profiles.active指明讀取的是dev文件,變量 { spring. application.name}和變 spring.profiles.active},二者以「-」相連,構成了向config server讀取的配置文件名

  因此這裏讀取的配置文件是config-client-dev.properties

  3.三、程序的入口類,寫一個API接口「/hi」,返回從配置中心讀取的id變量的值,代碼以下:

@SpringBootApplication
@RestController
public class ConfigClientApplication {

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


    @Value("${id}")
    String id;
    @RequestMapping(value = "/hi")
    public String hi(){
        return id;
    }
}

  3.四、啓動config server 和config client工程,打開網址訪問:http://localhost:8881/hi,網頁顯示:

config-test-native

四、從git遠程倉庫讀取配置文件

  4.一、修改config-server的配置文件application.properties,修改內容以下:

spring.application.name=config-server
server.port=8888

#spring.cloud.config.server.native.search-locations= classpath:/shared
#spring.profiles.active=native

spring.cloud.config.server.git.uri=https://github.com/lis-ylfy/config-test/
spring.cloud.config.server.git.searchPaths=lis
spring.cloud.config.label=master
spring.cloud.config.server.git.username=
spring.cloud.config.server.git.password=
  • spring.cloud.config.server.git.uri:配置git倉庫地址
  • spring.cloud.config.server.git.searchPaths:配置倉庫路徑
  • spring.cloud.config.label:配置倉庫的分支
  • spring.cloud.config.server.git.username:訪問git倉庫的用戶名
  • spring.cloud.config.server.git.password:訪問git倉庫的用戶密碼

  

  若是Git倉庫爲公開倉庫,能夠不填寫用戶名和密碼,若是是私有倉庫須要填寫,本例子是公開倉庫,放心使用。遠程倉庫 https://github.com/lis-ylfy/config-test/ 中有個文件config-client-dev.properties文件內容以下:

id  = config-test
name = study-cloud 

  4.二、重啓config server 和config client工程,打開網址訪問:http://localhost:8881/hi,網頁顯示:

config-test
相關文章
相關標籤/搜索