Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the function of NettyServer in dubbo

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

What is the role of NettyServer in dubbo? I believe many inexperienced people don't know what to do about it. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.

AbstractServer

Dubbo-2.7.3/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/transport/AbstractServer.java

Public abstract class AbstractServer extends AbstractEndpoint implements Server {protected static final String SERVER_THREAD_POOL_NAME = "DubboServerHandler"; private static final Logger logger = LoggerFactory.getLogger (AbstractServer.class); ExecutorService executor; private InetSocketAddress localAddress; private InetSocketAddress bindAddress; private int accepts; private int idleTimeout; public AbstractServer (URL url, ChannelHandler handler) throws RemotingException {super (url, handler); localAddress = getUrl (). ToInetSocketAddress () String bindIp = getUrl (). GetParameter (Constants.BIND_IP_KEY, getUrl (). GetHost ()); int bindPort = getUrl (). GetParameter (Constants.BIND_PORT_KEY, getUrl (). GetPort ()); if (url.getParameter (ANYHOST_KEY, false) | | NetUtils.isInvalidLocalHost (bindIp)) {bindIp = ANYHOST_VALUE;} bindAddress = new InetSocketAddress (bindIp, bindPort) This.accepts = url.getParameter (ACCEPTS_KEY, DEFAULT_ACCEPTS); this.idleTimeout = url.getParameter (IDLE_TIMEOUT_KEY, DEFAULT_IDLE_TIMEOUT); try {doOpen (); if (logger.isInfoEnabled ()) {logger.info ("Start" + getClass (). GetSimpleName () + "bind" + getBindAddress () + ", export" + getLocalAddress ()) } catch (Throwable t) {throw new RemotingException (url.toInetSocketAddress (), null, "Failed to bind" + getClass (). GetSimpleName () + "on" + getLocalAddress () + ", cause:" + t.getMessage (), t);} / / fixme replace this with better method DataStore dataStore = ExtensionLoader.getExtensionLoader (DataStore.class). GetDefaultExtension () Executor = (ExecutorService) dataStore.get (Constants.EXECUTOR_SERVICE_COMPONENT_KEY, Integer.toString (url.getPort ();} protected abstract void doOpen () throws Throwable; protected abstract void doClose () throws Throwable; @ Override public void reset (URL url) {if (url = = null) {return } try {if (url.hasParameter (ACCEPTS_KEY)) {int a = url.getParameter (ACCEPTS_KEY, 0); if (a > 0) {this.accepts = a;} catch (Throwable t) {logger.error (t.getMessage (), t) } try {if (url.hasParameter (IDLE_TIMEOUT_KEY)) {int t = url.getParameter (IDLE_TIMEOUT_KEY, 0); if (t > 0) {this.idleTimeout = t } catch (Throwable t) {logger.error (t.getMessage (), t);} try {if (url.hasParameter (THREADS_KEY) & & executor instanceof ThreadPoolExecutor & &! executor.isShutdown ()) {ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor Int threads = url.getParameter (THREADS_KEY, 0); int max = threadPoolExecutor.getMaximumPoolSize (); int core = threadPoolExecutor.getCorePoolSize (); if (threads > 0 & (threads! = max | | threads! = core)) {if (threads)

< core) { threadPoolExecutor.setCorePoolSize(threads); if (core == max) { threadPoolExecutor.setMaximumPoolSize(threads); } } else { threadPoolExecutor.setMaximumPoolSize(threads); if (core == max) { threadPoolExecutor.setCorePoolSize(threads); } } } } } catch (Throwable t) { logger.error(t.getMessage(), t); } super.setUrl(getUrl().addParameters(url.getParameters())); } @Override public void send(Object message, boolean sent) throws RemotingException { Collection channels = getChannels(); for (Channel channel : channels) { if (channel.isConnected()) { channel.send(message, sent); } } } @Override public void close() { if (logger.isInfoEnabled()) { logger.info("Close " + getClass().getSimpleName() + " bind " + getBindAddress() + ", export " + getLocalAddress()); } ExecutorUtil.shutdownNow(executor, 100); try { super.close(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } try { doClose(); } catch (Throwable e) { logger.warn(e.getMessage(), e); } } @Override public void close(int timeout) { ExecutorUtil.gracefulShutdown(executor, timeout); close(); } @Override public InetSocketAddress getLocalAddress() { return localAddress; } public InetSocketAddress getBindAddress() { return bindAddress; } public int getAccepts() { return accepts; } public int getIdleTimeout() { return idleTimeout; } @Override public void connected(Channel ch) throws RemotingException { // If the server has entered the shutdown process, reject any new connection if (this.isClosing() || this.isClosed()) { logger.warn("Close new channel " + ch + ", cause: server is closing or has been closed. For example, receive a new connect request while in shutdown process."); ch.close(); return; } Collection channels = getChannels(); if (accepts >

0 & & channels.size () > accepts) {logger.error ("Close channel" + ch + ", cause: The server" + ch.getLocalAddress () + "connections greater than max config" + accepts); ch.close (); return;} super.connected (ch);} @ Override public void disconnected (Channel ch) throws RemotingException {Collection channels = getChannels () If (channels.isEmpty ()) {logger.warn ("All clients has disconnected from" + ch.getLocalAddress () + ". You can graceful shutdown now. ");} super.disconnected (ch);}}

The constructor of AbstractServer reads bindAddress, accepts, idleTimeout from url, and then executes the doOpen method; the close method closes the executor, executes the parent class close method, and then executes the doClose method; the connected method first determines whether the channels exceeds the channels value, and if it exceeds the close;disconnected method, the parent class disconnected method is executed

NettyServer

Dubbo-2.7.3/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyServer.java

Public class NettyServer extends AbstractServer implements Server {private static final Logger logger = LoggerFactory.getLogger (NettyServer.class); / * * the cache for alive worker channel. * / private Map channels; / * * netty server bootstrap. * / private ServerBootstrap bootstrap; / * * the boss channel that receive connections and dispatch these to worker channel. * / private io.netty.channel.Channel channel; private EventLoopGroup bossGroup; private EventLoopGroup workerGroup; public NettyServer (URL url, ChannelHandler handler) throws RemotingException {/ / you can customize name and type of client thread pool by THREAD_NAME_KEY and THREADPOOL_KEY in CommonConstants. / / the handler will be warped: MultiMessageHandler- > HeartbeatHandler- > handler super (url, ChannelHandlers.wrap (handler, ExecutorUtil.setThreadName (url, SERVER_THREAD_POOL_NAME));} / * * Init and start netty server * * @ throws Throwable * / @ Override protected void doOpen () throws Throwable {bootstrap = new ServerBootstrap (); bossGroup = new NioEventLoopGroup (1, new DefaultThreadFactory ("NettyServerBoss", true)) WorkerGroup = new NioEventLoopGroup (getUrl (). GetPositiveParameter (IO_THREADS_KEY, Constants.DEFAULT_IO_THREADS), new DefaultThreadFactory ("NettyServerWorker", true)); 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 {/ / FIXME: should we use getTimeout ()? Int idleTimeout = UrlUtils.getIdleTimeout (getUrl ()); NettyCodecAdapter adapter = new NettyCodecAdapter (getCodec (), getUrl (), NettyServer.this) Ch.pipeline () / / .addLast ("logging", new LoggingHandler (LogLevel.INFO)) / / for debug .addLast ("decoder", adapter.getDecoder ()) .addLast ("encoder", adapter.getEncoder ()) .addLast ("server-idle-handler", new IdleStateHandler (0,0, idleTimeout) MILLISECONDS) .addLast ("handler", nettyServerHandler) }}); / / bind ChannelFuture channelFuture = bootstrap.bind (getBindAddress ()); channelFuture.syncUninterruptibly (); channel = channelFuture.channel ();} @ Override protected void doClose () throws Throwable {try {if (channel! = null) {/ / unbind. Channel.close ();}} catch (Throwable e) {logger.warn (e.getMessage (), e);} try {Collection channels = getChannels () If (channels! = null & & channels.size () > 0) {for (org.apache.dubbo.remoting.Channel channel: channels) {try {channel.close ();} catch (Throwable e) {logger.warn (e.getMessage (), e) } catch (Throwable e) {logger.warn (e.getMessage (), e);} try {if (bootstrap! = null) {bossGroup.shutdownGracefully (); workerGroup.shutdownGracefully () } catch (Throwable e) {logger.warn (e.getMessage (), e);} try {if (channels! = null) {channels.clear ();}} catch (Throwable e) {logger.warn (e.getMessage (), e) } @ Override public Collection getChannels () {Collection chs = new HashSet (); for (Channel channel: this.channels.values ()) {if (channel.isConnected ()) {chs.add (channel);} else {channels.remove (NetUtils.toAddressString (channel.getRemoteAddress () } return chs;} @ Override public Channel getChannel (InetSocketAddress remoteAddress) {return channels.get (NetUtils.toAddressString (remoteAddress));} @ Override public boolean canHandleIdle () {return true;} @ Override public boolean isBound () {return channel.isActive ();}}

NettyServer inherits AbstractServer and implements doOpen and doClose methods; doOpen method creates netty's ServerBootstrap, bossGroup, workerGroup;doClose methods close channel, bossGroup, workerGroup

NettyTransporter

Dubbo-2.7.3/dubbo-remoting/dubbo-remoting-netty4/src/main/java/org/apache/dubbo/remoting/transport/netty4/NettyTransporter.java

Public class NettyTransporter implements Transporter {public static final String NAME = "netty"; @ Override public Server bind (URL url, ChannelHandler listener) throws RemotingException {return new NettyServer (url, listener);} @ Override public Client connect (URL url, ChannelHandler listener) throws RemotingException {return new NettyClient (url, listener);}}

NettyTransporter implements the Transporter interface, and its bind method creates NettyServer

Summary

NettyServer inherits AbstractServer and implements doOpen and doClose methods; doOpen method creates netty's ServerBootstrap, bossGroup, workerGroup;doClose methods close channel, bossGroup, workerGroup

After reading the above, have you mastered the role of NettyServer in dubbo? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report