1. 程式人生 > >(二)Spring Cloud實踐:使用Spring Cloud Config實現分散式配置管理

(二)Spring Cloud實踐:使用Spring Cloud Config實現分散式配置管理

Spring Cloud Config為微服務應用提供了統一的分散式配置管理,將配置檔案放到git上,所有的微服務應用均從git上獲取這些配置檔案。  該種情況下,如果將配置檔案放在第三方提供的版本控制器上,需要網路可訪問,另外,也可以自己搭建gitlab私服,來存放自己的 配置檔案(這種可能更好一些)。

 模組4 configserver

完整的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.news</groupId>
	<artifactId>configserver</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>configserver</name>
	<description>spring config</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<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.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

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


</project>

修改configserver啟動類,添加註釋@EnableConfigServer,表明這是一個Config  Server,添加註釋@EnableEurekaClient 將其註冊到服務中心。具體程式碼如下:

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

/**
 *@Description:
 *@Author: Young
 *@@CreateDate:
 */
@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
public class ConfigserverApplication {

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

修改application.yml配置,具體內容如下:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/  #Eureka Server伺服器
server:
  port: 8090

spring:
  cloud:
    config:
      server:
        git:
          uri: http://192.168.56.109:9999/avatar/config.git #配置git倉庫的地址
          searchPaths: config  #git倉庫地址下的相對地址,可以配置多個,用,分割。
          username: root
          password: centos1234
  application:
    name: config-server

該模組正常啟動前提:git創庫能夠正常訪問,並且searchPaths存在。啟動後,就可以看到該服務註冊到了服務中心。

Config Client端的配置

這裡需要修改一下模組2 eurekaclient,新增spring-cloud-starter-config依賴,完整的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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>com.news</groupId>
	<artifactId>eurekaclient</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>eurekaclient</name>
	<description>學習Spring Cloud微服務框架</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.0.6.RELEASE</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
		<spring-cloud.version>Finchley.SR2</spring-cloud.version>
	</properties>

	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-actuator</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</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-test</artifactId>
			<scope>test</scope>
		</dependency>
	</dependencies>

	<dependencyManagement>
		<dependencies>
			<dependency>
				<groupId>org.springframework.cloud</groupId>
				<artifactId>spring-cloud-dependencies</artifactId>
				<version>${spring-cloud.version}</version>
				<type>pom</type>
				<scope>import</scope>
			</dependency>
		</dependencies>
	</dependencyManagement>

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


</project>

修改配置檔案,Config client需要一個名為bootstrap.yml的配置檔案,用來配置config Server的URL和要訪問的git具體分支。

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8080/eureka/
spring:
  cloud:
    config:
      label: master  #預設git分支
      profile: client-dev #檔名字的後半部分,一般用來區分開發環境和正式環境
      #uri: http://localhost:8090/  #通過uri指定config服務端
      discovery: #分散式環境下自動發現配置服務
         enabled: true
         serviceId: config-server
      name: config #配置檔名字的前半部分,可以自定義

依據bootstrap.yml檔案中的配置,在gitlab上配置檔案config-client-dev.yml的路徑為/avator/config/config-client-dev.yml。

修改HelloController,程式碼如下:

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

/**
 * @program: springlearn
 * @description: 學習EurekaClient
 * @author: Mr.Young
 * @create: 2018-11-14 11:13
 **/
@RestController
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "hello! This is Spring Cloud Eureka Client!";
    }

    @Value("${test}")
    String confighello;

    @RequestMapping(value = "/configtest")
    public String configtest(){
        return confighello;
    }
}

通過@Value可以讀取gitlab伺服器上配置檔案中對應標籤的值。在瀏覽器 輸入:http://localhost:8081/configtest,如下圖所示:

配置自動更新

配置自動更新目的是在更改配置檔案後,不重啟正在執行的微服務的情況下,讓各個微服務感知到配置檔案內容的變化。

在上面的例項中,手動更改config-client-dev.yml檔案內容後,比如將test=config test修改為test=config test 2。 在瀏覽器輸入:http://localhost:8081/configtest,結果並沒有變化。

Spring cloud通過在客戶端提交POST請求,可以重新整理配置內容,讓微服務可以感知到配置檔案中修改的內容。

下面來修改模組2 eurekaclient,讓其能夠獲取到配置檔案內容的更新。

首先,在pom.xml中新增依賴項spring-boot-starter-actuator,該模組提供了一套監控的功能,可監控程式的執行時狀態。

其次,修改HelloController.java檔案,在主類上添加註解@RefreshScope,修改後的檔案內容如下:

package com.news.eurekaclient;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: springlearn
 * @description: 學習EurekaClient
 * @author: Mr.Young
 * @create: 2018-11-14 11:13
 **/
@RestController
@RefreshScope
public class HelloController {
    @RequestMapping("/")
    public String home() {
        return "hello! This is Spring Cloud Eureka Client!";
    }

    @Value("${test}")
    String confighello;

    @RequestMapping(value = "/configtest")
    public String configtest(){
        return confighello;
    }
}

通過postman模擬傳送post 請求:http://localhost:8081/actuator/refresh(查到很多參考資料中,都寫http://localhost:8081/refresh,呵呵),貼個圖吧!

表示變數test的值,已經 重新整理。

哈哈,寫部落格還是有幫助的,之前“配置自動更新”一直沒有調通,又查了一些技術文件,加上猜測,終於可以了。另外,還有人提到通過Webhook自動觸發配置更新, 由於本人也不是專業搞 web開發的,沒有進行實際的使用,感興趣的小夥伴可以試試。

又完成一篇,一天過去了大半, 總結真是比較耗時間!