yangyoupeng

重构,增加LRU缓存

......@@ -7,13 +7,9 @@ import java.util.Collection;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import org.apache.catalina.loader.WebappClassLoader;
import org.apache.catalina.loader.WebappClassLoaderBase;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zhaoonline.support.gateway.servlet.ApiManagerServlet;
public enum ApiClassLoaderManager {
INSTANCE;
private static Logger LOG=LoggerFactory.getLogger(ApiClassLoaderManager.class);
......
......@@ -3,12 +3,8 @@ package com.zhaoonline.support.gateway.apijar;
import java.io.File;
import java.io.FileFilter;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLClassLoader;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.jar.JarFile;
import java.util.zip.ZipFile;
......
package com.zhaoonline.support.gateway.apijar;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
......@@ -12,8 +10,6 @@ import java.util.Properties;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.util.ResourceUtils;
import com.zhaoonline.support.gateway.utils.IOUtils;
/**
......@@ -78,7 +74,7 @@ public class ApiManagerConfiguration {
try {
// File file = ResourceUtils.getFile(fileName);
Resource file = IOUtils.getFile(fileName);
LOG.info("ResourceUtils.getFile {} with fileNama{}",file,fileName);
LOG.info("IOUtils.getFile {} with fileNama{}",file,fileName);
reader =file.getInputStream();
propeties.load(reader);
Map<String, String> map = (Map)propeties;
......
......@@ -13,8 +13,6 @@ import java.util.ArrayList;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.tools.jar.resources.jar;
public class JarLoader {
private static Logger LOGGER =LoggerFactory.getLogger(JarLoader.class);
......
......@@ -19,7 +19,6 @@ import java.util.Set;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.zhaoonline.support.gateway.config.GWConstants;
import com.zhaoonline.support.gateway.listener.GWEventConstants;
import com.zhaoonline.support.gateway.listener.GWEventListenerRegistry;
import com.zhaoonline.support.gateway.listener.NewJarAddEvent;
......
......@@ -72,29 +72,36 @@ public enum ServiceManager{
DubboService cachedService = (DubboService) dubboServiceCache.get(serviceName);
if(cachedService == null){
LOG.info("no cache for dubbo service [{}],create new instance for service",serviceInfo.getServiceName());
DubboService dubboSerivce=new DubboService(serviceInfo);
try {
dubboSerivce.init();
monitorStatus(dubboSerivce,config);
} catch (Exception e) {
LOG.info("fail to init the dubbo service interface [{}],cause by [{}]",serviceInfo,e.getMessage());
e.printStackTrace();
return null;
}
LOG.info("cache dubbo service with key [{}]",serviceName);
dubboServiceCache.put(serviceName,dubboSerivce);
return dubboSerivce;
return createNewDubboService(serviceInfo, config, serviceName);
}else{
LOG.info("get dubbo service from cache with key [{}]",serviceName);
if(cachedService.isClosed()){
LOG.info("the cached dubbo service interface {} is closed by {},just return null",serviceInfo.getServiceInterface(),LifeCycleCheckerManager.class.getName());
dubboServiceCache.remove(serviceName);
cachedService=null;
return createNewDubboService(serviceInfo, config, serviceName);
}
}
return cachedService;
}
private DubboService createNewDubboService(ServiceInfo serviceInfo, GateWayConfiguration config,
String serviceName) {
DubboService dubboSerivce=new DubboService(serviceInfo);
try {
dubboSerivce.init();
monitorStatus(dubboSerivce,config);
} catch (Exception e) {
LOG.info("fail to init the dubbo service interface [{}],cause by [{}]",serviceInfo,e.getMessage());
e.printStackTrace();
return null;
}
LOG.info("cache dubbo service with key [{}]",serviceName);
dubboServiceCache.put(serviceName,dubboSerivce);
return 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,config));
......
package com.zhaoonline.support.gateway.main;
import org.slf4j.LoggerFactory;
import ch.qos.logback.classic.Logger;
public class ApplicationIT {
public static void main(String[] args) throws InterruptedException {
Application.main(args);
Logger root = (Logger)LoggerFactory.getLogger("com.zhaoonline");
System.out.println("------getLevel().levelStr---------"+root.getLevel().levelStr);
System.out.println("------appenderName---------"+root.getAppender("addfa").getName());
root.warn("this is a test ");
root.getAppender("addfa").addWarn("this is a test11111");
}
}
......