From c900c2f831b2027f23f41c068ba8fd4eb50b17f3 Mon Sep 17 00:00:00 2001 From: "xin.cao" Date: Thu, 11 Apr 2019 10:49:28 +0800 Subject: [PATCH] ok --- jswitcher-ui/dependency-reduced-pom.xml | 6 +- jswitcher-ui/pom.xml | 4 +- .../jswitcher/ui/config/RootConfig.java | 29 +++++++++ .../jswitcher/ui/config/WebConfig.java | 11 ---- .../ui/controller/SwitcherController.java | 64 +++++++++++-------- .../jswitcher/ui/core/CustomCacheManager.java | 40 ++++++++++++ .../{templates => static}/index.html | 0 7 files changed, 112 insertions(+), 42 deletions(-) create mode 100644 jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/RootConfig.java create mode 100644 jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/core/CustomCacheManager.java rename jswitcher-ui/src/main/resources/{templates => static}/index.html (100%) diff --git a/jswitcher-ui/dependency-reduced-pom.xml b/jswitcher-ui/dependency-reduced-pom.xml index 7fc8b8a..ee18999 100644 --- a/jswitcher-ui/dependency-reduced-pom.xml +++ b/jswitcher-ui/dependency-reduced-pom.xml @@ -106,9 +106,9 @@ provided - org.springframework.boot - spring-boot-starter-thymeleaf - 2.1.3.RELEASE + com.github.ben-manes.caffeine + caffeine + 2.6.2 provided diff --git a/jswitcher-ui/pom.xml b/jswitcher-ui/pom.xml index b0b0f9a..840ee11 100644 --- a/jswitcher-ui/pom.xml +++ b/jswitcher-ui/pom.xml @@ -37,8 +37,8 @@ spring-boot-starter-web - org.springframework.boot - spring-boot-starter-thymeleaf + com.github.ben-manes.caffeine + caffeine org.springframework.boot diff --git a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/RootConfig.java b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/RootConfig.java new file mode 100644 index 0000000..51a13c0 --- /dev/null +++ b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/RootConfig.java @@ -0,0 +1,29 @@ +package com.github.xincao9.jswitcher.ui.config; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; +import com.github.xincao9.jswitcher.ui.core.CustomCacheManager; +import java.util.concurrent.TimeUnit; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * + * @author xincao9@gmail.com + */ +@Configuration +public class RootConfig { + + /** + * + * @return + */ + @Bean + public CustomCacheManager customCacheManager() { + CustomCacheManager customCacheManager = new CustomCacheManager(); + Cache defaultCache = Caffeine.newBuilder().recordStats().expireAfterWrite(5, TimeUnit.SECONDS).maximumSize(5000).build(); + customCacheManager.setDefaultCache(defaultCache); + return customCacheManager; + } + +} \ No newline at end of file diff --git a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/WebConfig.java b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/WebConfig.java index 96a4ba5..bead693 100644 --- a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/WebConfig.java +++ b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/config/WebConfig.java @@ -20,7 +20,6 @@ import java.util.List; import org.springframework.context.annotation.Configuration; import org.springframework.http.converter.HttpMessageConverter; -import org.springframework.web.servlet.config.annotation.ViewControllerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; /** @@ -31,16 +30,6 @@ @Configuration public class WebConfig implements WebMvcConfigurer { - /** - * 配置视图 - * - * @param registry - */ - @Override - public void addViewControllers(ViewControllerRegistry registry) { - registry.addViewController("/").setViewName("index"); // 首页 - } - /** * 设置消息转化器 * diff --git a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java index bd82836..ec413aa 100644 --- a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java +++ b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/controller/SwitcherController.java @@ -17,6 +17,7 @@ import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; +import com.github.benmanes.caffeine.cache.Cache; import com.github.xincao9.jsonrpc.core.DiscoveryService; import com.github.xincao9.jsonrpc.core.JsonRPCClient; import com.github.xincao9.jsonrpc.core.constant.ResponseCode; @@ -26,10 +27,10 @@ import com.github.xincao9.jsonrpc.core.protocol.Response; import com.github.xincao9.jswitcher.api.service.SwitcherService; import com.github.xincao9.jswitcher.api.vo.Switcher; +import com.github.xincao9.jswitcher.ui.core.CustomCacheManager; import java.util.ArrayList; import java.util.Base64; import java.util.Collections; -import java.util.Date; import java.util.List; import java.util.Map; import java.util.Objects; @@ -64,33 +65,44 @@ public class SwitcherController { private static final String ON = "on"; private static final String OFF = "off"; private static final String SET = "set"; + private static final String SWITCHER_LIST = "switcher:list"; + + @Autowired + private CustomCacheManager customCacheManager; /** * 获取开关列表 * * @return 开关列表 */ - private List> getKeys() { + private List> getSwitcheres() { try { + Cache>> cache = customCacheManager.getDefaultCache(); + List> resp; + resp = cache.getIfPresent(SWITCHER_LIST); + if (resp != null) { + return resp; + } + resp = new ArrayList(); List endpoints = discoveryService.query(SwitcherService.class.getTypeName()); if (endpoints == null || endpoints.isEmpty()) { return Collections.EMPTY_LIST; } - List> keys = new ArrayList(); AtomicInteger no = new AtomicInteger(0); - endpoints.forEach((endpoint) -> { + for (Endpoint endpoint : endpoints) { List switcheres = getKeysByHostAndPort(endpoint.getHost(), endpoint.getPort()); - if (!(switcheres == null || switcheres.isEmpty())) { - switcheres.forEach((switcher) -> { - JSONObject v0 = JSONObject.parseObject(JSONObject.toJSONString(endpoint)); - JSONObject v1 = JSONObject.parseObject(JSONObject.toJSONString(switcher)); - v0.putAll(v1); - v0.put("no", no.addAndGet(1)); - keys.add(v0); - }); + if (switcheres == null || switcheres.isEmpty()) { + continue; } - }); - Collections.sort(keys, (Map o1, Map o2) -> { + for (Switcher switcher : switcheres) { + JSONObject v0 = JSONObject.parseObject(JSONObject.toJSONString(endpoint)); + JSONObject v1 = JSONObject.parseObject(JSONObject.toJSONString(switcher)); + v0.putAll(v1); + v0.put("no", no.addAndGet(1)); + resp.add(v0); + } + } + Collections.sort(resp, (Map o1, Map o2) -> { String application1 = String.valueOf(o1.get("application")); String application2 = String.valueOf(o2.get("application")); if (!application1.equalsIgnoreCase(application2)) { @@ -113,7 +125,10 @@ private List> getKeys() { } return 0; }); - return keys; + if (!resp.isEmpty()) { + cache.put(SWITCHER_LIST, resp); + } + return resp; } catch (Throwable e) { LOGGER.error(e.getMessage()); } @@ -128,7 +143,7 @@ private List> getKeys() { @GetMapping("applications") public ResponseEntity> applications() { try { - List> keys = getKeys(); + List> keys = getSwitcheres(); if (keys == null || keys.isEmpty()) { return ResponseEntity.status(400).build(); } @@ -148,7 +163,7 @@ public ResponseEntity> applications() { @GetMapping("application/{application}/endpoints") public ResponseEntity>> applicationEndpoints(@PathVariable String application) { try { - List> keys = getKeys(); + List> keys = getSwitcheres(); if (keys == null || keys.isEmpty()) { return ResponseEntity.status(400).build(); } @@ -178,17 +193,14 @@ public ResponseEntity>> endpointKeys(@PathVariable Stri if (StringUtils.isBlank(host) || port == null || port <= 0 || port > 65535) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).build(); } - List switchers = getKeysByHostAndPort(host, port); - if (switchers == null || switchers.isEmpty()) { + List> keys = getSwitcheres(); + if (keys == null || keys.isEmpty()) { return ResponseEntity.status(400).build(); } - AtomicInteger no = new AtomicInteger(0); - return ResponseEntity.ok(switchers.stream().map((t) -> { - Map map = JSONObject.parseObject(JSONObject.toJSONString(t)).getInnerMap(); - map.put("no", no.incrementAndGet()); - map.put("createTime", new Date()); - return map; - }).collect(Collectors.toList())); + keys = keys.stream() + .filter((key) -> (StringUtils.equals(host, String.valueOf(key.get("host"))) && port.intValue() == (Integer) key.get("port"))) + .collect(Collectors.toList()); + return ResponseEntity.ok(keys); } /** diff --git a/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/core/CustomCacheManager.java b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/core/CustomCacheManager.java new file mode 100644 index 0000000..a65b7b2 --- /dev/null +++ b/jswitcher-ui/src/main/java/com/github/xincao9/jswitcher/ui/core/CustomCacheManager.java @@ -0,0 +1,40 @@ +package com.github.xincao9.jswitcher.ui.core; + +import com.github.benmanes.caffeine.cache.Cache; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * + * @author xincao9@gmail.com + */ +public class CustomCacheManager { + + private final Map caches = new ConcurrentHashMap(); + private Cache defaultCache; + + public Cache getCache(String name) { + if (caches.containsKey(name)) { + return caches.get(name); + } + if (defaultCache != null) { + return defaultCache; + } + return null; + } + + public void register(String name, Cache cache) { + if (!caches.containsKey(name) && cache != null && caches.size() <= 100) { + caches.put(name, cache); + } + } + + public void setDefaultCache(Cache cache) { + this.defaultCache = cache; + } + + public Cache getDefaultCache() { + return this.defaultCache; + } + +} diff --git a/jswitcher-ui/src/main/resources/templates/index.html b/jswitcher-ui/src/main/resources/static/index.html similarity index 100% rename from jswitcher-ui/src/main/resources/templates/index.html rename to jswitcher-ui/src/main/resources/static/index.html