全部博文(2065)
分类: Java
2009-11-09 12:31:46
接口即服务!!!
RMI的架构图
The Transport Layer makes
the connection between JVMs.
All connections are stream-based network connections that use TCP/IP.
两台不同的主机之间的JVM之间的传输是建立在TCP/IP上面的。
How does a client find an RMI remote
service?
A:
Clients find remote services by using a naming or directory service.
This may seem like circular logic.
How can a client locate a service by using a service?
In fact, that is exactly the case.
A naming or directory service is run on a well-known host and port
number.
即指定好服务器的域名与端口就可以正常作通讯处理了!
RMI can use many different directory services, including
the Java Naming and Directory Interface (JNDI).
RMI itself includes a simple service called the RMI Registry,
rmiregistry
.
The RMI Registry runs on each machine that hosts remote service
objects and accepts queries for services, by default on port 1099.
RMI可以使用JNDI的哦。也可以使用自身的注册服务机制的!
On the client side, the RMI Registry is accessed through
the static class
.
It provides the method
that a client uses to query a registry.
The method
lookup()
accepts a URL
that specifies the server host name and the name
of the desired service.
The method returns a remote reference to the service object.
The URL takes the form:
rmi://
[:]
/
客户端会去寻找到这个服务出来!
学会如何去构建一个简单的RMI
A working RMI system is composed of several parts.
可以生成这样的两个文件出来:Stud文件!
开始绑定服务。启动服务才能监听到端口!
import java.rmi.Naming;
public class CalculatorServer {
public CalculatorServer() {
try {
Calculator c = new CalculatorImpl();
Naming.bind("rmi://localhost:1025/CalculatorService", c);
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new CalculatorServer();
}
}
因为我的电脑端口1099被占用就只能用其他的端口号了!
5、编写客户端代码
import cn.tianya.server.Calculator;
public class CalculatorClient {
public static void main(String[] args) {
try {
Calculator c = (Calculator)Naming.lookup("rmi://localhost:1025/CalculatorService");
System.out.println(c.sub(4, 6));
} catch (Exception e) {
e.printStackTrace();
}
}
}
6.
If all goes well, the registry will start running and you can switch
to the next console.
In the second console start the server hosting the
CalculatorService
, and enter the following:
>java CalculatorServer
It will start, load the implementation into memory and wait for a client connection.
In the last console, start the client program.
>java CalculatorClient
If all goes well you will see the following output:
That's it; you have created a working RMI system.
Even though you ran the three consoles on the same computer, RMI
uses your network stack and TCP/IP to communicate between the
three separate JVMs.
This is a full-fledged RMI system.
JVM之间的通讯是通过走TCP/IP的!
When the propertyjava.rmi.server.useCodebaseOnly
is set totrue
, then the JVM will load
classes from either a location specified by
theCLASSPATH
environment variable
or the URL specified in this property.
Since almost all firewalls recognize the HTTP protocol,
可以好好看看主块的东西哦!
This causes the execution of the CGI script, java-rmi.cgi 走CGI了!