1. 程式人生 > 實用技巧 >SpringCloud-7-Config

SpringCloud-7-Config

Spring Cloud Config

目錄

1. 概述

  • Spring Cloud Config 為微服務架構中的微服務提供集中化的外部配置支援, 配置伺服器為各個不同微服務應用的所有環節提供了一個中心化的外部配置

Spring Cloud Config分為服務端客戶端兩部分

2. 服務端配置

1. 匯入依賴

<?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>SpringCloud</artifactId>
        <groupId>com.wang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringCloud-Config-server-3344</artifactId>

    <dependencies>
        <!--Web-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Spring Cloud Config Server-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>

</project>

主要是web和config server的依賴

2. 配置檔案

server:
  port: 3344
spring:
  application:
    name: springcloud-config-server
  #連線遠端倉庫
  #通過config-server, 可以訪問其中的資源以及配置
  cloud:
    config:
      server:
        git:
          uri: https://github.com/hello-world-cn/Spring-Cloud-Config.git
          default-label: main

注意

  • 預設的label為master
  • Github現在master改為了main, 需要修改 default-label 屬性, 否則讀不到

3. 配置主啟動類

package com.wang.sringcloud;

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

@SpringBootApplication
//開啟Config Server
@EnableConfigServer
public class Config_Server_3344 {
    public static void main(String[] args) {
        SpringApplication.run(Config_Server_3344.class, args);
    }
}

注意

  • 利用註解 @EnableConfigServer 開啟Config Server服務

3. 客戶端配置

1. 匯入依賴

<?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>SpringCloud</artifactId>
        <groupId>com.wang</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>SpringCloud-Config-Client-3355</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--Spring Cloud Client Config-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
            <version>2.1.1.RELEASE</version>
        </dependency>
    </dependencies>


</project>

注意

  • 客戶端這裡是 spring-cloud-starter-config

2. 配置檔案

  • bootstrap.yaml
# 系統級別的配置, 防止與遠端衝突, 這裡級別最高
spring:
  cloud:
    config:
      uri: http://localhost:3344  #連線到本地的Config Server, 通過服務端連線到github
      name: config-client #需要從git上讀取的資源名稱, 不需要字尾
      profile: test
      label: main
  • application.yaml
# 使用者級別的配置
spring:
  application:
    name: SpringCloud-Config-Client-3355

注意

  • 這裡我們配置了兩個配置檔案, 他們都可以被SpringBoot識別到, 區別是優先順序不同
  • 我們在bootstrap中配置springcloud client 的配置, 防止遠端配置與我們衝突. 這樣無論如何都是本地的配置優先順序最高
  • uri, name, profile, label 四個要素缺一不可

3. 配置主啟動類

package com.wang.springcloud;

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

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

主啟動類不需要任何除了SpringBoot以外的註解, 因為客戶端是連線本地的服務端!

4. 配置測試用的controller

方便測試, 我們配置了controller

package com.wang.springcloud.controller;

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

@RestController
public class ConfigClientController {

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

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

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

    @RequestMapping("/config")
    public String getConfig() {
        return "applicationName: " + applicationName + "\n" +
                "eurekaServer: " + eurekaServer + "\n" +
                "port: " + port;
    }
}

注意

  • 由於我們在遠端的配置檔案上已經規定了埠, 客戶端只是中轉給服務端去訪問的, 因此我們要訪問的埠號與遠端配置一致!

  • @Value("${}") ==> 用於獲取配置檔案中的屬性值, 這裡讀的是遠端配置檔案中的值

5. 實際運用

  • 先啟動Server
  • 本地的配置只需要寫bootstrap讀取遠端的配置即可
  • 本地配置利用Server提供的uri跳轉, 不要亂寫!
  • 實現了配置和服務的解耦, 方便運維除錯!