SpringCloud2.0 Config 分布式配置中心 基礎教程(十一)


Spring Cloud Config 簡介

Spring Cloud Config為分布式系統中的外部化配置提供服務器和客戶端支持。使用Config Server,您可以在所有環境中管理應用程序的外部屬性。客戶端和服務器上的概念映射與Spring Environment和PropertySource抽象,因此它們非常適合Spring應用程序,但可以與任何語言運行的任何應用程序一起使用。當應用程序通過部署管道從開發到測試並進入生產時,您可以管理這些環境之間的配置,並確保應用程序具有遷移時需要運行的所有內容。服務器存儲后端的默認實現使用git,因此它可以輕松支持配置環境的標記版本,以及可用於管理內容的各種工具。添加替代實現並使用Spring配置插入它們很容易。

Spring Cloud Config 特點

Spring Cloud Config Server 功能:

  • 用於外部配置的HTTP,基於資源的API(名稱 - 值對或等效的YAML內容)
  • 加密和解密屬性值(對稱或非對稱)
  • 使用可輕松嵌入Spring Boot應用程序 @EnableConfigServer

Spring Cloud Config Client 功能:

  • 綁定到Config Server並Environment使用遠程屬性源初始化Spring
  • 加密和解密屬性值(對稱或非對稱)

實踐操作

1、創建【配置中心服務端】,即 Spring Cloud Server

1.1、新建 Spring Boot 工程,工程名稱:springcloud-config-server

1.2、工程 pom.xml 文件添加如下依賴:

<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>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

1.3、在工程啟動類中,添加注解 @EnableConfigServer

package com.miniooc.configserver;

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

/**
 * ConfigServerApplication
 *
 * @author 宋陸
 * @version 1.0.0
 */
@EnableConfigServer
@SpringBootApplication
public class ConfigServerApplication {

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

}

1.4、新建工程配置文件 application.yml ,配置內容(加載本地配置方案):

server:
  port: 13801

spring:
  application:
    name: config-server
  profiles:
    active: native # 加載本地配置
  cloud:
    config:
      server:
        native:
          # 不指定路徑的話,默認搜索 resources 目錄
          search-locations: C:/openspace/springcloud-config-server/config/

eureka:
  instance:
    hostname: localhost
    # 表示eureka client間隔多久去拉取服務注冊信息,默認為30秒,如果要迅速獲取服務注冊狀態,可以縮小該值
    lease-renewal-interval-in-seconds: 15
    # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超時時間,在這個時間內若沒收到下一次心跳,則將移除該instance。
    # 默認為90秒
    # 如果該值太大,則很可能將流量轉發過去的時候,該instance已經不存活了。
    # 如果該值設置太小了,則instance則很可能因為臨時的網絡抖動而被摘除掉。
    # 該值至少應該大於 leaseRenewalIntervalInSeconds
    lease-expiration-duration-in-seconds: 45
  client:
    serviceUrl:
      defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/

上述配置內容,為Config Server加載【本地配置】方案

2、創建【配置中心客戶端】,即 Spring Cloud Client

2.1、新建 Spring Boot 工程,工程名稱:springcloud-config-client

2.2、工程 pom.xml 文件添加如下依賴:

<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>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

2.3、新建工程配置文件 bootstrap.yml (注:不是application.yml),配置內容:

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      uri: http://localhost:13801

2.4、新建工程從服務端請求的配置文件 config-client-dev.yml

客戶端從服務端獲取資源配置的路徑規則如下:

  • /{application}/{profile}[/{label}]
  • /{application}-{profile}.yml
  • /{label}/{application}-{profile}.yml
  • /{application}-{profile}.properties
  • /{label}/{application}-{profile}.properties

本例,使用的是第二條規則,命名文件。

配置內容:

server:
  port: 52601

spring:
  application:
    name: config-client

eureka:
  instance:
    hostname: localhost
    # 表示eureka client間隔多久去拉取服務注冊信息,默認為30秒,如果要迅速獲取服務注冊狀態,可以縮小該值
    lease-renewal-interval-in-seconds: 15
    # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超時時間,在這個時間內若沒收到下一次心跳,則將移除該instance。
    # 默認為90秒
    # 如果該值太大,則很可能將流量轉發過去的時候,該instance已經不存活了。
    # 如果該值設置太小了,則instance則很可能因為臨時的網絡抖動而被摘除掉。
    # 該值至少應該大於 leaseRenewalIntervalInSeconds
    lease-expiration-duration-in-seconds: 45
  client:
    serviceUrl:
      defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/
# 配置客戶端輸出演示用
info: local-config-client-dev

config-client-dev.yml 文件放置在 1.4 小節 search-locations 指定的目錄 C:/openspace/springcloud-config-server/config/ 中,如果,服務端不配置該參數,就放在默認目錄,即服務端的resources根目錄中。

2.5、創建【配置客戶端】控制器類 EurekaClientController

package com.miniooc.configclient.controller;

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

import javax.servlet.http.HttpServletRequest;

/**
 * ConfigClientController
 *
 * @author 宋陸
 * @version 1.0.0
 */
@RestController
public class ConfigClientController {

    @Value("${info}")
    private String info;

    /**
     * 提供的一個restful服務
     *
     * @return 返回  配置中的info信息
     */
    @RequestMapping("/info")
    public String info() {
        return info;
    }
}

這個controller主要是為了演示是否成功讀取到了【配置服務端】的配置文件。

3、運行演示

3.1、啟動【服務中心】集群,工程名:springcloud-eureka-server

3.2、啟動【配置中心服務端】,工程名:springcloud-config-server

3.3、啟動【配置中心客戶端】,工程名:springcloud-config-client

3.4、打開瀏覽器,訪問配置中心客戶端restful服務,http://localhost:52601/info

客戶端成功從服務端獲取到了資源文件,並進行了輸出。以上的配置中心服務端是從本地加載資源文件發給客戶端的。下面我們改造服務端,讓服務端從遠程git獲取資源文件返回給客戶端。

4、修改【配置中心服務端】,從遠程git加載資源文件

4.1、修改【配置中心服務端】工程配置文件 application.yml ,配置內容(加載遠程git配置方案):

server:
  port: 13801

spring:
  application:
    name: config-server
  cloud:
    config:
      server:
        git:
          # 配置git倉庫的地址
          uri: https://gitee.com/songchuanlu/springcloud-config-repo/
          # git倉庫地址下的相對地址,可以配置多個,用,分割。
          # search-paths: config-repo
          # git倉庫的賬號
          # username:
          # git倉庫的密碼
          # password:

eureka:
  instance:
    hostname: localhost
    # 表示eureka client間隔多久去拉取服務注冊信息,默認為30秒,如果要迅速獲取服務注冊狀態,可以縮小該值
    lease-renewal-interval-in-seconds: 15
    # 表示eureka server至上一次收到client的心跳之后,等待下一次心跳的超時時間,在這個時間內若沒收到下一次心跳,則將移除該instance。
    # 默認為90秒
    # 如果該值太大,則很可能將流量轉發過去的時候,該instance已經不存活了。
    # 如果該值設置太小了,則instance則很可能因為臨時的網絡抖動而被摘除掉。
    # 該值至少應該大於 leaseRenewalIntervalInSeconds
    lease-expiration-duration-in-seconds: 45
  client:
    serviceUrl:
      defaultZone: http://localhost:9527/eureka/,http://localhost:9528/eureka/,http://localhost:9529/eureka/

4.2、重新啟動【配置中心服務端】,工程名:springcloud-config-server

4.3、重新啟動【配置中心客戶端】,工程名:springcloud-config-client

4.4、刷新配置中心客戶端restful服務,http://localhost:52601/info

客戶端成功從服務端獲取到了資源文件,並進行了輸出。且輸出的信息是配置中心服務端從遠程git倉庫加載的資源文件。

5、修改【配置中心客戶端】,通過服務名訪問【配置中心服務端】

為了保證配置中心服務端的服務高可用,一般會進行集群配置。那么配置中心客戶端,就要采取訪問服務名的方式,訪問配置中心服務端

5.1、修改【配置中心客戶端】工程配置文件 bootstrap.yml ,配置內容

spring:
  application:
    name: config-client
  cloud:
    config:
      label: master
      profile: dev
      fail-fast: true
      #uri: http://localhost:13801
      discovery:
        enabled: true
        service-id: config-server

5.2、重新啟動【配置中心客戶端】,工程名:springcloud-config-client

5.3、刷新配置中心客戶端restful服務,http://localhost:52601/info


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM