资源注册中心功能
1.接收注册任务
2.接收注销任务
3.提供给资源接收端合理的资源节点列表。
对于资源管理中心来说,同服务发现的资源注册中心,和资源注册的功能相同。
RegisterServer
资源管理中心的启动入口类
public class RegisterServer{
private int rpcPort;
private Register rt;
private rmiServer rs;
private Object lock;
private rmiCilent rc;
private CilentProxy cp;
private volatile NetNode currentNode;
public RegisterServer() {
rs = new rmiServer();
lock = new Object();
rt = new Register();
cp = new CilentProxy();
rc = new rmiCilent();
rc.setIce(new CheckProviderException());
cp.setRmicilent(rc);
}
public void setRpcPort(int port) {
this.rpcPort = port;
rs.setPort(this.rpcPort);
}
public void shudown() {
rs.close();
}
public void start() {
rs.start();
}
public void checkRobustness() {
Set<NetNode> nodeList = rt.getAllNode();
Iterator<NetNode> list = nodeList.iterator();
while(list.hasNext()) {
currentNode = list.next();
rc.setId(currentNode.getIp());
rc.setPort(currentNode.getPort());
Iserver server = cp.getProxy(Iserver.class);
server.robustness();
}
}
class CheckProviderException implements ICilentException{
@Override
public void peerAbnormalDrop() {
rt.logOutServer(currentNode);
}
}
}
IRegister
/*
*注册中心的功能接口
*1.注册资源
*2.注销资源
*3.提供节点列表
*/
public interface IRegister {
List<NetNode> getServerList(String serverDemand);
void registServer(NetNode serverNode, List<String> ResourceName);
void logOutServer(NetNode serverNode);
}
Register
资源管理中心的功能实现类
@rmiInterface(rmiter= {IRegister.class})
public class Register implements IRegister{
private static final Map<String, List<NetNode>> serverMap
= new ConcurrentHashMap<String, List<NetNode>>();
private static final Map<NetNode,Object> serverNodeMap = new ConcurrentHashMap<NetNode, Object>();
private static final Object object= new Object();
public Register() {
}
public Set<NetNode> getAllNode(){
return serverNodeMap.keySet();
}
@Override
public List<NetNode> getServerList(String serverDemand) {
return serverMap.get(serverDemand);
}
public void Rew(INetNode node) {
System.out.println(node);
}
@Override
public void registServer(NetNode serverNode, List<String> resourceName) {
if(resourceName == null) {
return;
}
serverNodeMap.put(serverNode, object);
for(String one : resourceName) {
if(!serverMap.containsKey(one)) {
List<NetNode> nodeList = new ArrayList<NetNode>();
nodeList.add(serverNode);
serverMap.put(one, nodeList);
}else {
List<NetNode> nodeList = serverMap.get(one);
nodeList.add(serverNode);
}
}
}
@Override
public void logOutServer(NetNode serverNode) {
Set<String> keys = serverMap.keySet();
Iterator<String> keyIter = keys.iterator();
serverNodeMap.remove(serverNode);
while(keyIter.hasNext()) {
String key = keyIter.next();
List<NetNode> nodeList =serverMap.get(key);
Iterator<NetNode> values = nodeList.iterator();
while(values.hasNext()) {
NetNode one = values.next();
values.remove();
if(nodeList.isEmpty()) {
keyIter.remove();
}
}
}
}
}
CheckAlive
/*
* 用于检测资源提供端的健壮性
*/
public class CheckAlive {
private RegisterServer rs;
private Timer timer;
public CheckAlive() {
timer = new Timer();
timer.setIevent(new check());
}
public void setRegister(RegisterServer rs) {
this.rs = rs;
}
public void startCheak() {
timer.start();
}
public void stopCheak() {
timer.stop();
}
class check implements IEvent{
@Override
public void dealEvent() {
rs.checkRobustness();
}
}
}
多文件传输是基于服务发现框架的,多以对于资源管理中心的功能是相同的,区别在于在服务发现框架中,提供者和注册中心采用的是长连接的模式,在多文件传输中我们采用的是短连接的方式。
所有对于资源管理中心来讲,既是一个短连接的服务端,也是短连接的客户端,因为资源管理中心需要链接发送端(资源提供端)检测是否还能够给记性提供资源的服务。