天天看點

SpringCloud服務配置中心

SpringCloud Config簡介

Spring Cloud Config 是 Spring Cloud 團隊建立的一個全新項目,用來為分布式系統中的基礎設施和微服務應用提供集中化的外部配置支援,它分為服務端與用戶端兩個部分。其中服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配接配置倉庫并為用戶端提供擷取配置資訊、加密 / 解密資訊等通路接口;而用戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置内容,并在啟動的時候從配置中心擷取和加載配置資訊。Spring Cloud Config 實作了對服務端和用戶端中環境變量和屬性配置的抽象映射,是以它除了适用于 Spring 建構的應用程式之外,也可以在任何其他語言運作的應用程式中使用。由于 Spring Cloud Config 實作的配置中心預設采用 Git 來存儲配置資訊,是以使用 Spring Cloud Config 建構的配置伺服器,天然就支援對微服務應用配置資訊的版本管理,并且可以通過 Git 用戶端工具來友善的管理和通路配置内容。當然它也提供了對其他存儲方式的支援,比如:GIT倉庫、SVN 倉庫、本地化檔案系統。

Config Server端主要和Git/SVN伺服器

通俗點,就是統一管理配置,包括友善切換環境配置,以及修改配置無需動代碼,省心省力;

如果用上SpringCloud Bus,能實作無需重新開機,自動感覺配置變化以及應用新配置;

Config Server基本使用

根據前面SpringCloud架構圖,首先第一步,要搞個 configServer來聯通遠端GIT倉庫,來讀取遠端配置;

這裡GIT倉庫,我們一般選用GitHub https://github.com/,或者碼雲  https://gitee.com/  

我們這裡用GitHub示範

建個倉庫 microservice-config  然後 Git下載下傳本地;

上傳一個配置檔案上到git倉庫,application.yml 記住要utf-8編碼,否則亂碼,解析各種問題;

 案例:

建立:microservice-config-server-4001

pom.yml:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lingerqi</groupId>
        <artifactId>testSpringcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-config-server-4001</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>
      

啟動類加上:@EnableConfigServer

倉庫的Http位址:

在application.yml配置下:

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/xyls0030/-microservice-config.git
      

主要是要配置一個git請求位址: 

本地Hosts加個本地域名映射:

127.0.0.1  configserver.lingerqi.com

然後我們請求:http://configserver.lingerqi.com:4001/application-xxx.yml

傳回結果了正确的文本結果;

至于請求路徑,有比對規則:

/{application}/{profile}[/{label}]

/{application}-{profile}.yml

/{label}/{application}-{profile}.yml

/{application}-{profile}.properties

/{label}/{application}-{profile}.properties

Config Client基本使用

根據前面的config原理圖,我們需要建立Client端調用server端,最終實作client端擷取遠端git配置資訊;

送出三個配置檔案到遠端git庫;

application.yml:

---
spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
port: 111
---
spring:
  profiles: test
port: 222
      

crm-dev.yml:

port:
  777
      

crm-test.yml:

port:
  888
      

建立一個module  microservice-config-client-5001

加下依賴:

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

我們項目啟動的時候,就要調用server config端,擷取配置資訊,是以這裡要bootstrap.yml配置檔案,優先級最高:

spring:
  application:
    name: application-dev
  cloud:
    config:
      name: crm
      uri: http://configserver.lingerqi.com:4001
      profile: test
      label: master
      fail-fast: true
      

application.yml

server:
  port: 5001
  context-path: /      

再搞一個 ConfigClientController 類 測試顯示端口:

package com.lingerqi.microserviceconfigclient5001.controller;

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

@RestController
public class ConfigClientController {
 
    @Value("${port}")
    private String port;
 
    @GetMapping("/getPort")
    public String getPort() {
        return "測試你通路的yml檔案的端口是:【"+port+"】";
    }

}
      

本地hosts我們給加配置:

127.0.0.1  client-config.lingerqi.com

http://client-config.lingerqi.com:5001/getPort

即可擷取遠端端口配置資訊;

   

Config整合Eureka

首先是eureka整合config

我們先搞個配置檔案到git;

eureka_config.yml

spring:
  profiles:
    active:
    - dev
---
server:
  port: 2004
  context-path: /
spring:
  profiles: dev
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false     #false 由于該應用為注冊中心,是以設定為false,代表不向注冊中心注冊自己。
    fetch-registry: false     #false 由于注冊中心的職責就是維護服務執行個體,它并不需要去檢索服務,是以也設定為false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設定與Eureka注冊中心互動的位址,查詢服務和注冊服務用到       #設定與Eureka注冊中心互動的位址,查詢服務和注冊服務用到
---
server:
  port: 2005
  context-path: /
spring:
  profiles: test
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false     #false 由于該應用為注冊中心,是以設定為false,代表不向注冊中心注冊自己。
    fetch-registry: false     #false 由于注冊中心的職責就是維護服務執行個體,它并不需要去檢索服務,是以也設定為false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/       #設定與Eureka注冊中心互動的位址,查詢服務和注冊服務用到       #設定與Eureka注冊中心互動的位址,查詢服務和注冊服務用到
      

 建立module: microservice-eureka-server-config

pom.yml

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

bootstrap.yml

spring:
  application:
    name: microservice-eureka-server-config
  cloud:
    config:
      name: eureka_config
      uri: http://configserver.lingerqi.com:4001  # 配置configserver位址
      profile: dev  # 級别
      label: master  # 分支 git中 預設master
      

application.yml:

spring:
  application:
    name: microservice-eureka-server-config      

啟動類加上:@EnableEurekaServer

我們啟動 microservice-config-server-4001

再啟動 microservice-eureka-server-config-2004

測試連接配接

http://localhost:2004/ 或

http://eureka2001.lingerqi.com:2004/

然後我們就是把服務提供者和config整合,把服務提供者注冊到eureka;

我們搞個配置provider_config.yml,push到遠端GIT;

spring:
  profiles:
    active: dev
---
server:
  port: 1007
  context-path: /

# 資料源配置
spring:
  profiles: dev
  application:
    name: microservice-student
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8
    username: mybatis_ssm
    password: xiaoli
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost  #eureka用戶端主機執行個體名稱
    appname: microservice-student  #用戶端服務名
    instance-id: microservice-student:1007 #用戶端執行個體名稱
    prefer-ip-address: true #顯示IP
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka   #把服務注冊到eureka注冊中心

info:
  groupId: com.lingerqi.testSpringcloud
  artifactId: microservice-student-provider-config-1007
  version: 1.0-SNAPSHOT
  userName: http://lingerqi.com
  phone: 123456
---
server:
  port: 1008
  context-path: /

# 資料源配置
spring:
  profiles: test
  application:
    name: microservice-student
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    driver-class-name: com.mysql.jdbc.Driver
    url: jdbc:mysql://localhost:3306/mybatis_ssm?useUnicode=true&characterEncoding=utf8
    username: mybatis_ssm
    password: xiaoli
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost  #eureka用戶端主機執行個體名稱
    appname: microservice-student  #用戶端服務名
    instance-id: microservice-student:1008 #用戶端執行個體名稱
    prefer-ip-address: true #顯示IP
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka   #把服務注冊到eureka注冊中心

info:
  groupId: com.lingerqi.testSpringcloud
  artifactId: microservice-student-provider-config-1008
  version: 1.0-SNAPSHOT
  userName: http://lingerqi.com
  phone: 123456
      

建立module:microservice-student-provider-config-1004

pom.yml:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.lingerqi</groupId>
        <artifactId>testSpringcloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-provider-config</artifactId>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.lingerqi</groupId>
            <artifactId>microservice-common</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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
        </dependency>
        <!--  修改後立即生效,熱部署  -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>springloaded</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>com.lingerqi</groupId>
            <artifactId>microservice-common</artifactId>
            <version>1.0-SNAPSHOT</version>
            <scope>compile</scope>
        </dependency>

        <!--添加注冊中心Eureka相關配置-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>

        <!-- actuator監控引入 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>      

bootstrap.yml:

 spring:
  application:
    name: microservice-student-provider-config
  cloud:
    config:
      name: provider_config
      uri: http://configserver.lingerqi.com:4001  # 配置configserver位址
      profile: dev  # 級别
      label: master  # 分支 git中 預設master      

application.yml

spring:
  application:
    name: microservice-student-provider-config
      

  

說明成功注冊到服務注冊中心了;

Config配置搜尋路徑

前面我們所有的GIT遠端端配置檔案都是跟目錄的,所有請求預設都是根目錄,但是有時候,項目很多,配置檔案需要根據子目錄來劃分,這時候,就需要來配置搜尋路徑了;比如aaa項目的配置檔案放aaa目錄下,bbb項目的配置檔案放bbb目錄下,不配置的話 是找不到的那些配置檔案的,我們需要配置search-paths屬性實作;

microservice-config-server-4001 configserver端 加個配置 

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
          uri: https://github.com/lixiao12/microservice-config.git
          search-paths: aaa,bbb
      

  

分别搞3個目錄aaa,bbb,ccc 裡面分别放3個配置檔案 nns.yml,nns2.yml,nn3.yml;

spring:
  profiles:
    active: dev
---
spring:
  profiles: dev
name: aaadev
---
spring:
  profiles: test
name: aaatest      

然後傳到遠端git;

我們啟動:microservice-config-server-4001

浏覽器:http://configserver.lingerqi.com:4001/nns-test.yml