zookeeper 监听 动态修改ribbon和hystrix配置

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值