package com.pa.springcloud.Listener;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Callable;
import java.util.concurrent.Executors;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.zookeeper.discovery.ZookeeperServiceWatch;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;
import com.alibaba.fastjson.JSONObject;
import com.google.common.util.concurrent.ListeningExecutorService;
import com.google.common.util.concurrent.MoreExecutors;
import com.netflix.config.ConfigurationManager;
import com.pa.hystrix.CustomHystrix;
import com.pa.vo.HystrixVo;
import com.pa.vo.RibbonVo;
@Component
public class ZookeeperDynamicSettingConfig implements TreeCacheListener,ApplicationListener<InstanceRegisteredEvent<?>>{
private static final Logger LOGGER = LoggerFactory.getLogger(ZookeeperDynamicSettingConfig.class);
@Autowired
private SpringClientFactory clientFactory;
@Autowired
private ZookeeperServiceWatch zookeeperServiceWatch;
private static final String ROOT_SERVICES = "/services/";
private static final String SERVER_RIBBON_INFO = "ServerRibbonInfo";
private static final String HYSTRIX_PARMETER = "HystrixParmeter";
private static final String CLIENT_RIBBON_INFO = "ClientRibbonInfo";
//key为path,value为一个以RibbonInfo为key,RibbonVo对象为value的map
private static Map<String,Map<String, Object>> ribbonMap = new HashMap<String, Map<String, Object>>();
//key为path,value为一个以uri的唯一key,HystrixVo对象为value的map
private static Map<String,Map<String, Object>> hystrixMap = new HashMap<String,Map<String, Object>>();
//key like /services/openapi-gateway-core
public static Map<String,Map<String, Map<String, String>>> hystrixRequestMap = new HashMap<String, Map<String, Map<String, String>>>();
public static Map<String,Map<String, Map<String, String>>> hystrixRequestSecondMap = new HashMap<String, Map<String, Map<String, String>>>();
public String applicationName ;
private static ListeningExecutorService pool = MoreExecutors
.listeningDecorator(Executors.newSingleThreadExecutor());
// private static ListeningExecutorService pool = MoreExecutors
// .listeningDecorator(Executors.newFixedThreadPool(20));
// private static List<ListenableFuture> callbackList = new ArrayList<ListenableFuture>();
@Override
public void onApplicationEvent(InstanceRegisteredEvent<?> event) {
TreeCache treeCache = zookeeperServiceWatch.getCache();
treeCache.getListenable().addListener(this);
}
private String getpersonalPath() {
if (this.applicationName == null) {
this.applicationName = ConfigurationManager.getConfigInstance().getStringArray("spring.application.name")[0];
};
return this.applicationName;
}
private void handleTreeCacheEvent(TreeCacheEvent event){
ChildData data = event.getData();
Map<String, Object> mapData = (Map<String, Object>) JSONObject.parse(data.getData());
if(data !=null && !mapData.isEmpty()){
switch (event.getType()) {
case NODE_ADDED:
doNodeAddedWithPathAndMapData(data.getPath(), mapData);
break;
case NODE_REMOVED:
doNodeRemovedWithPathAndMapData(data.getPath(), mapData