Java Rpc RMI

Java RmI原生的RPC。

PS: 继承Remote接口的接口方法必须手动抛出异常RemoteException,否则server都端启动会报错。

1. API端:

1.1  IHello.java

package com.xh.rmi.api;

import java.rmi.Remote;
import java.rmi.RemoteException;

public interface IHello extends Remote {

    String hello(String username) throws RemoteException;

    UserDto setAge(UserDto userDto) throws RemoteException;
}

1.2  UserDto.java

package com.xh.rmi.api;

import java.io.Serializable;

public class UserDto implements Serializable {

    private String username;

    private int age;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "UserDto{" +
                "username='" + username + '\'' +
                ", age=" + age +
                '}';
    }
}

 

2. Server端:

2.1 IHelloImpl.java

package com.xh.rmi.server;

import com.xh.rmi.api.IHello;
import com.xh.rmi.api.UserDto;

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

public class IHelloImpl extends UnicastRemoteObject implements IHello {

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

    @Override
    public String hello(String username) {
        System.out.println("Connected Successfully!");
        return String.format("你好, %s", username);
    }

    @Override
    public UserDto setAge(UserDto userDto) throws RemoteException {
        userDto.setAge(26);
        return userDto;
    }
}

2.2 ServerMain.java

package com.xh.rmi.server;

import com.xh.rmi.api.IHello;

import java.net.MalformedURLException;
import java.rmi.AlreadyBoundException;
import java.rmi.Naming;
import java.rmi.RemoteException;
import java.rmi.registry.LocateRegistry;

public class ServerMain {

    public static void main(String[] args) throws RemoteException
            , AlreadyBoundException, MalformedURLException {
        IHello hello = new IHelloImpl();
        LocateRegistry.createRegistry(8888);
        System.setProperty("java.rmi.server.hostname", "127.0.0.1");
        Naming.bind("rmi://localhost:8888/RHello", hello);
        System.out.println("远程Hello对象绑定成功!");
    }
}

3. Client端: ClientMain.java

package com.xh.rmi.client;

import com.xh.rmi.api.IHello;
import com.xh.rmi.api.UserDto;

import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;

public class ClientMain {

    public static void main(String[] args) throws RemoteException, NotBoundException, MalformedURLException {
        IHello hello = (IHello) Naming.lookup("rmi://127.0.0.1:8888/RHello");
        System.out.println(hello.hello("张三"));

        UserDto userDto = new UserDto();
        userDto.setUsername("黄雅");
        UserDto dto = hello.setAge(userDto);
        System.out.println(dto.toString());

    }
}

4. 运行结果:

4.1 server

Connected to the target VM, address: '127.0.0.1:50548', transport: 'socket'
远程Hello对象绑定成功!
Connected Successfully!
Disconnected from the target VM, address: '127.0.0.1:50548', transport: 'socket'

 

4.2 client

Connected to the target VM, address: '127.0.0.1:50621', transport: 'socket'
你好, 张三
UserDto{username='黄雅', age=26}
Disconnected from the target VM, address: '127.0.0.1:50621', transport: 'socket'

5. 源码地址

https://github.com/xieyucan/rmi-demo

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值