- 整合grpc,根据官网推荐的需要自行实现客户端与服务端,详细实现可参考grpc官网
- 快速实现整合,grpc-spring-boot-starter
<dependency> <groupId>net.devh</groupId> <artifactId>grpc-spring-boot-starter</artifactId> <version>2.11.0.RELEASE</version> </dependency>
-
详细内容请参考示例代码
-
定义proto文件
-
使用grpc-java编译生成java代码
<properties> <os.plugin.version>1.7.0</os.plugin.version> <grpc.version>1.31.1</grpc.version> <protoc.version>3.13.0</protoc.version> <protobuf.plugin.version>0.6.1</protobuf.plugin.version> </properties> <dependencies> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-stub</artifactId> <version>${grpc.version}</version> </dependency> <dependency> <groupId>io.grpc</groupId> <artifactId>grpc-protobuf</artifactId> <version>${grpc.version}</version> </dependency> <dependency> <groupId>com.google.protobuf</groupId> <artifactId>protobuf-java</artifactId> <version>${protoc.version}</version> </dependency> </dependencies> <build> <extensions> <extension> <groupId>kr.motd.maven</groupId> <artifactId>os-maven-plugin</artifactId> <version>${os.plugin.version}</version> </extension> </extensions> <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>${protobuf.plugin.version}</version> <extensions>true</extensions> <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>${project.basedir}/src/main/proto</protoSourceRoot> <!--默认值--> <!--<outputDirectory>${project.build.directory}/generated-sources/protobuf/java</outputDirectory>--> <outputDirectory>${project.basedir}/src/main/java</outputDirectory> <!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件--> <clearOutputDirectory>false</clearOutputDirectory> <!--更多配置信息可以查看https://www.xolstice.org/protobuf-maven-plugin/compile-mojo.html--> </configuration> <executions> <execution> <!--在执行mvn compile的时候会执行以下操作--> <phase>compile</phase> <goals> <!--生成OuterClass类--> <goal>compile</goal> <!--生成Grpc类--> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> </build>
compile 至此,java代码生成完成
-
使用protoc生成swagger与go代码
protoc --proto_path=../interactive-common/src/main/proto --go_out=plugins=grpc,Mgoogle/protobuf/descriptor.proto=github.com/golang/protobuf/protoc-gen-go/descriptor:. google/api/annotations.proto google/api/http.proto protoc --proto_path=../interactive-common/src/main/proto --go_out=plugins=grpc,Mgoogle/api/annotations.proto=interactive-interview-grpc-gateway/google/api:. common.proto protoc --proto_path=../interactive-common/src/main/proto --go_out=plugins=grpc,Mgoogle/api/annotations.proto=interactive-interview-grpc-gateway/google/api,Mcommon.proto=interactive-interview-grpc-gateway/interactive/msg/protobuf:. interView/interView.proto protoc --proto_path=../interactive-common/src/main/proto --grpc-gateway_out=logtostderr=true,Mcommon.proto=interactive-interview-grpc-gateway/interactive/msg/protobuf:. interView/interView.proto protoc --proto_path=../interactive-common/src/main/proto --swagger_out=logtostderr=true:. interView/interView.proto protoc --proto_path=../interactive-common/src/main/proto --grpc-gateway_out=logtostderr=true:. common.proto protoc --proto_path=../interactive-common/src/main/proto --swagger_out=logtostderr=true:. common.proto
import ( "context" "flag" "fmt" "github.com/golang/glog" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/rs/cors" "google.golang.org/grpc" // 此处导入的是protoc生成的 interview "interactive-interview-grpc-gateway/interactive/msg/interview/protobuf" "net/http" ) // grpc端口 var ( grpcServerEndpoint = flag.String("grpc-server-endpoint", "127.0.0.1:9003", "gRPC server endpoint") httpServerEndpoint = flag.String("http", ":19003", "http server address") version = flag.Bool("version", false, "show version") ) var VersionName = "1.0.0" func run() error { ctx := context.Background() ctx, cancel := context.WithCancel(ctx) defer cancel() mux := runtime.NewServeMux(runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{OrigName: true, EmitDefaults: true})) //mux := runtime.NewServeMux() //headers:=&metadata.MD{"token":[]string{"XYZ"}} //grpc.Header(headers) opts := []grpc.DialOption{grpc.WithInsecure()} // RegisterInterViewServiceHandlerFromEndpoint 连接GRPC服务端 err := interview.RegisterInterViewServiceHandlerFromEndpoint(ctx, mux, *grpcServerEndpoint, opts) if err != nil { return err } glog.Info("Http服务:", *httpServerEndpoint) return http.ListenAndServe(*httpServerEndpoint, cors.AllowAll().Handler(mux)) } func main() { flag.Parse() defer glog.Flush() if *version { fmt.Println(VersionName) return } if err := run(); err != nil { glog.Fatal(err) } }
至此,go客户端编写完成
-
编译生成exe文件
go build -o [filename].exe main.go
启动服务端,再启动客户端,即可访问proto中定义的接口。