springcloud?gateway高級(jí)功能之集成apollo后動(dòng)態(tài)刷新路由方式
springcloud gateway集成apollo后動(dòng)態(tài)刷新路由
springcloud集成apollo后動(dòng)態(tài)刷新路由配置
官網(wǎng)給的demo如下
import com.ctrip.framework.apollo.enums.PropertyChangeType;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
/**
* @author ksewen
* @date 2019/5/175:24 PM
*/
@Configuration
public class GatewayPropertiesRefresher implements ApplicationContextAware,ApplicationEventPublisherAware {
private static final Logger logger = LoggerFactory.getLogger(GatewayPropertiesRefresher.class);
private static final String ID_PATTERN = "spring\\.cloud\\.gateway\\.routes\\[\\d+\\]\\.id";
private static final String DEFAULT_FILTER_PATTERN = "spring\\.cloud\\.gateway\\.default-filters\\[\\d+\\]\\.name";
private ApplicationContext applicationContext;
private ApplicationEventPublisher publisher;
@Autowired
private GatewayProperties gatewayProperties;
@Autowired
private RouteDefinitionWriter routeDefinitionWriter;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
this.publisher = applicationEventPublisher;
}
@ApolloConfigChangeListener(interestedKeyPrefixes = "spring.cloud.gateway.",value="recommend-facade.yml")
public void onChange(ConfigChangeEvent changeEvent) {
refreshGatewayProperties(changeEvent);
}
/***
* 刷新org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator中定義的routes
*
* @param changeEvent
* @return void
* @author ksewen
* @date 2019/5/21 2:13 PM
*/
private void refreshGatewayProperties(ConfigChangeEvent changeEvent) {
logger.info("Refreshing GatewayProperties!");
preDestroyGatewayProperties(changeEvent);
this.applicationContext.publishEvent(new EnvironmentChangeEvent(changeEvent.changedKeys()));
refreshGatewayRouteDefinition();
logger.info("GatewayProperties refreshed!");
}
/***
* GatewayProperties沒有@PreDestroy和destroy方法
* org.springframework.cloud.context.properties.ConfigurationPropertiesRebinder#rebind(java.lang.String)中destroyBean時(shí)不會(huì)銷毀當(dāng)前對(duì)象
* 如果把spring.cloud.gateway.前綴的配置項(xiàng)全部刪除(例如需要?jiǎng)討B(tài)刪除最后一個(gè)路由的場(chǎng)景),initializeBean時(shí)也無法創(chuàng)建新的bean,則return當(dāng)前bean
* 若仍保留有spring.cloud.gateway.routes[n]或spring.cloud.gateway.default-filters[n]等配置,initializeBean時(shí)會(huì)注入新的屬性替換已有的bean
* 這個(gè)方法提供了類似@PreDestroy的操作,根據(jù)配置文件的實(shí)際情況把org.springframework.cloud.gateway.config.GatewayProperties#routes
* 和org.springframework.cloud.gateway.config.GatewayProperties#defaultFilters兩個(gè)集合清空
*
* @param
* @return void
* @author ksewen
* @date 2019/5/21 2:13 PM
*/
private synchronized void preDestroyGatewayProperties(ConfigChangeEvent changeEvent) {
logger.info("Pre Destroy GatewayProperties!");
final boolean needClearRoutes = this.checkNeedClear(changeEvent, ID_PATTERN, this.gatewayProperties.getRoutes().size());
if (needClearRoutes) {
this.gatewayProperties.setRoutes(new ArrayList<>());
}
final boolean needClearDefaultFilters = this.checkNeedClear(changeEvent, DEFAULT_FILTER_PATTERN, this.gatewayProperties.getDefaultFilters().size());
if (needClearDefaultFilters) {
this.gatewayProperties.setRoutes(new ArrayList<>());
}
logger.info("Pre Destroy GatewayProperties finished!");
}
private void refreshGatewayRouteDefinition() {
logger.info("Refreshing Gateway RouteDefinition!");
this.publisher.publishEvent(new RefreshRoutesEvent(this));
logger.info("Gateway RouteDefinition refreshed!");
}
/***
* 根據(jù)changeEvent和定義的pattern匹配key,如果所有對(duì)應(yīng)PropertyChangeType為DELETED則需要清空GatewayProperties里相關(guān)集合
*
* @param changeEvent
* @param pattern
* @param existSize
* @return boolean
* @author ksewen
* @date 2019/5/23 2:18 PM
*/
private boolean checkNeedClear(ConfigChangeEvent changeEvent, String pattern, int existSize) {
return changeEvent.changedKeys().stream().filter(key -> key.matches(pattern))
.filter(key -> {
ConfigChange change = changeEvent.getChange(key);
return PropertyChangeType.DELETED.equals(change.getChangeType());
}).count() == existSize;
}
}這里有個(gè)坑
如果是沒有用到默認(rèn)的application.properties,這里我們就需要填寫namespace了
如下,
需要配置value=“使用的namespace”
@ApolloConfigChangeListener(interestedKeyPrefixes = "spring.cloud.gateway.",value="xxxx.yml")
public void onChange(ConfigChangeEvent changeEvent) {
refreshGatewayProperties(changeEvent);
}springcloud集成apollo的注意事項(xiàng)
apollo的集成注意事項(xiàng)
1、建立本地緩存目錄(這里的本地指的是java應(yīng)用程序所在的pc):/opt/data
本地緩存路徑默認(rèn)位于以下路徑,所以請(qǐng)確保/opt/data或C:\opt\data\目錄存在,且應(yīng)用有讀寫權(quán)限。
- Mac/Linux: /opt/data/{appId}/config-cache
- Windows: C:\opt\data{appId}\config-cache
2、要在本地目錄下設(shè)置連接apollo環(huán)境變量。
在下面的目錄下面,建立一個(gè)server.properties文件
- Mac/Linux: /opt/settings
- Windows: C:\opt\settings
文件內(nèi)容如下:(DEV是SpringColoud 要連接的 apollo 配置 )
env=DEV

3、如果使用 apollo 自帶的 eureka 。
那么發(fā)布到 apollo 的yml文件需要指定配置(eureka注意不能省略)
eureka.client.service-url.defaultZone http://【apollo所在的ip】:8080/eureka
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
Maven中央倉庫發(fā)布的實(shí)現(xiàn)方法
最近做了個(gè)項(xiàng)目,希望能夠上傳到maven中央倉庫,給更多的人使用,于是就產(chǎn)生了這次項(xiàng)目發(fā)布經(jīng)歷。感興趣的可以一起來參考一下2021-06-06
分析Spring框架之設(shè)計(jì)與實(shí)現(xiàn)資源加載器
Spring框架是由于軟件開發(fā)的復(fù)雜性而創(chuàng)建的。然而,Spring的用途不僅僅限于服務(wù)器端的開發(fā)。從簡(jiǎn)單性、可測(cè)試性和松耦合性角度而言,絕大部分Java應(yīng)用都可以從Spring中受益。今天來分析它的設(shè)計(jì)與實(shí)現(xiàn)資源加載器,從Spring.xml解析和注冊(cè)Bean對(duì)象2021-06-06
SpringBoot favicon Chrome設(shè)置問題解決方案
在本篇文章里小編給大家分享的是關(guān)于SpringBoot favicon Chrome設(shè)置問題實(shí)例內(nèi)容,小的朋友們可以參考學(xué)習(xí)下。2020-02-02
springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的
今天小編就為大家分享一篇關(guān)于springmvc中RequestMappingHandlerAdapter與HttpMessageConverter的裝配講解,小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-01-01
java實(shí)現(xiàn)簡(jiǎn)單圖片上傳下載功能
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)單圖片上傳下載功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-10-10

