Spring Cloud--Config配置中心

前言

Github:https://github.com/yihonglei/SpringCloud-Study

config服務端:config-server

config客戶端:config-client

config配置中心:

https://github.com/yihonglei/lanhuigu-cloud-config(文件路徑)

https://github.com/yihonglei/lanhuigu-cloud-config.git(git地址,公開路徑,無需權限訪問)

一 config概述

Spring Cloud Config爲分佈式系統中的外部配置提供服務器和客戶端支持。

將你的配置放在遠程,然後本地從遠程獲取配置。示意圖:

Config服務端特性:

  • HTTP,爲外部配置提供基於資源的API(鍵值對,或者等價的YAML內容);
  • 屬性值的加密和解密(對稱加密和非對稱加密);
  • 通過使用@EnableConfigServer在Spring boot應用中非常簡單的嵌入;

Config客戶端的特性(特指Spring應用):

  • 綁定Config服務端,並使用遠程的屬性源初始化Spring環境;
  • 屬性值的加密和解密(對稱加密和非對稱加密);

二 config-server(config服務端)

從遠程地址拉取配置文件。

1、項目結構

2、pom.xml

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

3、application.yml

server:
  port: 9004
spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          # git爲public權限,可以不加用戶名、密碼訪問
          uri: https://github.com/yihonglei/lanhuigu-cloud-config.git
          username:
          password:

4、ConfigServerApplication(啓動類)

package com.lanhuigu.config.server;

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

/**
 * config服務端,負責拿到遠端地址內容,比如git
 *
 * @auther: yihonglei
 * @date: 2019-07-15 17:24
 */
@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigServerApplication.class, args);
    }
}

@EnableConfigServer啓動config server註解。

5、獲取配置

1)配置中心內容

2)獲取application-dev.yml配置內容

http://localhost:9004/master/application-dev.yml

可以通過http://localhost:9004/master/application-test.yml獲取測試配置內容。

訪問配置中心文件規則如下:

通過{application}-{profiles}.yml訪問,比如:http://localhost:9004/application-test.yml

通過/{application}/{profiles}/{label}訪問,比如:http://localhost:9004/application/test/master

通過/{label}/{application}-{profiles}.yml訪問,比如:http://localhost:9004/master/application-test.yml

如果是properties文件,通過/{application}-{profile}.properties或/{label}/{application}-{profile}.properties訪問。

其中{application}表示最後後一個“-”之前的所有內容,比如order-server-dev.yml,則{application}爲order-server,

{profiles}爲dev。

三 config-client(config客戶端)

1、項目結構

2、pom.xml

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

3、application.yml

spring:
  application:
    name: config-client
server:
  port: 8001

config-client工程配置spring.application.name=config-client,server.port=8001。 

4、bootstrap.yml

# config-client從config-server獲取配置,config-server從遠程git獲取配置
# 將獲取的配置,替換掉config-client工程中的application.yml配置內容。
# 配置會組成請求config-server地址,獲取內容ConfigClient.yml裏面test內容:
# http://localhost:9004/master/order-dev.yml
spring:
  application:
    name: order
  cloud:
    config:
      uri: http://localhost:9004
      profile: dev
      label: master

spring.application.name=order,order爲訪問路徑的{application};

spring.cloud.config.uri配置config-server端服務地址;

spring.cloud.config.profile=dev,dev爲訪問路徑的{profiles};

spring.cloud.config.label=master,master爲訪問路徑的{label};

config-client會根據請求地址去服務端獲取配置並替換掉config-client工程的application.yml裏面的配置。

5、ConfigController(測試控制層)

package com.lanhuigu.config.client.controller;

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

import java.util.HashMap;
import java.util.Map;

/**
 * 從配置中心獲取配置。
 * config-client-->config-server-->git
 *
 * @author yihonglei
 * @date 2019-07-15 17:58
 */
@RestController
public class ConfigController {

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

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

    @RequestMapping("/getConfigRemote")
    public Map<String, Object> getConfig4Remote() {
        Map<String, Object> retMap = new HashMap<>();
        retMap.put("applicationName", applicationName);
        retMap.put("port", serverPort);
        return retMap;
    }
}

6、ConfigClientApplication(啓動類)

package com.lanhuigu.config.client;

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

/**
 * config客戶端,負責從config服務端獲取內容
 *
 * @auther: yihonglei
 * @date: 2019-07-15 17:57
 */
@SpringBootApplication
public class ConfigClientApplication {
    public static void main(String[] args) {
        SpringApplication.run(ConfigClientApplication.class, args);
    }
}

7、服務訪問和分析

1)啓動服務,訪問ConfigController

請求地址:http://localhost:9005/getConfigRemote

2)邏輯分析

config-client的application.yml配置的端口是8001,理論上我們訪問config-client應該也要用8001,

實際上使用的是9005,這個9005是根據bootstrap.yml配置從config-server端獲取order-dev.yml文件,

並將內容覆蓋掉application.yml中相同屬性的值,所以,config-client用的是配置中心獲取的值。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章