赞
踩
- <properties>
- <!-- grpc -->
- <protobuf.version>3.5.1</protobuf.version>
- <protobuf-plugin.version>0.6.1</protobuf-plugin.version>
- <grpc.version>1.42.1</grpc.version>
- <os-maven-plugin.version>1.6.0</os-maven-plugin.version>
- </properties>
-
- <!-- grpc -->
- <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>io.grpc</groupId>
- <artifactId>grpc-netty</artifactId>
- <version>${grpc.version}</version>
- </dependency>
- <build>
- <extensions>
- <!-- os-maven-plugin -->
- <extension>
- <groupId>kr.motd.maven</groupId>
- <artifactId>os-maven-plugin</artifactId>
- <version>${os-maven-plugin.version}</version>
- </extension>
- </extensions>
- <plugins>
- <!-- protobuf-maven-plugin -->
- <plugin>
- <groupId>org.xolstice.maven.plugins</groupId>
- <artifactId>protobuf-maven-plugin</artifactId>
- <version>${protobuf-plugin.version}</version>
- <configuration>
- <protocArtifact>com.google.protobuf:protoc:${protobuf.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.basedir}/src/main/java</outputDirectory>
- <!--设置是否在生成java文件之前清空outputDirectory的文件,默认值为true,设置为false时也会覆盖同名文件-->
- <clearOutputDirectory>false</clearOutputDirectory>
- </configuration>
- <executions>
- <execution>
- <goals>
- <goal>compile</goal>
- <goal>compile-custom</goal>
- </goals>
- </execution>
- </executions>
- </plugin>
-
- </plugins>
-
- </build>
在src/java同级添加proto文件夹,内部添加proto文件
修改java_package,生成包路径
- syntax = "proto3";
-
- option java_multiple_files = true;
- option java_package = "com.bhl.biz.grpc";
-
- package media_broker;
-
- // The service definition.
- service MediaBroker {
- rpc open(MediaObject) returns (MediaStatus) {};
-
- rpc fetchFrame(MediaObject) returns (FrameObject) {};
- rpc fetchImage(MediaObject) returns (ImageObject) {};
-
- rpc close(MediaObject) returns (MediaStatus) {};
- }
-
- message MediaObject {
- string address = 1;
- }
-
- message MediaStatus {
- bool status = 1;
- string message = 2;
- }
依次执行protobuf:compile、compile-custom
随后生成对应代码文件
- import io.grpc.ManagedChannel;
- import io.grpc.ManagedChannelBuilder;
-
- private ManagedChannel channel;
- private MediaBrokerGrpc.MediaBrokerBlockingStub blockingStub;
-
- void init(){
- // 127.0.0.1 50051
- channel = ManagedChannelBuilder.forAddress(grpcConfig.getHost(), grpcConfig.getPort()).usePlaintext().build();
- blockingStub = MediaBrokerGrpc.newBlockingStub(channel);
- }
核心文件 MediaBrokerGrpc
三种通信方式
请求为已注册的服务
- // 请求对象
- MediaObject object = MediaObject.newBuilder().setAddress(address).build();
- // 发送请求
- MediaStatus status = blockingStub.open(object);
- log.info(status.toString());
- boolean result = status.getStatus();
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。