Implementing a remote Method call (RPC) using Netty
In many cases, we may need to call the remote method. For example, we have a unified filter, other services need to call Bron filter for weighing, for example, we need to call the unified cache data, such as we need to invoke some service methods across machines, and so on. Remote method calls can be used at these times.
Next, you'll begin by explaining the steps to implement a remote method call using Netty.
The Code directory structure is:
NETTYNETTY/CLIENT//RPC message Callback Class NETTY/CLIENT/MESSAGECALLBACK.JAVA//RPC client pipeline initialization netty/client/ MESSAGESENDCHANNELINITIALIZER.JAVA//RPC Client Message Delivery Execution (dynamic proxy) class netty/client/messagesendexecutor.java// RPC Client message Send processing class NETTY/CLIENT/MESSAGESENDHANDLER.JAVA//RPC client thread task processing netty/client/messagesendinitializetask.java// RPC Client Agent NETTY/CLIENT/MESSAGESENDPROXY.JAVA//RPC Client Message Serialization protocol framework netty/client/rpcsendserializeframe.java// RPC Client server configuration load netty/client/rpcserverloader.javanetty/interfnetty/interf.impl//interface implementation class netty/interf/impl/ calculateimpl.java//interface NETTY/INTERF/CALCULATE.JAVANETTY/SERIALIZENETTY/SERIALIZE.KRYO//KRYO RPC messages are encoded, Decoding class netty/serialize/kryo/kryocodecutil.java//kryo xxxnetty/serialize/kryo/kryodecoder.java//kryo Encoder netty/serialize/kryo/kryoencoder.java//kryo Factory class netty/serialize/kryo/kryopoolfactory.java//kryo RPC Serialization class NETTY/SERIALIZE/KRYO/KRYOSERIALIZE.JAVA//RPC message encoding, decoding interface netty/serialize/messagecodecutil.java//message xxx netty/serialize/messagedecoder.java//Message encoder NETTY/SERIALIZE/MESSAGEENCODER.JAVA//RPC message sequenceThe NETTY/SERIALIZE/RPCSERIALIZE.JAVA//RPC/deserialization interface defines the sequence of message sequencing protocol selector Interfaces netty/serialize/rpcserializeframe.java// RPC Message Order serialization protocol type netty/serialize/rpcserializeprotocol.javanetty/server//service-side: thread pool exception policy netty/server/ Abortpolicywithreport.java//rpc Server execution Module netty/server/messagerecvchannelinitializer.java//Server execution Module netty/server/ MESSAGERECVEXECUTOR.JAVA//RPC Server Message processing NETTY/SERVER/MESSAGERECVHANDLER.JAVA//RPC Server message Thread task processing netty/server/ messagerecvinitializetask.java//Thread Factory: It's actually a wrapper over the runable, setting some information and monitoring information on the thread netty/server/namedthreadfactory.java// RPC server-side Message Serialization protocol framework netty/server/rpcrecvserializeframe.java//custom thread pool netty/server/rpcthreadpool.java//message request Body netty/ messagerequest.java//response of the request Body netty/messageresponse.java//client netty/nettyclient.java//Server Netty/nettyserver.java
To start the Nettyserver server:
public static void Main (string[] args) {Messagerecvexecutor executor = new Messagerecvexecutor ("127.0.0.1:8686", R PcSerializeProtocol.KRYOSERIALIZE.name ()); try {executor.startserver (); } catch (Exception e) {e.printstacktrace (); } }
Console printing:
RPC Server Start Success!ip:127.0.0.1port:8686protocol:rpcserializeprotocol[serializeprotocol=kryo,name= Kryoserialize,ordinal=1]
Start the Nettyclient client and call the Add method in the Calculate interface remotely:
final static MessageSendExecutor executor = new MessageSendExecutor("127.0.0.1:8686",RpcSerializeProtocol.KRYOSERIALIZE.name());
final static Calculate cal = executor.execute(Calculate.class);
public static void main(String[] args) throws Exception {
-
for Span class= "pun" > ( int i = 1 ; i < 100000 ; i ++)
ThreadExecuteUtil.submitTaskBlock(ThreadExecuteUtil.CLIENT, new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
int result = cal.add(10, 20);
System.out.println("result:"+result);
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}, 1000);
}
Implementing a remote Method call (RPC) using Netty