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.CuratorFrameworkFactory;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@@ -48,6 +55,10 @@ import java.util.stream.Collectors;
public class DubboController {
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:}")
private String serviceZookeeperUrl;
@Value("${zyplayer.doc.dubbo.zookeeper.metadata-url:}")
@@ -70,6 +81,12 @@ public class DubboController {
serverClient.start();
}
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);
metadataClient = CuratorFrameworkFactory.newClient(metadataZookeeperUrl, retryPolicy);
metadataClient.start();
@@ -188,6 +205,23 @@ public class DubboController {
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")
public DocResponseJson findDocInfo(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 (ClassNotFoundException e) {
return DocResponseJson.warn("未找到指定类,请引入相关包,类名:" + param.getService());
DubboDocInfo definition = this.getDefinitionByJar(param);
if (definition == null) {
definition = this.getDefinitionByMetadata(param);
}
if (definition == null) {
return DocResponseJson.warn("未找到指定类请引入相关包或开启metadata类名" + param.getService());
}
Map<String, DubboDocInfo> docInfoMap = new HashMap<>();
String dubboServiceDoc = mgDubboStorageService.get(StorageKeys.DUBBO_SERVICE_DOC);
@@ -228,10 +247,10 @@ public class DubboController {
DubboDocInfo dubboDocInfo = docInfoMap.get(function);
if (dubboDocInfo == null) {
dubboDocInfo = new DubboDocInfo();
dubboDocInfo.setParams(paramList);
dubboDocInfo.setParams(definition.getParams());
dubboDocInfo.setFunction(function);
dubboDocInfo.setVersion(1);
dubboDocInfo.setResultType(resultType);
dubboDocInfo.setResultType(definition.getResultType());
dubboDocInfo.setService(param.getService());
dubboDocInfo.setMethod(param.getMethod());
docInfoMap.put(function, dubboDocInfo);
@@ -328,8 +347,11 @@ public class DubboController {
}
List<DubboInfo> providerList = new LinkedList<>();
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 -> {
String tempStr = val;
try {
@@ -364,5 +386,80 @@ public class DubboController {
}
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日
*/
public class DubboRequestParam {
private String application;
private String service;
private String method;
private String ip;
@@ -61,4 +62,12 @@ public class DubboRequestParam {
public void setParams(String 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 () {
// 无论发布在哪、如何修改源码,请勿删除本行原作者信息,感谢
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();
},
methods: {
@@ -238,14 +238,16 @@
},
handleNodeClick(data) {
if (data.children == null) {
console.log(data);
var path = data.interface;
var application = data.application;
var docInfo = app.dubboDocMap[path];
if (!!docInfo) {
this.createDocInfo(path, data.method);
} else {
var service = path.substring(0, path.lastIndexOf("."));
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) {
if (validateResult(json)) {
if (!!json.data) {

View File

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

View File

@@ -1,10 +1,8 @@
package com.zyplayer.doc.manage.framework.config;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.zyplayer.doc.manage.framework.interceptor.RequestInfoInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
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.WebMvcConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
@Component
@Configuration

View File

@@ -9,6 +9,11 @@ server:
servlet:
context-path: /zyplayer-doc-manage
multipart:
enabled: true
max-file-size: 100MB
max-request-size: 100MB
zyplayer:
doc:
# 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
# username: root
# password: root
wiki:
upload-path: e:/tmp/wikiFiles
mybatis-plus:
mapper-locations: classpath:/mapper/**/*Mapper.xml

View File

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