Showing
8 changed files
with
78 additions
and
4 deletions
... | @@ -20,11 +20,13 @@ import org.slf4j.Logger; | ... | @@ -20,11 +20,13 @@ import org.slf4j.Logger; |
20 | import org.slf4j.LoggerFactory; | 20 | import org.slf4j.LoggerFactory; |
21 | 21 | ||
22 | import com.zhaoonline.support.gateway.config.GWConstants; | 22 | import com.zhaoonline.support.gateway.config.GWConstants; |
23 | +import com.zhaoonline.support.gateway.listener.GWEventConstants; | ||
23 | import com.zhaoonline.support.gateway.listener.GWEventListenerRegistry; | 24 | import com.zhaoonline.support.gateway.listener.GWEventListenerRegistry; |
24 | import com.zhaoonline.support.gateway.listener.NewJarAddEvent; | 25 | import com.zhaoonline.support.gateway.listener.NewJarAddEvent; |
25 | import com.zhaoonline.support.gateway.utils.Utils; | 26 | import com.zhaoonline.support.gateway.utils.Utils; |
26 | 27 | ||
27 | public class LocalFileSystemApiJarHandler implements ApiJarHandler { | 28 | public class LocalFileSystemApiJarHandler implements ApiJarHandler { |
29 | + | ||
28 | private GWEventListenerRegistry listeners=GWEventListenerRegistry.INSTANCE; | 30 | private GWEventListenerRegistry listeners=GWEventListenerRegistry.INSTANCE; |
29 | private static Logger LOG = LoggerFactory.getLogger(LocalFileSystemApiJarHandler.class); | 31 | private static Logger LOG = LoggerFactory.getLogger(LocalFileSystemApiJarHandler.class); |
30 | private Path apiJarDir; | 32 | private Path apiJarDir; |
... | @@ -77,6 +79,7 @@ public class LocalFileSystemApiJarHandler implements ApiJarHandler { | ... | @@ -77,6 +79,7 @@ public class LocalFileSystemApiJarHandler implements ApiJarHandler { |
77 | 79 | ||
78 | for(File file:filelist){ | 80 | for(File file:filelist){ |
79 | NewJarAddEvent event=new NewJarAddEvent(); | 81 | NewJarAddEvent event=new NewJarAddEvent(); |
82 | + event.addAdditionInfo(GWEventConstants.JAR_ADD_WHEN_INIT, true); | ||
80 | event.setNewJarPath(file.toPath()); | 83 | event.setNewJarPath(file.toPath()); |
81 | listeners.sendEvent(event); | 84 | listeners.sendEvent(event); |
82 | } | 85 | } | ... | ... |
... | @@ -13,4 +13,8 @@ public class GWEventConstants { | ... | @@ -13,4 +13,8 @@ public class GWEventConstants { |
13 | public static final String EVENT_GROUP_JAR="apiMan"; | 13 | public static final String EVENT_GROUP_JAR="apiMan"; |
14 | 14 | ||
15 | public static final String UNLOAD_ALLJAR = "unloadAll"; | 15 | public static final String UNLOAD_ALLJAR = "unloadAll"; |
16 | + | ||
17 | + public static final String JAR_ADD_WHEN_INIT = "loadFromInit"; | ||
18 | + | ||
19 | + public static final String JAR_INFO_FLUSH = "init.jarinfoflush"; | ||
16 | } | 20 | } | ... | ... |
... | @@ -46,7 +46,7 @@ public class JarEventListener implements GWEventListener { | ... | @@ -46,7 +46,7 @@ public class JarEventListener implements GWEventListener { |
46 | //之所以不用URLClassLoader,是因为集成了Dubbo,dubbo加载interfaceclass是从Thread.currentThread().getContextClassLoader(),加载class类的。所以我们使用 | 46 | //之所以不用URLClassLoader,是因为集成了Dubbo,dubbo加载interfaceclass是从Thread.currentThread().getContextClassLoader(),加载class类的。所以我们使用 |
47 | //JarLoader.addFile来加载jar | 47 | //JarLoader.addFile来加载jar |
48 | JarLoader.addFile(jarPath); | 48 | JarLoader.addFile(jarPath); |
49 | - //从页面正常upload上来的jar,不包含{@link GWConstants.NOT_ADDTOREPOSITRY},要将event添加到Repository中 | 49 | + //从页面正常upload上来的jar,以及系统启动的时候不包含,不包含{@link GWConstants.NOT_ADDTOREPOSITRY},要将event添加到Repository中 |
50 | //由于节点之间做jar同步的时候,测试不需要添加到Repository中,GWConstants.NOT_ADDTOREPOSITRY的字段又{@see ApiJarScanner#upload2Local(File archiveFile)}来指定 | 50 | //由于节点之间做jar同步的时候,测试不需要添加到Repository中,GWConstants.NOT_ADDTOREPOSITRY的字段又{@see ApiJarScanner#upload2Local(File archiveFile)}来指定 |
51 | if(!event.getEventSource().containsKey(GWConstants.NOT_ADDTOREPOSITRY)){ | 51 | if(!event.getEventSource().containsKey(GWConstants.NOT_ADDTOREPOSITRY)){ |
52 | repositry.saveJarEvent(event); | 52 | repositry.saveJarEvent(event); | ... | ... |
... | @@ -5,8 +5,11 @@ import java.util.ArrayList; | ... | @@ -5,8 +5,11 @@ import java.util.ArrayList; |
5 | import java.util.HashMap; | 5 | import java.util.HashMap; |
6 | import java.util.List; | 6 | import java.util.List; |
7 | import java.util.Map; | 7 | import java.util.Map; |
8 | +import java.util.concurrent.ExecutionException; | ||
8 | 9 | ||
9 | import org.elasticsearch.action.delete.DeleteResponse; | 10 | import org.elasticsearch.action.delete.DeleteResponse; |
11 | +import org.elasticsearch.action.get.GetRequestBuilder; | ||
12 | +import org.elasticsearch.action.get.GetResponse; | ||
10 | import org.elasticsearch.action.index.IndexResponse; | 13 | import org.elasticsearch.action.index.IndexResponse; |
11 | import org.elasticsearch.action.search.SearchRequestBuilder; | 14 | import org.elasticsearch.action.search.SearchRequestBuilder; |
12 | import org.elasticsearch.action.search.SearchResponse; | 15 | import org.elasticsearch.action.search.SearchResponse; |
... | @@ -37,6 +40,7 @@ import com.zhaoonline.support.gateway.utils.Utils; | ... | @@ -37,6 +40,7 @@ import com.zhaoonline.support.gateway.utils.Utils; |
37 | public class JarEventRepository { | 40 | public class JarEventRepository { |
38 | private static final String JAR_PORT = "port"; | 41 | private static final String JAR_PORT = "port"; |
39 | private static final String JAR_HOST = "host"; | 42 | private static final String JAR_HOST = "host"; |
43 | + | ||
40 | private static final String JAR_FILE_NAME = "jarFileName"; | 44 | private static final String JAR_FILE_NAME = "jarFileName"; |
41 | private static Logger LOG=LoggerFactory.getLogger(JarEventRepository.class); | 45 | private static Logger LOG=LoggerFactory.getLogger(JarEventRepository.class); |
42 | 46 | ||
... | @@ -49,7 +53,7 @@ public class JarEventRepository { | ... | @@ -49,7 +53,7 @@ public class JarEventRepository { |
49 | private ElasticConfiguration elasticconfig ; | 53 | private ElasticConfiguration elasticconfig ; |
50 | TransportClient client =null; | 54 | TransportClient client =null; |
51 | private GateWayConfiguration gatewatConfig; | 55 | private GateWayConfiguration gatewatConfig; |
52 | - | 56 | + private String jarinfoflush ="true"; |
53 | public JarEventRepository(ElasticConfiguration elasticconfig,GateWayConfiguration gatewatConfig) throws Exception{ | 57 | public JarEventRepository(ElasticConfiguration elasticconfig,GateWayConfiguration gatewatConfig) throws Exception{ |
54 | this.elasticconfig=elasticconfig; | 58 | this.elasticconfig=elasticconfig; |
55 | this.gatewatConfig=gatewatConfig; | 59 | this.gatewatConfig=gatewatConfig; |
... | @@ -65,6 +69,11 @@ public class JarEventRepository { | ... | @@ -65,6 +69,11 @@ public class JarEventRepository { |
65 | } | 69 | } |
66 | this.host=host; | 70 | this.host=host; |
67 | this.port=port; | 71 | this.port=port; |
72 | + | ||
73 | + jarinfoflush=this.gatewatConfig.getString(GWEventConstants.JAR_INFO_FLUSH); | ||
74 | + if(ObjectUtils.isEmpty(jarinfoflush)){ | ||
75 | + jarinfoflush="true"; | ||
76 | + } | ||
68 | } | 77 | } |
69 | public String getHost() { | 78 | public String getHost() { |
70 | return host; | 79 | return host; |
... | @@ -81,10 +90,14 @@ public class JarEventRepository { | ... | @@ -81,10 +90,14 @@ public class JarEventRepository { |
81 | * @param event | 90 | * @param event |
82 | * @return boolean<BR> | 91 | * @return boolean<BR> |
83 | * @throws JsonProcessingException | 92 | * @throws JsonProcessingException |
93 | + * @throws ExecutionException | ||
94 | + * @throws InterruptedException | ||
84 | */ | 95 | */ |
85 | public boolean saveJarEvent(GWEvent event) throws JsonProcessingException{ | 96 | public boolean saveJarEvent(GWEvent event) throws JsonProcessingException{ |
86 | Map<String,Object> newInfo=new HashMap<String,Object>(); | 97 | Map<String,Object> newInfo=new HashMap<String,Object>(); |
87 | 98 | ||
99 | + | ||
100 | + | ||
88 | Object jarPathObject=event.getEventSource().get(GWEventConstants.JAR_PATH); | 101 | Object jarPathObject=event.getEventSource().get(GWEventConstants.JAR_PATH); |
89 | if(!ObjectUtils.isEmpty(jarPathObject)){ | 102 | if(!ObjectUtils.isEmpty(jarPathObject)){ |
90 | Path jarPath =(Path)jarPathObject; | 103 | Path jarPath =(Path)jarPathObject; |
... | @@ -94,11 +107,36 @@ public class JarEventRepository { | ... | @@ -94,11 +107,36 @@ public class JarEventRepository { |
94 | newInfo.put(JAR_PORT, Integer.valueOf(this.port)); | 107 | newInfo.put(JAR_PORT, Integer.valueOf(this.port)); |
95 | 108 | ||
96 | String docString=Utils.toJson(newInfo); | 109 | String docString=Utils.toJson(newInfo); |
110 | + | ||
111 | + //如果是从{@link LocalFileSystemApiJarHandler}的初始化方法中传递过来的event,则必须要对目前现有的jarinfo进行查询判断 | ||
112 | + //判断启动配置参数从{@link LocalFileSystemApiJarHandler} | ||
113 | + Object jarAddedWhenInit=event.getEventSource().get(GWEventConstants.JAR_ADD_WHEN_INIT); | ||
114 | + if(!ObjectUtils.isEmpty(jarAddedWhenInit)&& Boolean.valueOf(String.valueOf(jarAddedWhenInit))){ | ||
115 | +// GetRequestBuilder getBuilder=client.prepareGet(GWConstants.DEFAUL_JAREVENT_INDEX, DEFAUL_JAREVENT_TYPE,key); | ||
116 | +// | ||
117 | +// GetResponse getResponse=null; | ||
118 | +// try { | ||
119 | +// getResponse = getBuilder.execute().get(); | ||
120 | +// if(getResponse !=null){ | ||
121 | +// LOG.info("do not insert jar event info:[{}],because it is existed",newInfo); | ||
122 | +// return false; | ||
123 | +// } | ||
124 | +// } catch (InterruptedException | ExecutionException e) { | ||
125 | +// e.printStackTrace(); | ||
126 | +// LOG.info("error happend when query jar file [{}] info ,cause by [{}],nothing to insert jar event info",key,e.getMessage()); | ||
127 | +// return false; | ||
128 | +// } | ||
129 | + if(!Boolean.valueOf(String.valueOf(jarinfoflush))){ | ||
130 | + LOG.info("do not insert jar event info:[{}],because property init.jarinfoflush ",newInfo,jarinfoflush); | ||
131 | + return true; | ||
132 | + } | ||
133 | + } | ||
134 | + | ||
97 | //我们使用文件名作为key | 135 | //我们使用文件名作为key |
98 | IndexResponse indexResponse= client.prepareIndex(GWConstants.DEFAUL_JAREVENT_INDEX, DEFAUL_JAREVENT_TYPE,key) | 136 | IndexResponse indexResponse= client.prepareIndex(GWConstants.DEFAUL_JAREVENT_INDEX, DEFAUL_JAREVENT_TYPE,key) |
99 | .setSource(docString).get(); | 137 | .setSource(docString).get(); |
100 | if(indexResponse !=null){ | 138 | if(indexResponse !=null){ |
101 | - LOG.info("success to jar event info:[{}],id:[{}]",newInfo,indexResponse.getId()); | 139 | + LOG.info("success to insert jar event info:[{}],id:[{}]",newInfo,indexResponse.getId()); |
102 | return true; | 140 | return true; |
103 | } | 141 | } |
104 | } | 142 | } | ... | ... |
... | @@ -24,11 +24,13 @@ public class JarEventRepositoryFactory { | ... | @@ -24,11 +24,13 @@ public class JarEventRepositoryFactory { |
24 | 24 | ||
25 | public JarEventRepository jarEventRepository() throws Exception{ | 25 | public JarEventRepository jarEventRepository() throws Exception{ |
26 | GateWayConfiguration gatewatConfig=new GateWayConfiguration(); | 26 | GateWayConfiguration gatewatConfig=new GateWayConfiguration(); |
27 | + String jarinfoflush =environment.getProperty(GWEventConstants.JAR_INFO_FLUSH); | ||
27 | String port = environment.getProperty("server.port"); | 28 | String port = environment.getProperty("server.port"); |
28 | String host = environment.getProperty("server.address"); | 29 | String host = environment.getProperty("server.address"); |
29 | Map<String,String> map=new HashMap<String,String>(); | 30 | Map<String,String> map=new HashMap<String,String>(); |
30 | map.put("port", port); | 31 | map.put("port", port); |
31 | map.put("host", host); | 32 | map.put("host", host); |
33 | + map.put(GWEventConstants.JAR_INFO_FLUSH, jarinfoflush); | ||
32 | gatewatConfig.addProperties(map); | 34 | gatewatConfig.addProperties(map); |
33 | JarEventRepository rep=new JarEventRepository(elasticconfig, gatewatConfig); | 35 | JarEventRepository rep=new JarEventRepository(elasticconfig, gatewatConfig); |
34 | return rep; | 36 | return rep; | ... | ... |
... | @@ -102,5 +102,21 @@ public enum ServiceManager{ | ... | @@ -102,5 +102,21 @@ public enum ServiceManager{ |
102 | 102 | ||
103 | 103 | ||
104 | 104 | ||
105 | + /** | ||
106 | + * Method name: clearServiceCache <BR> | ||
107 | + * Description: clearServiceCache,可供手动清空service cache<BR> | ||
108 | + * Remark: <BR> | ||
109 | + * @return boolean<BR> | ||
110 | + */ | ||
111 | + public boolean clearServiceCache(){ | ||
112 | + Set<String> dubboEntrySet=dubboServiceCache.keySet(); | ||
113 | + for(String key:dubboEntrySet){ | ||
114 | + LOG.info("clear dubbo cache for [{}] ",key); | ||
115 | + dubboServiceCache.remove(key); | ||
116 | + LOG.info("Dubbo Service:[{}] has bean shutdown,done to clear dubbo cache for [{}]",key,key); | ||
117 | + } | ||
118 | + return true; | ||
119 | + } | ||
120 | + | ||
105 | 121 | ||
106 | } | 122 | } | ... | ... |
... | @@ -34,6 +34,7 @@ import com.zhaoonline.support.gateway.apijar.ApiUploadResult; | ... | @@ -34,6 +34,7 @@ import com.zhaoonline.support.gateway.apijar.ApiUploadResult; |
34 | import com.zhaoonline.support.gateway.apijar.FileInfo; | 34 | import com.zhaoonline.support.gateway.apijar.FileInfo; |
35 | import com.zhaoonline.support.gateway.listener.JarEventRepository; | 35 | import com.zhaoonline.support.gateway.listener.JarEventRepository; |
36 | import com.zhaoonline.support.gateway.listener.JarEventRepositoryFactorySingleton; | 36 | import com.zhaoonline.support.gateway.listener.JarEventRepositoryFactorySingleton; |
37 | +import com.zhaoonline.support.gateway.service.ServiceManager; | ||
37 | import com.zhaoonline.support.gateway.utils.Utils; | 38 | import com.zhaoonline.support.gateway.utils.Utils; |
38 | 39 | ||
39 | /** | 40 | /** |
... | @@ -120,6 +121,14 @@ public class ApiManagerServlet extends HttpServlet { | ... | @@ -120,6 +121,14 @@ public class ApiManagerServlet extends HttpServlet { |
120 | handleDownloadApi(req,resp); | 121 | handleDownloadApi(req,resp); |
121 | } else if (hasSubPath(req, "/unload")){ | 122 | } else if (hasSubPath(req, "/unload")){ |
122 | handleUnloadApi(req,resp); | 123 | handleUnloadApi(req,resp); |
124 | + }else if (hasSubPath(req, "/clearcache")){ | ||
125 | + LOG.info("begin to clear all service cache"); | ||
126 | + ServiceManager.INSTANCE.clearServiceCache(); | ||
127 | + ApiResponse apiResponse=new ApiResponse(); | ||
128 | + Map<String,String> ret =new HashMap<String,String>(); | ||
129 | + ret.put("success", "all cache have been cleared "); | ||
130 | + apiResponse.setData(ret); | ||
131 | + Utils.toJSON(apiResponse, resp.getOutputStream(), true); | ||
123 | } | 132 | } |
124 | } | 133 | } |
125 | 134 | ... | ... |
... | @@ -3,12 +3,14 @@ application.message=\u8D75\u6D8C\u5728\u7EBFAPI GateWay\u6B22\u8FCE\u60A8 | ... | @@ -3,12 +3,14 @@ application.message=\u8D75\u6D8C\u5728\u7EBFAPI GateWay\u6B22\u8FCE\u60A8 |
3 | spring.application.name=apigateway | 3 | spring.application.name=apigateway |
4 | server.port=80 | 4 | server.port=80 |
5 | #server.address=192.168.0.162 | 5 | #server.address=192.168.0.162 |
6 | -server.address=192.168.3.79 | 6 | +server.address=192.168.1.102 |
7 | 7 | ||
8 | es.host=192.168.0.162 | 8 | es.host=192.168.0.162 |
9 | es.port=9300 | 9 | es.port=9300 |
10 | es.cluster.name=zhaoonline | 10 | es.cluster.name=zhaoonline |
11 | 11 | ||
12 | +init.jarinfoflush=false | ||
13 | + | ||
12 | spring.mvc.view.prefix: /WEB-INF/view/ | 14 | spring.mvc.view.prefix: /WEB-INF/view/ |
13 | spring.mvc.view.suffix: .jsp | 15 | spring.mvc.view.suffix: .jsp |
14 | 16 | ... | ... |
-
Please register or login to post a comment