0%

java学习之JNDI-RMI篇

what

java命名和目录接口(java Naming and Directory Interface,简称jndi),是java的一个目录服务应用接口,它提供一个目录系统,并将服务名称与对象关联起来,从而使得开发人员在开发过程中可以使用名称来访问对象。

JNDI体系结构由一个API(应用程序编程接口)和一个SPI(服务提供商接口)组成。Java应用程序使用此API访问各种命名和目录服务。SPI支持透明地插入各种命名和目录服务,从而允许使用JNDI技术的API的Java应用程序访问其服务。

在官方手册中有说四个spi

image-20200903161957063

how

JNDI是一个抽象接口,接下来用RMI实现一下JNDI体系过程

前置知识

RMI可通过rmic命令生成静态远程对象通信的存根

也可通过UnicastRemoteObject类实现生成动态的存根

image-20200904170318797

LocateRegistry获取对特定主机(包括本地主机)上的引导程序远程对象注册表的引用,或创建接受特定端口上的调用的远程对象注册表

image-20200904170259940

Registry是一个简单远程对象注册表的远程接口,它提供了存储和检索以任意字符串名称绑定的远程对象引用的方法

image-20200904170628003

实现

创建一个远程接口类hello.java

1
2
3
4
5
6
7
8
package jndi.rmi;

import java.rmi.Remote;

public interface Hello extends Remote{
String sayHello() throws java.rmi.RemoteException;

}

创建rmi服务端RmiServer.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package jndi.rmi;

import java.rmi.AlreadyBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;
import java.rmi.server.UnicastRemoteObject;


public class RmiServer implements Hello{
public RmiServer() {
// TODO Auto-generated constructor stub
}

@Override
public String sayHello() throws RemoteException {
return "Hello Wolrd! I'm Sherlock Lee!!!";
}
public static void main(String[] args) {
try {
//创建和导出远程对象
RmiServer testRmiServer = new RmiServer();
Hello stubHello = (Hello) UnicastRemoteObject.exportObject(testRmiServer,0);
// 注册且绑定远程对象
Registry helloRegistry = LocateRegistry.createRegistry(1099);
helloRegistry.bind("HelloY", stubHello);
System.out.println("Rmi Server is beginning.Connecting me and giving you some respones.........");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (AlreadyBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}

创建rmi客户端RmiClient.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
package jndi.rmi;

import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;
import java.rmi.registry.Registry;

public class RmiClient {
private RmiClient() {}

public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(args[0]);
String host = (args.length < 1) ? "localhost" : args[0];
String portString = (args.length < 2) ? "1099" : args[1];
int port = Integer.parseInt(portString);
try {
//注册且获取远程对象
Registry rmiRegistry = LocateRegistry.getRegistry(host,port);
Hello stub = (Hello) rmiRegistry.lookup("HelloY");
String response = stub.sayHello();
System.out.println("Response is "+ "\""+response+"\"");
} catch (RemoteException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NotBoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}

}

就这样就可以简单的建立一个rmi通信

References

https://blog.csdn.net/u012734723/article/details/78023382

https://www.jianshu.com/p/2c78554a3f36