SpringCloudConfig相關配置簡介、使用、整合Eureka


目的:

  1、SpringCloud Config簡介

  2、Config Server基本使用

  3、Config Client基本使用

  4、Config整合Eureka

  5、Config配置搜索路徑

 


SpringCloud Config簡介

 SpringCloud Config為微服務架構中的微服務提供集中化的外部配置支持,配置服務器為各個不同微服務應用的所有環境提供了一個中心化的外部配置。

 SpringCloud Config分為服務端和客戶端兩部分。
 服務端也稱為分布式配置中心,它是一個獨立的微服務應用,用來連接配置服務器並為客戶端提供獲取配置信息,加密/解密信息等訪問接口。
 客戶端則是通過指定的配置中心來管理應用資源,以及與業務相關的配置內容,並在啟動的時候從配置中心獲取和加載配置信息,配置服務器默認采用git來存儲配置信息,這樣就有助於對環境配置進行版本管理,並且可以通過git客戶端工具來方便的管理和訪問配置內容。
用途:
 集中管理配置文件。
 不同環境不同配置,動態化的配置更新,分環境部署比如dev/test/prod/beta/release。
 運行期間動態調整配置,不再需要在每個服務部署的機器上編寫配置文件,服務會向配置中心。
 統一拉取配置自己的信息。
 當配置發生變動時,服務不需要重啟即可感知到配置的變化並應用新的配置。
 將配置信息以REST就接口的形式暴露。

 

 

 

 

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

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

如果用上SpringCloud Bus,能實現無需重啟,自動感知配置變化以及應用新配置;


Config Server基本使用

首先第一步,要搞個 configServer來聯通遠程GIT倉庫,來讀取遠程配置;

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

我們這里GitHub演示github官網:https://github.com/

關於github在window系統中使用可以查看博客https://www.cnblogs.com/huangting/p/11684508.html

建個倉庫 microservice-config  然后 Git下載本地

把倉庫克隆到本地倉庫以后把項目中的application.yml文件復制進去在push到github倉庫中,測試yml文件可否上傳到github

其中出現的window轉義問題解決可看我上篇博客:https://www.cnblogs.com/huangting/p/11945774.html

 

成功。那么我們來玩一下Springcloudserver和github倉庫的配置文件交互

首先在ideaul中創建一個Springboot工程,microservice-config-server-4001

然后在pom中添加一個依賴:

 

<?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.ht</groupId>
        <artifactId>htSpringCloud</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>

 

在啟動類ConfigServerApplication_4001中添加注釋@EnableConfigServer

package com.ht.microserviceconfigserver4001;

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

@EnableConfigServer
@SpringBootApplication
public class MicroserviceConfigServer4001Application {

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

}

然后配置yml文件,給它添加一段集合git的url (記住訪問git的http地址而不是ssh地址)

server:
  port: 4001

spring:
  application:
    name:  microservice-config
  cloud:
    config:
      server:
        git:
         uri: https://github.com/haungting/htmicroservice-config.git 
//從git你的倉庫里面復制下來的http地址,url變化因人而異,這樣git才可以請求

單單是這樣還不行,我們還要配置本地映射:

找到本機C:\Windows\System32\drivers\etc 地址下的host文件

加以下配置:

127.0.0.1 configserver.ht.com

 

 

 

 

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

 返回結果了正確的文本結果;

 

請求路徑,也有相應的匹配規則:

The HTTP service has resources in the form:

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

/{application}-{profile}.yml

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

/{application}-{profile}.properties

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

 

詳細可參考:https://www.cnblogs.com/hellxz/p/9306507.html


 

Config Client基本使用

  這里我們需要建立Client端去調用server端,然后實現client端獲取遠程git配置信息

因為測試需要所以我們提交三個配置文件到遠程git庫

application.yml:

 

  active: dev 這里設置獲取的文件名

 

 

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

crm-dev.yml:

port:
  777

crm-test.yml:

port:
  888

然后我們新建一個springboot工程  microservice-config-client-5001

 給項目pom文件中添加依賴並且修改父id:

<parent>
        <groupId>com.ht</groupId>
        <artifactId>htSpringCloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>


<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>

因為git在讀取配置文件的之后最先讀取bootstrap.yml,並且bootstrap.yml中存放的都是項目中通用不改變的配置,

我們項目啟動的時候,要調用server config端,獲取配置信息

所以這里要bootstrap.yml配置文件

 

spring:
  application:
    name: application-dev
  cloud:
    config:
      name: crm //做拼接的,所以必須要和git上面的文件開頭名一樣
      uri: http://configserver.ht.com:4001
      profile: test //這里去設置獲取的端口文件名,根據上面的文件這里默認獲取test那么端口就是888
      label: master
      fail-fast: true

 這里給git配置了請求那么也需要配置本地映射否則無法訪問:

找到本機C:\Windows\System32\drivers\etc 地址下的host文件添加配置:

127.0.0.1  client-config.ht.com

我們再來添加一個動態獲取git倉庫里面存放端口的文件類

ConfigClientController

package com.ht.microserviceconfigclient5001.controller;

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

/**
 * 這個類可以根據git中yml文件配置更換去動態獲取端口
 */
@RestController
public class ConfigClientController {
 
    @Value("${port}")
    private String port;
 
    @GetMapping("/getPort")
    public String getPort() {
        return "測試你訪問的yml文件的端口是:【"+port+"】";
    }

}

啟動類不需要添加注釋這里就不貼代碼了

先啟動4001,在啟動5001

然后頁面訪問:

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

因為項目中bootstrap.yml文件中獲取test文件中的端口所以獲取的是888

根據上面push到git倉庫的文件進行訪問測試調用端口

http://configserver.ht.com:4001/application-dev.yml

 通過git倉庫中application.yml文件去調用dev文件的端口

 

http://configserver.ht.com:4001/application-test.yml

 通過git倉庫中application.yml文件去調用dev文件的端口

 

我們進去git倉庫中去修改active獲取的文件為test

 

然后在去訪問

很顯然active環境變成test

 

 

 


Config整合Eureka

   eureka整合config以及服務器提供者整合config

  首先是eureka整合config

  我們先搞個配置文件到git

spring:
  profiles:
    active:
    - dev
---
server:
  port: 2004
  context-path: /
spring:
  profiles: dev
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
---
server:
  port: 2005
  context-path: /
spring:
  profiles: test
eureka:
  instance:
    hostname: localhost
  client:
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

 然后在push到倉庫中去

 

 

新建Springboot工程:microservice-eureka-server-config-2004

添加pom.xml:

<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.ht.com:4001  # 配置configserver地址
      profile: dev  # 級別
      label: master  # 分支 git中 默認master

application.yml

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

啟動類:

 

package com.ht.microserviceeurekaserverconfig2004;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer
public class MicroserviceEurekaServerConfig2004Application {

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

}

 

先啟動 microservice-config-server-4001

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

測試連接

http://localhost:2004/

http://eureka2001.ht.com:2004/

 

 

說明成功讀取遠程Git配置,然后eureka啟動OK

然后我們把服務提供者和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/test?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1007
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka

info:
  groupId: com.ht.htSpringCloud
  artifactId: microservice-student-provider-config-1007
  version: 1.0-SNAPSHOT
  userName: http://ht.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/test?useUnicode=true&characterEncoding=utf8
    username: root
    password: root
  jpa:
    hibernate:
      ddl-auto: update
    show-sql: true

eureka:
  instance:
    hostname: localhost
    appname: microservice-student
    instance-id: microservice-student:1008
    prefer-ip-address: true
  client:
    service-url:
      defaultZone: http://localhost:2004/eureka

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

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

pom.xml:

<?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.ht</groupId>
        <artifactId>htSpringCloud</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <artifactId>microservice-student-provider-config</artifactId>

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

    <dependencies>
        <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.ht</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.ht.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/haungting/htmicroservice-config.git
          search-paths: aaa,bbb

搞2個目錄aaa,bbb 里面分別放3個配置文件 nns1.yml,nns2.yml

根據github的application.yml文件去寫就行啦,差不多就這樣

 

 

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

然后push到遠程倉庫git

啟動:microservice-config-server-4001

在瀏覽器中輸入:http://configserver.ht.com:4001/nns2-test.yml或者http://localhost:4001/nns-test.yml

 

 

謝謝觀看!!


免責聲明!

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



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