dubbo优化,wiki上传文件

This commit is contained in:
暮光:城中城
2019-03-04 23:20:40 +08:00
parent 6f30ef0f49
commit 7357058694
9 changed files with 236 additions and 44 deletions

View File

@@ -17,10 +17,17 @@ import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework; import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory; import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry; import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.utils.UrlUtils;
import org.apache.dubbo.metadata.definition.model.FullServiceDefinition;
import org.apache.dubbo.metadata.definition.model.MethodDefinition;
import org.apache.dubbo.metadata.identifier.MetadataIdentifier;
import org.apache.dubbo.rpc.service.GenericService; import org.apache.dubbo.rpc.service.GenericService;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
@@ -48,6 +55,10 @@ import java.util.stream.Collectors;
public class DubboController { public class DubboController {
private static Logger logger = LoggerFactory.getLogger(DubboController.class); private static Logger logger = LoggerFactory.getLogger(DubboController.class);
private final static String DEFAULT_ROOT = "dubbo";
private final static String METADATA_NODE_NAME = "service.data";
private String root;
@Value("${zyplayer.doc.dubbo.zookeeper.url:}") @Value("${zyplayer.doc.dubbo.zookeeper.url:}")
private String serviceZookeeperUrl; private String serviceZookeeperUrl;
@Value("${zyplayer.doc.dubbo.zookeeper.metadata-url:}") @Value("${zyplayer.doc.dubbo.zookeeper.metadata-url:}")
@@ -70,6 +81,12 @@ public class DubboController {
serverClient.start(); serverClient.start();
} }
if (StringUtils.isNotBlank(metadataZookeeperUrl)) { if (StringUtils.isNotBlank(metadataZookeeperUrl)) {
URL url = UrlUtils.parseURL(metadataZookeeperUrl, Collections.emptyMap());
String group = url.getParameter(Constants.GROUP_KEY, DEFAULT_ROOT);
if (!group.startsWith(Constants.PATH_SEPARATOR)) {
group = Constants.PATH_SEPARATOR + group;
}
this.root = group;
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3); RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
metadataClient = CuratorFrameworkFactory.newClient(metadataZookeeperUrl, retryPolicy); metadataClient = CuratorFrameworkFactory.newClient(metadataZookeeperUrl, retryPolicy);
metadataClient.start(); metadataClient.start();
@@ -188,6 +205,23 @@ public class DubboController {
return DocResponseJson.ok(dubboInfoVo); return DocResponseJson.ok(dubboInfoVo);
} }
/**
* 获取文档详情,依据类名生成
*
* @author 暮光:城中城
* @since 2019年2月10日
**/
@GetMapping(value = "/test")
public DocResponseJson test() throws Exception {
String path = getNodePath("com.zyplayer.dubbo.service.UserService", null, null, "dubbo-provider");
if (metadataClient.checkExists().forPath(path) == null) {
return DocResponseJson.ok(path);
}
String metadata = new String(metadataClient.getData().forPath(path));
FullServiceDefinition fullServiceDefinition = JSON.parseObject(metadata, FullServiceDefinition.class);
return DocResponseJson.ok(fullServiceDefinition);
}
/** /**
* 获取文档详情,依据类名生成 * 获取文档详情,依据类名生成
* *
@@ -196,27 +230,12 @@ public class DubboController {
**/ **/
@PostMapping(value = "/findDocInfo") @PostMapping(value = "/findDocInfo")
public DocResponseJson findDocInfo(DubboRequestParam param) { public DocResponseJson findDocInfo(DubboRequestParam param) {
String resultType = null; DubboDocInfo definition = this.getDefinitionByJar(param);
List<DubboDocInfo.DubboDocParam> paramList = new LinkedList<>(); if (definition == null) {
try { definition = this.getDefinitionByMetadata(param);
Class clazz = Class.forName(param.getService()); }
Method[] methods = clazz.getMethods(); if (definition == null) {
for (Method method : methods) { return DocResponseJson.warn("未找到指定类请引入相关包或开启metadata类名" + param.getService());
String methodName = method.getName();
if (methodName.equals(param.getMethod())) {
resultType = method.getGenericReturnType().getTypeName();
Type[] parameterTypes = method.getGenericParameterTypes();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameterTypes.length; i++) {
DubboDocInfo.DubboDocParam docParam = new DubboDocInfo.DubboDocParam();
docParam.setParamName(parameters[i].getName());
docParam.setParamType(parameterTypes[i].getTypeName());
paramList.add(docParam);
}
}
}
} catch (ClassNotFoundException e) {
return DocResponseJson.warn("未找到指定类,请引入相关包,类名:" + param.getService());
} }
Map<String, DubboDocInfo> docInfoMap = new HashMap<>(); Map<String, DubboDocInfo> docInfoMap = new HashMap<>();
String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC); String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC);
@@ -228,10 +247,10 @@ public class DubboController {
DubboDocInfo dubboDocInfo = docInfoMap.get(function); DubboDocInfo dubboDocInfo = docInfoMap.get(function);
if (dubboDocInfo == null) { if (dubboDocInfo == null) {
dubboDocInfo = new DubboDocInfo(); dubboDocInfo = new DubboDocInfo();
dubboDocInfo.setParams(paramList); dubboDocInfo.setParams(definition.getParams());
dubboDocInfo.setFunction(function); dubboDocInfo.setFunction(function);
dubboDocInfo.setVersion(1); dubboDocInfo.setVersion(1);
dubboDocInfo.setResultType(resultType); dubboDocInfo.setResultType(definition.getResultType());
dubboDocInfo.setService(param.getService()); dubboDocInfo.setService(param.getService());
dubboDocInfo.setMethod(param.getMethod()); dubboDocInfo.setMethod(param.getMethod());
docInfoMap.put(function, dubboDocInfo); docInfoMap.put(function, dubboDocInfo);
@@ -328,8 +347,11 @@ public class DubboController {
} }
List<DubboInfo> providerList = new LinkedList<>(); List<DubboInfo> providerList = new LinkedList<>();
for (String dubboStr : dubboList) { for (String dubboStr : dubboList) {
List<String> providers = serverClient.getChildren().forPath("/dubbo/" + dubboStr + "/providers"); String path = "/dubbo/" + dubboStr + "/providers";
if (metadataClient.checkExists().forPath(path) == null) {
continue;
}
List<String> providers = serverClient.getChildren().forPath(path);
List<DubboInfo.DubboNodeInfo> nodeList = providers.stream().map(val -> { List<DubboInfo.DubboNodeInfo> nodeList = providers.stream().map(val -> {
String tempStr = val; String tempStr = val;
try { try {
@@ -364,5 +386,80 @@ public class DubboController {
} }
return providerList; return providerList;
} }
private DubboDocInfo getDefinitionByMetadata(DubboRequestParam param) {
try {
String path = getNodePath(param.getService(), null, null, param.getApplication());
if (metadataClient.checkExists().forPath(path) == null) {
return null;
}
String resultType = null;
String metadata = new String(metadataClient.getData().forPath(path));
FullServiceDefinition definition = JSON.parseObject(metadata, FullServiceDefinition.class);
List<DubboDocInfo.DubboDocParam> paramList = new LinkedList<>();
for (MethodDefinition method : definition.getMethods()) {
if (Objects.equals(method.getName(), param.getMethod())) {
String[] parameterTypes = method.getParameterTypes();
resultType = method.getReturnType();
for (int i = 0; i < parameterTypes.length; i++) {
DubboDocInfo.DubboDocParam docParam = new DubboDocInfo.DubboDocParam();
docParam.setParamType(parameterTypes[i]);
docParam.setParamName("arg" + i);
paramList.add(docParam);
}
}
}
DubboDocInfo dubboDocInfo = new DubboDocInfo();
dubboDocInfo.setParams(paramList);
dubboDocInfo.setResultType(resultType);
return dubboDocInfo;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private DubboDocInfo getDefinitionByJar(DubboRequestParam param) {
String resultType = null;
List<DubboDocInfo.DubboDocParam> paramList = new LinkedList<>();
try {
Class clazz = Class.forName(param.getService());
Method[] methods = clazz.getMethods();
for (Method method : methods) {
String methodName = method.getName();
if (methodName.equals(param.getMethod())) {
resultType = method.getGenericReturnType().getTypeName();
Type[] parameterTypes = method.getGenericParameterTypes();
Parameter[] parameters = method.getParameters();
for (int i = 0; i < parameterTypes.length; i++) {
DubboDocInfo.DubboDocParam docParam = new DubboDocInfo.DubboDocParam();
docParam.setParamName(parameters[i].getName());
docParam.setParamType(parameterTypes[i].getTypeName());
paramList.add(docParam);
}
}
}
} catch (Exception e) {
return null;
}
DubboDocInfo dubboDocInfo = new DubboDocInfo();
dubboDocInfo.setParams(paramList);
dubboDocInfo.setResultType(resultType);
return dubboDocInfo;
}
String toRootDir() {
if (root.equals(Constants.PATH_SEPARATOR)) {
return root;
}
return root + Constants.PATH_SEPARATOR;
}
String getNodePath(String serviceInterface, String version, String group, String application) {
MetadataIdentifier metadataIdentifier = new MetadataIdentifier(serviceInterface, version, group, Constants.PROVIDER_SIDE, application);
return toRootDir() + metadataIdentifier.getUniqueKey(MetadataIdentifier.KeyTypeEnum.PATH) + Constants.PATH_SEPARATOR + METADATA_NODE_NAME;
}
} }

View File

@@ -7,6 +7,7 @@ package com.zyplayer.doc.dubbo.controller.param;
* @since 2019年2月10日 * @since 2019年2月10日
*/ */
public class DubboRequestParam { public class DubboRequestParam {
private String application;
private String service; private String service;
private String method; private String method;
private String ip; private String ip;
@@ -61,4 +62,12 @@ public class DubboRequestParam {
public void setParams(String params) { public void setParams(String params) {
this.params = params; this.params = params;
} }
public String getApplication() {
return application;
}
public void setApplication(String application) {
this.application = application;
}
} }

View File

@@ -226,7 +226,7 @@
}, },
mounted: function () { mounted: function () {
// 无论发布在哪、如何修改源码,请勿删除本行原作者信息,感谢 // 无论发布在哪、如何修改源码,请勿删除本行原作者信息,感谢
console.log("%c项目信息\n开发者列表暮光城中城\n项目地址https://gitee.com/zyplayer/zyplayer-doc","color:red"); console.log("%c项目信息\n开发者列表暮光城中城\n项目地址https://gitee.com/zyplayer/zyplayer-doc", "color:red");
this.doGetServiceList(); this.doGetServiceList();
}, },
methods: { methods: {
@@ -238,14 +238,16 @@
}, },
handleNodeClick(data) { handleNodeClick(data) {
if (data.children == null) { if (data.children == null) {
console.log(data);
var path = data.interface; var path = data.interface;
var application = data.application;
var docInfo = app.dubboDocMap[path]; var docInfo = app.dubboDocMap[path];
if (!!docInfo) { if (!!docInfo) {
this.createDocInfo(path, data.method); this.createDocInfo(path, data.method);
} else { } else {
var service = path.substring(0, path.lastIndexOf(".")); var service = path.substring(0, path.lastIndexOf("."));
var method = path.substring(path.lastIndexOf(".") + 1, path.length); var method = path.substring(path.lastIndexOf(".") + 1, path.length);
var param = {service: service, method: method}; var param = {service: service, method: method, application: application};
ajaxTemp("zyplayer-doc-dubbo/doc-dubbo/findDocInfo", "post", "json", param, function (json) { ajaxTemp("zyplayer-doc-dubbo/doc-dubbo/findDocInfo", "post", "json", param, function (json) {
if (validateResult(json)) { if (validateResult(json)) {
if (!!json.data) { if (!!json.data) {

View File

@@ -31,6 +31,7 @@ function createTreeViewByTree(json, keywords) {
continue; continue;
} }
var methods = json[i].nodeList[0].methods; var methods = json[i].nodeList[0].methods;
var application = json[i].nodeList[0].application;
for (var j = 0; j < methods.length; j++) { for (var j = 0; j < methods.length; j++) {
var interfaceTemp = interface + "." + methods[j]; var interfaceTemp = interface + "." + methods[j];
var keyArr = interfaceTemp.split("."); var keyArr = interfaceTemp.split(".");
@@ -68,6 +69,7 @@ function createTreeViewByTree(json, keywords) {
tempPathObj.children = null; tempPathObj.children = null;
tempPathObj.method = methods[j]; tempPathObj.method = methods[j];
tempPathObj.interface = tempPath; tempPathObj.interface = tempPath;
tempPathObj.application = application;
app.treePathDataMap.set(tempPath, json[i]); app.treePathDataMap.set(tempPath, json[i]);
} }
} }

View File

@@ -1,10 +1,8 @@
package com.zyplayer.doc.manage.framework.config; package com.zyplayer.doc.manage.framework.config;
import java.nio.charset.Charset; import com.alibaba.fastjson.serializer.SerializerFeature;
import java.text.SimpleDateFormat; import com.alibaba.fastjson.support.config.FastJsonConfig;
import java.util.ArrayList; import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import java.util.List;
import com.zyplayer.doc.manage.framework.interceptor.RequestInfoInterceptor; import com.zyplayer.doc.manage.framework.interceptor.RequestInfoInterceptor;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@@ -17,9 +15,10 @@ import org.springframework.stereotype.Component;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature; import java.nio.charset.Charset;
import com.alibaba.fastjson.support.config.FastJsonConfig; import java.text.SimpleDateFormat;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter; import java.util.ArrayList;
import java.util.List;
@Component @Component
@Configuration @Configuration

View File

@@ -9,6 +9,11 @@ server:
servlet: servlet:
context-path: /zyplayer-doc-manage context-path: /zyplayer-doc-manage
multipart:
enabled: true
max-file-size: 100MB
max-request-size: 100MB
zyplayer: zyplayer:
doc: doc:
# dubbo相关配置 # dubbo相关配置
@@ -51,6 +56,8 @@ zyplayer:
# url: jdbc:mysql://127.0.0.1:3306/zyplayer_doc_manage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useSSL=false # url: jdbc:mysql://127.0.0.1:3306/zyplayer_doc_manage?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&useSSL=false
# username: root # username: root
# password: root # password: root
wiki:
upload-path: e:/tmp/wikiFiles
mybatis-plus: mybatis-plus:
mapper-locations: classpath:/mapper/**/*Mapper.xml mapper-locations: classpath:/mapper/**/*Mapper.xml

View File

@@ -57,6 +57,11 @@
<artifactId>zyplayer-doc-core</artifactId> <artifactId>zyplayer-doc-core</artifactId>
<version>${zyplayer.doc.version}</version> <version>${zyplayer.doc.version}</version>
</dependency> </dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-http</artifactId>
<version>4.1.8</version>
</dependency>
</dependencies> </dependencies>
<licenses> <licenses>
<license> <license>

View File

@@ -0,0 +1,70 @@
package com.zyplayer.doc.wiki.controller;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.util.RandomUtil;
import com.zyplayer.doc.core.json.DocResponseJson;
import com.zyplayer.doc.core.json.ResponseJson;
import com.zyplayer.doc.data.config.security.DocUserDetails;
import com.zyplayer.doc.data.config.security.DocUserUtil;
import com.zyplayer.doc.data.repository.manage.entity.WikiPageFile;
import com.zyplayer.doc.data.service.manage.WikiPageFileService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.annotation.Resource;
import java.io.File;
import java.util.Date;
/**
* 文档控制器
*
* @author 暮光:城中城
* @since 2019年2月17日
*/
@RestController
@RequestMapping("/zyplayer-doc-wiki/common")
public class WikiCommonController {
private static Logger logger = LoggerFactory.getLogger(WikiCommonController.class);
@Value("${zyplayer.doc.wiki.upload-path:}")
private String uploadPath;
@Resource
WikiPageFileService wikiPageFileService;
@PostMapping("/upload")
public ResponseJson<Object> upload(WikiPageFile wikiPageFile, @RequestParam("files") MultipartFile file) {
//通过CommonsMultipartFile的方法直接写文件注意这个时候
try {
String fileName = file.getOriginalFilename();
String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
DocUserDetails currentUser = DocUserUtil.getCurrentUser();
String path = uploadPath + "/" + DateTime.now().toString("yyyy/MM/dd") + "/";
File newFile = new File(path);
if (!newFile.exists()) {
newFile.mkdir();
}
path += RandomUtil.simpleUUID() + fileSuffix;
newFile = new File(path);
file.transferTo(newFile);
wikiPageFile.setFileUrl(path);
wikiPageFile.setFileName(fileName);
wikiPageFile.setCreateTime(new Date());
wikiPageFile.setCreateUserId(currentUser.getUserId());
wikiPageFile.setCreateUserName(currentUser.getUsername());
wikiPageFile.setDelFlag(0);
} catch (Exception e) {
e.printStackTrace();
return DocResponseJson.warn("失败");
}
wikiPageFileService.save(wikiPageFile);
return DocResponseJson.ok(wikiPageFile);
}
}

View File

@@ -41,17 +41,17 @@
<div class="wiki-files"> <div class="wiki-files">
<div style="margin-bottom: 5px; height: 40px;"> <div style="margin-bottom: 5px; height: 40px;">
<div style="float: right;"> <div style="float: right;">
<!--:on-preview="handlePreview"-->
<!--:on-remove="handleRemove"-->
<!--:before-remove="beforeRemove"-->
<!--:on-exceed="handleExceed"-->
<el-upload <el-upload
class="upload-demo" class="upload-demo"
action="https://jsonplaceholder.typicode.com/posts/" action="zyplayer-doc-wiki/common/upload"
:on-preview="handlePreview" name="files"
:on-remove="handleRemove"
:before-remove="beforeRemove"
multiple
show-file-list show-file-list
:limit="3" multiple
:on-exceed="handleExceed" :limit="3">
:file-list="fileList">
<el-button icon="el-icon-upload">上传附件</el-button> <el-button icon="el-icon-upload">上传附件</el-button>
</el-upload> </el-upload>
</div> </div>
@@ -140,6 +140,7 @@
wikiPage: {}, wikiPage: {},
pageContent: {}, pageContent: {},
pageFileList: [], pageFileList: [],
uploadFileList: [],
} }
}, },
watch: { watch: {
@@ -247,7 +248,7 @@
init(){ init(){
var E = window.wangEditor; var E = window.wangEditor;
page.newPageContentEditor = new E('#newPageContentDiv'); page.newPageContentEditor = new E('#newPageContentDiv');
page.newPageContentEditor.customConfig.uploadImgServer = '/upload-img'; page.newPageContentEditor.customConfig.uploadImgServer = 'zyplayer-doc-wiki/common/upload';
page.newPageContentEditor.create(); page.newPageContentEditor.create();
} }
} }