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

2023-12-13 04:12:44

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

1.项目结构如下

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

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

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中调用即可

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

}

文章来源:https://blog.csdn.net/qq_45467608/article/details/134850993
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。