【gRPC实现java端调用go的服务】

实现一个客户端调用go服务端的简单服务

1.项目结构如下

在lib下面的存在一个simple.proto文件,我们使用插件protobuf-maven-plugin对其进行编译。配置如下:

xml 复制代码
 <properties>
        <os-maven-plugin.version>1.5.0.Final</os-maven-plugin.version>
        <protobuf-maven-plugin.version>0.5.1</protobuf-maven-plugin.version>
        <protoc.version>3.5.1-1</protoc.version>
        <protobuf.version>3.6.0</protobuf.version>
        <grpc.version>1.13.1</grpc.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>${protobuf.version}</version>
        </dependency>
        <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>
    </dependencies>

    <build>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>${os-maven-plugin.version}</version>
            </extension>
        </extensions>
        <plugins>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>${protobuf-maven-plugin.version}</version>
                <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>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                            <goal>compile-custom</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

利用插件进行编译

后可以获得对应的文件。

2. Client

在client下创建一个grpc的包,并将以上两个文件放入。最后创建一个SimpleClient。

java 复制代码
package com.iq50.client.grpc;

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;

/**
 * gRPC客户端示例
 */
public class SimpleClient {

    // gRPC通信通道
    private final ManagedChannel channel;
    
    // 自动生成的存根
    private final SimpleGrpc.SimpleBlockingStub blockingStub;

    /**
     * 构造函数,创建SimpleClient实例
     * @param host 服务器主机名
     * @param port 服务器端口
     */
    public SimpleClient(String host, int port){
        this(ManagedChannelBuilder.forAddress(host, port).usePlaintext());
    }

    /**
     * 私有构造函数,接受ManagedChannelBuilder参数
     * @param channelBuilder 通道构建器
     */
    private SimpleClient(ManagedChannelBuilder<?> channelBuilder) {
        // 构建通信通道
        channel = channelBuilder.build();
        // 根据通道返回的信息创建存根
        blockingStub = SimpleGrpc.newBlockingStub(channel);
    }

    /**
     * 关闭通信通道
     * @throws InterruptedException 线程中断异常
     */
    public void shutdown() throws InterruptedException {
        // 关闭通道并等待最多5秒钟确保关闭完成
        channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
    }

    /**
     * 向gRPC服务发送问候消息
     * @param name 问候消息的名称
     * @return 服务响应的消息
     */
    public String sayHello(String name) {
        // 创建HelloRequest对象
        SimpleOuterClass.HelloRequest req = SimpleOuterClass.HelloRequest.newBuilder().setName(name).build();
        // 调用服务方法获取响应
        SimpleOuterClass.HelloReplay replay = blockingStub.sayHello(req);
        // 返回服务响应的消息
        return replay.getMessage();
    }
}

最后在Application中调用即可

java 复制代码
package com.iq50.client;

import com.iq50.client.grpc.SimpleClient;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ClientApplication {

    public static void main(String[] args) {

        SpringApplication.run(ClientApplication.class, args);

        SimpleClient client = new SimpleClient("127.0.0.1",50051);
        String replay = client.sayHello("Jack");
        try {
            client.shutdown();
        } catch (InterruptedException e) {
            System.out.println("channel关闭异常:"+ e.getMessage());
        }
        System.out.println("回应是"+replay);
    }

}
相关推荐
anlogic几秒前
Java基础 8.18
java·开发语言
喂完待续22 分钟前
【Tech Arch】Spark为何成为大数据引擎之王
大数据·hadoop·python·数据分析·spark·apache·mapreduce
练习时长一年1 小时前
AopAutoConfiguration源码阅读
java·spring boot·intellij-idea
王者鳜錸2 小时前
PYTHON让繁琐的工作自动化-猜数字游戏
python·游戏·自动化
源码宝2 小时前
【智慧工地源码】智慧工地云平台系统,涵盖安全、质量、环境、人员和设备五大管理模块,实现实时监控、智能预警和数据分析。
java·大数据·spring cloud·数据分析·源码·智慧工地·云平台
若天明2 小时前
深度学习-计算机视觉-微调 Fine-tune
人工智能·python·深度学习·机器学习·计算机视觉·ai·cnn
David爱编程3 小时前
面试必问!线程生命周期与状态转换详解
java·后端
倔强青铜三3 小时前
苦练Python第39天:海象操作符 := 的入门、实战与避坑指南
人工智能·python·面试
LKAI.3 小时前
传统方式部署(RuoYi-Cloud)微服务
java·linux·前端·后端·微服务·node.js·ruoyi
HeyZoeHey3 小时前
Mybatis执行sql流程(一)
java·sql·mybatis