当前位置:   article > 正文

Springboot之接入gRPC

Springboot之接入gRPC

1、maven依赖

  1. <properties>
  2. <!-- grpc -->
  3. <protobuf.version>3.5.1</protobuf.version>
  4. <protobuf-plugin.version>0.6.1</protobuf-plugin.version>
  5. <grpc.version>1.42.1</grpc.version>
  6. <os-maven-plugin.version>1.6.0</os-maven-plugin.version>
  7. </properties>
  8. <!-- grpc -->
  9. <dependency>
  10. <groupId>io.grpc</groupId>
  11. <artifactId>grpc-stub</artifactId>
  12. <version>${grpc.version}</version>
  13. </dependency>
  14. <dependency>
  15. <groupId>io.grpc</groupId>
  16. <artifactId>grpc-protobuf</artifactId>
  17. <version>${grpc.version}</version>
  18. </dependency>
  19. <dependency>
  20. <groupId>io.grpc</groupId>
  21. <artifactId>grpc-netty</artifactId>
  22. <version>${grpc.version}</version>
  23. </dependency>

2、编译环境构建

  1. <build>
  2. <extensions>
  3. <!-- os-maven-plugin -->
  4. <extension>
  5. <groupId>kr.motd.maven</groupId>
  6. <artifactId>os-maven-plugin</artifactId>
  7. <version>${os-maven-plugin.version}</version>
  8. </extension>
  9. </extensions>
  10. <plugins>
  11. <!-- protobuf-maven-plugin -->
  12. <plugin>
  13. <groupId>org.xolstice.maven.plugins</groupId>
  14. <artifactId>protobuf-maven-plugin</artifactId>
  15. <version>${protobuf-plugin.version}</version>
  16. <configuration>
  17. <protocArtifact>com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}</protocArtifact>
  18. <pluginId>grpc-java</pluginId>
  19. <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
  20. <!--默认值-->
  21. <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
  22. <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
  23. <!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
  24. <clearOutputDirectory>false</clearOutputDirectory>
  25. </configuration>
  26. <executions>
  27. <execution>
  28. <goals>
  29. <goal>compile</goal>
  30. <goal>compile-custom</goal>
  31. </goals>
  32. </execution>
  33. </executions>
  34. </plugin>
  35. </plugins>
  36. </build>

3、添加proto文件

在src/java同级添加proto文件夹,内部添加proto文件

修改java_package,生成包路径

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. option java_package = "com.bhl.biz.grpc";
  4. package media_broker;
  5. // The service definition.
  6. service MediaBroker {
  7. rpc open(MediaObject) returns (MediaStatus) {};
  8. rpc fetchFrame(MediaObject) returns (FrameObject) {};
  9. rpc fetchImage(MediaObject) returns (ImageObject) {};
  10. rpc close(MediaObject) returns (MediaStatus) {};
  11. }
  12. message MediaObject {
  13. string address = 1;
  14. }
  15. message MediaStatus {
  16. bool status = 1;
  17. string message = 2;
  18. }

4、生成代码

依次执行protobuf:compile、compile-custom

随后生成对应代码文件

5、注册grpc

官方参考demo

https://github.com/grpc/grpc-java/blob/master/examples/src/main/java/io/grpc/examples/helloworld/HelloWorldClient.java

  1. import io.grpc.ManagedChannel;
  2. import io.grpc.ManagedChannelBuilder;
  3. private ManagedChannel channel;
  4. private MediaBrokerGrpc.MediaBrokerBlockingStub blockingStub;
  5. void init(){
  6. // 127.0.0.1 50051
  7. channel = ManagedChannelBuilder.forAddress(grpcConfig.getHost(), grpcConfig.getPort()).usePlaintext().build();
  8. blockingStub = MediaBrokerGrpc.newBlockingStub(channel);
  9. }

核心文件 MediaBrokerGrpc

三种通信方式

6、发送请求

请求为已注册的服务

  1. // 请求对象
  2. MediaObject object = MediaObject.newBuilder().setAddress(address).build();
  3. // 发送请求
  4. MediaStatus status = blockingStub.open(object);
  5. log.info(status.toString());
  6. boolean result = status.getStatus();

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/116526?site
推荐阅读
相关标签
  

闽ICP备14008679号