兼容文件模式和Apollo模式.
This commit is contained in:
parent
ce404ecf6b
commit
430d22b680
|
@ -6,7 +6,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class Change implements ApplicationEventPublisherAware {
|
public class ConfigRefresh implements ApplicationEventPublisherAware {
|
||||||
|
|
||||||
private ApplicationEventPublisher publisher;
|
private ApplicationEventPublisher publisher;
|
||||||
|
|
|
@ -1,24 +1,15 @@
|
||||||
package cn.ecpark.service.usergw.config;
|
package cn.ecpark.service.usergw.config;
|
||||||
|
|
||||||
import javax.annotation.Resource;
|
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Configurable;
|
|
||||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
||||||
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
|
|
||||||
import org.springframework.cloud.gateway.route.RouteDefinitionWriter;
|
|
||||||
import org.springframework.context.ApplicationEventPublisher;
|
|
||||||
import org.springframework.context.ApplicationEventPublisherAware;
|
|
||||||
import org.springframework.stereotype.Service;
|
|
||||||
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
|
||||||
|
|
||||||
import com.ctrip.framework.apollo.Config;
|
import com.ctrip.framework.apollo.Config;
|
||||||
|
import com.ctrip.framework.apollo.ConfigChangeListener;
|
||||||
import com.ctrip.framework.apollo.ConfigService;
|
import com.ctrip.framework.apollo.ConfigService;
|
||||||
import com.ctrip.framework.apollo.model.ConfigChange;
|
|
||||||
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
|
||||||
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
|
|
||||||
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.context.ApplicationContext;
|
||||||
|
|
||||||
|
import cn.ecpark.service.usergw.biz.events.ConfigRefresh;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -26,19 +17,51 @@ import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
|
||||||
// @ConditionalOnProperty(prefix="app", value="id", matchIfMissing=true)
|
// @ConditionalOnProperty(prefix="app", value="id", matchIfMissing=true)
|
||||||
// @Configurable
|
// @Configurable
|
||||||
|
|
||||||
public class ConfigApollo {
|
public class ConfigApollo implements ConfigChangeListener {
|
||||||
|
|
||||||
// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigApollo.class);
|
// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigApollo.class);
|
||||||
|
|
||||||
// @ApolloConfig("gateway.yml")
|
// @ApolloConfig("gateway.yml")
|
||||||
private Config config;
|
private Config config;
|
||||||
|
@Autowired
|
||||||
|
private ApplicationContext appContext;
|
||||||
|
|
||||||
|
@Value("${apollo.meta:}")
|
||||||
|
private String apolloMeta;
|
||||||
|
|
||||||
|
@Value("${app.id:}")
|
||||||
|
private String appID;
|
||||||
|
|
||||||
|
private int tryConnect;
|
||||||
|
|
||||||
public ConfigApollo() {
|
public ConfigApollo() {
|
||||||
|
this.tryConnect = 1;
|
||||||
}
|
}
|
||||||
// @ApolloConfigChangeListener("gateway.yml")
|
|
||||||
private void listenApolloChange(ConfigChangeEvent changeEvent) {
|
|
||||||
|
|
||||||
|
public void Connect() {
|
||||||
|
if(this.isConnected() && this.tryConnect > 0) {
|
||||||
|
this.tryConnect --;
|
||||||
|
config = ConfigService.getConfig("gateway.yml");
|
||||||
|
config.addChangeListener(this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isConnected() {
|
||||||
|
if(!this.appID.equals("") && !this.apolloMeta.equals("") ) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// @ApolloConfigChangeListener("gateway.yml")
|
||||||
|
// public void listenApolloChange(ConfigChangeEvent changeEvent) {
|
||||||
|
|
||||||
|
// }
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onChange(ConfigChangeEvent changeEvent) {
|
||||||
|
ConfigRefresh change = (ConfigRefresh)appContext.getBean(ConfigRefresh.class);
|
||||||
|
change.notifyChanged();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -55,4 +78,32 @@ public class ConfigApollo {
|
||||||
this.config = config;
|
this.config = config;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return String return the apolloMeta
|
||||||
|
*/
|
||||||
|
public String getApolloMeta() {
|
||||||
|
return apolloMeta;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param apolloMeta the apolloMeta to set
|
||||||
|
*/
|
||||||
|
public void setApolloMeta(String apolloMeta) {
|
||||||
|
this.apolloMeta = apolloMeta;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return String return the appID
|
||||||
|
*/
|
||||||
|
public String getAppID() {
|
||||||
|
return appID;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param appID the appID to set
|
||||||
|
*/
|
||||||
|
public void setAppID(String appID) {
|
||||||
|
this.appID = appID;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -48,11 +48,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
@Value("${yame.config:}")
|
@Value("${yame.config:}")
|
||||||
private String yameConfigPath;
|
private String yameConfigPath;
|
||||||
|
|
||||||
@Value("${apollo.meta:}")
|
|
||||||
private String apolloMeta;
|
|
||||||
|
|
||||||
@Value("${app.id:}")
|
|
||||||
private String appID;
|
|
||||||
|
|
||||||
private List<FilterDefinition> defaultFilters = new ArrayList<>();
|
private List<FilterDefinition> defaultFilters = new ArrayList<>();
|
||||||
private HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>>();
|
private HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>> specialField = new HashMap<String, BiConsumer<ReferenceConfig<GenericService>, Object>>();
|
||||||
|
@ -76,58 +72,16 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
ignoreKey.add("filters");
|
ignoreKey.add("filters");
|
||||||
}
|
}
|
||||||
|
|
||||||
private InputStream loadLocalFileConfig() {
|
|
||||||
Object inputStream = null;
|
|
||||||
String[] gatewayConfigPathList;
|
|
||||||
if (yameConfigPath.equals("")) {
|
|
||||||
gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
|
|
||||||
} else {
|
|
||||||
gatewayConfigPathList = new String[] { yameConfigPath };
|
|
||||||
}
|
|
||||||
for (String gatewayConfigPath : gatewayConfigPathList) {
|
|
||||||
gatewayConfigPath = gatewayConfigPath.trim();
|
|
||||||
inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath);
|
|
||||||
if (inputStream != null) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return (InputStream) inputStream;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
private InputStream loadApolloConfig() {
|
|
||||||
if (true) return null;
|
|
||||||
ConfigApollo configApollo = appContext.getBean(ConfigApollo.class);
|
|
||||||
DefaultConfig config = (DefaultConfig) configApollo.getConfig();
|
|
||||||
try {
|
|
||||||
Field m_configRepository = DefaultConfig.class.getDeclaredField("m_configRepository");
|
|
||||||
m_configRepository.setAccessible(true);
|
|
||||||
ConfigRepository cr = (ConfigRepository) m_configRepository.get(config);
|
|
||||||
Field configFile = PropertiesCompatibleFileConfigRepository.class.getDeclaredField("configFile");
|
|
||||||
configFile.setAccessible(true);
|
|
||||||
YmlConfigFile ymlConfigFile = (YmlConfigFile) configFile.get(cr);
|
|
||||||
String content = ymlConfigFile.getContent();
|
|
||||||
log.info("Load Apollo YAML Config: \n{}", content);
|
|
||||||
return new ByteArrayInputStream(content.getBytes());
|
|
||||||
} catch (NoSuchFieldException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (SecurityException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IllegalArgumentException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
} catch (IllegalAccessException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@SuppressWarnings("unchecked")
|
@SuppressWarnings("unchecked")
|
||||||
public Flux<RouteDefinition> getRouteDefinitions() {
|
public Flux<RouteDefinition> getRouteDefinitions() {
|
||||||
defaultFilters.clear();
|
defaultFilters.clear();
|
||||||
InputStream inputStream;
|
InputStream inputStream;
|
||||||
if(!this.apolloMeta.equals("") && !this.appID.equals("")) {
|
|
||||||
inputStream = loadApolloConfig();
|
ConfigApollo configApollo = appContext.getBean(ConfigApollo.class);
|
||||||
|
configApollo.Connect();
|
||||||
|
if(configApollo.isConnected()) {
|
||||||
|
inputStream = loadApolloConfig(configApollo);
|
||||||
} else {
|
} else {
|
||||||
inputStream = loadLocalFileConfig();
|
inputStream = loadLocalFileConfig();
|
||||||
}
|
}
|
||||||
|
@ -169,13 +123,55 @@ public class ConfigGateway implements RouteDefinitionLocator {
|
||||||
if (!routeList.isEmpty()) {
|
if (!routeList.isEmpty()) {
|
||||||
return Flux.fromIterable(routeList);
|
return Flux.fromIterable(routeList);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return Flux.empty();
|
return Flux.empty();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private InputStream loadLocalFileConfig() {
|
||||||
|
Object inputStream = null;
|
||||||
|
String[] gatewayConfigPathList;
|
||||||
|
if (yameConfigPath.equals("")) {
|
||||||
|
gatewayConfigPathList = new String[] { "gateway.yml", "gateway.yaml", "Gateway.yml", "Gateway.yaml" };
|
||||||
|
} else {
|
||||||
|
gatewayConfigPathList = new String[] { yameConfigPath };
|
||||||
|
}
|
||||||
|
for (String gatewayConfigPath : gatewayConfigPathList) {
|
||||||
|
gatewayConfigPath = gatewayConfigPath.trim();
|
||||||
|
inputStream = this.getClass().getClassLoader().getResourceAsStream(gatewayConfigPath);
|
||||||
|
if (inputStream != null) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return (InputStream) inputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private InputStream loadApolloConfig(ConfigApollo configApollo) {
|
||||||
|
DefaultConfig config = (DefaultConfig) configApollo.getConfig();
|
||||||
|
try {
|
||||||
|
Field m_configRepository = DefaultConfig.class.getDeclaredField("m_configRepository");
|
||||||
|
m_configRepository.setAccessible(true);
|
||||||
|
ConfigRepository cr = (ConfigRepository) m_configRepository.get(config);
|
||||||
|
Field configFile = PropertiesCompatibleFileConfigRepository.class.getDeclaredField("configFile");
|
||||||
|
configFile.setAccessible(true);
|
||||||
|
YmlConfigFile ymlConfigFile = (YmlConfigFile) configFile.get(cr);
|
||||||
|
String content = ymlConfigFile.getContent();
|
||||||
|
log.info("Load Apollo YAML Config: \n{}", content);
|
||||||
|
return new ByteArrayInputStream(content.getBytes());
|
||||||
|
} catch (NoSuchFieldException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (SecurityException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalArgumentException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
} catch (IllegalAccessException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* @param routeList 路由列表
|
* @param routeList 路由列表
|
||||||
|
|
|
@ -11,6 +11,11 @@ import reactor.core.publisher.Mono;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
public class ConfigBean {
|
public class ConfigBean {
|
||||||
|
@Bean
|
||||||
|
ConfigApollo configApollo() {
|
||||||
|
return new ConfigApollo();
|
||||||
|
}
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
public GenericServicePool genericServicePool() {
|
public GenericServicePool genericServicePool() {
|
||||||
return new GenericServicePool();
|
return new GenericServicePool();
|
||||||
|
@ -26,10 +31,5 @@ public class ConfigBean {
|
||||||
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
|
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Bean
|
|
||||||
ConfigApollo configApollo() {
|
|
||||||
ConfigApollo ca = new ConfigApollo();
|
|
||||||
|
|
||||||
return ca;
|
|
||||||
}
|
|
||||||
}
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
|
|
||||||
spring.application.name=gateway
|
spring.application.name=gateway
|
||||||
|
|
||||||
# app.id=gateway
|
app.id=gateway
|
||||||
# apollo.meta=http://localhost:8180
|
apollo.meta=http://localhost:8180
|
||||||
# local.meta=http://localhost:8180
|
# local.meta=http://localhost:8180
|
||||||
# dev.meta=http://localhost:8180
|
# dev.meta=http://localhost:8180
|
||||||
# fat.meta=http://localhost:8180
|
# fat.meta=http://localhost:8180
|
||||||
|
|
Loading…
Reference in New Issue
Block a user