标签:dubbo netty 停机 优雅 destroyAll org import logger
dubbo中使用了netty,但是它修改了netty的代码,其中有个bug,导致无法优雅停机。
下面的代码用于解决这个问题。
package org.jsirenia.dubbo;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import javax.annotation.PreDestroy;
import org.jboss.netty.channel.ChannelFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
/**
* 依赖dubbo2.5.3
* 解决优雅停机的问题(dubbo间接创建的非守护线程导致的不能优雅停机) 参考文章
* https://jeremy-xu.oschina.io/2016/12/%E8%A7%A3%E5%86%B3dubbo%E5%AF%BC%E8%87%B4tomcat%E6%97%A0%E6%B3%95%E4%BC%98%E9%9B%85shutdown%E7%9A%84%E9%97%AE%E9%A2%98/
* 解决方案:
* 在停掉应用的回调里面,比如spring的PreDestroy,比如ServletContextListener的contextDestroyed,
* 获取NettryClient的静态属性 channelFactory,调用其releaseExternalResource方法。
*
*/
public class DubboNettyClientResourceReleaser {
private Logger logger = LoggerFactory.getLogger(this.getClass());
// 最低优先级
@PreDestroy
@Order(Ordered.LOWEST_PRECEDENCE)
public void destroy() {
try {
// 先释放dubbo所占用的资源
Class<?> protocolConfigClass = Class.forName("com.alibaba.dubbo.config.ProtocolConfig");
Method destroyAllMethod = protocolConfigClass.getMethod("destroyAll");
destroyAllMethod.invoke(protocolConfigClass);
//ProtocolConfig.destroyAll();
logger.info("ProtocolConfig destroyAll success");
} catch (Exception e) {
logger.error("ProtocolConfig destroyAll error", e);
}
try {
// 用反射释放NettyClient所占用的资源, 以避免不能优雅shutdown的问题
releaseNettyClientExternalResources();
logger.info("Release NettyClient's external resources");
} catch (Exception e) {
logger.error("Release NettyClient's external resources error", e);
}
}
private void releaseNettyClientExternalResources() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, ClassNotFoundException {
Class<?> nettyClientClass = Class.forName("com.alibaba.dubbo.remoting.transport.netty.NettyClient");
Field field = nettyClientClass.getDeclaredField("channelFactory");
field.setAccessible(true);
ChannelFactory channelFactory = (ChannelFactory) field.get(nettyClientClass);
channelFactory.releaseExternalResources();
field.setAccessible(false);
}
}
标签:dubbo,netty,停机,优雅,destroyAll,org,import,logger 来源: https://blog.csdn.net/zhoujiaping123/article/details/91629494
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。