Commit bcec9771 by 房斌

1解决dubbo tomcat 加载类的判断。没有使用不加载 2执行顺序 tomcat优先dubbo 停机

parent 5f09c068
......@@ -4,4 +4,5 @@ public interface StopService <T>{
public T stop();
}
package com.secoo.mall.dubbo.spring.boot.autoconfigure;
package com.secoo.mall.dubbo.service;
import com.alibaba.fastjson.JSON;
import com.secoo.mall.common.core.service.StopService;
......@@ -7,62 +7,49 @@ import com.secoo.mall.common.util.date.DateUtil;
import com.secoo.mall.common.util.log.LoggerUtil;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetails;
import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.utils.Stack;
import com.secoo.mall.dubbo.service.impl.ServletConnectShoutDownHock;
import org.apache.catalina.connector.Connector;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.extension.ExtensionLoader;
import com.secoo.mall.dubbo.service.impl.TomcatConnectShoutDownHock;
import org.apache.dubbo.common.utils.NetUtils;
import org.apache.dubbo.config.DubboShutdownHook;
import org.apache.dubbo.config.spring.extension.SpringExtensionFactory;
import org.apache.dubbo.registry.Registry;
import org.apache.dubbo.registry.support.AbstractRegistryFactory;
import org.apache.dubbo.registry.zookeeper.ZookeeperRegistry;
import org.apache.dubbo.rpc.Protocol;
import org.apache.dubbo.rpc.model.ApplicationModel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
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.ApplicationContextAware;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ApplicationEventMulticaster;
import org.springframework.context.event.ContextClosedEvent;
import org.springframework.core.Ordered;
import java.lang.reflect.Field;
import java.util.*;
import static org.springframework.context.support.AbstractApplicationContext.APPLICATION_EVENT_MULTICASTER_BEAN_NAME;
public class GracefullyShoutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>, TomcatConnectorCustomizer, ApplicationContextAware {
private static final int TIMEOUT = 10;
public class GracefullyShoutDown implements CommandLineRunner, ApplicationListener<ContextClosedEvent>,ApplicationContextAware , Ordered {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
// @Autowired
ServletConnectShoutDownHock servletConnectShoutDownHock;
// TomcatConnectShoutDownHock tomcatConnectShoutDownHock;
private ApplicationContext context;
// @Autowired
private ProviderService providerService ;
// private ProviderService providerService ;
// @Autowired
UpDatas dubboUpdata;
public GracefullyShoutDown(ServletConnectShoutDownHock servletConnectShoutDownHock,ProviderService providerService,UpDatas dubboUpdata){
this.servletConnectShoutDownHock=servletConnectShoutDownHock;
this.providerService=providerService;
this.dubboUpdata=dubboUpdata;
}
// public GracefullyShoutDown(TomcatConnectShoutDownHock tomcatConnectShoutDownHock,ProviderService providerService,UpDatas dubboUpdata){
// this.tomcatConnectShoutDownHock=tomcatConnectShoutDownHock;
// this.providerService=providerService;
// this.dubboUpdata=dubboUpdata;
// }
public GracefullyShoutDown(UpDatas dubboUpdata) {
this.dubboUpdata = dubboUpdata;
}
//容器初始化后执行
@Override
public void run(String... args) throws Exception {
logger.info("dddddddddddddduuuuuuuuuuuuuuuuubbbbbbbbbbbboooooooooooolllllllllll!!!!");
if (DubboShutdownHook.getDubboShutdownHook() != null) {
//TODO 判断是否dubbo组件,如果没有就不执行卸载和 shutDownhook
//hock卸载
......@@ -72,52 +59,52 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
Class clz = SpringExtensionFactory.class;
multicaster.removeApplicationListener((ApplicationListener) getPrivateConst("SHUTDOWN_HOOK_LISTENER"));
}
}
//test-------------------------------------> begin
// ExecutorDetails result = new ExecutorDetails();
// result.setCode(0);
// List<ExecutorDetail> details = new ArrayList<ExecutorDetail>();
// result.setT(details);
// String ip = NetUtils.getIpByHost(NetUtils.getLocalAddress().getHostName());
// String name = ApplicationModel.getApplication();
// result.setIp(ip);
// result.setName(name);
////
// try {
// Map<String, StopService> map = this.context.getBeansOfType(StopService.class);
// if (map != null && map.size() > 0) {
// Set<StopService> ts = new TreeSet<StopService>();
// for (StopService service : map.values()) {
//test-------------------------------------> begin
// ExecutorDetails result = new ExecutorDetails();
// result.setCode(0);
// List<ExecutorDetail> details = new ArrayList<ExecutorDetail>();
// result.setT(details);
// String ip = NetUtils.getIpByHost(NetUtils.getLocalAddress().getHostName());
// String name = ApplicationModel.getApplication();
// result.setIp(ip);
// result.setName(name);
//
// Map<String, StopService> map = this.context.getBeansOfType(StopService.class);
// if (map != null && map.size() > 0) {
// Set<StopService> ts = new TreeSet<StopService>();
// for (StopService service : map.values()) {
// if (!(service instanceof TomcatConnectShoutDownHock)) {
// ts.add(service);
// }
// //按定义顺序执行
// Iterator<StopService> it = ts.iterator();
// while (it.hasNext()) {
// StopService service = (StopService) it.next();
// ExecutorDetail one = (ExecutorDetail) service.stop();
// if (one != null) {
// details.add(one);
// if (one.getCode() != 0) {
// result.setCode(-1);
// } }
////
//// }
//// } catch (Exception e) {
//// LoggerUtil.error("matrix.GracefullyShoutDown.error", e);
//// }
//// try {
//// dubboUpdata.upData(result);
//// } catch (Exception e) {
//// LoggerUtil.error("matrix.GracefullyShoutDown update .error data:" + JSON.toJSONString(result), e);
//// }
// }
// //按定义顺序执行
// Iterator<StopService> it = ts.iterator();
// while (it.hasNext()) {
// StopService service = (StopService) it.next();
// ExecutorDetail one = (ExecutorDetail) service.stop();
// if (one != null) {
// details.add(one);
// if (one.getCode() != 0) {
// result.setCode(-1);
// }
// }
//
// }
// } catch (Exception e) {
// LoggerUtil.error("matrix.GracefullyShoutDown.error", e);
// }
// try {
// dubboUpdata.upData(result);
// } catch (Exception e) {
// LoggerUtil.error("matrix.GracefullyShoutDown update .error data:" + JSON.toJSONString(result), e);
// }
// }
//test------------------------------------->
//test------------------------------------->
}
......@@ -132,25 +119,10 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
return null;
}
//容器关闭后执行
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());
}
}
}
@Override
public void onApplicationEvent(ContextClosedEvent contextClosedEvent) {
logger.info("222222222222 closeevent");
ExecutorDetails result = new ExecutorDetails();
result.setCode(0);
List<ExecutorDetail> details = new ArrayList<ExecutorDetail>();
......@@ -167,13 +139,14 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
if (map != null && map.size() > 0) {
Set<StopService> ts = new TreeSet<StopService>();
for (StopService service : map.values()) {
ts.add(service);
if(!(service instanceof TomcatGracefullyShutDown)) {
ts.add(service);
}
}
//按定义顺序执行
Iterator<StopService> it = ts.iterator();
while (it.hasNext()) {
StopService service = (StopService) it.next();
//TODO tomcator 的connector是否加载
ExecutorDetail one = (ExecutorDetail) service.stop();
if (one != null) {
details.add(one);
......@@ -195,14 +168,15 @@ public class GracefullyShoutDown implements CommandLineRunner, ApplicationListen
logger.info("GracefullyShoutDown execute end :name:"+name+" ip:"+ip+" time:"+ DateUtil.getDateTime());
}
@Override
public void customize(Connector connector) {
servletConnectShoutDownHock.setConnector(connector);
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context=applicationContext;
logger.info("222222222222 setContext");
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
public int getOrder() {
return 2;
}
}
......@@ -40,7 +40,7 @@ public class DubboCustomerDownHock extends AbstractStop {
public ExecutorDetail stop() {
ExecutorDetail detail = new ExecutorDetail();
detail.setBeginTime(DateUtil.getDateTime());
detail.setServiceName("dubboDownHock1013");
detail.setServiceName("dubboDownHock1025");
List<String> str = new ArrayList<String>();
detail.setDetail(str);
detail.setCode(0);
......
......@@ -5,8 +5,6 @@ import com.secoo.mall.common.util.string.StringUtil;
import com.secoo.mall.common.core.bean.gracefulshowtdownBean.ExecutorDetail;
import com.secoo.mall.dubbo.service.AbstractStop;
import org.apache.catalina.connector.Connector;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
......@@ -14,8 +12,7 @@ import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
//springboot 容器关闭
@Component
public class ServletConnectShoutDownHock extends AbstractStop {
public class TomcatConnectShoutDownHock extends AbstractStop {
private volatile Connector connector;
public Connector getConnector() {
......@@ -67,6 +64,6 @@ public class ServletConnectShoutDownHock extends AbstractStop {
@Override
public Integer getHandleTypeOrder() {
return 1;
return -1;
}
}
......@@ -5,15 +5,17 @@ import com.secoo.mall.dubbo.monitor.config.ConfigCenter;
import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.dubbo.service.impl.ProviderServiceImpl;
import com.secoo.mall.dubbo.service.GracefullyShoutDown;
import com.secoo.mall.dubbo.service.TomcatGracefullyShutDown;
import com.secoo.mall.dubbo.service.impl.DubboCustomerDownHock;
import com.secoo.mall.dubbo.service.DubboUpdata;
import com.secoo.mall.dubbo.service.impl.ServletConnectShoutDownHock;
import com.secoo.mall.dubbo.service.impl.TomcatConnectShoutDownHock;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.apache.dubbo.registry.Registry;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
......@@ -23,18 +25,15 @@ import org.springframework.context.annotation.DependsOn;
public class MatrixGracefulShutDownAutoConfiguration {
@Bean
@ConditionalOnClass(RegistryServerSync.class)
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
ProviderService createProviderService(Registry registry, RegistryServerSync sync) {
return new ProviderServiceImpl(registry,sync);
}
@Bean
ServletConnectShoutDownHock createServletConnectShoutDownHock() {
return new ServletConnectShoutDownHock();
}
@Bean
@ConditionalOnBean(ConfigCenter.class)
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
@ConditionalOnClass(ConfigCenter.class)
@DependsOn("configCenter")
UpDatas dubboUpdata( ConfigCenter configCenter){
DubboUpdata transport= new DubboUpdata();
......@@ -46,27 +45,52 @@ public class MatrixGracefulShutDownAutoConfiguration {
}
@Bean
public GracefullyShoutDown createGraceObject(ServletConnectShoutDownHock servletConnectShoutDownHock,ProviderService providerService,UpDatas dubboUpdata) {
return new GracefullyShoutDown( servletConnectShoutDownHock, providerService, dubboUpdata);
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
DubboCustomerDownHock createDubboConsumerDownHock(ProviderService providerService) {
return new DubboCustomerDownHock(providerService);
}
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
public RegistryServerSync createSynObject(Registry registry) {
return new RegistryServerSync(registry);
}
@Bean
DubboCustomerDownHock createDubboConsumerDownHock(ProviderService providerService) {
return new DubboCustomerDownHock(providerService);
}
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
ConfigCenter configCenter(){
return new ConfigCenter();
}
@Bean
@ConditionalOnClass(TomcatConnectorCustomizer.class)
TomcatConnectShoutDownHock createServletConnectShoutDownHock() {
return new TomcatConnectShoutDownHock();
}
@Bean
@ConditionalOnClass(TomcatConnectShoutDownHock.class)
public TomcatGracefullyShutDown createSpringbootTomcatInit(TomcatConnectShoutDownHock tomcatConnectShoutDownHock){
return new TomcatGracefullyShutDown(tomcatConnectShoutDownHock);
}
// @Bean
// public GracefullyShoutDown createGraceObject(TomcatConnectShoutDownHock servletConnectShoutDownHock, ProviderService providerService, UpDatas dubboUpdata) {
// return new GracefullyShoutDown( servletConnectShoutDownHock, providerService, dubboUpdata);
// }
@Bean
@ConditionalOnExpression("!T(org.springframework.util.StringUtils).isEmpty('${dubbo.registry.address:}')")
public GracefullyShoutDown createGraceObject( UpDatas dubboUpdata) {
return new GracefullyShoutDown(dubboUpdata);
}
}
\ No newline at end of file
......@@ -8,7 +8,6 @@ import com.secoo.mall.dubbo.monitor.dubbo.service.ProviderService;
import com.secoo.mall.dubbo.monitor.dubbo.service.RegistryServerSync;
import com.secoo.mall.dubbo.monitor.dubbo.service.impl.ProviderServiceImpl;
import com.secoo.mall.dubbo.service.ServletConnectShoutDownHock;
import com.secoo.mall.dubbo.swagger.annotations.EnableDubboSwagger;
import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.beans.factory.annotation.Value;
......
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