spring_cloud config 配置中心及利用Github實現自動化熱加載配置 原

    spring_cloud有着強大的生態支持,其自帶的分佈式配置中心可以有效解決分佈式環境中配置不統一的問題,提供一箇中心化的配置中心。並且依靠其spring_bus(rabbitMq提供訂閱)和github或者gitlab自帶的webhook(鉤子函數)可以實現將修改好後的配置push到遠程git地址後,通過訪問配置服務器的endPoints接口地址,便可將配置中心的變化推送到各個集羣服務器中。

    Spring Cloud Config 是用來爲分佈式系統中的基礎設施和微服務應用提供集中化的外部配置支持,它分爲服務端與客戶端兩個部分。其中服務端也稱爲分佈式配置中心,它是一個獨立的微服務應用,用來連接配置倉庫併爲客戶端提供獲取配置信息、加密 / 解密信息等訪問接口;而客戶端則是微服務架構中的各個微服務應用或基礎設施,它們通過指定的配置中心來管理應用資源與業務相關的配置內容,並在啓動的時候從配置中心獲取和加載配置信息。Spring Cloud Config 實現了對服務端和客戶端中環境變量和屬性配置的抽象映射,所以它除了適用於 Spring 構建的應用程序之外,也可以在任何其他語言運行的應用程序中使用。由於 Spring Cloud Config 實現的配置中心默認採用 Git 來存儲配置信息,所以使用 Spring Cloud Config 構建的配置服務器,天然就支持對微服務應用配置信息的版本管理,並且可以通過 Git 客戶端工具來方便的管理和訪問配置內容。當然它也提供了對其他存儲方式的支持,比如:SVN 倉庫、本地化文件系統。

    話不多說,來看代碼:

首先本次採用的spring_cloud版本是:Finchley.RELEASE。spring_boot版本是2.0.3.RELEASE,低版本的spring_cloud並沒有actuator/bus-refresh這個endPoints接口地址,所以使用時要注意

首先是配置中心服務器,需要以下4個引用:

        <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-bus</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-stream-binder-rabbit</artifactId>
        </dependency>

其次是配置文件:

server.port=20000
#服務的git倉庫地址
spring.cloud.config.server.git.uri=https://github.com/narutoform/springCloudConfig
#配置文件所在的目錄
spring.cloud.config.server.git.search-paths=/**
#配置文件所在的分支
spring.cloud.config.label=master
#git倉庫的用戶名
spring.cloud.config.username=narutoform
#git倉庫的密碼
spring.cloud.config.password=*****
spring.application.name=springCloudConfigService
eureka.client.service-url.defaultZone=http://localhost:10000/eureka
eureka.instance.preferIpAddress=true
#rabbitmq
spring.rabbitmq.host=192.168.210.130
spring.rabbitmq.port=5672
spring.rabbitmq.username=guest
spring.rabbitmq.password=guest
spring.rabbitmq.publisher-confirms=true
management.endpoints.web.exposure.include=bus-refresh

其中要注意將bus-refresh接口打開,並且用戶名和密碼只有訪問需要權限的項目是才需要,例如gitlab,但github是不需要的,此外rabbitMq的配置如果不需要配置熱更新是不需要寫的

啓動類:

package cn.chinotan;

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

@SpringBootApplication
@EnableConfigServer
@EnableEurekaClient
@ServletComponentScan
public class StartConfigServerEureka {

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

}

需要將此配置中心註冊到euerka上去

接下來就是配置中心的客戶端配置,本次準備了兩個客戶端,組成集羣進行演示

客戶端需要的引用爲:

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

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

配置文件爲:bootstrap.yml

#開啓配置服務發現
spring.cloud.config.discovery.enabled: true
spring.cloud.config.enabled: true
#配置服務實例名稱
spring.cloud.config.discovery.service-id: springCloudConfigService
#配置文件所在分支
spring.cloud.config.label: master
spring.cloud.config.profile: prod
#配置服務中心
spring.cloud.config.uri: http://localhost:20000/
eureka.client.service-url.defaultZone: http://localhost:10000/eureka
eureka.instance.preferIpAddress: true
management.endpoints.web.exposure.include: bus-refresh

注意配置中心必須寫到bootstrap.yml中,因爲bootstrap.yml要先於application.yml讀取

下面是application.yml配置

server.port: 40000
spring.application.name: springCloudConfigClientOne
#rabbitmq
spring.rabbitmq.host: 192.168.210.130
spring.rabbitmq.port: 5672
spring.rabbitmq.username: guest
spring.rabbitmq.password: guest
spring.rabbitmq.publisher-confirms: true

注意客戶端如果要熱更新也需要引入spring_bus相關配置和rabbitmq相關配置,打開bus-refresh接口才行,客戶端不需要輸入遠程git的地址,只需從剛剛配置好的服務器中讀取就行,連接時需要配置配置服務器的erruka的serverId,本文中是springCloudConfigService,此外還可以指定label(分支)和profile(環境)

在配置中心服務器啓動好後便可以啓動客戶端來讀取服務器取到的配置

客戶端啓動如下:

可以看到客戶端在啓動時會去配置中心服務器去取服務器從遠程git倉庫取到的配置

在客戶端中加入如下代碼,便可以直接讀取遠程配置中心的配置了

package cn.chinotan.controller;

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

@RestController
@RefreshScope
public class ConfigClientController {

    @Value("${key}")
    private String key;

    @GetMapping("/key")
    public String getProfile() {
        return this.key;
    }
}

遠程配置中心結構爲:

 

要注意客戶端需要在你希望改變的配置中加入@RefreshScope才能夠進行配置的熱更新,否則訂閱的客戶端不知道將哪個配置進行更新

此外客戶端訪問的那個地址,也可以get直接訪問,從而判斷配置中心服務器是否正常啓動

通過訪問http://localhost:20000/springCloudConfig/default接口就行

證明配置服務中心可以從遠程程序獲取配置信息。

http請求地址和資源文件映射如下:

/{application}/{profile}[/{label}]
/{application}-{profile}.yml
/{label}/{application}-{profile}.yml
/{application}-{profile}.properties
/{label}/{application}-{profile}.properties
現在我們在客戶端上訪問之前寫的那個controller來得到配置文件中的配置

可見客戶端能夠從服務器拿到遠程配置文件中的信息

其實客戶端在啓動時便會通過spring_boot自帶的restTemplate發起一個GET請求,從而得到服務器的信息,源碼如下:

private Environment getRemoteEnvironment(RestTemplate restTemplate,
			ConfigClientProperties properties, String label, String state) {
		String path = "/{name}/{profile}";
		String name = properties.getName();
		String profile = properties.getProfile();
		String token = properties.getToken();
		int noOfUrls = properties.getUri().length;
		if (noOfUrls > 1) {
			logger.info("Multiple Config Server Urls found listed.");
		}

		Object[] args = new String[] { name, profile };
		if (StringUtils.hasText(label)) {
			if (label.contains("/")) {
				label = label.replace("/", "(_)");
			}
			args = new String[] { name, profile, label };
			path = path + "/{label}";
		}
		ResponseEntity<Environment> response = null;

		for (int i = 0; i < noOfUrls; i++) {
			Credentials credentials = properties.getCredentials(i);
			String uri = credentials.getUri();
			String username = credentials.getUsername();
			String password = credentials.getPassword();

			logger.info("Fetching config from server at : " + uri);

			try {
				HttpHeaders headers = new HttpHeaders();
				addAuthorizationToken(properties, headers, username, password);
				if (StringUtils.hasText(token)) {
					headers.add(TOKEN_HEADER, token);
				}
				if (StringUtils.hasText(state) && properties.isSendState()) {
					headers.add(STATE_HEADER, state);
				}

				final HttpEntity<Void> entity = new HttpEntity<>((Void) null, headers);
				response = restTemplate.exchange(uri + path, HttpMethod.GET, entity,
						Environment.class, args);
			}
			catch (HttpClientErrorException e) {
				if (e.getStatusCode() != HttpStatus.NOT_FOUND) {
					throw e;
				}
			}
			catch (ResourceAccessException e) {
				logger.info("Connect Timeout Exception on Url - " + uri
						+ ". Will be trying the next url if available");
				if (i == noOfUrls - 1)
					throw e;
				else
					continue;
			}

			if (response == null || response.getStatusCode() != HttpStatus.OK) {
				return null;
			}

			Environment result = response.getBody();
			return result;
		}

		return null;
	}

之後,我們試試配置文件熱更新

我們在啓動服務器和客戶端是,會發現,rabbitMq多了一個交換機和幾個隊列,spring_bus正是通過這這個topic交換機來進行變更配置的通知個推送的,效果如下:

在更改遠程配置文件後,調用配置服務器的http://localhost:20000/actuator/bus-refresh接口後:

可以看到,進行了消息傳遞,將變化的結果進行了推送

 

其中調用http://localhost:20000/actuator/bus-refresh是因爲服務器在啓動時暴露出來了這個接口

可以看到這個是一個POST請求,而且其接口在調用之後什麼也不返回,而且低版本spring_cloud中沒有這個接口

這樣是可以實現了客戶端集羣熱更新配置文件,但是還的手動調用http://localhost:20000/actuator/bus-refresh接口,有什麼辦法可以在遠程配置倉庫文件更改後自動進行向客戶端推送呢,答案是通過github或者是gitlab的webhook(鉤子函數)進行,打開gitHub的管理界面可以看到如下信息,點擊add webhook進行添加鉤子函數

由於我沒有公網地址,只能通過內網穿透進行端口映射,使用的是ngrok進行的

 

這樣便可以通過http://chinogo.free.idcfengye.com這個公網域名訪問到我本機的服務了

但是這樣就可以了嗎,還是太年輕

可以看到GitHub在進行post請求的同時默認會在body加上這麼一串載荷(payload)

還沒有取消發送載荷的功能,於是我們的spring boot因爲無法正常反序列化這串載荷而報了400錯誤:

Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: JSON parse error: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `java.lang.String` out of START_ARRAY token

於是自然而然的想到修改body爲空來避免json發生轉換異常,開始修改body,於是去HttpServletRequest中去尋找setInputStream方法,servlet其實爲我們提供了一個HttpServletRequestMapper的包裝類,我們通過繼承該類重寫getInputStream方法返回自己構造的ServletInputStream即可達到修改request中body內容的目的。這裏爲了避免節外生枝我直接返回了一個空的body。
自定義的wrapper類

package cn.chinotan.config;

import javax.servlet.ReadListener;
import javax.servlet.ServletInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.ByteArrayInputStream;
import java.io.IOException;

/**
 * @program: test
 * @description: 過濾webhooks,清空body
 * @author: xingcheng
 * @create: 2018-10-14 17:56
 **/
public class CustometRequestWrapper extends HttpServletRequestWrapper {

    public CustometRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public ServletInputStream getInputStream() throws IOException {
        byte[] bytes = new byte[0];
        ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes);

        return new ServletInputStream() {
            @Override
            public boolean isFinished() {
                return byteArrayInputStream.read() == -1 ? true:false;
            }

            @Override
            public boolean isReady() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {

            }

            @Override
            public int read() throws IOException {
                return byteArrayInputStream.read();
            }
        };
    }
}

實現過濾器

package cn.chinotan.config;

import org.springframework.core.annotation.Order;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @program: test
 * @description: 過濾器
 * @author: xingcheng
 * @create: 2018-10-14 17:59
 **/
@WebFilter(filterName = "bodyFilter", urlPatterns = "/*")
@Order(1)
public class MyFilter implements Filter {
    
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        
    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest httpServletRequest = (HttpServletRequest)servletRequest;

        String url = new String(httpServletRequest.getRequestURI());

        //只過濾/actuator/bus-refresh請求
        if (!url.endsWith("/bus-refresh")) {
            filterChain.doFilter(servletRequest, servletResponse);
            return;
        }

        //使用HttpServletRequest包裝原始請求達到修改post請求中body內容的目的
        CustometRequestWrapper requestWrapper = new CustometRequestWrapper(httpServletRequest);

        filterChain.doFilter(requestWrapper, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

別忘了啓動類加上這個註解:

@ServletComponentScan

這樣便可以進行配置文件遠程修改後,無需啓動客戶端進行熱加載了

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