java对接grpc接口详解

文档说明

通过java的方式对接grpc接口,在java项目中属于第一次对接,编写文档记录一下,方便后期的对接,增加对接效率。

对接细节

一.项目架构
后端采用Spring Boot、Spring Security、
Redis & Jwt

二.对接细节
1.pom中导入grpc需要的包依赖
下面展示一些 内联代码片

<!-- grpc -->
<protobuf.version>3.5.1</protobuf.version>
<protobuf-plugin.version>0.6.1</protobuf-plugin.version>
<grpc.version>1.42.1</grpc.version>

三.项目中添加protobuf-maven编译插件

<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>

在这里插入图片描述

确定编译插件生效及指定文件生成目录
在这里插入图片描述

四.同级目录添加proto文件
在这里插入图片描述

指定输出地址
在这里插入图片描述

生成的文件在我们指定的目录下面
在这里插入图片描述

五.配置grpc地址及相关信息

grpc:
  client:
    local-grpc-server:
      address: 'static://10.251.21.20:31484'
      enableKeepAlive: true
      keepAliveWithoutCalls: true
      negotiationType: plaintext

六.grpc简单调用实例

@GrpcClient("local-grpc-server")
private EngineGrpc.EngineBlockingStub engineStub;


public String sendMessage() {
    try {
        final EngineGrpcProto.AsyncReply response = engineStub.scanAsync(EngineGrpcProto.ScanAsyncParams.newBuilder()
                .setApkUrl("xxx")
                .setReqId("1234-12345678-12345678-12345678-123456")
                .setCateOpt(65535)
                .setScanOpt(18999035)
                .setExtendFuncOpt(11)
                .setTimeout(600)
                .setEngineVer("292-pc64")
                .setVirusLibUrl("xxx")
                .setLicenseUrl("xxx")
                .setCallbackUrl("someurl")
                .setPriority(12)
                .setWaitUntil(Timestamp.newBuilder()
                        .setNanos(0)
                        .setSeconds(1661258870)
                        .build())
                .addApi(0, EngineGrpcProto.Api.newBuilder()
                        .setCmd("GetAllVirNames")
                        .build()
                )
                .build()
        );
        return response.getReqId();
    } catch (final StatusRuntimeException e) {
        return "FAILED with " + e.getStatus().getCode().name();
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一事无成只会写代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值