Skip to content

Commit

Permalink
ok
Browse files Browse the repository at this point in the history
  • Loading branch information
upliveapp committed Apr 11, 2019
1 parent af41a81 commit c900c2f
Show file tree
Hide file tree
Showing 7 changed files with 112 additions and 42 deletions.
6 changes: 3 additions & 3 deletions jswitcher-ui/dependency-reduced-pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,9 @@
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<version>2.1.3.RELEASE</version>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
<version>2.6.2</version>
<scope>provided</scope>
</dependency>
<dependency>
Expand Down
4 changes: 2 additions & 2 deletions jswitcher-ui/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/
@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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -31,16 +30,6 @@
@Configuration
public class WebConfig implements WebMvcConfigurer {

/**
* 配置视图
*
* @param registry
*/
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/").setViewName("index"); // 首页
}

/**
* 设置消息转化器
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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<Map<String, Object>> getKeys() {
private List<Map<String, Object>> getSwitcheres() {
try {
Cache<String, List<Map<String, Object>>> cache = customCacheManager.getDefaultCache();
List<Map<String, Object>> resp;
resp = cache.getIfPresent(SWITCHER_LIST);
if (resp != null) {
return resp;
}
resp = new ArrayList();
List<Endpoint> endpoints = discoveryService.query(SwitcherService.class.getTypeName());
if (endpoints == null || endpoints.isEmpty()) {
return Collections.EMPTY_LIST;
}
List<Map<String, Object>> keys = new ArrayList();
AtomicInteger no = new AtomicInteger(0);
endpoints.forEach((endpoint) -> {
for (Endpoint endpoint : endpoints) {
List<Switcher> 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<String, Object> o1, Map<String, Object> 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<String, Object> o1, Map<String, Object> o2) -> {
String application1 = String.valueOf(o1.get("application"));
String application2 = String.valueOf(o2.get("application"));
if (!application1.equalsIgnoreCase(application2)) {
Expand All @@ -113,7 +125,10 @@ private List<Map<String, Object>> getKeys() {
}
return 0;
});
return keys;
if (!resp.isEmpty()) {
cache.put(SWITCHER_LIST, resp);
}
return resp;
} catch (Throwable e) {
LOGGER.error(e.getMessage());
}
Expand All @@ -128,7 +143,7 @@ private List<Map<String, Object>> getKeys() {
@GetMapping("applications")
public ResponseEntity<Set<String>> applications() {
try {
List<Map<String, Object>> keys = getKeys();
List<Map<String, Object>> keys = getSwitcheres();
if (keys == null || keys.isEmpty()) {
return ResponseEntity.status(400).build();
}
Expand All @@ -148,7 +163,7 @@ public ResponseEntity<Set<String>> applications() {
@GetMapping("application/{application}/endpoints")
public ResponseEntity<List<Pair<String, String>>> applicationEndpoints(@PathVariable String application) {
try {
List<Map<String, Object>> keys = getKeys();
List<Map<String, Object>> keys = getSwitcheres();
if (keys == null || keys.isEmpty()) {
return ResponseEntity.status(400).build();
}
Expand Down Expand Up @@ -178,17 +193,14 @@ public ResponseEntity<List<Map<String, Object>>> endpointKeys(@PathVariable Stri
if (StringUtils.isBlank(host) || port == null || port <= 0 || port > 65535) {
return ResponseEntity.status(HttpStatus.BAD_REQUEST).build();
}
List<Switcher> switchers = getKeysByHostAndPort(host, port);
if (switchers == null || switchers.isEmpty()) {
List<Map<String, Object>> 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<String, Object> 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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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 [email protected]
*/
public class CustomCacheManager {

private final Map<String, Cache> caches = new ConcurrentHashMap();
private Cache defaultCache;

public <K, V> Cache<K, V> 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;
}

}

0 comments on commit c900c2f

Please sign in to comment.