增加grpc文档支持
This commit is contained in:
1
pom.xml
1
pom.xml
@@ -25,5 +25,6 @@
|
||||
<module>zyplayer-doc-dubbo</module>
|
||||
<module>zyplayer-doc-wiki</module>
|
||||
<module>zyplayer-doc-data</module>
|
||||
<module>zyplayer-doc-grpc</module>
|
||||
</modules>
|
||||
</project>
|
||||
|
||||
161
zyplayer-doc-grpc/pom.xml
Normal file
161
zyplayer-doc-grpc/pom.xml
Normal file
@@ -0,0 +1,161 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<groupId>com.zyplayer</groupId>
|
||||
<artifactId>zyplayer-doc-grpc</artifactId>
|
||||
<version>1.0.1</version>
|
||||
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>1.4.2.RELEASE</version>
|
||||
</parent>
|
||||
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||
<java.version>1.8</java.version>
|
||||
|
||||
<grpc.version>1.16.1</grpc.version>
|
||||
<protoc.version>3.6.1</protoc.version> <!-- Same version as grpc-proto -->
|
||||
<grpc-java-spring.version>0.0.3</grpc-java-spring.version>
|
||||
</properties>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.alibaba</groupId>
|
||||
<artifactId>fastjson</artifactId>
|
||||
<version>1.2.56</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty</artifactId>
|
||||
<version>${grpc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-protobuf</artifactId>
|
||||
<version>${grpc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.nxest.grpc</groupId>
|
||||
<artifactId>grpc-java-spring</artifactId>
|
||||
<version>${grpc-java-spring.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zyplayer</groupId>
|
||||
<artifactId>zyplayer-doc-core</artifactId>
|
||||
<version>1.0.1</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<licenses>
|
||||
<license>
|
||||
<name>The Apache Software License, Version 2.0</name>
|
||||
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||
</license>
|
||||
</licenses>
|
||||
<scm>
|
||||
<connection>scm:git@git.oschina.net:zyplayer/zyplayer-doc.git</connection>
|
||||
<developerConnection>scm:git@git.oschina.net:zyplayer/zyplayer-doc.git</developerConnection>
|
||||
<url>git@git.oschina.net:zyplayer/zyplayer-doc.git</url>
|
||||
</scm>
|
||||
|
||||
<distributionManagement>
|
||||
<snapshotRepository>
|
||||
<id>snapshots</id>
|
||||
<url>https://oss.sonatype.org/content/repositories/snapshots/</url>
|
||||
</snapshotRepository>
|
||||
<repository>
|
||||
<id>snapshots</id>
|
||||
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
|
||||
</repository>
|
||||
</distributionManagement>
|
||||
|
||||
<build>
|
||||
<extensions>
|
||||
<extension>
|
||||
<groupId>kr.motd.maven</groupId>
|
||||
<artifactId>os-maven-plugin</artifactId>
|
||||
<version>1.5.0.Final</version>
|
||||
</extension>
|
||||
</extensions>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<targetPath>META-INF/resources/</targetPath>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>**/*.java</exclude>
|
||||
</excludes>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
<resource>
|
||||
<directory>src/main/webapp</directory>
|
||||
<includes>
|
||||
<include>**/*</include>
|
||||
</includes>
|
||||
<filtering>false</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>build-helper-maven-plugin</artifactId>
|
||||
<version>3.0.0</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>add-resource</id>
|
||||
<phase>generate-resources</phase>
|
||||
<goals>
|
||||
<goal>add-resource</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/java</directory>
|
||||
<directory>src/main/proto</directory>
|
||||
<directory>src/main/resources</directory>
|
||||
</resource>
|
||||
</resources>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.xolstice.maven.plugins</groupId>
|
||||
<artifactId>protobuf-maven-plugin</artifactId>
|
||||
<version>0.5.1</version>
|
||||
<configuration>
|
||||
<protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
|
||||
</protocArtifact>
|
||||
<pluginId>grpc-java</pluginId>
|
||||
<pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
|
||||
</pluginArtifact>
|
||||
<protoSourceRoot>src/main/proto</protoSourceRoot>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<goals>
|
||||
<goal>compile</goal>
|
||||
<goal>compile-custom</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
||||
@@ -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<Object> grpcServiceList = SpringContextUtil.getBeanWithAnnotation(GrpcService.class);
|
||||
if (grpcServiceList == null || grpcServiceList.size() <= 0) {
|
||||
return DocResponseJson.ok();
|
||||
}
|
||||
List<GrpcDocInfo> grpcDocInfoList = new LinkedList<>();
|
||||
for (Object service : grpcServiceList) {
|
||||
List<GrpcDocInfo> infoList = this.getDefinitionByJar(service.getClass());
|
||||
if (infoList != null) {
|
||||
grpcDocInfoList.addAll(infoList);
|
||||
}
|
||||
}
|
||||
// 找所有的参数列表
|
||||
for (GrpcDocInfo grpcDocInfo : grpcDocInfoList) {
|
||||
ColumnInfo columnInfo = new ColumnInfo();
|
||||
columnInfo.setType(grpcDocInfo.getParamType());
|
||||
List<ColumnInfo> 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<GrpcDocInfo> grpcDocInfoList = this.getDefinitionByJar(Class.forName(service));
|
||||
String executeResult = "执行失败";
|
||||
if (grpcDocInfoList != null && grpcDocInfoList.size() > 0) {
|
||||
Map<String, Object> paramMap = JSON.parseObject(params, new TypeReference<HashMap<String, Object>>() {});
|
||||
executeResult = this.executeFunction(grpcDocInfoList.get(0), paramMap);
|
||||
}
|
||||
return executeResult;
|
||||
}
|
||||
|
||||
private String executeFunction(GrpcDocInfo grpcDocInfo, Map<String, Object> paramMap) throws Exception {
|
||||
Class<?> aClass = Class.forName(grpcDocInfo.getParamType());
|
||||
Method getMoney = aClass.getMethod("newBuilder");
|
||||
Object newBuilder = getMoney.invoke(aClass);
|
||||
|
||||
List<MethodParam> 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<MethodParam> getSetterFunction(Class clazz) {
|
||||
List<MethodParam> 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<ColumnInfo> findClassColumns(Class clazz) throws Exception {
|
||||
Method getMoney = clazz.getMethod("newBuilder");
|
||||
Object newBuilder = getMoney.invoke(clazz);
|
||||
|
||||
List<MethodParam> paramList = this.getSetterFunction(newBuilder.getClass());
|
||||
List<ColumnInfo> subInfoList = new LinkedList<>();
|
||||
for (MethodParam param : paramList) {
|
||||
ColumnInfo info = new ColumnInfo();
|
||||
info.setType(param.getType());
|
||||
if (!Const.BASE_TYPE.contains(param.getType())) {
|
||||
List<ColumnInfo> classColumn = this.findClassColumns(Class.forName(param.getType()));
|
||||
info.setParam(classColumn);
|
||||
}
|
||||
subInfoList.add(info);
|
||||
}
|
||||
return subInfoList;
|
||||
}
|
||||
|
||||
private List<GrpcDocInfo> getDefinitionByJar(Class clazz) {
|
||||
List<GrpcDocInfo> 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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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<ColumnInfo> 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<ColumnInfo> getParam() {
|
||||
return param;
|
||||
}
|
||||
|
||||
public void setParam(List<ColumnInfo> param) {
|
||||
this.param = param;
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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<DubboNodeInfo> 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<DubboNodeInfo> getNodeList() {
|
||||
return nodeList;
|
||||
}
|
||||
|
||||
public void setNodeList(List<DubboNodeInfo> nodeList) {
|
||||
this.nodeList = nodeList;
|
||||
}
|
||||
|
||||
public String getInterfaceX() {
|
||||
return interfaceX;
|
||||
}
|
||||
|
||||
public void setInterfaceX(String interfaceX) {
|
||||
this.interfaceX = interfaceX;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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 {
|
||||
}
|
||||
@@ -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());
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
@@ -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> T getBean(Class<T> clz) {
|
||||
return context.getBean(clz);
|
||||
}
|
||||
|
||||
public static Object getBean(String string) {
|
||||
return getApplicationContext().getBean(string);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取类
|
||||
* @param annotationType annotation
|
||||
* @return 类对象
|
||||
*/
|
||||
public static List<Object> getBeanWithAnnotation(Class<? extends Annotation> annotationType) {
|
||||
if (context == null) {
|
||||
return null;
|
||||
}
|
||||
Map<String, Object> beansWithAnnotation = context.getBeansWithAnnotation(annotationType);
|
||||
return new LinkedList<>(beansWithAnnotation.values());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<String> 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());
|
||||
}
|
||||
}
|
||||
@@ -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<HelloResponse> responseObserver) {
|
||||
HelloResponse reply = HelloResponse.newBuilder().setMessage("Hello " + request.getName()).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sayTime(Empty request, StreamObserver<TimeResponse> responseObserver) {
|
||||
TimeResponse reply = TimeResponse.newBuilder().setTime(Timestamp.newBuilder().build()).build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
}
|
||||
@@ -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<ChatMsg> responseObserver) {
|
||||
ChatMsg reply = ChatMsg.newBuilder().setToken("sendMsg").build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sendImage(ChatMsg request, StreamObserver<ChatMsg> responseObserver) {
|
||||
ChatMsg reply = ChatMsg.newBuilder().setToken("sendImage").build();
|
||||
responseObserver.onNext(reply);
|
||||
responseObserver.onCompleted();
|
||||
}
|
||||
}
|
||||
36
zyplayer-doc-grpc/src/main/proto/HelloWorld.proto
Normal file
36
zyplayer-doc-grpc/src/main/proto/HelloWorld.proto
Normal file
@@ -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;
|
||||
}
|
||||
34
zyplayer-doc-grpc/src/main/proto/ZyplayerChat.proto
Normal file
34
zyplayer-doc-grpc/src/main/proto/ZyplayerChat.proto
Normal file
@@ -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;
|
||||
}
|
||||
@@ -109,6 +109,11 @@
|
||||
<artifactId>zyplayer-doc-data</artifactId>
|
||||
<version>${zyplayer.doc.version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.zyplayer</groupId>
|
||||
<artifactId>zyplayer-doc-grpc</artifactId>
|
||||
<version>${zyplayer.doc.version}</version>
|
||||
</dependency>
|
||||
<!--zyplayer-doc-swagger-->
|
||||
<dependency>
|
||||
<groupId>com.zyplayer</groupId>
|
||||
|
||||
@@ -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 {
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user