通过rmi实现远程rpc(可以认为java自带Dubbo RPC)

背景:

发现公司几个运行10年的游戏,用的竟然是rmi,而我只听说过dubbo 和 基于netty的rpc,于是就补充了下rmi。

其次,是最近对于跨服的思考,如何避免回调也需要用同步写法,rmi比较适合。

1)api

游戏服之间的交互 // 必须抛出RemoteException异常

复制代码
package com.example.testsb.testrmi.api;

import com.example.testsb.testrmi.common.CSLogin;
import com.example.testsb.testrmi.common.SCLogin;

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

public interface IGameService extends Remote {
    // 简单的rpc接口
    String hello(String str) throws RemoteException;

    // 请求和返回都是复杂对象的rpc接口
    SCLogin login(CSLogin req) throws RemoteException;
}

2)通用部分

复制代码
package com.example.testsb.testrmi.common;

public class Constant {
    public final static int GAME_SERVICE_PORT = 6000;
    public final static String GAME_SERVICE_URL = String.format("rmi://localhost:%d/IGameService", GAME_SERVICE_PORT);
}

复杂协议 // 必须实现:Serializable接口

复制代码
package com.example.testsb.testrmi.common;

import lombok.Data;

import java.io.Serializable;

@Data
public class CSLogin implements Serializable {
    private static final long serialVersionUID = 1L;

    private String name;
    private String password;
}

package com.example.testsb.testrmi.common;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class SCLogin implements Serializable {
    private static final long serialVersionUID = 1L;

    private long uid;
}

3)服务端

复制代码
package com.example.testsb.testrmi.server;

import com.example.testsb.testrmi.api.IGameService;
import com.example.testsb.testrmi.common.Constant;
import com.example.testsb.testrmi.server.impl.GameServiceImpl;

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

public class Server {
    public static void main(String[] args) throws RemoteException, MalformedURLException {
        IGameService gameService = new GameServiceImpl();
        LocateRegistry.createRegistry(Constant.GAME_SERVICE_PORT);
        Naming.rebind(Constant.GAME_SERVICE_URL, gameService);
    }
}

/*
[2024-03-19 07:14:13.601] [RMI TCP Connection(4)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:13.602] [RMI TCP Connection(4)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:13.603] [RMI TCP Connection(4)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:13.604] [RMI TCP Connection(4)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.975] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.982] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.984] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.986] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.988] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.993] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.995] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:29.997] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:30.000] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
[2024-03-19 07:14:30.002] [RMI TCP Connection(6)-192.168.152.1] [GameServiceImpl.java:24] ([INFO][com.example.testsb.testrmi.server.impl.GameServiceImpl] 1000 123
 */

实现

复制代码
package com.example.testsb.testrmi.server.impl;

import com.example.testsb.testrmi.api.IGameService;
import com.example.testsb.testrmi.common.CSLogin;
import com.example.testsb.testrmi.common.SCLogin;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class GameServiceImpl extends UnicastRemoteObject implements IGameService {
    public GameServiceImpl() throws RemoteException {
        super();
    }

    @Override
    public String hello(String str) {
        return str + "_world";
    }

    @Override
    public SCLogin login(CSLogin req) {
        log.info("{} {}", req.getName().length(), req.getPassword());
        return new SCLogin(1);
    }
}

4)客户端

复制代码
package com.example.testsb.testrmi.client;

import com.example.testsb.testrmi.api.IGameService;
import com.example.testsb.testrmi.common.CSLogin;
import com.example.testsb.testrmi.common.Constant;
import com.example.testsb.testrmi.common.SCLogin;
import lombok.extern.slf4j.Slf4j;

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

@Slf4j
public class Client {
    public static void main(String[] args) throws MalformedURLException, NotBoundException, RemoteException {
        IGameService gameService = (IGameService) Naming.lookup(Constant.GAME_SERVICE_URL);

        String name = "";
        for (int i = 0; i < 100; i++) {
            name += "123456789a";
        }

        for (int j = 0; j < 10; j++) {
            // 测试hello
            String hello = gameService.hello("hello");
            log.info("{}", hello);

            // 测试登录接口
            CSLogin csLogin = new CSLogin();
            csLogin.setName(name);
            csLogin.setPassword("123");

            SCLogin login = gameService.login(csLogin);
            log.info("{}", login);
        }
    }
}

/*
[2024-03-19 07:14:29.970] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.977] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.981] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.982] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.983] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.984] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.985] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.986] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.987] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.988] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.993] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.994] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.995] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.996] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.997] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:29.998] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:29.999] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:30.000] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
[2024-03-19 07:14:30.001] [main] [Client.java:26] ([INFO][com.example.testsb.testrmi.client.Client] hello_world
[2024-03-19 07:14:30.003] [main] [Client.java:33] ([INFO][com.example.testsb.testrmi.client.Client] SCLogin(uid=1)
 */

总结:

1.rmi可以看出来非常简单。

2.基于java的序列化和反序列化。

3.已解决粘包问题。

相关推荐
{⌐■_■}8 小时前
【gRPC】HTTP/2协议,HTTP/1.x中线头阻塞问题由来,及HTTP/2中的解决方案,RPC、Protobuf、HTTP/2 的关系及核心知识点汇总
网络·网络协议·计算机网络·http·rpc·golang
凯雀安全1 天前
printspoofer的RPC调用接口的简单代码
qt·网络协议·rpc
chilavert3181 天前
从RPA项目说说RPC和MQ的使用。
开发语言·qt·rpc·rabbitmq
名誉寒冰3 天前
# KVstorageBaseRaft-cpp 项目 RPC 模块源码学习
qt·学习·rpc
what_20183 天前
分布式2(限流算法、分布式一致性算法、Zookeeper )
分布式·网络协议·rpc
AI+程序员在路上4 天前
Web Service及其实现技术(SOAP、REST、XML-RPC)介绍
xml·rpc·web
wumu_Love6 天前
git 报错:错误:RPC 失败。curl 28 Failed to connect to github.com port 443 after 75000
git·rpc·github
星星点点洲8 天前
RPC、gRPC和HTTP的区别
网络协议·http·rpc
极小狐8 天前
如何减少极狐GitLab 容器镜像库存储?
运维·git·rpc·kubernetes·ssh·gitlab·terraform
Mr-Apple10 天前
Decode rpc invocation failed: null -> DecodeableRpcInvocation
网络·网络协议·rpc