【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);
    }

}
相关推荐
bug攻城狮2 分钟前
Spring Boot 2.6+ 整合 PageHelper 启动报错:循环依赖解决方案全解析
java·spring boot·后端
前端世界4 分钟前
用Python手写一个能识花的感知器模型——Iris分类实战详解
开发语言·python·分类
少林and叔叔13 分钟前
基于yolov5.7.0的人工智能算法的下载、开发环境搭建(pycharm)与运行测试
人工智能·pytorch·python·yolo·目标检测·pycharm
好学且牛逼的马33 分钟前
MyBatis-Plus的深度解析
java
苏纪云35 分钟前
数据结构<C++>——数组
java·数据结构·c++·数组·动态数组
合作小小程序员小小店35 分钟前
旧版本附近停车场推荐系统demo,基于python+flask+协同推荐(基于用户信息推荐),开发语言python,数据库mysql,
人工智能·python·flask·sklearn·推荐算法
动能小子ohhh44 分钟前
Langchain从零开始到应用落地案例[AI智能助手]【3】---使用Paddle-OCR识别优化可识别图片进行解析回答
人工智能·python·pycharm·langchain·ocr·paddle·1024程序员节
典则1 小时前
STM32FreeRtos入门(五)——同步互斥与通信
java·jvm·stm32
你不是我我1 小时前
【Java 开发日记】我们来讲一讲阻塞队列及其应用
java·开发语言
互联网中的一颗神经元1 小时前
小白python入门 - 9. Python 列表2 ——从基础操作到高级应用
java·开发语言·python