In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article focuses on "how to use Netty to achieve a simple RPC", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn "how to use Netty to achieve simple RPC"!
1. Demand
Imitating dubbo, the consumer and provider agree on the interface and protocol, the consumer remotely invokes the provider, the provider returns a string, and the consumer prints the data returned by the provider. The underlying network communication uses Netty 4.1.16.
2. Design
Create an interface that defines abstract methods. Used for agreements between consumers and providers.
Create a provider that needs to listen to the consumer's request and return data as agreed.
To create a consumer, this class needs to transparently call methods that it does not exist, and internally needs to use Netty to request the provider to return data.
3. Realize
1. Create a maven project and import Netty 4.1.16.
Cn.thinkinjavarpc-demo1.0-SNAPSHOT io.netty netty-all 4.1.16.Final
two。 The project directory structure is as follows:
3. Design interface
=
A simple hello world:
Public interface HelloService {String hello (String msg);}
4. Provider-related implementation
=
4.1. First implement the convention interface, which is used to return client data:
/ * implementation class * / public class HelloServiceImpl implements HelloService {public String hello (String msg) {return msg! = null? Msg + "- > I am fine.": "I am fine.";}}
4.2. Implement Netty server and custom handler
Start the Netty Server code:
Private static void startServer0 (String hostName, int port) {try {ServerBootstrap bootstrap = new ServerBootstrap (); NioEventLoopGroup eventLoopGroup = new NioEventLoopGroup (); bootstrap.group (eventLoopGroup) .channel (NioServerSocketChannel.class) .childHandler (new ChannelInitializer () {@ Override protected void initChannel (SocketChannel ch) throws Exception {ChannelPipeline p = ch.pipeline (); p.addLast (new StringDecoder ()) P.addLast (new StringEncoder ()); p.addLast (new HelloServerHandler ());}}); bootstrap.bind (hostName, port). Sync ();} catch (InterruptedException e) {e.printStackTrace ();}}
The above code adds a codec handler of type String and a custom handler.
The custom handler logic is as follows:
/ * used to process request data * / public class HelloServerHandler extends ChannelInboundHandlerAdapter {@ Override public void channelRead (ChannelHandlerContext ctx, Object msg) {/ / how to comply with the convention, call the local method and return the data if (msg.toString (). StartsWith (ClientBootstrap.providerName)) {String result = new HelloServiceImpl () .hello (msg.toString (). Substring (msg.toString (). LastIndexOf ("#") + 1); ctx.writeAndFlush (result) }}}
It is shown here to determine whether or not to comply with the convention (not using a complex protocol, just a string judgment), then create a concrete implementation class and call the method to write back to the client. Why is Netty so popular? Why?
You also need a startup class:
Public class ServerBootstrap {public static void main (String [] args) {NettyServer.startServer ("localhost", 8088);}}
Well, the code about the provider is finished, mainly to create a netty server, implement a custom handler, and custom handler to determine whether it conforms to the agreement between them (it's a protocol). If so, create an implementation class for the interface and call its method to return a string.
5. Consumer-related implementation
One thing consumers need to pay attention to is that calls need to be transparent, that is, framework users do not have to care about the underlying network implementation. Here we can use JDK's dynamic proxy to do this.
Idea: the client calls the proxy method, returns a proxy object that implements the HelloService interface, calls the method of the proxy object, and returns the result.
We need to tamper with the proxy. When we call the proxy method, we need to initialize the Netty client and request data from the server and return the data.
5.1. First create a class related to the agent
Public class RpcConsumer {private static ExecutorService executor = Executors. NewFixedThreadPool (Runtime.getRuntime (). AvailableProcessors ()); private static HelloClientHandler client; / * create a proxy object * / public. At this point, I believe you have a deeper understanding of "how to use Netty to achieve simple RPC". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.