Implementing a remote Method call (RPC) using Netty

Source: Internet
Author: User

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:

  
 
  1.    final static MessageSendExecutor executor = new MessageSendExecutor("127.0.0.1:8686",RpcSerializeProtocol.KRYOSERIALIZE.name());

  2.    final static Calculate cal = executor.execute(Calculate.class);


  3.    public static void main(String[] args) throws Exception {


  4.         for Span class= "pun" > ( int i = 1 ; i < 100000 ; i ++)

  5.        ThreadExecuteUtil.submitTaskBlock(ThreadExecuteUtil.CLIENT, new Runnable() {


  6.            @Override

  7.            public void run() {

  8.                long start = System.currentTimeMillis();

  9.                int result = cal.add(10, 20);

  10.                System.out.println("result:"+result);

  11.                long end = System.currentTimeMillis();

  12.                System.out.println(end-start);

  13.            }

  14.        }, 1000);


  15.    }



Implementing a remote Method call (RPC) using Netty

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.