既然使用grpc,那么我们不可能每次都生成服务,所以我们先生成一些公共服务,并通过Any来实现自定义化
proto文件:
orange_grpc_metadata.proto:
syntax = "proto3";
option java_package = "com.orange.grpc.proto.auto";
option java_outer_classname = "MetaData";
message Metadata{
string type = 3 ;
string client = 8 ;
map<string,string> headers = 7;
}
orange_grpc_restload.proto
syntax = "proto3";
import "google/protobuf/any.proto";
import "orange_grpc_metadata.proto";
option java_package = "com.orange.grpc.proto.auto";
option java_outer_classname = "RestLoad";
message Restload{
Metadata metadata = 2;
google.protobuf.Any body = 3;
}
orange_grpc_service.proto
syntax = "proto3";
import "orange_grpc_restload.proto";
option java_package = "com.orange.grpc.proto.auto";
service RequestStream{
rpc requestStream (Restload) returns (stream Restload);
}
service Request {
rpc request (Restload) returns (Restload);
}
service BiRequestStream{
rpc requestBiStream (stream Restload) returns (stream Restload) ;
}
pom:
<?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>org.example</groupId>
<artifactId>orange-grpc</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<grpc.version>1.41.1</grpc.version>
</properties>
<dependencies>
<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>io.grpc</groupId>
<artifactId>grpc-stub</artifactId>
<version>${grpc.version}</version>
</dependency>
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.1</version>
</dependency>
</dependencies>
<build>
<extensions>
<extension>
<groupId>kr.motd.maven</groupId>
<artifactId>os-maven-plugin</artifactId>
<version>1.5.0.Final</version>
</extension>
</extensions>
<plugins>
<plugin>
<groupId>org.xolstice.maven.plugins</groupId>
<artifactId>protobuf-maven-plugin</artifactId>
<version>0.5.0</version>
<configuration>
<protoSourceRoot>${project.basedir}/src/main/resources/proto</protoSourceRoot>
<!-- <outputDirectory>${project.basedir}/src/main/java</outputDirectory>-->
<!--suppress UnresolvedMavenProperty -->
<protocArtifact>com.google.protobuf:protoc:3.5.1:exe:${os.detected.classifier}</protocArtifact>
<pluginId>grpc-java</pluginId>
<!--suppress UnresolvedMavenProperty -->
<pluginArtifact>io.grpc:protoc-gen-grpc-java:1.14.0:exe:${os.detected.classifier}</pluginArtifact>
</configuration>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>compile-custom</goal>
</goals>
</execution>
</executions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>8</source>
<target>8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
完成后进程构建,然后将生成的java文件拷贝到包目录
|