11.config分佈式配置中心

一、簡述

在這裏插入圖片描述
一個分佈式系統有可能包括非常多微服務,每個微服務都有獨自的配置文件,當系統變更時,有可能需要修改很多服務的配置文件,導致運維繁瑣,容易出問題,所以需要一套集中式的、動態的配置管理設施。spring cloud提供了Config來解決該問題。
個人理解:config配置中心是c/s結構,服務端作用是連接github,客戶端就可以通過連接服務端,獲取到github中的文件,以後每個服務的配置信息都作爲客戶端通過不同的訪問名稱獲取不同的github上的配置信息,這樣就實現了直接修改github上的配置來統一管理配置文件。
至於爲什麼要用到c/s結構,而不是在各個服務直接通過http來獲取github上的文件。我認爲是:一旦github上的路徑有變動,或者修改了密碼,或者直接換了github賬號等,那麼所有服務器都需要修改連接github的配置,會非常麻煩。所以中間加了一個專門連接github的服務,這樣只需要修改一個服務就可以了。

分佈式配置中心對比

二、建立config服務端

1.在git上上傳文件

我是在碼雲上創建配置文件,地址爲:https://gitee.com/saiyaren-mayun/sw-microservice-config.git,又創建demo文件夾,以及microservice-consumer-dev.yml、microservice-consumer-sit.yml。
文件內容就是以前的microservice-consumer服務的配置文件:

server:
  port: 8080

spring:
  application:
    name: microservice-consumer  
eureka:
  instance:
    instance-id: microservice-consumer-feign-hystrix
  register-with-eureka: true
  client:
    service-url:
      defaultZone: http://eureka2001.com:2001/eureka/,http://eureka2002.com:2002/eureka/,http://eureka2003.com:2003/eureka/

feign: 
  hystrix: 
    enabled: true

創建microservice-config-server

pom:

<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">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>com.syr.springcloud</groupId>
    <artifactId>microservice</artifactId>
    <version>0.0.1-SNAPSHOT</version>
  </parent>
  <artifactId>microservice-config-server</artifactId>
  <dependencies>
  	<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-config-server</artifactId>
		</dependency>
  </dependencies>
</project>

application.yml:
這裏我gitee上創建的是私有倉庫,所以需要用戶名和密碼:

server:
  port: 6001
  
spring:
  application:
    name: microservice-config-server
  cloud:
    config:
      server:
        git:
          uri: https://gitee.com/saiyaren-mayun/sw-microservice-config.git
          search-paths:
          - demo
          default-label: master
          
          username: xxxxx
          password: xxxxx
          
eureka:
  client:
    service-url:
      defaultZone: http://eureka2001.com:2001/eureka/,http://eureka2002.com:2002/eureka/,http://eureka2003.com:2003/eureka/
  instance:       
    instance-id: microservice-config-server6001               #服務主機名稱
    prefer-ip-address: true                                   #訪問路徑可以顯示IP地址
 
info: 
  app.name: microservicecloud-provider
  company.name: www.syr.com
  build.artifactId: $project.artifactId$
  build.version: $project.version$     
                    

配置好後,直接在主方法上開啓就可以了:


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

三、建立config客戶端

這裏演示消費者使用config啓動
複製前面的項目microservice-consumer-feign-hystrix8080爲microservice-consumer-config-client。
pom:

<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">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>com.syr.springcloud</groupId>
		<artifactId>microservice</artifactId>
		<version>0.0.1-SNAPSHOT</version>
	</parent>
	<artifactId>microservice-consumer-config-client</artifactId>
	<dependencies>
		<dependency>
			<groupId>com.syr.springcloud</groupId>
			<artifactId>microservice-api</artifactId>
			<version>${project.version}</version>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<!-- 修改後立即生效,熱部署 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>springloaded</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
		</dependency>
		<!-- Ribbon相關 -->
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-eureka</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.cloud</groupId>
			<artifactId>spring-cloud-starter-feign</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-hystrix</artifactId>
		</dependency>
	</dependencies>
</project>

不再使用application.yml文件,而是使用更高一級的bootstrap。yml文件。bootstrap.yml爲系統級的配置文件,application.yml爲用戶級的配置文件,bootstrap.yml的優先級高,服務啓動時會優先加載。bootstrap中主要配置了連接config服務端,獲取指定的配置信息,bootstrap.yml內容爲:

spring:
  cloud:
    config:
      name: microservice-consumer #需要從gitee上讀取的資源名,沒有yml後綴;microservice-consumer-config-client.yml爲已經長傳到gitee的配置文件
      profile: dev
      label: master
      uri: http://127.0.0.1:6001 #config的服務端地址,等config的客戶端和服務端啓動後,客戶端去查找這個指定的config的服務端,通過服務端獲取gitee上配置文件信息

配置完成。

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