兼容文件模式和Apollo模式.

This commit is contained in:
huangsimin 2019-08-14 14:17:45 +08:00
parent ce404ecf6b
commit 430d22b680
5 changed files with 131 additions and 84 deletions

View File

@ -6,7 +6,7 @@ import org.springframework.context.ApplicationEventPublisherAware;
import org.springframework.stereotype.Service;
@Service
public class Change implements ApplicationEventPublisherAware {
public class ConfigRefresh implements ApplicationEventPublisherAware {
private ApplicationEventPublisher publisher;

View File

@ -1,24 +1,15 @@
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.ConfigChangeListener;
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.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.ApplicationContext;
import cn.ecpark.service.usergw.biz.events.ConfigRefresh;
@ -26,22 +17,54 @@ import com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig;
// @ConditionalOnProperty(prefix="app", value="id", matchIfMissing=true)
// @Configurable
public class ConfigApollo {
public class ConfigApollo implements ConfigChangeListener {
// private static final org.slf4j.Logger log = org.slf4j.LoggerFactory.getLogger(ConfigApollo.class);
// @ApolloConfig("gateway.yml")
private Config config;
@Autowired
private ApplicationContext appContext;
@Value("${apollo.meta:}")
private String apolloMeta;
@Value("${app.id:}")
private String appID;
private int tryConnect;
public ConfigApollo() {
}
// @ApolloConfigChangeListener("gateway.yml")
private void listenApolloChange(ConfigChangeEvent changeEvent) {
this.tryConnect = 1;
}
/**
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();
}
/**
* @return Config return the config
*/
public Config getConfig() {
@ -55,4 +78,32 @@ public class ConfigApollo {
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;
}
}

View File

@ -48,11 +48,7 @@ public class ConfigGateway implements RouteDefinitionLocator {
@Value("${yame.config:}")
private String yameConfigPath;
@Value("${apollo.meta:}")
private String apolloMeta;
@Value("${app.id:}")
private String appID;
private List<FilterDefinition> defaultFilters = new ArrayList<>();
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");
}
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
@SuppressWarnings("unchecked")
public Flux<RouteDefinition> getRouteDefinitions() {
defaultFilters.clear();
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 {
inputStream = loadLocalFileConfig();
}
@ -169,13 +123,55 @@ public class ConfigGateway implements RouteDefinitionLocator {
if (!routeList.isEmpty()) {
return Flux.fromIterable(routeList);
}
}
}
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 路由列表

View File

@ -11,6 +11,11 @@ import reactor.core.publisher.Mono;
@Component
public class ConfigBean {
@Bean
ConfigApollo configApollo() {
return new ConfigApollo();
}
@Bean
public GenericServicePool genericServicePool() {
return new GenericServicePool();
@ -26,10 +31,5 @@ public class ConfigBean {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostString());
}
@Bean
ConfigApollo configApollo() {
ConfigApollo ca = new ConfigApollo();
return ca;
}
}

View File

@ -1,8 +1,8 @@
spring.application.name=gateway
# app.id=gateway
# apollo.meta=http://localhost:8180
app.id=gateway
apollo.meta=http://localhost:8180
# local.meta=http://localhost:8180
# dev.meta=http://localhost:8180
# fat.meta=http://localhost:8180