记录学习JMI的开发过程及遇到的问题

(156) 2024-04-14 10:01:01

1.JMI开发背景:java的原生RPC方案,现阶段想学习一下,但是碰到以下问题,还未解决,希望知道怎么解决的给提个方案。

开发逻辑分为四部分:

        1. 继承java.rmi.Remote接口提供抽象方法;

        2. 继承UnicastRemoteObject,实现Remote接口;

        3. 提供服务端程序

        4. 提供客户端程序

1. 继承Remote接口提供抽象方法

package com.company;

import java.rmi.Remote;

public interface IRService extends Remote {
    String service(String content) throws  Exception;
}

2. 继承UnicastRemoteObject,实现Remote接口;

package com.company;

import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;

public class ServiceImpl extends UnicastRemoteObject implements IRService {

    private String name;

    protected ServiceImpl(String name) throws RemoteException {
        this.name = name;
    }

    @Override
    public String service(String content) throws Exception {
        return "server +"+content;
    }
}

3. 提供服务端程序

package com.company;

import javax.naming.Context;
import javax.naming.InitialContext;

public class Server {
    public static void main(String[] args) {
        try{
            IRService irService = new ServiceImpl("service02");
            Context initialContext = new InitialContext();
            initialContext.rebind("rmi:127.0.0.1/service02",irService);
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.println("000000!");
    }
}

4. 提供客户端程序

package com.company;

import javax.naming.Context;
import javax.naming.InitialContext;

public class Client {
    public static void main(String[] args) {
        String url = "rmi://127.0.0.1/";
        try{
            Context initialContext = new InitialContext();
            IRService irservice = (IRService)initialContext.lookup(url+"service02");
            Class subClass = irservice.getClass();
            System.out.println(irservice+"is"+subClass.getName());

            Class[] interfaces = subClass.getInterfaces();
            for(int i = 0;i<interfaces.length;i++){
                System.out.println("implements"+interfaces[i].getName()+"interface");
            }
            System.out.println(irservice.service("hello"));
        }catch (Exception e){
            e.printStackTrace();
        }
    }
}

测试结果:

1.启动服务端程序报错:

D:\software\jdk9\bin\java.exe -agentlib:jdwp=transport=dt_socket,address=127.0.0.1:56250,suspend=y,server=n -javaagent:C:\Users\genghongbo\AppData\Local\JetBrains\IntelliJIdea2021.2\captureAgent\debugger-agent.jar -Dfile.encoding=UTF-8 -classpath "D:\ideaWK\test\out\production\test;D:\software\IntelliJ IDEA 2021.2\lib\idea_rt.jar" com.company.Server
Connected to the target VM, address: '127.0.0.1:56250', transport: 'socket'
javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 192.168.43.181; nested exception is: 
	java.net.ConnectException: Connection refused: connect]
	at jdk.naming.rmi/com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:178)
	at java.naming/com.sun.jndi.toolkit.url.GenericURLContext.rebind(GenericURLContext.java:253)
	at java.naming/javax.naming.InitialContext.rebind(InitialContext.java:425)
	at com.company.Server.main(Server.java:11)
Caused by: java.rmi.ConnectException: Connection refused to host: 192.168.43.181; nested exception is: 
	java.net.ConnectException: Connection refused: connect
	at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
	at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:214)
	at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:200)
	at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
	at java.rmi/sun.rmi.registry.RegistryImpl_Stub.rebind(RegistryImpl_Stub.java:147)
	at jdk.naming.rmi/com.sun.jndi.rmi.registry.RegistryContext.rebind(RegistryContext.java:176)
	... 3 more
Caused by: java.net.ConnectException: Connection refused: connect
	at java.base/java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.base/java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
	at java.base/java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at java.base/java.net.Socket.connect(Socket.java:540)
	at java.base/java.net.Socket.<init>(Socket.java:436)
	at java.base/java.net.Socket.<init>(Socket.java:213)
	at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
	at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
	... 8 more
000000!

2.启动客户端程序报错:

D:\software\jdk9\bin\java.exe "-javaagent:D:\software\IntelliJ IDEA 2021.2\lib\idea_rt.jar=62700:D:\software\IntelliJ IDEA 2021.2\bin" -Dfile.encoding=UTF-8 -classpath D:\ideaWK\test\out\production\test com.company.Client
javax.naming.ServiceUnavailableException [Root exception is java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
	java.net.ConnectException: Connection refused: connect]
	at jdk.naming.rmi/com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:137)
	at java.naming/com.sun.jndi.toolkit.url.GenericURLContext.lookup(GenericURLContext.java:207)
	at java.naming/javax.naming.InitialContext.lookup(InitialContext.java:409)
	at com.company.Client.main(Client.java:11)
Caused by: java.rmi.ConnectException: Connection refused to host: 127.0.0.1; nested exception is: 
	java.net.ConnectException: Connection refused: connect
	at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:619)
	at java.rmi/sun.rmi.transport.tcp.TCPChannel.createConnection(TCPChannel.java:214)
	at java.rmi/sun.rmi.transport.tcp.TCPChannel.newConnection(TCPChannel.java:200)
	at java.rmi/sun.rmi.server.UnicastRef.newCall(UnicastRef.java:338)
	at java.rmi/sun.rmi.registry.RegistryImpl_Stub.lookup(RegistryImpl_Stub.java:112)
	at jdk.naming.rmi/com.sun.jndi.rmi.registry.RegistryContext.lookup(RegistryContext.java:133)
	... 3 more
Caused by: java.net.ConnectException: Connection refused: connect
	at java.base/java.net.DualStackPlainSocketImpl.connect0(Native Method)
	at java.base/java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
	at java.base/java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:400)
	at java.base/java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:243)
	at java.base/java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:225)
	at java.base/java.net.PlainSocketImpl.connect(PlainSocketImpl.java:148)
	at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:402)
	at java.base/java.net.Socket.connect(Socket.java:591)
	at java.base/java.net.Socket.connect(Socket.java:540)
	at java.base/java.net.Socket.<init>(Socket.java:436)
	at java.base/java.net.Socket.<init>(Socket.java:213)
	at java.rmi/sun.rmi.transport.tcp.TCPDirectSocketFactory.createSocket(TCPDirectSocketFactory.java:40)
	at java.rmi/sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
	... 8 more

Process finished with exit code 0

THE END

发表回复