Commit 3b33c210 by 房斌

tomcat connect 挪位置,之前功能正常

parent 3b4ed9b7
package com.secoo.mall.common.core.service;
public interface StopService <BizResponse>{
public BizResponse stop();
}
......@@ -17,8 +17,8 @@
package com.secoo.mall.dubbo.monitor.dubbo.service;
import com.secoo.mall.dubbo.monitor.dubbo.model.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.model.dto.ServiceDTO;
import java.util.List;
......
package com.secoo.mall.dubbo.service;
import com.secoo.mall.common.core.service.StopService;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.domain.Provider;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import org.apache.dubbo.common.extension.ExtensionLoader;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.rpc.Protocol;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Iterator;
import java.util.List;
public class DubboCustomerDownHock implements StopService<StringBuilder> {
@Autowired
private final ProviderService providerService;
public DubboCustomerDownHock() {
providerService = null;
}
@Override
public StringBuilder stop() {
StringBuilder builderStr = new StringBuilder();
String lineSeparator = System.getProperty("line.separator", "\n");
try {
//step1 register销毁
AbstractRegistryFactory.destroyAll();
//TODO step2 写在阿波罗里,具体数据 以后根据客户端上报的数据配置,目前无详细依据。等待
Thread.sleep(10000);
String ip = "";
String name = "";
final List<Provider> serviceDTOS = providerService.getServiceDTOSByQuery("ip", ip, "");
//step3 check检查
Iterator<Provider> iteratorCheck = serviceDTOS.iterator();
while (iteratorCheck.hasNext()) {
Provider obj = iteratorCheck.next();
if (StringUtil.isNoneEmpty(obj.getAddress()) && obj.getApplication().equals(name)) { //本应用的服务
List<String> check = providerService.findServicesByAddress(obj.getAddress());
if (check != null && check.size() > 0) {
builderStr.append("matrix-monitor off DubboService check service:" + obj.getUrl() + " already on line after off " + lineSeparator);
}
}
}
//step4 协议层销毁
destoryProtocol();
} catch (Exception e) {
builderStr.append("matirx-monitor offDubbo error:" +e.toString()+lineSeparator);
}
return builderStr;
}
public void destoryProtocol(){
ExtensionLoader<Protocol> loader = ExtensionLoader.getExtensionLoader(Protocol.class);
for (String protocolName : loader.getLoadedExtensions()) {
try {
Protocol protocol = loader.getLoadedExtension(protocolName);
if (protocol != null) {
protocol.destroy();
}
} catch (Throwable t) {
LoggerUtil.info(t.getMessage());
}
}
}
}
......@@ -19,6 +19,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
public class GracefullyShoutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, TomcatConnectorCustomizer {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
......@@ -56,7 +57,7 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
try {
this.connector.pause();
//TODO 挪到定义的实现类里面
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
......@@ -76,7 +77,11 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
}
}
logger.info("DubboShutdownHook begin time------------>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
DubboShutdownHook.getDubboShutdownHook().doDestroy();
//DubboShutdownHook.getDubboShutdownHook().doDestroy();
logger.info("DubboShutdownHook begin end------------>" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
......
package com.secoo.mall.dubbo.spring.boot.autoconfigure;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import org.apache.catalina.connector.Connector;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.tomcat.util.threads.ThreadPoolExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextClosedEvent;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
public class GracefullyShoutDownYuanShi implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, TomcatConnectorCustomizer {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
private ApplicationContext context;
private static final int TIMEOUT = 10;
private volatile Connector connector;
@Autowired
ProviderService providerService;
//容器初始化后执行
@Override
public void run(String... args) throws Exception {
logger.info("nimam2-------------->");
if(DubboShutdownHook.getDubboShutdownHook()!=null) {
DubboShutdownHook.getDubboShutdownHook().unregister();
//TODO 遍历listern 然后移除,一种方法更改dubbo源码,第二种方法,反射搞定。
// ApplicationEventMulticaster multicaster= context.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
// multicaster.removeApplicationListener(SpringExtensionFactory.SHUTDOWN_HOOK_LISTENER);
logger.info("dubbo unreister success");
}else{
logger.info("dubbo unreister obj is null");
}
logger.info(JSON.toJSONString("query--------->"+providerService.getServiceDTOSByQuery("ip", "172.17.76.196", "")));
}
//容器关闭后执行
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
logger.info("zidingyi zhixing application close version2=============time:" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
try {
this.connector.pause();
//TODO 挪到定义的实现类里面
Executor executor = this.connector.getProtocolHandler().getExecutor();
if (executor instanceof ThreadPoolExecutor) {
try {
ThreadPoolExecutor threadPoolExecutor = (ThreadPoolExecutor) executor;
threadPoolExecutor.shutdown();
logger.info("apache connector executor shutdown=============time:" + DateUtil.getDateTime());
if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
logger.info("Tomcat thread pool did not shut down gracefully within "
+ TIMEOUT + " seconds. Proceeding with forceful shutdown");
threadPoolExecutor.shutdownNow();
if (!threadPoolExecutor.awaitTermination(TIMEOUT, TimeUnit.SECONDS)) {
logger.info("Tomcat thread pool did not terminate");
}
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
}
}
logger.info("DubboShutdownHook begin time------------>" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
//DubboShutdownHook.getDubboShutdownHook().doDestroy();
logger.info("DubboShutdownHook begin end------------>" +new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SSS").format(new Date() ));
} catch (Exception e) {
logger.error("matrix.GracefullyShoutDown.error", e);
}
}
@Override
public void customize(Connector connector) {
this.connector = connector;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment