微服務:Spring Cloud Config 配置中心

對於傳統的單體應用而言,常使用配置文件來管理所有配置,比如SpringBootapplication.yml文件,但是在微服務架構中全部手動修改的話很麻煩而且不易維護。微服務的配置管理一般有以下需求:
  • 集中配置管理,一個微服務架構中可能有成百上千個微服務,所以集中配置管理是很重要的。
  • 不同環境不同配置,比如數據源配置在不同環境(開發,生產,測試)中是不同的。
  • 運行期間可動態調整。例如,可根據各個微服務的負載情況,動態調整數據源連接池大小等
  • 配置修改後可自動更新。如配置內容發生變化,微服務可以自動更新配置
綜上所述對於微服務架構而言,一套統一的,通用的管理配置機制是不可缺少的總要組成部分。常見的做法就是通過配置服務器進行管理。
 

一、Spring Cloud Config簡介

Spring Cloud Confifig爲分佈式系統中的外部配置提供服務器和客戶端支持。使用Confifig Server,您可以爲所有環境中的應用程序管理其外部屬性。它非常適合spring應用,也可以使用在其他語言的應用上。隨着應用程序通過從開發到測試和生產的部署流程,您可以管理這些環境之間的配置,並確定應用程序具有遷移時需要運行的一切。服務器存儲後端的默認實現使用git,因此它輕鬆支持標籤版本的配置環境,以及可以訪問用於管理內容的各種工具。
 
Spring Cloud Confifig服務端特性:
  • HTTP,爲外部配置提供基於資源的API(鍵值對,或者等價的YAML內容)
  • 屬性值的加密和解密(對稱加密和非對稱加密)
  • 通過使用@EnableConfifigServerSpring boot應用中非常簡單的嵌入。
  • 綁定Confifig服務端,並使用遠程的屬性源初始化Spring環境
  • 屬性值的加密和解密(對稱加密和非對稱加密)

簡單的來說:SpringCloudConfig就是我們配置中心,在單體應用中,配置文件不多,可以很方便處理。但是對於微服務架構來說,每個微服務就有:測試環境、生產環境、線上環境等。而微服務項目又有多個,所以呢,我們可以採用配置中心來進行統一管理。

看一張小圖:

我們的配置文件實際上放在碼雲或者github上,我們搭建一個配置服務拿到遠端的配置,然後發給微服務。

二、搭建一個小項目

在碼雲上搭建一個倉庫,然後放兩個配置文件!!

搭建一個項目:

   <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-config-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
server:
  port: 9999 #服務端口
spring:
  application:
    name: config-server #指定服務名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/Zesystem/config-resposity.git
          username: 碼雲賬號
          password: 碼雲密碼
          skip-ssl-validation: true
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.config.server.EnableConfigServer;

@SpringBootApplication
@EnableConfigServer
public class ConfigServerApplication {

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

直接啓動就可可以了!!獲取到對應的配置信息

微服務配置

 <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.32</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <!--引入EurekaClient-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>

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

bootstrap.xml

spring:
  cloud:
    config:
      name: application #應用名稱,需要對應git中配置文件名稱的前半部分
      profile: pro #開發環境
      label: master #git中的分支
      uri: http://localhost:9999 #config-server的請求地址

啓動程序(還有註冊中心哦):

這樣是可以訪問的。說明配置成功!!!

但是這樣依然無法解決這樣一個問題:我們在碼雲上面更改配置文件信息,項目不會立即更新,需要重新啓動項目,這樣非常不友好!!我們看怎麼來解決。

三、配置手動刷新

造成不能立即更新的原因是微服務有緩存,當你更改之後不會再次請求git拿取最新值。

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

修改配置服務(開放請求刷新端點)

management:
  endpoints:
    web:
      exposure:
        include: refresh

在git上修改完配置文件之後,需要先訪問localhost:9001/actuator/refresh。這樣配置就刷新了,可以訪問最新的數據。

四、高可用

在之前的代碼中,客戶端都是直接調用配置中心的server端來獲取配置文件信息。這樣就存在了一個問題,客戶端和服務端的耦合性太高,如果server端要做集羣,客戶端只能通過原始的方式來路由,server端改變IP地址的時候,客戶端也需要修改配置,不符合springcloud服務治理的理念。springcloud提供了這樣的解決方案,我們只需要將server端當做一個服務註冊到eureka中,client端去eureka中去獲取配置中心server端的服務既可。

我們可以構建多個config-server,將信息放到註冊中心,然後微服務從註冊中心中獲取,而不是從config-server中直接獲取

config-service

   <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>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

修改配置註冊到註冊中心

eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:9000/eureka/
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #spring.cloud.client.ip-address:獲取ip地址

 修改配置微服務信息

測試是沒問題的,大家可以看一下項目源代碼就好!

五、連接消息總線

消息總線bus
在微服務架構中,通常會使用輕量級的消息代理來構建一個共用的消息主題來連接各個微服務實例,它廣播的消息會被所有在註冊中心的微服務實例監聽和消費,也稱消息總線。SpringCloud中也有對應的解決方案,SpringCloud Bus 將分佈式的節點用輕量的消息代理連接起來,可以很容易搭建消息總線,配合SpringCloud confifig 實現微服務應用配置信息的動態更新。

根據此圖我們可以看出利用Spring Cloud Bus做配置更新的步驟:
  • 提交代碼觸發post請求給bus/refresh
  • server端接收到請求併發送給Spring Cloud Bus
  • Spring Cloud bus接到消息並通知給其它客戶端
  • 其它客戶端接收到通知,請求Server端獲取最新配置
  • 全部客戶端均獲取到最新的配置
消息總線整合:
配置中心配置
 <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-bus</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

 

server:
  port: 9999 #服務端口
spring:
  application:
    name: config-server #指定服務名
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/Zesystem/config-resposity.git
          username:
          password:
          skip-ssl-validation: true
  rabbitmq:
    host: 127.0.0.1
    port: 5672
    username: user
    password: password
management:
  endpoints:
    web:
      exposure:
        include: refresh
eureka:
  client:
    serviceUrl:
      defaultZone: http://127.0.0.1:9000/eureka/
  instance:
    preferIpAddress: true
    instance-id: ${spring.cloud.client.ip-address}:${server.port} #spring.cloud.client.ip-address:獲取ip地址

boostrap.xml配置: 

spring:
  cloud:
    config:
      name: application #應用名稱,需要對應git中配置文件名稱的前半部分
      profile: pro #開發環境
      label: master #git中的分支
#      uri: http://localhost:9999 #config-server的請求地址
      #通過註冊中心獲取config-server配置
      discovery:
        enabled: true #開啓服務發現
        service-id: config-server
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9000/eureka/
  instance:
    prefer-ip-address: true #使用ip地址註冊
    instance-id: ${spring.cloud.client.ip-address}:${server.port}

碼雲上面的配置修改:

 

源碼:[email protected]:Zesystem/SpringCloudConfig.git

 

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