use of com.alibaba.nacos.api.config.listener.Listener in project hummer-framework by hummer-team.
the class ListenerConfig method addListener.
private void addListener(List<String> gatewayGroup, String groupId, long timeoutMillis, ConfigService configService, boolean isFirstLoading) throws NacosException {
Set<DegradeRule> degradeRules = Sets.newConcurrentHashSet();
Set<GatewayFlowRule> gatewayFlowRules = Sets.newConcurrentHashSet();
Set<ApiDefinition> apiDefinitions = Sets.newConcurrentHashSet();
for (String dataId : gatewayGroup) {
Listener listener = getListener();
String configVal = configService.getConfigAndSignListener(dataId, groupId, timeoutMillis, listener);
if (!Strings.isNullOrEmpty(configVal)) {
// refresh gateway config
log.debug("refresh config done,config value is \n{}", configVal);
DoorGoodConfig doorGoodConfig = JSON.parseObject(configVal, new TypeReference<DoorGoodConfig>() {
});
refreshGatewayRule(doorGoodConfig);
listenerMap.put(dataId, ListenerEvent.builder().dataId(dataId).groupId(groupId).listener(listener).sentinelConfig(doorGoodConfig.getSentinelConfig()).build());
if (isFirstLoading) {
addSentinelConfigForInitLoad(degradeRules, gatewayFlowRules, apiDefinitions, doorGoodConfig);
} else {
// refresh sentinel config
refreshSentinelConfig(doorGoodConfig);
}
}
}
firstLoadingSentinel(isFirstLoading, degradeRules, gatewayFlowRules, apiDefinitions);
}
use of com.alibaba.nacos.api.config.listener.Listener in project camellia by netease-im.
the class CamelliaRedisProxyNacosConfiguration method nacosConfigService.
@Bean
public NacosProxyDamicConfSupport nacosConfigService(CamelliaRedisProxyNacosProperties properties) {
try {
NacosProxyDamicConfSupport support = new NacosProxyDamicConfSupport();
if (properties.isEnable()) {
Properties nacosProps = new Properties();
if (properties.getServerAddr() != null) {
nacosProps.put("serverAddr", properties.getServerAddr());
}
Map<String, String> nacosConf = properties.getNacosConf();
if (nacosConf != null) {
for (Map.Entry<String, String> entry : nacosConf.entrySet()) {
nacosProps.put(entry.getKey(), entry.getValue());
}
}
ConfigService configService = NacosFactory.createConfigService(nacosProps);
support.setConfigService(configService);
for (CamelliaRedisProxyNacosProperties.ConfFile confFile : properties.getConfFileList()) {
NacosProxyDamicConfSupport.ReloadCallback reloadCallback = () -> {
String content = configService.getConfig(confFile.getDataId(), confFile.getGroup(), properties.getTimeoutMs());
updateConf(content, confFile.getFileName());
};
reloadCallback.reload();
support.addReloadCallback(reloadCallback);
configService.addListener(confFile.getDataId(), confFile.getGroup(), new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String content) {
updateConf(content, confFile.getFileName());
}
});
}
} else {
logger.info("camellia redis proxy nacos not enable");
}
return support;
} catch (Exception e) {
throw new IllegalArgumentException(e);
}
}
use of com.alibaba.nacos.api.config.listener.Listener in project simple4h-study by simplecxm.
the class NacosDynamicRoutingConfig method refreshRouting.
@Bean
public void refreshRouting() throws NacosException {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, nacosServer);
// properties.put(PropertyKeyConst.NAMESPACE, "8282c713-da90-486a-8438-2a5a212ef44f");
ConfigService configService = NacosFactory.createConfigService(properties);
configService.addListener(DATA_ID, GROUP, new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
log.info(configInfo);
boolean refreshGatewayRoute = JSONObject.parseObject(configInfo).getBoolean("refreshGatewayRoute");
if (refreshGatewayRoute) {
List<RouteEntity> list = JSON.parseArray(JSONObject.parseObject(configInfo).getString("routeList")).toJavaList(RouteEntity.class);
for (RouteEntity route : list) {
update(assembleRouteDefinition(route));
}
} else {
log.info("路由未发生变更");
}
}
});
}
use of com.alibaba.nacos.api.config.listener.Listener in project tutorials-java by Artister.
the class NacosConfigRunner method run.
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(String.format("Initial name=%s, age=%d", name, age));
nacosConfigProperties.configServiceInstance().addListener("nacos-config-example.properties", "DEFAULT_GROUP", new Listener() {
/**
* Callback with latest config data.
*
* For example, config data in Nacos is:
*
* user.name=Nacos user.age=25
*
* @param configInfo latest config data for specific dataId in Nacos
* server
*/
@Override
public void receiveConfigInfo(String configInfo) {
Properties properties = new Properties();
try {
properties.load(new StringReader(configInfo));
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("config changed: " + properties);
}
@Override
public Executor getExecutor() {
return null;
}
});
}
use of com.alibaba.nacos.api.config.listener.Listener in project dynamic-threadpool by acmenlt.
the class NacosCloudRefresherHandler method afterPropertiesSet.
@Override
public void afterPropertiesSet() throws Exception {
Map<String, String> nacosConfig = bootstrapCoreProperties.getNacos();
nacosConfigManager.getConfigService().addListener(nacosConfig.get("data-id"), nacosConfig.get("group"), new Listener() {
@Override
public Executor getExecutor() {
return dynamicRefreshExecutorService;
}
@Override
public void receiveConfigInfo(String configInfo) {
dynamicRefresh(configInfo);
}
});
}
Aggregations