Commit bcec9771 by 房斌

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

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