-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
- Loading branch information
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<module type="JAVA_MODULE" version="4"> | ||
<component name="NewModuleRootManager" inherit-compiler-output="true"> | ||
<exclude-output /> | ||
<content url="file://$MODULE_DIR$"> | ||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" /> | ||
</content> | ||
<orderEntry type="inheritedJdk" /> | ||
<orderEntry type="sourceFolder" forTests="false" /> | ||
<orderEntry type="library" name="KotlinJavaRuntime" level="project" /> | ||
</component> | ||
</module> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
classname=reflection.Person | ||
methodname=staticMethod |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package RMI; | ||
|
||
import java.rmi.registry.LocateRegistry; | ||
import java.rmi.registry.Registry; | ||
|
||
public class ClientSimple { | ||
public static void main(String[] args) { | ||
try{ | ||
Registry registry = LocateRegistry.getRegistry(9999); // 得到端口号为9999的RMI注册器 | ||
|
||
Service service1 = (Service) registry.lookup("RemoteService1"); | ||
Service service2 = (Service) registry.lookup("RemoteService2"); | ||
|
||
Class stubClass = service1.getClass(); | ||
System.out.println("service1是"+ stubClass.getName() +"的实例"); | ||
|
||
Class[] stubInterface = stubClass.getInterfaces(); | ||
for (int i = 0; i < stubInterface.length; i++){ | ||
System.out.println(stubInterface[i]); | ||
} | ||
|
||
System.out.println(service1.echo("Hello!")); | ||
System.out.println(service2.echo("Hi!")); | ||
|
||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package RMI; | ||
|
||
import RMI_RPC.Server.HelloService; | ||
import RMI_RPC.Server.HelloServiceImpl; | ||
|
||
import java.rmi.registry.LocateRegistry; | ||
import java.rmi.registry.Registry; | ||
import java.util.concurrent.Executors; | ||
|
||
public class ServerSimple { | ||
public static void main(String[] args) { | ||
try { | ||
Service service1 = new ServiceImpl("service1"); | ||
Service service2 = new ServiceImpl("service2"); | ||
|
||
// 创建并启动注册器Registry | ||
Registry registry = LocateRegistry.createRegistry(9999); | ||
System.out.println("\n服务器已启动...\n"); | ||
|
||
registry.rebind("RemoteService1",service1); // service1 是一个 远程对象,名字是 RemoteService1 | ||
registry.rebind("RemoteService2",service2); | ||
|
||
System.out.println("创建了两个远程对象: "); | ||
System.out.println(service1.getClass().getName()); | ||
System.out.println(service2.getClass().getName()); | ||
|
||
// 该程序不会停止,因为RMI注册器会一直监听9999端口,监听客户端有没有查找远程对象 | ||
|
||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package RMI; | ||
|
||
import java.rmi.Remote; | ||
import java.rmi.RemoteException; | ||
|
||
public interface Service extends Remote { | ||
public String echo (String msg) throws RemoteException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package RMI; | ||
|
||
import java.rmi.Remote; | ||
import java.rmi.RemoteException; | ||
|
||
public interface ServiceFactory extends Remote { | ||
public Service getService(String serviceName) throws RemoteException; | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package RMI; | ||
|
||
import java.rmi.RemoteException; | ||
import java.rmi.server.UnicastRemoteObject; | ||
|
||
public class ServiceImpl extends UnicastRemoteObject implements Service { | ||
|
||
private String name; | ||
|
||
protected ServiceImpl(String name) throws RemoteException { | ||
this.name = name; | ||
} | ||
|
||
// client -远程调用-> server.service | ||
// 在 server 执行完毕后,将结果返回 client | ||
@Override | ||
public String echo(String msg) throws RemoteException{ | ||
System.out.println(name + ": 调用echo()方法。"); | ||
return "echo: " + msg + " from " + name; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package RMI_RPC.Client; | ||
|
||
public interface Client { | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package RMI_RPC.Client; | ||
|
||
import java.io.ObjectInputStream; | ||
import java.io.ObjectOutputStream; | ||
import java.lang.reflect.InvocationHandler; | ||
import java.lang.reflect.Method; | ||
import java.net.Inet4Address; | ||
import java.net.InetSocketAddress; | ||
import java.lang.reflect.Proxy; | ||
import java.net.Socket; | ||
|
||
import static java.net.Proxy.*; | ||
|
||
public class ClientImpl { | ||
// 获取代表服务端接口的动态代理对象 | ||
// serviceName: 请求的接口名 | ||
// addr: 带请求服务端的ip:端口 | ||
public static <T> T getRemoteProxyObj(Class serviceInterface, InetSocketAddress addr) { | ||
/* | ||
Proxy.newProxyInstance(a,b,c) | ||
a: 类加载器:要代理哪个类,就将那个类的类加载器传入第一个参数 | ||
b:需要代理的对象,具有哪些方法 --接口 | ||
*/ | ||
return (T) Proxy.newProxyInstance(serviceInterface.getClassLoader(), new Class<?>[]{serviceInterface}, new InvocationHandler() { | ||
@Override | ||
// proxy: 代理的对象 | ||
// method:代理的方法 | ||
// args:代理方法的参数列表 | ||
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { | ||
// 客户端向服务端发送请求:请求具体某一个接口 | ||
Socket client = new Socket(); | ||
ObjectOutputStream out = null; | ||
ObjectInputStream in = null; | ||
|
||
try{ | ||
// socketAddress : IP + Port | ||
client.connect(addr); | ||
// 发送:序列化流(对象流) | ||
out = new ObjectOutputStream(client.getOutputStream()); | ||
// 需要发送的内容: | ||
// 1、接口的名字 | ||
out.writeUTF(serviceInterface.getName()); | ||
// 2、方法名及其参数、参数类型 | ||
out.writeUTF(method.getName()); | ||
out.writeObject(method.getParameterTypes()); | ||
out.writeObject(args); | ||
// 等待服务端处理... | ||
|
||
// 接收服务端处理后的返回值 | ||
in = new ObjectInputStream(client.getInputStream()); | ||
// 客户端 -> 服务端 -> 返回值 | ||
return in.readObject(); | ||
} catch (Exception e){ | ||
e.printStackTrace(); | ||
return null; | ||
} | ||
finally { | ||
if (in != null){ | ||
in.close(); | ||
} | ||
if (out != null){ | ||
out.close(); | ||
} | ||
} | ||
} | ||
}); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package RMI_RPC.Server; | ||
|
||
public interface HelloService { | ||
public String sayHi(String name); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package RMI_RPC.Server; | ||
|
||
public class HelloServiceImpl implements HelloService{ | ||
|
||
@Override | ||
public String sayHi(String name) { | ||
return "hi! "+ name; | ||
} | ||
} |