java框架之SpringCloud(7)-Config分佈式配置中心

前言

分佈式系統面臨的配置問題

微服務意味着要將單體應用中的業務拆分紅一個個子服務,每一個服務的粒度相對較小,所以系統中標會出現大量的服務。因爲每一個服務都須要必要的配置信息才能運行,因此一套集中式的、動態的配置管理設施是必不可少的。咱們每個微服務本身有一個 application.yml 文件,若是有上百個這樣的文件維護起來確定容易讓人崩潰,因此 SpringCloud 提供了 ConfigServer 來解決這個問題。git

SpringCloud Config是什麼

SpringCloud Config 爲微服務架構中的微服務提供集中化的外部配置支持,配置服務器爲各個不一樣微服務應用的全部環境提供了一箇中心化的外部配置github

SpringCloud Config 分爲服務端和客戶端兩部分:web

  • 服務端也成爲分佈式配置中心,它是一個獨立的微服務應用,用來鏈接配置服務器併爲客戶端提供獲取配置信息。
  • 客戶端則是經過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息,配置服務器默認採用 git 來存儲配置信息,這樣有助於對環境配置進行版本管理,而且能夠經過 git 客戶端工具來方便的管理和訪問配置內容。

SpringCloud Config的做用

  • 集中管理配置文件。
  • 不一樣環境不一樣配置,動態化的配置更新,分環境部署好比 dev/test/prod/beta/release。
  • 運行期間動態調整配置,再也不須要在每一個服務部署的機器上編寫配置文件,服務會向配置中心統一拉取配置本身的信息。
  • 當配置發生變更時,服務不須要重啓便可感應到配置的變化並應用新的配置。
  • 將配置信息以 REST 接口的形式暴露。

使用

Config服務端

一、在 GitHub 新建一個名爲 "microservicecloud-config" 的 Repository,提交以下編碼爲 UTF-8 的文件:spring

spring:
  profiles:
    active:
      - dev
---
spring:
  profiles: dev
  application:
    name: microservicecloud-config-dev
---
spring:
  profiles: test
  application:
    name: microservicecloud-config-test
application.yml

二、新建名爲 "microservicecloud-config-3344" 的子工程做爲配置中心服務,依賴以下: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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>zze.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-config-3344</artifactId>

    <dependencies>
        <!-- SpringCloud Config -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <!-- 圖形化監控 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <!-- 熔斷 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <!-- 熱部署插件 -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
pom.xml

三、配置倉庫地址:bootstrap

server:
  port: 3344

spring:
  application:
    name: microservicecloud-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/zze326/microservicecloud-config.git # 對應配置文件的 git 倉庫連接
application.yml

四、編寫主啓動類,使用註解啓用配置中心功能:服務器

package zze.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer // 標識當前服務爲配置中心
public class Application_3344 {

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

五、測試:架構

啓動項目,訪問 http://localhost:3344/application-dev.yml,能夠看到,返回內容爲 gibhub 中存放的配置:

test
HTTP服務具備如下格式的資源:
/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
其中「應用程序」做爲 SpringApplication 中的 spring.config.name 注入(即常規的 Spring Boot 應用程序中一般是「應用程序」),「配置文件」是活動配置文件(或逗號分隔列表的屬性),「label」是可選的 git 標籤(默認爲「master」)。

Config客戶端

一、新建以下配置文件,提交到 GitHub 的配置倉庫中:app

spring:
  profiles:
    active:
      - dev

---
server:
  port: 8201
spring:
  profiles: dev
  application:
    name: microservicecloud-config-client

eureka:
  client:
    service-url:
      defaultZone: http://www.eurekaserver1.com:7001/eureka

---
server:
  port: 8202
spring:
  profiles: test
  application:
    name: microservicecloud-config-client

eureka:
  client:
    service-url:
      defaultZone: http://www.eurekaserver1.com:7001/eureka
microservicecloud-config-client.yml

二、新建名爲 "microservicecloud-config-client-3355" 的子工程做爲使用配置中心的客戶端,依賴以下:maven

<?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">
    <parent>
        <artifactId>microservicecloud</artifactId>
        <groupId>zze.springcloud</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>microservicecloud-config-client-3355</artifactId>
    <dependencies>
        <!-- SpringCloud Config客戶端 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-hystrix</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jetty</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
    </dependencies>

</project>
pom.xml

三、新建系統級配置文件,指定配置中心地址和要讀取環境的配置:

spring:
  cloud:
    config:
      name: microservicecloud-config-client # 須要從 github 上讀取的資源名稱
      profile: dev # 環境,決定讀取哪一個環境的配置內容
      label: master
      uri: http://localhost:3344 # SpringCloud Config Server 地址
bootstrap.yml

四、新建主啓動類:

package zze.springcloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class Application_3355 {
    public static void main(String[] args) {
        SpringApplication.run(Application_3355.class, args);
    }
}
zze.springcloud.Application_3355

五、新建獲取配置信息的 Controller:

package zze.springcloud.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 經過 config 客戶端從 config server 獲取配置信息
 */
@RestController
public class ClientConfigController {

    @Value("${spring.application.name}")
    private String applicationName;

    @Value("${eureka.client.service-url.defaultZone}")
    private String eurekaServers;

    @Value("${server.port}")
    private String port;

    @GetMapping("/config")
    public String getConfig(){
        String str="applicationName = %s <br> eurekaServer = %s <br> port = %s";
        return String.format(str, applicationName, eurekaServers, port);
    }
}
zze.springcloud.controller.ClientConfigController

六、測試:

一、啓動 7001 Eureka Server
二、啓動 3344 配置中心服務
三、啓動 "microservicecloud-config-client-3355" 服務,能夠看到佔用端口爲 github 配置文件中 dev 環境配置下的 8201,訪問 localhost:8201/config:

能夠看到成功獲取並應用了 github 中的配置項
test

「application.yml」是用戶級的資源配置項,而「bootstrap.yml」是系統級的,優先級更高。

SpringCloud 會建立一個「Bootstrap Context」,做爲 Spring 應用的「Application Context」的父級上下文。初始化時,「Bootstrap Context」負責從外部源加載配置屬性並解析配置。這兩個上下文共享一個外部獲取的「Environment」。

「Bootstrap」屬性有高優先級,默認狀況下,它們不會被本地配置覆蓋。「Bootstrap Context」和「Application Context」有着不一樣的約定,因此新增了一個「bootstrap.yml」文件,保證「Bootstrap Context」和「Application Context」配置的分離。

完整示例下載 | 提取碼:3lb2

相關文章
相關標籤/搜索