SpringCloud 基礎(chǔ)教程(四)-配置中心入門

? ?我的博客:蘭陵笑笑生,歡迎瀏覽博客!

? ?上一章 SpringCloud基礎(chǔ)教程(三)-Eureka進(jìn)階當(dāng)中,我們在對Eureka的有了基本的基礎(chǔ)認(rèn)識之上,深入的了解Eureka高可用集群和其他的生產(chǎn)環(huán)境中用到的一些配置。本章將開始了解分布式環(huán)境下的配置中心。

前言

?為什么需要配置中心,在單體的應(yīng)用中,配置文件就可以很好的解決配置問題。但是在微服務(wù)架構(gòu)模式下,系統(tǒng)不可避免的被拆分了許多個(gè)微服務(wù)組件,如果還是通過以前的方式,配置的工作量會很大。為此,一個(gè)通用的分布式的配置管理中心是必不可少的。Spring Cloud提供了另外一個(gè)組件Spring Cloud Config。

?Spring Cloud Config提供了服務(wù)端和客戶端的支持,基于Spring環(huán)境,能夠無縫的集成Spring,默認(rèn)的實(shí)現(xiàn)是基于Git倉庫。當(dāng)然也支持SVN,本地文件等,甚至自定義實(shí)現(xiàn)。

一 、快速構(gòu)建Config Server配置服務(wù)端

?這里我們使用Git作為配置文件的存儲倉庫,首先建立Maven項(xiàng)目,并在Pom.xml文件中引入相關(guān)依賴。

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

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

?創(chuàng)建ConfigServerApplicaition.java 類,使用@EnableConfigServer注解,表示允許該服務(wù)以HTTP形式對外提供配置管理服務(wù):

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

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplicaition {

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

?添加applicaiton.yml,配置如下:

spring:
  application:
    name: single
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/lnxxs/springCloudConfig.git
          password:
          username:
          search-paths: conf
      label: master
  • spring.cloud.config.server.git.uri:配置Git的倉庫位置。
  • spring.cloud.config.server.git.search-paths:配置Git的倉庫路徑下的相對搜索位置。
  • spring.cloud.config.server.git.username:Git的用戶名,如果倉庫是公開的可以不填
  • spring.cloud.config.server.git.password:配置Git用戶密碼。
  • spring.cloud.config.lable:git的分支名稱

?在這之前,事先創(chuàng)建Git倉庫,添加配置文件,master分支添加如下文件和內(nèi)容:

ConfigServer.properties:       k1=master-default-v1

ConfigServer-dev.properties:k1=master-dev-v1

ConfigServer-test.properties:k1=master-test-v1

ConfigServer-pro.properties:k1=master-pro-v1

?在服務(wù)啟動后,可以基于HTTP請求訪問如下的URL進(jìn)行配置信息的獲取,獲取的格式有如下幾種:

  • /{application}/{profile}
  • /{application}/{profile}[/{lable}]
  • /{application}-{profile}.properties
  • /{lable}/{application}-{profile}.properties
  • /{lable}/{application}/{profile}[/{lable}

其中applicaiton是文件的名稱。如

1) http://localhost:6001/ConfigServer-dev.properties

file

2) http://localhost:6001/master/ConfigServer-dev.properties :加載指定分支的屬性:

file

3) http://localhost:6001/ConfigServer/default :顯示默認(rèn)的配置

{
    "name":"ConfigServer",
    "profiles":[
        "default"
    ],
    "label":null,
    "version":"f516174f308769468f1ac683cfeffa803a63c9af",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties",
            "source":{
                "k1":"master-default-v1"
            }
        }
    ]
}

4) http://localhost:6001/ConfigServer/dev :顯示默認(rèn)和dev的信息

{
    "name":"ConfigServer",
    "profiles":[
        "dev"
    ],
    "label":null,
    "version":"f516174f308769468f1ac683cfeffa803a63c9af",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-dev.properties",
            "source":{
                "k1":"master-dev-v1"
            }
        },
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties",
            "source":{
                "k1":"master-default-v1"
            }
        }
    ]
}

5) http://localhost:6001/ConfigServer/test/master 顯示指定的分支的test和默認(rèn)的配置

{
    "name":"ConfigServer",
    "profiles":[
        "test"
    ],
    "label":"master",
    "version":"f516174f308769468f1ac683cfeffa803a63c9af",
    "state":null,
    "propertySources":[
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer-test.properties",
            "source":{
                "k1":"master-test-v1"
            }
        },
        {
            "name":"https://gitee.com/lnxxs/springCloudConfig.git/ConfigServer.properties",
            "source":{
                "k1":"master-default-v1"
            }
        }
    ]
}

?如果通過以上的URL獲取到Git倉庫的配置信息,說明配置服務(wù)端正常。

二 、構(gòu)建Config Client配置客戶端

?接下來我們創(chuàng)建一個(gè)SpringBoot應(yīng)用作為客戶端來讀取Config Server中提供的配置。(我們開發(fā)的任何一個(gè)微服務(wù)組件都可以認(rèn)為是一個(gè)Config Client客戶端。)

?新建ConfigClientApplication,java啟動類,并在項(xiàng)目文件pom.xml中添加依賴:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--web依賴-->
        <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>

?添加bootstrap.yml,注意這些配置必須是在bootstrap.yml中,配置才能生效,如果配置在applicaiton.yml中是沒有任何效果的。

spring:
  application:
    name: server-client
  cloud:
    config:
        label: master
        name: ConfigServer
        profile: test
        uri: http://localhost:6001/
        

  • spring.cloud.config.label:指定分支
  • spring.cloud.config.name:指定配置文件的名稱,我的git倉庫的配置文件的名稱為ConfigServer
  • spring.cloud.config.profile:指定激活那個(gè)配置文件
  • spring.cloud.config.uri:指定配置中心的url,所有的配置信息都是從配置中心獲取。

?創(chuàng)建ConfigClientApplication.java啟動類,并添加EnableAutoConfiguration注解,表示自動獲取項(xiàng)目中的配置變量。

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

?創(chuàng)建控制器,測試獲取配置信息:

@RestController
public class ValueController {
    @Value("${k1}")
     String value;
    @GetMapping("/get")
    public String getValue(){
        return value;
    }
}

?調(diào)用接口測試,正確的獲取到了配置中心的信息:

file

三、其他配置

3.1 客戶端快速失敗

?在無法連接Config Server的時(shí)候,我們有時(shí)候要求客戶端需要啟動失敗,我們可以通過配置bootstrap配置項(xiàng):

spring:
  cloud:
    config:
        fail-fast: true
        retry:
          initial-interval: 1000
          max-attempts: 6
          max-interval: 2000
          multiplier: 1.1

3.2 客戶端重試

?如果我們要求客戶端組件在Config Server不可用是,讓客戶端重試,那么我們也可以通過設(shè)置:

spring:
  cloud:
    config:
        fail-fast: false

?同時(shí)在客戶端的配置文件中引入spring-retry和aop依賴:

         <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
            <version>1.2.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

3.3 HTTP權(quán)限

?如果需要對Config Server的http請求進(jìn)行賬號密碼控制,在服務(wù)端配置:

spring:
  application:
    name: single
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/lnxxs/springCloudConfig.git
          password:
          username:
          search-paths: conf
      label: master
      username: 
      password: 
      uri: http://localhost:6001/
      enabled: true
  security:
    user:
      name: user
      password: pwd

?注意用戶名、密碼的屬性是spring.security.user.name和spring.security.user.password

spring.cloud.config.username和spring.cloud.config.password服務(wù)端配置是沒有什么作用,是客戶端用來配置用的

?并在服務(wù)端的pom文件中引入security依賴:

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

?我們只需要將用戶名和密鑰復(fù)制并配置再每個(gè)客戶端中bootstrap.xml中就可以,客戶端是不需要引入security依賴的。

spring:
  application:
    name: server-client
  cloud:
    config:
        label: master
        profile: test
        uri: http://localhost:6001/
        name: ConfigServer
        username: user
        password: pwd


?在配置HTTP權(quán)限的時(shí)候,如果服務(wù)端在引入security之后,沒有配置用戶名和密碼,那么服務(wù)端項(xiàng)目啟動時(shí),就會隨機(jī)生成一個(gè)全局的密鑰。這里我們不使用隨機(jī)的密碼。如果配置了用戶名和密碼,那么客戶端必須同時(shí)配置一樣的用戶名和加密的密鑰。

四、總結(jié):

?這一篇文章簡單的介紹了Spring Cloud ConfIf組件的使用,并結(jié)合git倉庫搭建了分布式配置中心,實(shí)現(xiàn)了程序和配置的隔離,解耦編碼與環(huán)境之間的耦合,這是非常重要的,當(dāng)然這樣的配置還是存在確定,在實(shí)際的生成環(huán)境中,所有的服務(wù)配置都依賴于配置中心,那么如果配置中心出現(xiàn)宕機(jī)該怎么辦,下一章我們將繼續(xù)了解分布式配置中心的集群搭建.

?以上就是本期的分享,你可以關(guān)注本博客的#Spring Cloud基礎(chǔ)教程!#

? 還可以關(guān)注公眾號: 程序員笑笑生,關(guān)注更多精彩內(nèi)容!

  • file

本文由博客一文多發(fā)平臺 OpenWrite 發(fā)布!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容