java rmi怎样实现服务器间的通信

局域网中有一若干台主机,每台服务器上存有大量资源。其中一台为主服务器,其余为从服务器,主服务器先开启,其余从服务器开启后分别向主服务器发送自己的IP,表明自己开启(为后来的的资源搜索做准备)。主服务器把收到的IP汇总,放到一个文件里,分别发送的各个从服务器。
语言为java ,各位高手请问怎么实现
对照图,能具体点吗

首先要选用技术,这很明显做个socket就行,简单通讯,还可以扩展为非局域网的主机。

1,编写socket服务器端 放在主服务器上,利用开机启动的方式启动server
并保持监听某端口
2,编写socket客户端放在 你需要的从属服务器上,同样利用开机启动的方式
运行client,然后程序中利用 经过包装client.getOutputStream()的流,向服务器发送消息,就是那个ip (当字符串发,转换为byte[])

(细节我就不介绍了…… )
3,汇总的实质就是 server 利用
client.getInputStream()的包装流(假如叫in)
存放数据的数组,假如叫 bytep[] buf;
获得数据 in.read(buf,0,buf.length);

然后:
File file = new File("D://log.txt");//假如是你要的log文件
FileInputStream fis =new FileInputStream(file);
fis.write(buf);

这样就搞定了
细节注意点:由于log文件需要保留旧数据,所以写文件的时候要注意选用Acess流进行插入。另外,当从属服务器关闭的时候,也要发个信息过来,把对应的ip删除掉哦!

给我分呗
温馨提示:答案为网友推荐,仅供参考
第1个回答  2010-07-23
有几个建议
1:主服务器不用主动发IP给子服务器(主服务器不可能知道子服务器的数目),也没有必要主服务来发,完全可以让子服务器来主动向主服务索取IP。
2:结果完全没有必要以文件形式存放(每次开机要重新获得,没有必要做持久化),完全可以存到一个Set中。

根据以上:我做一个简单的实现:
***********************************************
IHost.java

package test.rmi.host;

import java.rmi.RemoteException;
/**
* remote interface
*
*/
public interface IHost extends java.rmi.Remote
{
void register(String ip) throws RemoteException;

String[] getAllSubServerList() throws RemoteException;

}

***********************************************
Host.java

package test.rmi.host.impl;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.HashSet;
import java.util.Set;

import test.rmi.host.IHost;

/**
*
* the implentation class of Ihost
*
*/
public class Host extends UnicastRemoteObject implements IHost
{
private static final long serialVersionUID = -2197983171132594422L;

private Set<String> subServers = new HashSet<String>();

public Host() throws RemoteException
{
super();
}

public String[] getAllSubServerList() throws RemoteException
{
String[] ips = new String[] {};
synchronized (this)
{
ips = subServers.toArray(ips);
}
return ips;
}

public void register(String ip) throws RemoteException
{
synchronized (this)
{
subServers.add(ip);
}
}
}
***********************************************
ServiceStartup.java

package test.rmi.host;

import java.rmi.Naming;

import test.rmi.host.impl.Host;

/**
* The main class for Host server,mainly for start up the host
*
*/
public class ServiceStartup
{
public static void main(String args[]) throws Exception
{

Host host = new Host();
Naming.rebind("HostServer", host);
}

}
***********************************************
SubServerStartup.java

package test.rmi.sub;

import java.net.InetAddress;
import java.rmi.Naming;

import test.rmi.host.IHost;

/**
* The main class for Sub server,mainly for start up the sub
*
*/
public class SubServerStartup
{
public static void main(String args[]) throws Exception
{
String hostIp = "";
IHost host = (IHost) Naming.lookup("rmi://" + hostIp + "/HostServer");
String myIp = InetAddress.getLocalHost().toString();
host.register(myIp);

//do something...
try{
//wait for other sub server start up
Thread.sleep(3000);
}catch(Exception e){}

String[] allSubServer = host.getAllSubServerList();
//do something...
}
}
***********************************************

本实现只是提供一个简单的思路和基本的技术实现,希望可以帮助到你
以上代码已经可以运行了,你说的具体点,是哪方面?是如何运行rmi程序吗?本回答被提问者采纳
第2个回答  2010-07-23
不一定非要用RIM
你可以用socket.
socket分server端程序和client端程序
你在主服务器上启动server端程序,监听某一个端口。
从服务器启动后,调用soket的client端,向主服务器的端口发送消息。主服务器收到消息后,启动一个线程来处理从服务器的请求,然后会给从服务器发送一个response。
这样就能实现你的需求了。

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网