`
ahua186186
  • 浏览: 555399 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

S-HSF配置服务管理者

 
阅读更多

public class ConfigManager {
	private static Logger logger = LoggerFactory.getLogger(ConfigManager.class);
	/**
	 * 每个客户端(服务提供者和服务消费者)对应的配置中心管理者
	 */
	private static ConcurrentHashMap<String, ConfigManager> map = new ConcurrentHashMap<String, ConfigManager>();
	/**
	 * 服务提供者注册的服务列表
	 */
	private ConcurrentHashMap<String, HashSet<ConfigServiceItemInfo>> configMap = new ConcurrentHashMap<String, HashSet<ConfigServiceItemInfo>>();
	/**
	 * 每个服务消费者IP列表(服务提供者注册时根据该IP列表推送配置信息)
	 */
	private ConcurrentHashMap<String, HashSet<String>> serviceGroupMap = new ConcurrentHashMap<String, HashSet<String>>();
	/**
	 * 每个服务消费者订阅的服务列表(服务消费者订阅时根据该服务列表推送配置信息)
	 */
	private ConcurrentHashMap<String, HashSet<String>> groupServiceMap = new ConcurrentHashMap<String, HashSet<String>>();

	public static ConfigManager getConfigManager() {
		HsfService hsfService = HsfContextHolder.getHsfService();
		if (hsfService == null) {
			throw new IllegalStateException(
					"there is not HsfService bind to current thread. can't get a configManager.");
		}
		//
		ConfigManager configManager = map.get(hsfService.getGroupName());
		if (configManager == null) {
			configManager = new ConfigManager();
			ConfigManager preValue = map.putIfAbsent(hsfService.getGroupName(), configManager);
			if (preValue != null) {
				configManager = preValue;
			}
		}
		return configManager;
	}

	public HashSet<String> getSubscribeGroups(String serviceName) {
		if (serviceName == null) {
			return null;
		}

		return serviceGroupMap.get(serviceName);
	}

	public HashSet<String> getSubscribeServices(String groupName) {
		if (groupName == null) {
			return null;
		}

		return groupServiceMap.get(groupName);
	}

	public void subscribe(String groupName, String... serviceArray) {
		if (groupName == null || serviceArray == null || serviceArray.length == 0) {
			logger.warn("subscribe with illegal arguments(groupName:{}, serviceArray:{})", groupName, serviceArray);
			return;
		}
		for (String serviceName : serviceArray) {
			HashSet<String> groupSet = getSubscribeGroupSet(serviceName);
			groupSet.add(groupName);
			//
			HashSet<String> serviceSet = getSubscribeSet(groupName);
			serviceSet.add(serviceName);
		}
	}

	public void unsubscribe(String groupName, String... serviceArray) {
		if (groupName == null || serviceArray == null || serviceArray.length == 0) {
			logger.warn("unsubscribe with illegal arguments(groupName:{}, serviceArray:{})", groupName, serviceArray);
			return;
		}
		for (String serviceName : serviceArray) {
			HashSet<String> groupSet = serviceGroupMap.get(serviceName);
			if (removeSetItem(groupSet, groupName)) {
				if (groupSet.size() == 0) {
					synchronized (groupSet) {
						if (groupSet.size() == 0) {
							serviceGroupMap.remove(serviceName);
						}
					}
				}
			}
			//
			HashSet<String> serviceSet = getSubscribeSet(groupName);
			serviceSet.remove(serviceName);
		}
	}

	private boolean removeSetItem(Set<String> set, String item) {
		if (set != null) {
			synchronized (set) {
				return set.remove(item);
			}
		}
		return false;
	}

	public void unsubscribe(String groupName) {
		if (groupName == null) {
			logger.warn("unsubscribe with illegal arguments(groupName:{})", groupName);
			return;
		}

		HashSet<String> serviceSet = groupServiceMap.get(groupName);
		if (serviceSet != null && serviceSet.size() > 0) {
			String[] serviceArray = new String[serviceSet.size()];
			serviceSet.toArray(serviceArray);
			unsubscribe(groupName, serviceArray);
		}
	}

	public ConcurrentHashMap<String, HashSet<ConfigServiceItemInfo>> getConfigMap() {
		return configMap;
	}

	public boolean update(ConfigServiceInfo configServiceInfo) {
		if (configServiceInfo == null || configServiceInfo.getItems() == null) {
			return false;
		}
		//
		for (Map.Entry<String, HashSet<ConfigServiceItemInfo>> entry : configServiceInfo.getItems().entrySet()) {
			HashSet<ConfigServiceItemInfo> set = getConfigSet(entry.getKey());
			synchronized (set) {
				String host = HsfContextHolder.getRemoteAddress().getHostName();
				for (ConfigServiceItemInfo configServiceItemInfo : entry.getValue()) {
					//
					if (AddressType.Port.equals(configServiceItemInfo.getAddressType())) {
						configServiceItemInfo.setAddress(host + ":" + configServiceItemInfo.getAddress());
						configServiceItemInfo.setAddressType(AddressType.Address);
					}
					//
					set.add(configServiceItemInfo);
				}
			}
		}

		return true;
	}

	public boolean remove(ConfigServiceInfo configServiceInfo) {
		if (configServiceInfo == null || configServiceInfo.getItems() == null) {
			return false;
		}
		//
		for (Map.Entry<String, HashSet<ConfigServiceItemInfo>> entry : configServiceInfo.getItems().entrySet()) {
			HashSet<ConfigServiceItemInfo> set = getConfigSet(entry.getKey());
			synchronized (set) {
				set.removeAll(entry.getValue());
			}
		}

		return true;
	}

	public void clear() {
		configMap.clear();
	}

	private HashSet<ConfigServiceItemInfo> getConfigSet(String serviceName) {
		HashSet<ConfigServiceItemInfo> configSet = configMap.get(serviceName);
		if (configSet == null) {
			configSet = new HashSet<ConfigServiceItemInfo>();
			HashSet<ConfigServiceItemInfo> preValue = configMap.putIfAbsent(serviceName, configSet);
			if (preValue != null) {
				configSet = preValue;
			}
		}
		return configSet;
	}

	private HashSet<String> getSubscribeGroupSet(String serviceName) {
		HashSet<String> groupSet = serviceGroupMap.get(serviceName);
		if (groupSet == null) {
			groupSet = new HashSet<String>();
			HashSet<String> preValue = serviceGroupMap.putIfAbsent(serviceName, groupSet);
			if (preValue != null) {
				groupSet = preValue;
			}
		}
		return groupSet;
	}

	private HashSet<String> getSubscribeSet(String groupName) {
		HashSet<String> serviceSet = groupServiceMap.get(groupName);
		if (serviceSet == null) {
			serviceSet = new HashSet<String>();
			HashSet<String> preValue = groupServiceMap.putIfAbsent(groupName, serviceSet);
			if (preValue != null) {
				serviceSet = preValue;
			}
		}
		return serviceSet;
	}
}


分享到:
评论

相关推荐

    taobao-hsf.tgz

    taobao-hsf安装包

    taobao-hsf.sar

    hsf框架所需工具包 taobao-hsf.sar 直接放入tomcat根目录tomcate/deploy/下即可

    淘宝好舒服 taobao-hsf

    taobao-hsf---淘宝好舒服

    taobao-HSF的两种安装方案

    NULL 博文链接:https://manning.iteye.com/blog/2412034

    summercool-hsf:自动从code.google.compsummercool-hsf导出

    笔者工作的这几年之中,总结并开发了如下几个框架: summercool(Web 框架,已经应用于某国内大型网络公司的等重要应用)、summercool-hsf(基于Netty实现的RPC框架,已经应用国内某移动互联网公司)、 summercool-...

    EDAS-HSF-BOOT

    EDAS 生产者 消费者 api 齐全 环境配好既可以使用................................................................

    summercool-hsf:从 code.google.compsummercool-hsf 自动导出

    summercool-hsf Automatically exported from code.google.com/p/summercool-hsf 1.目前为止性能最高的RPC远程通讯框架 2.也可以做为手机长连接的Server,经测试已经达到了50W以上的性能长连接 (需调整linux内核...

    taobao-hsf

    阿里云开发的容器,edas需要在taobao-tomcat中运行,保存后将内容解压至上述保存的 Ali-Tomcat 的 deploy 目录(d:\work\tomcat\deploy)下。

    Springboot+HSF分布式服务框架+EDAS注册中心,实现发布订阅模式

    Springboot+HSF分布式服务框架+EDAS注册中心,实现发布订阅模式

    HSF策划流程图.doc

    储存、隔离和发放等过程 运作 HSF物料过程管理 HSF制造过程管理 HSF供应链过程管理 HSF质量保证过程管理 公司 HSF控制计划 文件化的HSF方针和HSF目标,作为公司HSF符合性的承诺 文件化的HSF过程,确保符合客户的HSF...

    robot-hsf15:用于 Hacklab Summit Finland 2015 的 Arduino 机器人 roborally compo

    机器人-hsf15 用于 Hacklab Summit Finland 2015 roborally compo 的 Arduino 机器人。 如何做事 在robot-hsf15 目录中运行make命令(可选)。 在 Blender 中加载simulation/robotsimulation.blend并按 P 开始模拟...

    NIST by_Field 部分(hsf_0 和hsf_1)

    https://www.nist.gov/srd/nist-special-database-19 里面的by_field 搬运(图片格式) 注意仅仅是 其中的一部分 hsf_0 hsf_1 (原文件中有hsf_0 --hsf_7) 每个hsf中有数字 字母 小写字母, 大写字母 ,每个数字和...

    EDAS中HSF方式启动服务入门文档

    EDAS中HSF方式启动服务入门文档,详细描述了EDAS中HSF方式的启动过程

    hsf(或者dubbo)war包ftp更新-window可配置bat脚本

    下载目录和服务器源目录都可在config.txt中配置。 运行环境:windows、psftp(含在下载的压缩包中) 说明:如果安装了psftp,就不用再安装 工具用途说明: 适应于要从linux上下载多个不同目录下的不同文件到...

    HSF文件资料管理程序.doc

    HSF文件资料管理程序 1.0 目的: 确保绿色管理系统规定的文件、记录及资料能有效的制定、应用、储存及保管 并 能适时地取得正确有效的文件。 2.0 范围: 绿色管理系统规定之文件、记录及资料皆属之。 3.0 定义: 1. ...

    HSF项目例子IDEA 与 eclipse 开发环境说明

    HSF 官网Demo、IDEA 与 eclipse 开发环境说明;HSF,包结构等等

    抑制HSF-1增强PS-341诱导胶质瘤细胞凋亡

    抑制HSF-1增强PS-341诱导胶质瘤细胞凋亡,毕云科,刘耀华,背景与目的:蛋白酶体抑制剂能影响肿瘤生长并诱导细胞凋亡。PS-341作为第一个经研究证实并被美国FDA批准用于临床的蛋白酶体抑制剂,

    HSF实现原理讲解

    HSF 框架的原理讲解,主要包含了知识点:rpc,动态代理,HSF可以作为微服务的基础框架进行二次开发

    FZ-H800巷道注浆堵水加固水泥基外加剂试验研究

    通过分析可知,最佳外加剂掺量为10%,常温下10%外加剂掺量的配方浆液初凝时间184 s、终凝时间339 s,凝结时间快;7 d可达45 MPa,28 d强度可接近或达到70 MPa;抗渗等级可达P8以上;膨胀率可达到0.31。FZ-H800注浆专用水泥...

Global site tag (gtag.js) - Google Analytics