dubbo优化
This commit is contained in:
@@ -25,8 +25,11 @@ import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import javax.annotation.PreDestroy;
|
||||
import javax.annotation.Resource;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Parameter;
|
||||
import java.lang.reflect.Type;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.*;
|
||||
@@ -46,7 +49,9 @@ public class DubboController {
|
||||
private static Logger logger = LoggerFactory.getLogger(DubboController.class);
|
||||
|
||||
@Value("${zyplayer.doc.dubbo.zookeeper.url:}")
|
||||
private String zookeeperUrl;
|
||||
private String serviceZookeeperUrl;
|
||||
@Value("${zyplayer.doc.dubbo.zookeeper.metadata-url:}")
|
||||
private String metadataZookeeperUrl;
|
||||
@Value("${zyplayer.doc.dubbo.nacos.url:}")
|
||||
private String nacosUrl;
|
||||
@Value("${zyplayer.doc.dubbo.nacos.service:}")
|
||||
@@ -54,6 +59,28 @@ public class DubboController {
|
||||
@Resource
|
||||
private MgDubboStorageService mgDubboStorageService;
|
||||
|
||||
private CuratorFramework serverClient;
|
||||
private CuratorFramework metadataClient;
|
||||
|
||||
@PostConstruct
|
||||
private void init() {
|
||||
if (StringUtils.isNotBlank(serviceZookeeperUrl)) {
|
||||
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
|
||||
serverClient = CuratorFrameworkFactory.newClient(serviceZookeeperUrl, retryPolicy);
|
||||
serverClient.start();
|
||||
}
|
||||
if (StringUtils.isNotBlank(metadataZookeeperUrl)) {
|
||||
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
|
||||
metadataClient = CuratorFrameworkFactory.newClient(metadataZookeeperUrl, retryPolicy);
|
||||
metadataClient.start();
|
||||
}
|
||||
}
|
||||
|
||||
@PreDestroy
|
||||
private void preDestroy() {
|
||||
serverClient.close();
|
||||
}
|
||||
|
||||
/**
|
||||
* 重新获取所有的服务列表
|
||||
*
|
||||
@@ -63,7 +90,7 @@ public class DubboController {
|
||||
@PostMapping(value = "/reloadService")
|
||||
public DocResponseJson loadService() throws Exception {
|
||||
List<DubboInfo> providerList;
|
||||
if (StringUtils.isBlank(zookeeperUrl)) {
|
||||
if (StringUtils.isBlank(serviceZookeeperUrl)) {
|
||||
if (StringUtils.isBlank(nacosUrl) || StringUtils.isBlank(nacosService)) {
|
||||
return DocResponseJson.warn("zyplayer.doc.dubbo.zookeeper.url、zyplayer.doc.dubbo.nacos.url 参数均未配置");
|
||||
}
|
||||
@@ -169,6 +196,7 @@ public class DubboController {
|
||||
**/
|
||||
@PostMapping(value = "/findDocInfo")
|
||||
public DocResponseJson findDocInfo(DubboRequestParam param) {
|
||||
String resultType = null;
|
||||
List<DubboDocInfo.DubboDocParam> paramList = new LinkedList<>();
|
||||
try {
|
||||
Class clazz = Class.forName(param.getService());
|
||||
@@ -176,10 +204,13 @@ public class DubboController {
|
||||
for (Method method : methods) {
|
||||
String methodName = method.getName();
|
||||
if (methodName.equals(param.getMethod())) {
|
||||
resultType = method.getGenericReturnType().getTypeName();
|
||||
Type[] parameterTypes = method.getGenericParameterTypes();
|
||||
for (Type clas : parameterTypes) {
|
||||
Parameter[] parameters = method.getParameters();
|
||||
for (int i = 0; i < parameterTypes.length; i++) {
|
||||
DubboDocInfo.DubboDocParam docParam = new DubboDocInfo.DubboDocParam();
|
||||
docParam.setParamType(clas.getTypeName());
|
||||
docParam.setParamName(parameters[i].getName());
|
||||
docParam.setParamType(parameterTypes[i].getTypeName());
|
||||
paramList.add(docParam);
|
||||
}
|
||||
}
|
||||
@@ -187,9 +218,6 @@ public class DubboController {
|
||||
} catch (ClassNotFoundException e) {
|
||||
return DocResponseJson.warn("未找到指定类,请引入相关包,类名:" + param.getService());
|
||||
}
|
||||
if (paramList.isEmpty()) {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
Map<String, DubboDocInfo> docInfoMap = new HashMap<>();
|
||||
String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC);
|
||||
if (StringUtils.isNotBlank(dubboServiceDoc)) {
|
||||
@@ -203,6 +231,7 @@ public class DubboController {
|
||||
dubboDocInfo.setParams(paramList);
|
||||
dubboDocInfo.setFunction(function);
|
||||
dubboDocInfo.setVersion(1);
|
||||
dubboDocInfo.setResultType(resultType);
|
||||
dubboDocInfo.setService(param.getService());
|
||||
dubboDocInfo.setMethod(param.getMethod());
|
||||
docInfoMap.put(function, dubboDocInfo);
|
||||
@@ -293,16 +322,13 @@ public class DubboController {
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
private List<DubboInfo> getDubboInfoByZookeeper() throws Exception {
|
||||
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
|
||||
CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
|
||||
client.start();
|
||||
List<String> dubboList = client.getChildren().forPath("/dubbo");
|
||||
List<String> dubboList = serverClient.getChildren().forPath("/dubbo");
|
||||
if (dubboList == null || dubboList.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<DubboInfo> providerList = new LinkedList<>();
|
||||
for (String dubboStr : dubboList) {
|
||||
List<String> providers = client.getChildren().forPath("/dubbo/" + dubboStr + "/providers");
|
||||
List<String> providers = serverClient.getChildren().forPath("/dubbo/" + dubboStr + "/providers");
|
||||
|
||||
List<DubboInfo.DubboNodeInfo> nodeList = providers.stream().map(val -> {
|
||||
String tempStr = val;
|
||||
@@ -336,7 +362,6 @@ public class DubboController {
|
||||
dubboInfo.setNodeList(nodeList);
|
||||
providerList.add(dubboInfo);
|
||||
}
|
||||
client.close();
|
||||
return providerList;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@ public class DubboDocInfo {
|
||||
private String function;
|
||||
private String explain;
|
||||
private String result;
|
||||
private String resultType;
|
||||
private Integer version;
|
||||
private List<DubboDocParam> params;
|
||||
|
||||
@@ -21,7 +22,7 @@ public class DubboDocInfo {
|
||||
private String paramName;
|
||||
private String paramType;
|
||||
private String paramDesc;
|
||||
private Object paramVal;
|
||||
private Object paramValue;
|
||||
private Integer required;
|
||||
|
||||
public String getParamName() {
|
||||
@@ -48,12 +49,12 @@ public class DubboDocInfo {
|
||||
this.paramDesc = paramDesc;
|
||||
}
|
||||
|
||||
public Object getParamVal() {
|
||||
return paramVal;
|
||||
public Object getParamValue() {
|
||||
return paramValue;
|
||||
}
|
||||
|
||||
public void setParamVal(Object paramVal) {
|
||||
this.paramVal = paramVal;
|
||||
public void setParamValue(Object paramValue) {
|
||||
this.paramValue = paramValue;
|
||||
}
|
||||
|
||||
public Integer getRequired() {
|
||||
@@ -119,4 +120,12 @@ public class DubboDocInfo {
|
||||
public void setResult(String result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public String getResultType() {
|
||||
return resultType;
|
||||
}
|
||||
|
||||
public void setResultType(String resultType) {
|
||||
this.resultType = resultType;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,7 +73,7 @@
|
||||
<!--</div>-->
|
||||
<el-table :data="docParamList" border style="width: 100%; margin-bottom: 5px;">
|
||||
<el-table-column label="顺序" width="100">
|
||||
<template slot-scope="scope">{{scope.$index + 1}}</template>
|
||||
<template slot-scope="scope">{{scope.$index}}</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="参数名" width="200">
|
||||
<template slot-scope="scope">
|
||||
@@ -97,6 +97,9 @@
|
||||
<el-button @click.prevent="saveDocInfoParam" type="primary" style="float: right;margin: 5px;">保存</el-button>
|
||||
<!--<el-button @click.prevent="addDocParam" style="float: right;margin: 5px;">添加</el-button>-->
|
||||
</el-form-item>
|
||||
<el-form-item label="返回值:">
|
||||
{{dubboInfo.docInfo.resultType}}
|
||||
</el-form-item>
|
||||
<el-form-item label="结果:">
|
||||
<div v-if="dubboInfoResultShow">
|
||||
<pre>{{dubboInfo.docInfo.result}}<el-button @click.prevent="dubboInfoResultShow = false;" style="float: right;">编辑</el-button></pre>
|
||||
@@ -120,11 +123,11 @@
|
||||
</el-select>
|
||||
<el-button slot="append" @click.prevent="requestExecute">执行</el-button>
|
||||
</el-input>
|
||||
<el-form label-width="100px"label-position="top">
|
||||
<el-form label-width="100px" label-position="top">
|
||||
<el-form-item label="请求参数:">
|
||||
<el-table :data="docParamRequestList" border style="width: 100%; margin: 10px 0;">
|
||||
<el-table-column label="顺序" width="100">
|
||||
<template slot-scope="scope">{{scope.$index + 1}}</template>
|
||||
<template slot-scope="scope">{{scope.$index}}</template>
|
||||
</el-table-column>
|
||||
<el-table-column label="参数名">
|
||||
<template slot-scope="scope">{{scope.row.paramName}}</template>
|
||||
@@ -306,10 +309,10 @@
|
||||
});
|
||||
},
|
||||
saveDocInfoExplain(){
|
||||
this.doSaveDocInfo(app.docInfoExplainInput, null, null);
|
||||
this.doSaveDocInfo(app.docInfoExplainInput, null, null, true);
|
||||
},
|
||||
saveDocInfoResult(){
|
||||
this.doSaveDocInfo(null, null, app.docInfoResultInput);
|
||||
this.doSaveDocInfo(null, null, app.docInfoResultInput, true);
|
||||
},
|
||||
saveDocInfoParam() {
|
||||
var docParamList = [];
|
||||
@@ -320,7 +323,7 @@
|
||||
}
|
||||
}
|
||||
var paramsJson = JSON.stringify(docParamList);
|
||||
this.doSaveDocInfo(null, paramsJson, null);
|
||||
this.doSaveDocInfo(null, paramsJson, null, true);
|
||||
},
|
||||
createDocParamRequestList() {
|
||||
var docParamList = [];
|
||||
@@ -332,10 +335,12 @@
|
||||
}
|
||||
app.docParamRequestList = docParamList;
|
||||
},
|
||||
doSaveDocInfo(explain, params, result){
|
||||
doSaveDocInfo(explain, params, result, showSuccess){
|
||||
var param = {
|
||||
service: app.dubboInfo.interface,
|
||||
method: app.dubboInfo.method,
|
||||
resultType: app.dubboInfo.resultType,
|
||||
paramValue: app.dubboInfo.paramValue,
|
||||
version: app.dubboInfo.docInfo.version || 0,
|
||||
explain: explain,
|
||||
result: result,
|
||||
@@ -349,7 +354,9 @@
|
||||
app.dubboInfoResultShow = true;
|
||||
app.docParamList = json.data.params || [];
|
||||
app.createDocParamRequestList();
|
||||
Toast.success("保存成功!");
|
||||
if (showSuccess) {
|
||||
Toast.success("保存成功!");
|
||||
}
|
||||
}
|
||||
});
|
||||
},
|
||||
@@ -398,7 +405,7 @@
|
||||
app.onlineDebugLoading = true;
|
||||
ajaxTemp("zyplayer-doc-dubbo/doc-dubbo/request", "post", "json", param, function (json) {
|
||||
app.onlineDebugLoading = false;
|
||||
if (validateResult(json)) {
|
||||
if (json.errCode == 200) {
|
||||
try {
|
||||
app.requestResult = Formatjson.processObjectToHtmlPre(JSON.parse(json.data), 0, false, false, false, false);
|
||||
} catch (e) {
|
||||
@@ -408,6 +415,10 @@
|
||||
app.requestResult = json.data;
|
||||
}
|
||||
}
|
||||
var paramsJson = JSON.stringify(app.docParamRequestList);
|
||||
app.doSaveDocInfo(null, paramsJson, null, false);
|
||||
} else {
|
||||
app.requestResult = json.errMsg;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@@ -16,6 +16,8 @@ zyplayer:
|
||||
# 优先使用zookeeper,未配置时找nacos的配置
|
||||
zookeeper:
|
||||
url: 127.0.0.1:2181
|
||||
# 服务参数那些信息的服务地址,dubbo7.0新特性
|
||||
metadata-url: 127.0.0.1:2181
|
||||
nacos:
|
||||
# url: http://127.0.0.1:8848/nacos
|
||||
# 服务名称,多个使用 ; 分割,nacos没办法获取所有的服务列表,所以需要指定
|
||||
|
||||
Reference in New Issue
Block a user