yangyoupeng

添加缓存清楚的rest接口。修改添加jar消息的接口。加入在启动时候,通过参数控制

是否要覆盖es上面的相关信息的内容。
......@@ -20,11 +20,13 @@ 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;
import com.zhaoonline.support.gateway.utils.Utils;
public class LocalFileSystemApiJarHandler implements ApiJarHandler {
private GWEventListenerRegistry listeners=GWEventListenerRegistry.INSTANCE;
private static Logger LOG = LoggerFactory.getLogger(LocalFileSystemApiJarHandler.class);
private Path apiJarDir;
......@@ -77,6 +79,7 @@ public class LocalFileSystemApiJarHandler implements ApiJarHandler {
for(File file:filelist){
NewJarAddEvent event=new NewJarAddEvent();
event.addAdditionInfo(GWEventConstants.JAR_ADD_WHEN_INIT, true);
event.setNewJarPath(file.toPath());
listeners.sendEvent(event);
}
......
......@@ -13,4 +13,8 @@ public class GWEventConstants {
public static final String EVENT_GROUP_JAR="apiMan";
public static final String UNLOAD_ALLJAR = "unloadAll";
public static final String JAR_ADD_WHEN_INIT = "loadFromInit";
public static final String JAR_INFO_FLUSH = "init.jarinfoflush";
}
......
......@@ -46,7 +46,7 @@ public class JarEventListener implements GWEventListener {
//之所以不用URLClassLoader,是因为集成了Dubbo,dubbo加载interfaceclass是从Thread.currentThread().getContextClassLoader(),加载class类的。所以我们使用
//JarLoader.addFile来加载jar
JarLoader.addFile(jarPath);
//从页面正常upload上来的jar,不包含{@link GWConstants.NOT_ADDTOREPOSITRY},要将event添加到Repository中
//从页面正常upload上来的jar,以及系统启动的时候不包含,不包含{@link GWConstants.NOT_ADDTOREPOSITRY},要将event添加到Repository中
//由于节点之间做jar同步的时候,测试不需要添加到Repository中,GWConstants.NOT_ADDTOREPOSITRY的字段又{@see ApiJarScanner#upload2Local(File archiveFile)}来指定
if(!event.getEventSource().containsKey(GWConstants.NOT_ADDTOREPOSITRY)){
repositry.saveJarEvent(event);
......
......@@ -5,8 +5,11 @@ import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetRequestBuilder;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.search.SearchRequestBuilder;
import org.elasticsearch.action.search.SearchResponse;
......@@ -37,6 +40,7 @@ import com.zhaoonline.support.gateway.utils.Utils;
public class JarEventRepository {
private static final String JAR_PORT = "port";
private static final String JAR_HOST = "host";
private static final String JAR_FILE_NAME = "jarFileName";
private static Logger LOG=LoggerFactory.getLogger(JarEventRepository.class);
......@@ -49,7 +53,7 @@ public class JarEventRepository {
private ElasticConfiguration elasticconfig ;
TransportClient client =null;
private GateWayConfiguration gatewatConfig;
private String jarinfoflush ="true";
public JarEventRepository(ElasticConfiguration elasticconfig,GateWayConfiguration gatewatConfig) throws Exception{
this.elasticconfig=elasticconfig;
this.gatewatConfig=gatewatConfig;
......@@ -65,6 +69,11 @@ public class JarEventRepository {
}
this.host=host;
this.port=port;
jarinfoflush=this.gatewatConfig.getString(GWEventConstants.JAR_INFO_FLUSH);
if(ObjectUtils.isEmpty(jarinfoflush)){
jarinfoflush="true";
}
}
public String getHost() {
return host;
......@@ -81,10 +90,14 @@ public class JarEventRepository {
* @param event
* @return boolean<BR>
* @throws JsonProcessingException
* @throws ExecutionException
* @throws InterruptedException
*/
public boolean saveJarEvent(GWEvent event) throws JsonProcessingException{
Map<String,Object> newInfo=new HashMap<String,Object>();
Object jarPathObject=event.getEventSource().get(GWEventConstants.JAR_PATH);
if(!ObjectUtils.isEmpty(jarPathObject)){
Path jarPath =(Path)jarPathObject;
......@@ -94,11 +107,36 @@ public class JarEventRepository {
newInfo.put(JAR_PORT, Integer.valueOf(this.port));
String docString=Utils.toJson(newInfo);
//如果是从{@link LocalFileSystemApiJarHandler}的初始化方法中传递过来的event,则必须要对目前现有的jarinfo进行查询判断
//判断启动配置参数从{@link LocalFileSystemApiJarHandler}
Object jarAddedWhenInit=event.getEventSource().get(GWEventConstants.JAR_ADD_WHEN_INIT);
if(!ObjectUtils.isEmpty(jarAddedWhenInit)&& Boolean.valueOf(String.valueOf(jarAddedWhenInit))){
// GetRequestBuilder getBuilder=client.prepareGet(GWConstants.DEFAUL_JAREVENT_INDEX, DEFAUL_JAREVENT_TYPE,key);
//
// GetResponse getResponse=null;
// try {
// getResponse = getBuilder.execute().get();
// if(getResponse !=null){
// LOG.info("do not insert jar event info:[{}],because it is existed",newInfo);
// return false;
// }
// } catch (InterruptedException | ExecutionException e) {
// e.printStackTrace();
// LOG.info("error happend when query jar file [{}] info ,cause by [{}],nothing to insert jar event info",key,e.getMessage());
// return false;
// }
if(!Boolean.valueOf(String.valueOf(jarinfoflush))){
LOG.info("do not insert jar event info:[{}],because property init.jarinfoflush ",newInfo,jarinfoflush);
return true;
}
}
//我们使用文件名作为key
IndexResponse indexResponse= client.prepareIndex(GWConstants.DEFAUL_JAREVENT_INDEX, DEFAUL_JAREVENT_TYPE,key)
.setSource(docString).get();
if(indexResponse !=null){
LOG.info("success to jar event info:[{}],id:[{}]",newInfo,indexResponse.getId());
LOG.info("success to insert jar event info:[{}],id:[{}]",newInfo,indexResponse.getId());
return true;
}
}
......
......@@ -24,11 +24,13 @@ public class JarEventRepositoryFactory {
public JarEventRepository jarEventRepository() throws Exception{
GateWayConfiguration gatewatConfig=new GateWayConfiguration();
String jarinfoflush =environment.getProperty(GWEventConstants.JAR_INFO_FLUSH);
String port = environment.getProperty("server.port");
String host = environment.getProperty("server.address");
Map<String,String> map=new HashMap<String,String>();
map.put("port", port);
map.put("host", host);
map.put(GWEventConstants.JAR_INFO_FLUSH, jarinfoflush);
gatewatConfig.addProperties(map);
JarEventRepository rep=new JarEventRepository(elasticconfig, gatewatConfig);
return rep;
......
......@@ -102,5 +102,21 @@ public enum ServiceManager{
/**
* Method name: clearServiceCache <BR>
* Description: clearServiceCache,可供手动清空service cache<BR>
* Remark: <BR>
* @return boolean<BR>
*/
public boolean clearServiceCache(){
Set<String> dubboEntrySet=dubboServiceCache.keySet();
for(String key:dubboEntrySet){
LOG.info("clear dubbo cache for [{}] ",key);
dubboServiceCache.remove(key);
LOG.info("Dubbo Service:[{}] has bean shutdown,done to clear dubbo cache for [{}]",key,key);
}
return true;
}
}
......
......@@ -34,6 +34,7 @@ import com.zhaoonline.support.gateway.apijar.ApiUploadResult;
import com.zhaoonline.support.gateway.apijar.FileInfo;
import com.zhaoonline.support.gateway.listener.JarEventRepository;
import com.zhaoonline.support.gateway.listener.JarEventRepositoryFactorySingleton;
import com.zhaoonline.support.gateway.service.ServiceManager;
import com.zhaoonline.support.gateway.utils.Utils;
/**
......@@ -120,6 +121,14 @@ public class ApiManagerServlet extends HttpServlet {
handleDownloadApi(req,resp);
} else if (hasSubPath(req, "/unload")){
handleUnloadApi(req,resp);
}else if (hasSubPath(req, "/clearcache")){
LOG.info("begin to clear all service cache");
ServiceManager.INSTANCE.clearServiceCache();
ApiResponse apiResponse=new ApiResponse();
Map<String,String> ret =new HashMap<String,String>();
ret.put("success", "all cache have been cleared ");
apiResponse.setData(ret);
Utils.toJSON(apiResponse, resp.getOutputStream(), true);
}
}
......
......@@ -3,12 +3,14 @@ application.message=\u8D75\u6D8C\u5728\u7EBFAPI GateWay\u6B22\u8FCE\u60A8
spring.application.name=apigateway
server.port=80
#server.address=192.168.0.162
server.address=192.168.3.79
server.address=192.168.1.102
es.host=192.168.0.162
es.port=9300
es.cluster.name=zhaoonline
init.jarinfoflush=false
spring.mvc.view.prefix: /WEB-INF/view/
spring.mvc.view.suffix: .jsp
......