This commit is contained in:
tanghc
2019-08-22 17:08:25 +08:00
parent bb19a34444
commit 299195e9a1
15 changed files with 115 additions and 119 deletions

View File

@@ -12,6 +12,7 @@ import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.DigestUtils;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
@@ -37,6 +38,16 @@ public class DocDiscovery {
private Map<String, Long> updateTimeMap = new HashMap<>(16);
public DocDiscovery() {
// 解决statusCode不等于200就抛异常问题
restTemplate.setErrorHandler(new DefaultResponseErrorHandler() {
@Override
protected boolean hasError(HttpStatus statusCode) {
return statusCode == null;
}
});
}
public synchronized void refresh(DocManager docManager) {
NamingService namingService = nacosDiscoveryProperties.namingServiceInstance();
List<ServiceInfo> subscribes = null;
@@ -71,13 +82,15 @@ public class DocDiscovery {
docManager.remove(serviceName);
} else {
for (Instance instance : allInstances) {
log.info("加载服务文档instance:{}", instance);
String url = getRouteRequestUrl(instance);
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
if (responseEntity.getStatusCode() == HttpStatus.OK) {
String body = responseEntity.getBody();
log.debug("加载{}文档,文档信息:{}", serviceName, body);
docManager.addDocInfo(serviceName, body);
docManager.addDocInfo(
serviceName
, body
, callback -> log.info("加载服务文档instance:{}", instance)
);
}
}
}

View File

@@ -3,13 +3,14 @@ package com.gitee.sop.websiteserver.manager;
import com.gitee.sop.websiteserver.bean.DocInfo;
import java.util.Collection;
import java.util.function.Consumer;
/**
* @author tanghc
*/
public interface DocManager {
void addDocInfo(String serviceId, String docJson);
void addDocInfo(String serviceId, String docJson, Consumer<DocInfo> callback);
DocInfo getByTitle(String title);

View File

@@ -16,6 +16,7 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.function.Consumer;
/**
* @author tanghc
@@ -40,7 +41,7 @@ public class DocManagerImpl implements DocManager, ApplicationListener<Heartbeat
private DocDiscovery docDiscovery;
@Override
public void addDocInfo(String serviceId, String docInfoJson) {
public void addDocInfo(String serviceId, String docInfoJson, Consumer<DocInfo> callback) {
String newMd5 = DigestUtils.md5DigestAsHex(docInfoJson.getBytes(StandardCharsets.UTF_8));
String oldMd5 = serviceIdMd5Map.get(serviceId);
if (Objects.equals(newMd5, oldMd5)) {
@@ -52,6 +53,7 @@ public class DocManagerImpl implements DocManager, ApplicationListener<Heartbeat
DocInfo docInfo = docParser.parseJson(docRoot);
docInfo.setServiceId(serviceId);
docDefinitionMap.put(docInfo.getTitle(), docInfo);
callback.accept(docInfo);
}
protected DocParser buildDocParser(JSONObject rootDoc) {