dubbo文档操作接口和页面
This commit is contained in:
@@ -4,9 +4,13 @@ import cn.hutool.http.HttpUtil;
|
||||
import com.alibaba.dubbo.rpc.service.GenericService;
|
||||
import com.alibaba.fastjson.JSON;
|
||||
import com.zyplayer.doc.core.json.DocResponseJson;
|
||||
import com.zyplayer.doc.dubbo.controller.param.DubboRequestParam;
|
||||
import com.zyplayer.doc.dubbo.framework.bean.DubboDocInfo;
|
||||
import com.zyplayer.doc.dubbo.framework.bean.DubboInfo;
|
||||
import com.zyplayer.doc.dubbo.framework.bean.NacosDubboInfo;
|
||||
import com.zyplayer.doc.dubbo.framework.bean.ReferenceConfigHolder;
|
||||
import com.zyplayer.doc.dubbo.framework.constant.StorageKeys;
|
||||
import com.zyplayer.doc.dubbo.framework.service.MgDubboStorageService;
|
||||
import org.apache.commons.lang.StringUtils;
|
||||
import org.apache.commons.lang.math.NumberUtils;
|
||||
import org.apache.curator.RetryPolicy;
|
||||
@@ -20,6 +24,7 @@ import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.net.URLDecoder;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
@@ -28,7 +33,7 @@ import java.util.stream.Collectors;
|
||||
* 文档控制器
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月8日
|
||||
* @since 2019年2月10日
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/zyplayer-doc-dubbo/doc-dubbo")
|
||||
@@ -41,9 +46,17 @@ public class DubboController {
|
||||
private String nacosUrl;
|
||||
@Value("${zyplayer.doc.dubbo.nacos.service:}")
|
||||
private String nacosService;
|
||||
@Resource
|
||||
private MgDubboStorageService mgDubboStorageService;
|
||||
|
||||
@GetMapping(value = "/getList")
|
||||
public DocResponseJson getDataSourceList() throws Exception {
|
||||
/**
|
||||
* 重新获取所有的服务列表
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
@GetMapping(value = "/reloadService")
|
||||
public DocResponseJson loadService() throws Exception {
|
||||
List<DubboInfo> providerList;
|
||||
if (StringUtils.isBlank(zookeeperUrl)) {
|
||||
if (StringUtils.isBlank(nacosUrl) || StringUtils.isBlank(nacosService)) {
|
||||
@@ -54,17 +67,100 @@ public class DubboController {
|
||||
} else {
|
||||
providerList = this.getDubboInfoByZookeeper();
|
||||
}
|
||||
GenericService bean = ReferenceConfigHolder.getBean(providerList.get(0).getNodeList().get(0));
|
||||
Object o = bean.$invoke("getUserList", new String[]{}, new String[]{});
|
||||
System.out.println(o);
|
||||
mgDubboStorageService.put(StorageKeys.DUBBO_SERVICE_LIST, JSON.toJSONString(providerList));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 请求执行服务
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
@GetMapping(value = "/request")
|
||||
public DocResponseJson request(DubboRequestParam param) {
|
||||
DubboInfo.DubboNodeInfo dubboNodeInfo = new DubboInfo.DubboNodeInfo();
|
||||
dubboNodeInfo.setIp(param.getIp());
|
||||
dubboNodeInfo.setPort(param.getPort());
|
||||
dubboNodeInfo.setInterfaceX(param.getService());
|
||||
String[] paramTypes = Optional.ofNullable(param.getParamTypes()).orElse(new String[]{});
|
||||
Object[] params = Optional.ofNullable(param.getParams()).orElse(new Object[]{});
|
||||
GenericService bean = ReferenceConfigHolder.getBean(dubboNodeInfo);
|
||||
try {
|
||||
Object result = bean.$invoke(param.getMethod(), paramTypes, params);
|
||||
return DocResponseJson.ok(result);
|
||||
} catch (Exception e) {
|
||||
e.printStackTrace();
|
||||
return DocResponseJson.warn("请求失败:" + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取文档列表
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
@GetMapping(value = "/getDocList")
|
||||
public DocResponseJson getDocList() {
|
||||
String dubboServiceList = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_LIST);
|
||||
String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC);
|
||||
if (StringUtils.isBlank(dubboServiceList)) {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
List<DubboInfo> providerList = JSON.parseArray(dubboServiceList, DubboInfo.class);
|
||||
if (StringUtils.isNotBlank(dubboServiceDoc)) {
|
||||
List<DubboDocInfo> docInfoList = JSON.parseArray(dubboServiceDoc, DubboDocInfo.class);
|
||||
Map<String, DubboDocInfo> docInfoMap = docInfoList.stream().collect(Collectors.toMap(DubboDocInfo::getService, val -> val));
|
||||
for (DubboInfo dubboInfo : providerList) {
|
||||
dubboInfo.setDocInfo(docInfoMap.get(dubboInfo.getInterfaceX()));
|
||||
}
|
||||
}
|
||||
return DocResponseJson.ok(providerList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 保存文档
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
@GetMapping(value = "/saveDoc")
|
||||
public DocResponseJson saveDoc(DubboDocInfo param) {
|
||||
String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC);
|
||||
Map<String, DubboDocInfo> docInfoMap = new HashMap<>();
|
||||
if (StringUtils.isNotBlank(dubboServiceDoc)) {
|
||||
List<DubboDocInfo> docInfoList = JSON.parseArray(dubboServiceDoc, DubboDocInfo.class);
|
||||
docInfoMap = docInfoList.stream().collect(Collectors.toMap(DubboDocInfo::getService, val -> val));
|
||||
}
|
||||
DubboDocInfo dubboDocInfo = docInfoMap.get(param.getService());
|
||||
if (dubboDocInfo != null) {
|
||||
Integer newVersion = Optional.ofNullable(param.getVersion()).orElse(1);
|
||||
Integer oldVersion = Optional.ofNullable(dubboDocInfo.getVersion()).orElse(1);
|
||||
if (oldVersion > newVersion) {
|
||||
return DocResponseJson.warn("已有用户在您之前修改过文档,请刷新后再修改");
|
||||
}
|
||||
param.setVersion(oldVersion + 1);
|
||||
} else {
|
||||
param.setVersion(1);
|
||||
}
|
||||
docInfoMap.put(param.getService(), param);
|
||||
List<DubboDocInfo> docInfoList = new ArrayList<>(docInfoMap.values());
|
||||
mgDubboStorageService.put(StorageKeys.DUBBO_SERVICE_DOC, JSON.toJSONString(docInfoList));
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过nacos方式获取所有服务
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
private List<DubboInfo> getDubboInfoByNacos() {
|
||||
List<DubboInfo> providerList = new LinkedList<>();
|
||||
String[] nacosServiceArr = nacosService.split(";");
|
||||
for (String service : nacosServiceArr) {
|
||||
String resultStr = HttpUtil.get(nacosUrl + "/v1/ns/instance/list?serviceName=" + service);
|
||||
String resultStr = HttpUtil.get(nacosUrl + "/v1/ns/instance/list?serviceName=providers:" + service);
|
||||
NacosDubboInfo dubboInstance = JSON.parseObject(resultStr, NacosDubboInfo.class);
|
||||
List<NacosDubboInfo.HostsBean> hosts = dubboInstance.getHosts();
|
||||
DubboInfo dubboInfo = new DubboInfo();
|
||||
@@ -76,14 +172,21 @@ public class DubboController {
|
||||
dubboNodeInfo.setInterfaceX(host.getMetadata().getInterfaceX());
|
||||
dubboNodeInfo.setMethods(host.getMetadata().getMethods().split(","));
|
||||
dubboNodeInfo.setApplication(host.getMetadata().getApplication());
|
||||
nodeList.add(dubboNodeInfo);
|
||||
}
|
||||
dubboInfo.setInterfaceX(service.substring(service.indexOf(":") + 1));
|
||||
dubboInfo.setInterfaceX(service);
|
||||
dubboInfo.setNodeList(nodeList);
|
||||
providerList.add(dubboInfo);
|
||||
}
|
||||
return providerList;
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过Zookeeper方式获取所有服务
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
**/
|
||||
private List<DubboInfo> getDubboInfoByZookeeper() throws Exception {
|
||||
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
|
||||
CuratorFramework client = CuratorFrameworkFactory.newClient(zookeeperUrl, retryPolicy);
|
||||
|
||||
@@ -0,0 +1,64 @@
|
||||
package com.zyplayer.doc.dubbo.controller.param;
|
||||
|
||||
/**
|
||||
* 请求参数对象
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
*/
|
||||
public class DubboRequestParam {
|
||||
private String service;
|
||||
private String method;
|
||||
private String ip;
|
||||
private Integer port;
|
||||
private String[] paramTypes;
|
||||
private Object[] params;
|
||||
|
||||
public String[] getParamTypes() {
|
||||
return paramTypes;
|
||||
}
|
||||
|
||||
public void setParamTypes(String[] paramTypes) {
|
||||
this.paramTypes = paramTypes;
|
||||
}
|
||||
|
||||
public String getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getIp() {
|
||||
return ip;
|
||||
}
|
||||
|
||||
public void setIp(String ip) {
|
||||
this.ip = ip;
|
||||
}
|
||||
|
||||
public Integer getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(Integer port) {
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public Object[] getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParams(Object[] params) {
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package com.zyplayer.doc.dubbo.framework.bean;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 请求参数对象
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2019年2月10日
|
||||
*/
|
||||
public class DubboDocInfo {
|
||||
private String service;
|
||||
private String method;
|
||||
private String explain;
|
||||
private Integer version;
|
||||
private List<DubboDocParam> params;
|
||||
|
||||
public class DubboDocParam {
|
||||
private String paramName;
|
||||
private String paramType;
|
||||
private String paramDesc;
|
||||
private Object paramVal;
|
||||
private Integer required;
|
||||
|
||||
public String getParamName() {
|
||||
return paramName;
|
||||
}
|
||||
|
||||
public void setParamName(String paramName) {
|
||||
this.paramName = paramName;
|
||||
}
|
||||
|
||||
public String getParamType() {
|
||||
return paramType;
|
||||
}
|
||||
|
||||
public void setParamType(String paramType) {
|
||||
this.paramType = paramType;
|
||||
}
|
||||
|
||||
public String getParamDesc() {
|
||||
return paramDesc;
|
||||
}
|
||||
|
||||
public void setParamDesc(String paramDesc) {
|
||||
this.paramDesc = paramDesc;
|
||||
}
|
||||
|
||||
public Object getParamVal() {
|
||||
return paramVal;
|
||||
}
|
||||
|
||||
public void setParamVal(Object paramVal) {
|
||||
this.paramVal = paramVal;
|
||||
}
|
||||
|
||||
public Integer getRequired() {
|
||||
return required;
|
||||
}
|
||||
|
||||
public void setRequired(Integer required) {
|
||||
this.required = required;
|
||||
}
|
||||
}
|
||||
|
||||
public Integer getVersion() {
|
||||
return version;
|
||||
}
|
||||
|
||||
public void setVersion(Integer version) {
|
||||
this.version = version;
|
||||
}
|
||||
public String getService() {
|
||||
return service;
|
||||
}
|
||||
|
||||
public void setService(String service) {
|
||||
this.service = service;
|
||||
}
|
||||
|
||||
public String getMethod() {
|
||||
return method;
|
||||
}
|
||||
|
||||
public void setMethod(String method) {
|
||||
this.method = method;
|
||||
}
|
||||
|
||||
public String getExplain() {
|
||||
return explain;
|
||||
}
|
||||
|
||||
public void setExplain(String explain) {
|
||||
this.explain = explain;
|
||||
}
|
||||
|
||||
public List<DubboDocParam> getParams() {
|
||||
return params;
|
||||
}
|
||||
|
||||
public void setParams(List<DubboDocParam> params) {
|
||||
this.params = params;
|
||||
}
|
||||
}
|
||||
@@ -11,6 +11,7 @@ import java.util.List;
|
||||
public class DubboInfo {
|
||||
@JSONField(name = "interface")
|
||||
private String interfaceX;
|
||||
private DubboDocInfo docInfo;
|
||||
private List<DubboNodeInfo> nodeList;
|
||||
|
||||
public static class DubboNodeInfo {
|
||||
@@ -78,4 +79,11 @@ public class DubboInfo {
|
||||
this.interfaceX = interfaceX;
|
||||
}
|
||||
|
||||
public DubboDocInfo getDocInfo() {
|
||||
return docInfo;
|
||||
}
|
||||
|
||||
public void setDocInfo(DubboDocInfo docInfo) {
|
||||
this.docInfo = docInfo;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -239,8 +239,6 @@ public class NacosDubboInfo {
|
||||
private String category;
|
||||
private String generic;
|
||||
private String anyhost;
|
||||
@JSONField(name = "bean.name")
|
||||
private String _$BeanName0; // FIXME check this code
|
||||
private String timestamp;
|
||||
|
||||
public String getSide() {
|
||||
@@ -323,14 +321,6 @@ public class NacosDubboInfo {
|
||||
this.anyhost = anyhost;
|
||||
}
|
||||
|
||||
public String get_$BeanName0() {
|
||||
return _$BeanName0;
|
||||
}
|
||||
|
||||
public void set_$BeanName0(String _$BeanName0) {
|
||||
this._$BeanName0 = _$BeanName0;
|
||||
}
|
||||
|
||||
public String getTimestamp() {
|
||||
return timestamp;
|
||||
}
|
||||
|
||||
@@ -33,6 +33,8 @@ public class ReferenceConfigHolder {
|
||||
referenceConfig.setApplication(application);
|
||||
referenceConfigMap.put(url, referenceConfig);
|
||||
}
|
||||
// 本项目没有dubbo里面申明的类,快放弃时看源码发现可以设置generic返回一个GenericService对象,通过$invoke去操作具体方法,感觉又打开了一扇大门
|
||||
// 本项目选择的不入侵的方式管理文档,所以文档里面就必须手动加参数,写文档那些了
|
||||
return (GenericService) referenceConfig.get();
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
package com.zyplayer.doc.dubbo.framework.constant;
|
||||
|
||||
/**
|
||||
* 存储数据的KEY常量类
|
||||
*
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月21日
|
||||
*/
|
||||
public class StorageKeys {
|
||||
// 所有文档地址
|
||||
public static final String DUBBO_SERVICE_DOC = "dubbo-service-doc";
|
||||
public static final String DUBBO_SERVICE_LIST = "dubbo-service-list";
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.zyplayer.doc.dubbo.framework.service;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 数据类型
|
||||
* @author 暮光:城中城
|
||||
* @since 2018-11-27
|
||||
*/
|
||||
public class MgDubboStorage implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
private String key;
|
||||
|
||||
private String value;
|
||||
|
||||
public MgDubboStorage() {
|
||||
|
||||
}
|
||||
|
||||
public MgDubboStorage(String key, String value) {
|
||||
this.key = key;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getKey() {
|
||||
return key;
|
||||
}
|
||||
|
||||
public void setKey(String key) {
|
||||
this.key = key;
|
||||
}
|
||||
|
||||
public String getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
public void setValue(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public static long getSerialversionuid() {
|
||||
return serialVersionUID;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
package com.zyplayer.doc.dubbo.framework.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 实现此类才能使用服务器端的存贮功能
|
||||
* dubbo的文档需要手动写的,比较重要,所以重起一个存储service,实现类尽量操作另外的库
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
*/
|
||||
public interface MgDubboStorageService {
|
||||
|
||||
/**
|
||||
* 获取存储的值
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
* @return 值
|
||||
*/
|
||||
String get(String key);
|
||||
|
||||
/**
|
||||
* 模糊获取存储的值
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
* @param value 值
|
||||
* @return 值
|
||||
*/
|
||||
List<MgDubboStorage> like(String key, String value);
|
||||
|
||||
/**
|
||||
* 存储数据
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
* @param value 值
|
||||
*/
|
||||
void put(String key, String value);
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
* @author 暮光:城中城
|
||||
* @since 2018年8月19日
|
||||
* @param key 参数
|
||||
*/
|
||||
void remove(String key);
|
||||
}
|
||||
Reference in New Issue
Block a user