In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "case Analysis of the use of Netty in Dubbo". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "Netty uses case Analysis in Dubbo".
When configuring the following information
Spring parses the above tags through DubboNamespaceHandler during startup.
Register the BeanDefinition corresponding to each tag in the BeanFactory.
Spring then generates the corresponding Bean instance according to BeanDefinition.
The above tag will eventually generate the corresponding ServiceBean instance.
/ / Source code location: com.alibaba.dubbo.config.spring.ServiceBeanpublic class ServiceBean extends ServiceConfig implements InitializingBean, DisposableBean, ApplicationContextAware, ApplicationListener, BeanNameAware, ApplicationEventPublisherAware {}
ServiceBean implements the ApplicationListener interface.
After Spring has created all the Bean, a ContextRefreshedEvent event is finally issued.
Therefore, the onApplicationEvent () method of ServiceBean is executed.
Public void onApplicationEvent (ContextRefreshedEvent event) {if (isDelay () & &! isExported () & &! isUnexported ()) {if (logger.isInfoEnabled ()) {logger.info ("The service ready on spring started. Service:" + getInterface ());} / / expose service export ();}}
Then we enter the process of service exposure.
Service exposure accomplishes two things. The first thing is to open the service through Netty and listen on the port.
The second thing is to register the service with the registry.
Follow up on the export () method, and you'll end up with the DubboProtocol class.
Take a general look at how it opens the service and listens to the port. Notice that there is an attribute requestHandler.
Public class DubboProtocol extends AbstractProtocol {private ExchangeHandler requestHandler = new ExchangeHandlerAdapter () {...}
Private ExchangeServer createServer (URL url) {/ / bind server = Exchangers.bind (url, requestHandler);}
}
Follow up on the bind () method, and you'll end up with NettyServer's doOpen () method.
Notice that there is an attribute nettyServerHandler.
/ / Source code location: com.alibaba.dubbo.remoting.transport.netty4.NettyServerprotected void doOpen () throws Throwable {bootstrap = new ServerBootstrap ()
BossGroup = new NioEventLoopGroup (1, new DefaultThreadFactory ("NettyServerBoss", true)); workerGroup = new NioEventLoopGroup (getUrl () .getPositiveParameter (Constants.IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS), new DefaultThreadFactory ("NettyServerWorker", true))
/ / important Handler final NettyServerHandler nettyServerHandler = new NettyServerHandler (getUrl (), this); channels = nettyServerHandler.getChannels ()
Bootstrap.group (bossGroup, workerGroup) .channel (NioServerSocketChannel.class) .childOption (ChannelOption.TCP_NODELAY, Boolean.TRUE) .childOption (ChannelOption.SO_REUSEADDR, Boolean.TRUE) .childOption (ChannelOption.ALLOCATOR) PooledByteBufAllocator.DEFAULT) .childHandler (new ChannelInitializer () {@ Override protected void initChannel (NioSocketChannel ch) throws Exception {NettyCodecAdapter adapter = new NettyCodecAdapter (getCodec (), getUrl (), NettyServer.this) Ch.pipeline () .addLast ("decoder", adapter.getDecoder ()) .addLast ("encoder", adapter.getEncoder ()) .addLast ("handler", nettyServerHandler); / / Handler} dealing with requests and responses) / / bind ChannelFuture channelFuture = bootstrap.bind (getBindAddress ()); channelFuture.syncUninterruptibly (); channel = channelFuture.channel ()
}
From the requestHandler property of the DubboProtocol class to the nettyServerHandler property of NettyServer.
This way will go through a lot of Handler, after layers of packaging, and finally packaged into NettyServerHandler.
It will go through the following Handler
NettyServerHandler
-> NettyServer
-> MultiMessageHandler
-> HeartbeatHandler
-> AllChannelHandler
-> DecodeHandler
-> HeaderExchangeHandler
-> ExchangeHandler
When the client connects to the server or sends data to the server
The request is first processed by NettyServerHandler, then passed on in turn, and finally to ExchangeHandler.
So are all these Handler executed by the same thread? Not really.
As shown above, there is an executor property in AllChannelHandler, which is a thread pool.
NettyServerHandler
-> NettyServer
-> MultiMessageHandler
-> HeartbeatHandler
-> AllChannelHandler
The above Handler are executed by the same thread, by the IO thread of Netty, and the name is similar to NettyServerWorker-5-7
-> DecodeHandler
-> HeaderExchangeHandler
-> ExchangeHandler
The above Handler is executed by another type of thread, by a thread pool in AllChannelHandler, and the name is similar to DubboServerHandler-2.0.1.15:20880-thread-57
That is to say, after receiving the request, the IO thread of Netty executes it in turn.
NettyServerHandler-> NettyServer->
MultiMessageHandler-> HeartbeatHandler
-> AllChannelHandler these five Handler.
The subsequent DecodeHandler is then executed by the thread pool in AllChannelHandler
-> HeaderExchangeHandler-> ExchangeHandler these three Handler.
With threads=200, threadpool=fixed is the thread pool in the red area of the configuration diagram. Thread pools are also a place to tune.
At this point, I believe you have a deeper understanding of "Netty case analysis in Dubbo". 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.