From ff91320ec89d7582111ad17eadd7e15cb50c360b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9A=AE=E5=85=89=EF=BC=9A=E5=9F=8E=E4=B8=AD=E5=9F=8E?= <806783409@qq.com> Date: Fri, 22 Mar 2019 19:01:27 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0grpc=E6=96=87=E6=A1=A3?= =?UTF-8?q?=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 1 + zyplayer-doc-grpc/pom.xml | 161 +++++++++++++++ .../grpc/controller/GrpcDocController.java | 187 ++++++++++++++++++ .../doc/grpc/controller/po/ColumnInfo.java | 42 ++++ .../doc/grpc/controller/po/GrpcDocInfo.java | 73 +++++++ .../doc/grpc/controller/po/GrpcInfo.java | 81 ++++++++ .../doc/grpc/controller/po/MethodParam.java | 31 +++ .../grpc/framework/config/EnableDocGrpc.java | 16 ++ .../config/GrpcClientConfiguration.java | 40 ++++ .../grpc/framework/config/GrpcProperties.java | 28 +++ .../config/GrpcServerConfiguration.java | 29 +++ .../framework/config/SpringContextUtil.java | 51 +++++ .../doc/grpc/framework/consts/Const.java | 32 +++ .../doc/grpc/service/HelloWorldService.java | 29 +++ .../doc/grpc/service/ZyplayerChatService.java | 25 +++ .../src/main/proto/HelloWorld.proto | 36 ++++ .../src/main/proto/ZyplayerChat.proto | 34 ++++ zyplayer-doc-manage/pom.xml | 5 + .../framework/config/DocGrpcConfig.java | 14 ++ 19 files changed, 915 insertions(+) create mode 100644 zyplayer-doc-grpc/pom.xml create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/GrpcDocController.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/ColumnInfo.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcDocInfo.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcInfo.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/MethodParam.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/EnableDocGrpc.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcClientConfiguration.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcProperties.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcServerConfiguration.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/SpringContextUtil.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/consts/Const.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/HelloWorldService.java create mode 100644 zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/ZyplayerChatService.java create mode 100644 zyplayer-doc-grpc/src/main/proto/HelloWorld.proto create mode 100644 zyplayer-doc-grpc/src/main/proto/ZyplayerChat.proto create mode 100644 zyplayer-doc-manage/src/main/java/com/zyplayer/doc/manage/framework/config/DocGrpcConfig.java diff --git a/pom.xml b/pom.xml index 59fdb29a..f788c2c5 100644 --- a/pom.xml +++ b/pom.xml @@ -25,5 +25,6 @@ zyplayer-doc-dubbo zyplayer-doc-wiki zyplayer-doc-data + zyplayer-doc-grpc diff --git a/zyplayer-doc-grpc/pom.xml b/zyplayer-doc-grpc/pom.xml new file mode 100644 index 00000000..8f1eb380 --- /dev/null +++ b/zyplayer-doc-grpc/pom.xml @@ -0,0 +1,161 @@ + + + 4.0.0 + + com.zyplayer + zyplayer-doc-grpc + 1.0.1 + + + org.springframework.boot + spring-boot-starter-parent + 1.4.2.RELEASE + + + + UTF-8 + UTF-8 + 1.8 + + 1.16.1 + 3.6.1 + 0.0.3 + + + + + org.springframework.boot + spring-boot-starter-web + + + com.alibaba + fastjson + 1.2.56 + + + io.grpc + grpc-netty + ${grpc.version} + + + io.grpc + grpc-protobuf + ${grpc.version} + + + com.nxest.grpc + grpc-java-spring + ${grpc-java-spring.version} + + + com.zyplayer + zyplayer-doc-core + 1.0.1 + + + + + + The Apache Software License, Version 2.0 + http://www.apache.org/licenses/LICENSE-2.0.txt + + + + scm:git@git.oschina.net:zyplayer/zyplayer-doc.git + scm:git@git.oschina.net:zyplayer/zyplayer-doc.git + git@git.oschina.net:zyplayer/zyplayer-doc.git + + + + + snapshots + https://oss.sonatype.org/content/repositories/snapshots/ + + + snapshots + https://oss.sonatype.org/service/local/staging/deploy/maven2/ + + + + + + + kr.motd.maven + os-maven-plugin + 1.5.0.Final + + + + + src/main/resources + META-INF/resources/ + + + src/main/java + + **/* + + + **/*.java + + false + + + src/main/webapp + + **/* + + false + + + + + org.codehaus.mojo + build-helper-maven-plugin + 3.0.0 + + + add-resource + generate-resources + + add-resource + + + + + src/main/java + src/main/proto + src/main/resources + + + + + + + + org.xolstice.maven.plugins + protobuf-maven-plugin + 0.5.1 + + com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier} + + grpc-java + io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier} + + src/main/proto + + + + + compile + compile-custom + + + + + + + + diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/GrpcDocController.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/GrpcDocController.java new file mode 100644 index 00000000..70ec50de --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/GrpcDocController.java @@ -0,0 +1,187 @@ +package com.zyplayer.doc.grpc.controller; + +import com.alibaba.fastjson.JSON; +import com.alibaba.fastjson.TypeReference; +import com.nxest.grpc.client.GrpcChannelFactory; +import com.nxest.grpc.server.GrpcService; +import com.zyplayer.doc.core.json.DocResponseJson; +import com.zyplayer.doc.grpc.controller.po.ColumnInfo; +import com.zyplayer.doc.grpc.controller.po.GrpcDocInfo; +import com.zyplayer.doc.grpc.controller.po.MethodParam; +import com.zyplayer.doc.grpc.framework.config.SpringContextUtil; +import com.zyplayer.doc.grpc.framework.consts.Const; +import io.grpc.Channel; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import javax.annotation.Resource; +import java.lang.reflect.Method; +import java.lang.reflect.Type; +import java.util.*; +import java.util.regex.Matcher; +import java.util.regex.Pattern; + +@RestController +@RequestMapping("/zyplayer-doc-grpc") +public class GrpcDocController { + + @Resource + GrpcChannelFactory grpcChannelFactory; + + @RequestMapping("/service") + public DocResponseJson service() throws Exception { + List grpcServiceList = SpringContextUtil.getBeanWithAnnotation(GrpcService.class); + if (grpcServiceList == null || grpcServiceList.size() <= 0) { + return DocResponseJson.ok(); + } + List grpcDocInfoList = new LinkedList<>(); + for (Object service : grpcServiceList) { + List infoList = this.getDefinitionByJar(service.getClass()); + if (infoList != null) { + grpcDocInfoList.addAll(infoList); + } + } + // 找所有的参数列表 + for (GrpcDocInfo grpcDocInfo : grpcDocInfoList) { + ColumnInfo columnInfo = new ColumnInfo(); + columnInfo.setType(grpcDocInfo.getParamType()); + List columnInfos = this.findClassColumns(Class.forName(grpcDocInfo.getParamType())); + columnInfo.setParam(columnInfos); + System.out.println(JSON.toJSONString(columnInfo)); + } + return DocResponseJson.ok(grpcDocInfoList); + } + + @RequestMapping("/execute") + public String execute(String service, String params) throws Exception { + List grpcDocInfoList = this.getDefinitionByJar(Class.forName(service)); + String executeResult = "执行失败"; + if (grpcDocInfoList != null && grpcDocInfoList.size() > 0) { + Map paramMap = JSON.parseObject(params, new TypeReference>() {}); + executeResult = this.executeFunction(grpcDocInfoList.get(0), paramMap); + } + return executeResult; + } + + private String executeFunction(GrpcDocInfo grpcDocInfo, Map paramMap) throws Exception { + Class aClass = Class.forName(grpcDocInfo.getParamType()); + Method getMoney = aClass.getMethod("newBuilder"); + Object newBuilder = getMoney.invoke(aClass); + + List setterFunction = this.getSetterFunction(newBuilder.getClass()); + for (MethodParam methodParam : setterFunction) { + Method setName = newBuilder.getClass().getMethod(methodParam.getSetterName(), Class.forName(methodParam.getType())); + Object paramObj = paramMap.get(methodParam.getName()); + newBuilder = setName.invoke(newBuilder, paramObj); + } + + Method build = newBuilder.getClass().getMethod("build"); + Object request = build.invoke(newBuilder); + + Class serviceClass = Class.forName(grpcDocInfo.getService()); + String serviceSuperName = serviceClass.getSuperclass().getName(); + String superClassName = serviceSuperName.substring(0, serviceSuperName.indexOf("$")); + + Class superClass = Class.forName(superClassName); + Method newBlockingStubMethod = superClass.getMethod("newBlockingStub", Channel.class); + Object blockingStub = newBlockingStubMethod.invoke(null, grpcChannelFactory.createChannel()); + + Method sayHello = blockingStub.getClass().getMethod(grpcDocInfo.getMethod(), Class.forName(grpcDocInfo.getParamType())); + Object response = sayHello.invoke(blockingStub, request); + + Method messageMethod = response.getClass().getMethod("getMessage"); + Object resultStr = messageMethod.invoke(response); + return resultStr.toString(); + } + + private String toLowerCaseFirstOne(String str) { + if (Character.isLowerCase(str.charAt(0))) { + return str; + } else { + return Character.toLowerCase(str.charAt(0)) + str.substring(1); + } + } + + private List getSetterFunction(Class clazz) { + List result = new LinkedList<>(); + Method[] methods = clazz.getDeclaredMethods(); + StringBuilder nameSb = new StringBuilder(); + for (Method method : methods) { + String methodName = method.getName(); + if (!methodName.startsWith("set") || methodName.endsWith("Bytes") + || methodName.equals("setField") || methodName.equals("setRepeatedField") + || methodName.equals("setUnknownFields")) { + continue; + } + Type[] parameterTypes = method.getGenericParameterTypes(); + if (parameterTypes.length == 1) { + String typeName = parameterTypes[0].getTypeName(); + if (typeName.endsWith("$Builder")) { + continue; + } + MethodParam param = new MethodParam(); + param.setSetterName(methodName); + param.setType(typeName); + String paramName = methodName.substring(3); + param.setName(this.toLowerCaseFirstOne(paramName)); + result.add(param); + } + nameSb.append(methodName).append(","); + } + System.out.println(nameSb); + return result; + } + + private List findClassColumns(Class clazz) throws Exception { + Method getMoney = clazz.getMethod("newBuilder"); + Object newBuilder = getMoney.invoke(clazz); + + List paramList = this.getSetterFunction(newBuilder.getClass()); + List subInfoList = new LinkedList<>(); + for (MethodParam param : paramList) { + ColumnInfo info = new ColumnInfo(); + info.setType(param.getType()); + if (!Const.BASE_TYPE.contains(param.getType())) { + List classColumn = this.findClassColumns(Class.forName(param.getType())); + info.setParam(classColumn); + } + subInfoList.add(info); + } + return subInfoList; + } + + private List getDefinitionByJar(Class clazz) { + List providerList = new LinkedList<>(); + try { + Method[] methods = clazz.getDeclaredMethods(); + for (Method method : methods) { + String methodName = method.getName(); + String resultType = ""; + String paramType = ""; + Type[] parameterTypes = method.getGenericParameterTypes(); + for (int i = 0; i < parameterTypes.length; i++) { + String typeName = parameterTypes[i].getTypeName(); + if (i == 0) { + paramType = parameterTypes[i].getTypeName(); + } else if (typeName.matches("io.grpc.stub.StreamObserver<.+>")) { + Pattern pattern = Pattern.compile("io.grpc.stub.StreamObserver<(.+)>"); + Matcher matcher = pattern.matcher(typeName); + if (matcher.find()) { + resultType = matcher.group(1); + } + } + } + GrpcDocInfo grpcDocInfo = new GrpcDocInfo(); + grpcDocInfo.setMethod(methodName); + grpcDocInfo.setService(clazz.getName()); + grpcDocInfo.setParamType(paramType); + grpcDocInfo.setResultType(resultType); + providerList.add(grpcDocInfo); + } + } catch (Exception e) { + return null; + } + return providerList; + } + +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/ColumnInfo.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/ColumnInfo.java new file mode 100644 index 00000000..1599745e --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/ColumnInfo.java @@ -0,0 +1,42 @@ +package com.zyplayer.doc.grpc.controller.po; + +import java.util.List; + +public class ColumnInfo { + private String name; + private String type; + private String desc; + private List param; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getDesc() { + return desc; + } + + public void setDesc(String desc) { + this.desc = desc; + } + + public List getParam() { + return param; + } + + public void setParam(List param) { + this.param = param; + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcDocInfo.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcDocInfo.java new file mode 100644 index 00000000..d6759a74 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcDocInfo.java @@ -0,0 +1,73 @@ +package com.zyplayer.doc.grpc.controller.po; + +/** + * 请求参数对象 + * + * @author 暮光:城中城 + * @since 2019年2月10日 + */ +public class GrpcDocInfo { + private String method; + private String service; + private String explain; + private String result; + private String paramType; + private String resultType; + private Integer version; + + 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 getExplain() { + return explain; + } + + public void setExplain(String explain) { + this.explain = explain; + } + + public String getResult() { + return result; + } + + public void setResult(String result) { + this.result = result; + } + + public String getResultType() { + return resultType; + } + + public void setResultType(String resultType) { + this.resultType = resultType; + } + + public String getParamType() { + return paramType; + } + + public void setParamType(String paramType) { + this.paramType = paramType; + } + + public String getMethod() { + return method; + } + + public void setMethod(String method) { + this.method = method; + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcInfo.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcInfo.java new file mode 100644 index 00000000..8051ea50 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/GrpcInfo.java @@ -0,0 +1,81 @@ +package com.zyplayer.doc.grpc.controller.po; + +import com.alibaba.fastjson.annotation.JSONField; + +import java.util.List; + +/** + * @author 暮光:城中城 + * @since 2019年1月10日 + **/ +public class GrpcInfo { + @JSONField(name = "interface") + private String interfaceX; + private List nodeList; + + public static class DubboNodeInfo { + private Integer port; + private String ip; + @JSONField(name = "interface") + private String interfaceX; + private String[] methods; + private String application; + + public Integer getPort() { + return port; + } + + public void setPort(Integer port) { + this.port = port; + } + + public String getIp() { + return ip; + } + + public void setIp(String ip) { + this.ip = ip; + } + + public String getInterfaceX() { + return interfaceX; + } + + public void setInterfaceX(String interfaceX) { + this.interfaceX = interfaceX; + } + + public String[] getMethods() { + return methods; + } + + public void setMethods(String[] methods) { + this.methods = methods; + } + + public String getApplication() { + return application; + } + + public void setApplication(String application) { + this.application = application; + } + } + + public List getNodeList() { + return nodeList; + } + + public void setNodeList(List nodeList) { + this.nodeList = nodeList; + } + + public String getInterfaceX() { + return interfaceX; + } + + public void setInterfaceX(String interfaceX) { + this.interfaceX = interfaceX; + } + +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/MethodParam.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/MethodParam.java new file mode 100644 index 00000000..da49815e --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/controller/po/MethodParam.java @@ -0,0 +1,31 @@ +package com.zyplayer.doc.grpc.controller.po; + +public class MethodParam { + private String name; + private String type; + private String setterName; + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getSetterName() { + return setterName; + } + + public void setSetterName(String setterName) { + this.setterName = setterName; + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/EnableDocGrpc.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/EnableDocGrpc.java new file mode 100644 index 00000000..272a1f67 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/EnableDocGrpc.java @@ -0,0 +1,16 @@ +package com.zyplayer.doc.grpc.framework.config; + +import org.springframework.context.annotation.ComponentScan; + +import java.lang.annotation.Documented; +import java.lang.annotation.Retention; +import java.lang.annotation.Target; + +@Retention(value = java.lang.annotation.RetentionPolicy.RUNTIME) +@Target(value = { java.lang.annotation.ElementType.TYPE }) +@Documented +@ComponentScan(basePackages = { + "com.zyplayer.doc.grpc", +}) +public @interface EnableDocGrpc { +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcClientConfiguration.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcClientConfiguration.java new file mode 100644 index 00000000..9c0429a1 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcClientConfiguration.java @@ -0,0 +1,40 @@ +package com.zyplayer.doc.grpc.framework.config; + +import com.nxest.grpc.client.*; +import com.nxest.grpc.client.configure.GrpcClientProperties; +import org.springframework.boot.autoconfigure.AutoConfigureAfter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.Resource; + +@Configuration +@AutoConfigureAfter(GrpcServerConfiguration.class) +public class GrpcClientConfiguration { + + @Resource + private GrpcProperties grpcProperties; + + @Bean + public GrpcClientProperties clientProperties() { + return grpcProperties.getClient(); + } + + + @Bean + public GrpcChannelFactory channelFactory() { + return new AddressChannelFactory(clientProperties()); + } + + @Bean + public GrpcClientInterceptorDiscoverer clientInterceptorDiscoverer() { + return new AnnotationClientInterceptorDiscoverer(); + } + + @Bean + @ConditionalOnClass(GrpcClient.class) + public GrpcClientBeanPostProcessor grpcClientBeanPostProcessor() { + return new GrpcClientBeanPostProcessor(channelFactory(), clientInterceptorDiscoverer()); + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcProperties.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcProperties.java new file mode 100644 index 00000000..0f4b5440 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcProperties.java @@ -0,0 +1,28 @@ +package com.zyplayer.doc.grpc.framework.config; + +import com.nxest.grpc.client.configure.GrpcClientProperties; +import com.nxest.grpc.server.configure.GrpcServerProperties; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConfigurationProperties(prefix = "grpc") +public class GrpcProperties { + + /** + * Grpc server properties + */ + private final GrpcServerProperties server = new GrpcServerProperties(); + /** + * Grpc client properties + */ + private final GrpcClientProperties client = new GrpcClientProperties(); + + public GrpcServerProperties getServer() { + return server; + } + + public GrpcClientProperties getClient() { + return client; + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcServerConfiguration.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcServerConfiguration.java new file mode 100644 index 00000000..6c92dbef --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/GrpcServerConfiguration.java @@ -0,0 +1,29 @@ +package com.zyplayer.doc.grpc.framework.config; + +import com.nxest.grpc.server.*; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import javax.annotation.Resource; + +@Configuration +public class GrpcServerConfiguration { + + @Resource + private GrpcProperties grpcProperties; + + @Bean + public GrpcServiceDiscoverer serviceDiscoverer() { + return new AnnotationGrpcServiceDiscoverer(); + } + + @Bean + public GrpcServerFactory severFactory() { + return new NettyGrpcServerFactory(serviceDiscoverer(), grpcProperties.getServer()); + } + + @Bean(name = "grpcServer", initMethod = "start", destroyMethod = "destroy") + public GrpcServer serverRunner() { + return severFactory().createServer(); + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/SpringContextUtil.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/SpringContextUtil.java new file mode 100644 index 00000000..2b123ad3 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/config/SpringContextUtil.java @@ -0,0 +1,51 @@ +package com.zyplayer.doc.grpc.framework.config; + +import org.springframework.beans.BeansException; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.lang.annotation.Annotation; +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +/** + * context工具类 + */ +@Component +public class SpringContextUtil implements ApplicationContextAware { + + public static ApplicationContext context; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + context = applicationContext; + } + + public static ApplicationContext getApplicationContext() { + return context; + } + + public static T getBean(Class clz) { + return context.getBean(clz); + } + + public static Object getBean(String string) { + return getApplicationContext().getBean(string); + } + + /** + * 获取类 + * @param annotationType annotation + * @return 类对象 + */ + public static List getBeanWithAnnotation(Class annotationType) { + if (context == null) { + return null; + } + Map beansWithAnnotation = context.getBeansWithAnnotation(annotationType); + return new LinkedList<>(beansWithAnnotation.values()); + } +} + diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/consts/Const.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/consts/Const.java new file mode 100644 index 00000000..2db31e8e --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/framework/consts/Const.java @@ -0,0 +1,32 @@ +package com.zyplayer.doc.grpc.framework.consts; + +import com.google.protobuf.ByteString; + +import java.math.BigDecimal; +import java.util.Date; +import java.util.HashSet; +import java.util.Set; + +public class Const { + + public static final Set BASE_TYPE; + static { + BASE_TYPE = new HashSet<>(); + BASE_TYPE.add(String.class.getName()); + BASE_TYPE.add(Integer.class.getName()); + BASE_TYPE.add(Long.class.getName()); + BASE_TYPE.add(Double.class.getName()); + BASE_TYPE.add(Date.class.getName()); + BASE_TYPE.add(Byte.class.getName()); + BASE_TYPE.add(Float.class.getName()); + BASE_TYPE.add(BigDecimal.class.getName()); + BASE_TYPE.add(ByteString.class.getName()); + + BASE_TYPE.add(char.class.getName()); + BASE_TYPE.add(int.class.getName()); + BASE_TYPE.add(long.class.getName()); + BASE_TYPE.add(double.class.getName()); + BASE_TYPE.add(byte.class.getName()); + BASE_TYPE.add(float.class.getName()); + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/HelloWorldService.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/HelloWorldService.java new file mode 100644 index 00000000..b04c74fa --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/HelloWorldService.java @@ -0,0 +1,29 @@ +package com.zyplayer.doc.grpc.service; + + +import com.google.protobuf.Empty; +import com.google.protobuf.Timestamp; +import com.nxest.grpc.server.GrpcService; +import com.zyplayer.doc.grpc.proto.HelloRequest; +import com.zyplayer.doc.grpc.proto.HelloResponse; +import com.zyplayer.doc.grpc.proto.TimeResponse; +import com.zyplayer.doc.grpc.proto.ZyplayerGreeterGrpc; +import io.grpc.stub.StreamObserver; + +@GrpcService +public class HelloWorldService extends ZyplayerGreeterGrpc.ZyplayerGreeterImplBase { + + @Override + public void sayHello(HelloRequest request, StreamObserver responseObserver) { + HelloResponse reply = HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + + @Override + public void sayTime(Empty request, StreamObserver responseObserver) { + TimeResponse reply = TimeResponse.newBuilder().setTime(Timestamp.newBuilder().build()).build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } +} diff --git a/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/ZyplayerChatService.java b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/ZyplayerChatService.java new file mode 100644 index 00000000..79df23e8 --- /dev/null +++ b/zyplayer-doc-grpc/src/main/java/com/zyplayer/doc/grpc/service/ZyplayerChatService.java @@ -0,0 +1,25 @@ +package com.zyplayer.doc.grpc.service; + + +import com.nxest.grpc.server.GrpcService; +import com.zyplayer.doc.grpc.proto.ChatMsg; +import com.zyplayer.doc.grpc.proto.ZyplayerChatGrpc; +import io.grpc.stub.StreamObserver; + +@GrpcService +public class ZyplayerChatService extends ZyplayerChatGrpc.ZyplayerChatImplBase { + + @Override + public void sendText(ChatMsg request, StreamObserver responseObserver) { + ChatMsg reply = ChatMsg.newBuilder().setToken("sendMsg").build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } + + @Override + public void sendImage(ChatMsg request, StreamObserver responseObserver) { + ChatMsg reply = ChatMsg.newBuilder().setToken("sendImage").build(); + responseObserver.onNext(reply); + responseObserver.onCompleted(); + } +} diff --git a/zyplayer-doc-grpc/src/main/proto/HelloWorld.proto b/zyplayer-doc-grpc/src/main/proto/HelloWorld.proto new file mode 100644 index 00000000..461d0e2b --- /dev/null +++ b/zyplayer-doc-grpc/src/main/proto/HelloWorld.proto @@ -0,0 +1,36 @@ +syntax = "proto3"; + +package helloworld; + +import "google/protobuf/empty.proto"; +import "google/protobuf/timestamp.proto"; + +option java_multiple_files = true; +option java_package = "com.zyplayer.doc.grpc.proto"; +option java_outer_classname = "HelloWorldProto"; + +// The greeting service definition. +service ZyplayerGreeter { + // Sends a greeting + rpc sayHello (HelloRequest) returns (HelloResponse) {} + + // Sends the current time + rpc sayTime (google.protobuf.Empty) returns (TimeResponse) {} +} + +// The request message containing the user's name. +message HelloRequest { + string name = 1; + string content = 2; + int32 time = 3; +} + +// The response message containing the greetings +message HelloResponse { + string message = 1; +} + +// The response message containing the time +message TimeResponse { + google.protobuf.Timestamp time = 1; +} diff --git a/zyplayer-doc-grpc/src/main/proto/ZyplayerChat.proto b/zyplayer-doc-grpc/src/main/proto/ZyplayerChat.proto new file mode 100644 index 00000000..0aeb64ea --- /dev/null +++ b/zyplayer-doc-grpc/src/main/proto/ZyplayerChat.proto @@ -0,0 +1,34 @@ +syntax = "proto3"; +option java_outer_classname = "ChatProto"; +option java_package = "com.zyplayer.doc.grpc.proto"; +import "google/protobuf/empty.proto"; +import "google/protobuf/timestamp.proto"; +package wechat; +option java_multiple_files = true; +service ZyplayerChat { + // 处理请求 + rpc sendText (ChatMsg) returns (ChatMsg) {} + + rpc sendImage (ChatMsg) returns (ChatMsg) {} +} +// 完整的grpc结构体 +message ChatMsg { + BaseMsg baseMsg = 1; + string token = 2; + string version = 3; + int32 timeStamp = 4; + string iP = 5; +} +// 请求消息结构体 +message BaseMsg { + int32 ret = 1; + int32 cmd = 2; + User user = 3; +} +// 用户结构体 +message User { + int64 uin = 1; + bytes cookies = 2; + bytes sessionKey = 3; + bytes nickname = 4; +} diff --git a/zyplayer-doc-manage/pom.xml b/zyplayer-doc-manage/pom.xml index 4d91f6a6..472dabc0 100644 --- a/zyplayer-doc-manage/pom.xml +++ b/zyplayer-doc-manage/pom.xml @@ -109,6 +109,11 @@ zyplayer-doc-data ${zyplayer.doc.version} + + com.zyplayer + zyplayer-doc-grpc + ${zyplayer.doc.version} + com.zyplayer diff --git a/zyplayer-doc-manage/src/main/java/com/zyplayer/doc/manage/framework/config/DocGrpcConfig.java b/zyplayer-doc-manage/src/main/java/com/zyplayer/doc/manage/framework/config/DocGrpcConfig.java new file mode 100644 index 00000000..b921959b --- /dev/null +++ b/zyplayer-doc-manage/src/main/java/com/zyplayer/doc/manage/framework/config/DocGrpcConfig.java @@ -0,0 +1,14 @@ +package com.zyplayer.doc.manage.framework.config; + +import com.zyplayer.doc.grpc.framework.config.EnableDocGrpc; +import org.springframework.context.annotation.Configuration; + +/** + * @Author + * @Date 2018/11/11 + **/ +@EnableDocGrpc +@Configuration +public class DocGrpcConfig { + +}