yangyoupeng

对dubboService添加生存时间的控制。超过默认1小时就销dubboservice对象

......@@ -50,7 +50,7 @@ public class GateWayConfiguration {
public Integer getInteger(String key, int defaultValue) {
Object valObject=configMap.get(key);
if(valObject!=null){
Integer.valueOf(String.valueOf(valObject));
return Integer.valueOf(String.valueOf(valObject));
}
return defaultValue;
}
......
......@@ -144,7 +144,7 @@ public class TransforFilter extends ZuulFilter implements InitializingBean,Dispo
}
private HttpResponse toDubboService(RequestContext context,HttpServletRequest request, ServiceInfo serviceInfo) {
DubboService service = ServiceManager.INSTANCE.buildDubboServiceFrom(serviceInfo);
DubboService service = ServiceManager.INSTANCE.buildDubboServiceFrom(serviceInfo,this.commonConfig);
checkServiceBuilded(service,context);
return doForward(context,request, service);
}
......@@ -246,7 +246,7 @@ public class TransforFilter extends ZuulFilter implements InitializingBean,Dispo
public void afterPropertiesSet() throws Exception {
LOGGER.info("begin to load gateway config file {}",configFileName);
commonConfig=GateWayConfiguration.loadFrom(configFileName);
LOGGER.info("done to load gateway config file {} with config item:",configFileName,commonConfig);
LOGGER.info("done to load gateway config file {} with config item: {}",configFileName,commonConfig);
}
......
......@@ -2,11 +2,14 @@ package com.zhaoonline.support.gateway.lifecycle;
import java.lang.ref.WeakReference;
import org.joda.time.DateTime;
import org.joda.time.Seconds;
import org.omg.PortableInterceptor.LOCATION_FORWARD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.alibaba.dubbo.rpc.service.EchoService;
import com.zhaoonline.support.gateway.filter.TransforFilter;
import com.zhaoonline.support.gateway.config.GateWayConfiguration;
import com.zhaoonline.support.gateway.service.AbstractService;
import com.zhaoonline.support.gateway.service.DubboService;
import com.zhaoonline.support.gateway.service.ServiceInfo;
......@@ -15,18 +18,33 @@ public class ServiceLifeCycleChecker implements LifeCycleChecker {
private static Logger LOGGER = LoggerFactory.getLogger(ServiceLifeCycleChecker.class);
final ServiceInfo serviceInfo;
final WeakReference<AbstractService> serviceRef;
public static Seconds evictTime=null;
EchoService echoService;
public ServiceLifeCycleChecker(AbstractService service){
this(service,new GateWayConfiguration());
}
public ServiceLifeCycleChecker(AbstractService service, GateWayConfiguration config) {
serviceInfo=service.getServicInfo();
this.serviceRef=new WeakReference(service);
if(service instanceof DubboService ){
DubboService dubboservice= (DubboService)service;
echoService =(EchoService) dubboservice.getAutomicReference().get();
}
evictTime=Seconds.seconds(config.getInteger("dubbo.service.evict.time", 3600));
}
@Override
public void run() {
AbstractService service=serviceRef.get();
Long startTime=service.getStartTime();
DateTime startDateTime=new DateTime(startTime);
Seconds liveTime=Seconds.secondsBetween(startDateTime, DateTime.now());
if(liveTime.isGreaterThan(evictTime)){
LOGGER.info("service [{}] live time is greatethan dubbo.service.evict.time [{}],just mark it closed",serviceInfo,evictTime.getSeconds());
service.setClosed(true);
}
if(service ==null || serviceRef.get().isClosed() ){
LOGGER.info ("dubbo service is closed now");
throw new RuntimeException(String.format("throw an exception to terminate the thread,the service %s is closed ", serviceInfo));
......
......@@ -57,7 +57,7 @@ public class BeanConfiguration {
@Bean
public AuthenticationProvider zhaoAuthenticationProvider(UserAuthInfoService userAuthInfoService){
public AuthenticationProvider zhaoAuthenticationProvider(CachingUserDetailsService userAuthInfoService){
AuthenticationProvider provider=new ZhaoUsernamePasswordAuthenticationProvider(userAuthInfoService);
return provider;
}
......
......@@ -13,6 +13,7 @@ import com.netflix.zuul.context.RequestContext;
public abstract class AbstractService<T,R> {
private transient boolean closed=false;
private Long startTime=null;
protected final ServiceInfo servicInfo;
......@@ -26,6 +27,14 @@ public abstract class AbstractService<T,R> {
public abstract void init() throws Exception;
public void markStart(){
startTime =System.currentTimeMillis();
}
public Long getStartTime(){
return startTime;
}
private String serviceType;
......
......@@ -414,6 +414,7 @@ public class DubboService extends AbstractService<HttpServletRequest, HttpRespon
//为了业务安全起见、我们不允许重试。
reference.setRetries(0);
getServiceInstance();
markStart();
}
private void setAttachmentFromServiceInfo() {
......
......@@ -302,6 +302,7 @@ public class HTTPService extends AbstractService<HttpServletRequest, HttpRespons
public void init() {
SOCKET_TIMEOUT =this.config.getInteger(GWConstants.GATEWAY_HOST_SOCKET_TIMEOUT_MILLIS, 10000);
CONNECTION_TIMEOUT=this.config.getInteger(GWConstants.GATEWAY_HOST_CONNECT_TIMEOUT_MILLIS, 2000);
markStart();
}
@Override
......
......@@ -58,14 +58,14 @@ public enum ServiceManager{
* @param serviceInfo
* @return DubboService<BR>
*/
public synchronized DubboService buildDubboServiceFrom(ServiceInfo serviceInfo) {
public synchronized DubboService buildDubboServiceFrom(ServiceInfo serviceInfo,GateWayConfiguration config) {
DubboService cachedService = dubboServiceCache.get(serviceInfo.getServiceInterface());
if(cachedService == null){
LOG.info("no cache for dubbo service {},create new instance for service",serviceInfo.getServiceInterface());
DubboService dubboSerivce=new DubboService(serviceInfo);
try {
dubboSerivce.init();
monitorStatus(dubboSerivce);
monitorStatus(dubboSerivce,config);
} catch (Exception e) {
LOG.info("fail to init the dubbo service interface {},cause by {}",serviceInfo.getServiceInterface(),e.getMessage());
e.printStackTrace();
......@@ -83,9 +83,9 @@ public enum ServiceManager{
return cachedService;
}
private void monitorStatus(DubboService dubboSerivce) {
private void monitorStatus(DubboService dubboSerivce,GateWayConfiguration config) {
LOG.info("assign the dubboservice {} to be status monitored by {}",dubboSerivce.getServicInfo(),LifeCycleCheckerManager.class.getName());
lifeCycleCheckerManager.submit(new ServiceLifeCycleChecker(dubboSerivce));
lifeCycleCheckerManager.submit(new ServiceLifeCycleChecker(dubboSerivce,config));
}
......
dubbo.service.evict.time=3600
\ No newline at end of file
......