GoLang与Java各自生成grpc代码流程介绍

 更新时间:2023年03月15日 11:16:04   作者:Json_Marz  
这篇文章主要介绍了GoLang与Java各自生成grpc代码流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

1.背景

由于公司的日志系统使用的是plumelog,最近生产环境老是报 jedis连接池不够,导致丢失日志,而且服务老是重启,怀疑跟日志系统有关,于是自己改造plumelog,使用go grpc生成server端,使用java grpc生成客户端,将日志以grpc服务形式传递到server端。

2.go生成grpc代码

2.1 安装

protc:https://github.com/protocolbuffers/protobuf/releases

选择自己所需版本,解压后将protoc.exe拷贝至go环境的bin目录下

2.2 安装对应插件

go install google.golang.org/protobuf/cmd/protoc-gen-go@v1.28
go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@v1.2

将生成的插件拷贝至go环境的bin目录下

编写.proto文件:

syntax = "proto3";
// 指定等会文件生成出来的package
package server;
option go_package = "plumelog/rpc;server";
// 定义request model
message PlumelogRequest{
  string message = 1; // 1代表顺序
}
// 定义response model
message PlumelogResponse{
  string message = 1; // 1代表顺序
}
// 定义服务主体
service PlumelogService{
  // 定义方法
  rpc GetPlumelog(PlumelogRequest) returns(PlumelogResponse);
}

项目结构图:

在终端cd到proto目录下,执行如下命令生成grpc代码:

 protoc --go_out=plugins=grpc:. server.proto

server端:

main.go:

import (
    "google.golang.org/grpc"
    "log"
    "net"
    "plumelog/rpc"
    "plumelog/server"
)
func main() {
    // 1. new一个grpc的server
    rpcServer := grpc.NewServer()
    // 2. 将刚刚我们新建的ProdService注册进去
    rpc.RegisterPlumelogServiceServer(rpcServer, new(server.RpcServer))
    // 3. 新建一个listener,以tcp方式监听8899端口
    listener, err := net.Listen("tcp", ":8899")
    if err != nil {
        log.Fatal("服务监听端口失败", err)
    }
    // 4. 运行rpcServer,传入listener
    _ = rpcServer.Serve(listener)
}

server.go

package server
import (
    "context"
    "plumelog/rpc"
)
type RpcServer struct {
}
var pushProducer *plumelog.Producer
func (*RpcServer) GetProductStock(ctx context.Context, req *rpc.PlumelogRequest) (*rpc.PlumelogResponse, error) {
    fmt.Println(req.Message)
    return &rpc.PlumelogResponse{Message: req.Message}, nil
}

3.java生成grpc代码

3.1 idea安装protobuf插件

3.2 创建maven项目

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <parent>
        <groupId>com.plumelog</groupId>
        <artifactId>plumelog</artifactId>
        <version>3.5</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>
    <artifactId>plumelog-logback</artifactId>
    <name>plumelog-logback</name>
    <packaging>jar</packaging>
    <dependencies>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-core</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>com.plumelog</groupId>
            <artifactId>plumelog-core</artifactId>
            <version>${project.parent.version}</version>
        </dependency>
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.google.protobuf/protobuf-java-util -->
        <dependency>
            <groupId>com.google.protobuf</groupId>
            <artifactId>protobuf-java-util</artifactId>
            <version>3.5.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.grpc/grpc-all -->
        <dependency>
            <groupId>io.grpc</groupId>
            <artifactId>grpc-all</artifactId>
            <version>1.12.0</version>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>javax.annotation</groupId>
            <artifactId>javax.annotation-api</artifactId>
            <version>1.3.2</version>
            <scope>compile</scope>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <!-- 依赖包插件 -->
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-dependency-plugin</artifactId>
                <executions>
                    <execution>
                        <id>copy-dependencies</id>
                        <phase>package</phase>
                        <goals>
                            <goal>copy-dependencies</goal>
                        </goals>
                        <configuration>
                            <outputDirectory>${project.build.directory}/lib</outputDirectory>
                            <!-- 是否不包含间接依赖 -->
                            <excludeTransitive>false</excludeTransitive>
                            <!-- 忽略版本 -->
                            <stripVersion>false</stripVersion>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.xolstice.maven.plugins</groupId>
                <artifactId>protobuf-maven-plugin</artifactId>
                <version>0.5.0</version>
                <configuration>
                    <protocArtifact>
                        com.google.protobuf:protoc:3.1.0:exe:${os.detected.classifier}
                    </protocArtifact>
                    <pluginId>grpc-java</pluginId>
                    <pluginArtifact>
                        io.grpc:protoc-gen-grpc-java:1.11.0:exe:${os.detected.classifier}
                    </pluginArtifact>
                </configuration>
                <!--                <executions>-->
                <!--                    <execution>-->
                <!--                        <goals>-->
                <!--                            <goal>compile</goal>-->
                <!--                            <goal>compile-custom</goal>-->
                <!--                        </goals>-->
                <!--                    </execution>-->
                <!--                </executions>-->
            </plugin>
        </plugins>
        <extensions>
            <extension>
                <groupId>kr.motd.maven</groupId>
                <artifactId>os-maven-plugin</artifactId>
                <version>1.6.2</version>
            </extension>
        </extensions>
    </build>
</project>

maven插件:

3.3 生成grpc代码

// 这个就是protobuf的中间文件
// 指定的当前proto语法的版本,有2和3
syntax = "proto3";
// 指定等会文件生成出来的package
package server;
option java_package = "com.plumelog.logback.rpc";
option java_multiple_files = false;
option java_outer_classname = "RpcClient";
// 定义request model
message PlumelogRequest{
  string message = 1; // 1代表顺序
}
// 定义response model
message PlumelogResponse{
  string message = 1; // 1代表顺序
}
// 定义服务主体
service PlumelogService{
  // 定义方法
  rpc GetPlumelog(PlumelogRequest) returns(PlumelogResponse);
}

ps:指定的package要与go 那边的proto指定的package一致,否则启动报找不到rpc服务

双击maven插件的protobuf:complie生成rpc代码,双击maven插件的protobuf:custom生成grpc代码

调用:

    private String rpcHost = "127.0.0.1";
    private int rpcPort = 8899;
    ManagedChannel channel = ManagedChannelBuilder.forAddress(rpcHost, rpcPort).usePlaintext().build();
    @Override
    protected void append(ILoggingEvent event) {
        if (event != null) {
            send(event);
        }
    }
    protected void send(ILoggingEvent event) {
        final BaseLogMessage logMessage = LogMessageUtil.getLogMessage(appName, env, event);
        if (logMessage instanceof RunLogMessage) {
            final String message = LogMessageUtil.getLogMessage(logMessage, event);
            PlumelogRpcClient.callRpcServer(channel, message);
        } 
    }
package com.plumelog.logback.util;
import com.plumelog.logback.rpc.PlumelogServiceGrpc;
import com.plumelog.logback.rpc.RpcClient;
import io.grpc.ManagedChannel;
public class PlumelogRpcClient {
    public static void callRpcServer(ManagedChannel channel, String message) {
        RpcClient.PlumelogRequest request = RpcClient.PlumelogRequest.newBuilder().setMessage(message).build();
        try {
            PlumelogServiceGrpc.newBlockingStub(channel).getProductStock(request);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

服务引入log在本地 maven仓库的坐标,启动即可,前提是go 服务先启动。

到此这篇关于GoLang与Java各自生成grpc代码流程介绍的文章就介绍到这了,更多相关Go生成grpc内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!

相关文章

  • 在go语言中安装与使用protobuf的方法详解

    在go语言中安装与使用protobuf的方法详解

    protobuf以前只支持C++, Python和Java等语言, Go语言出来后, 作为亲儿子, 那有不支持的道理呢? 这篇文章主要给大家介绍了关于在go语言中使用protobuf的相关资料,文中介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2017-08-08
  • Golang设计模式之适配器模式介绍和代码示例

    Golang设计模式之适配器模式介绍和代码示例

    适配器是一种结构型设计模式, 它能使不兼容的对象能够相互合作,可担任两个对象间的封装器, 它会接收对于一个对象的调用, 并将其转换为另一个对象可识别的格式和接口,本文将通过代码示例详细给大家介绍Golang的适配器模式
    2023-06-06
  • 一起来用GoLand开发第一个Go程序

    一起来用GoLand开发第一个Go程序

    当您在编辑器中工作时GoLand 会分析您的代码,寻找优化方法,并检测潜在和实际问题,下面这篇文章主要给大家介绍了关于用GoLand开发第一个Go程序的相关资料,文中通过图文介绍的非常详细,需要的朋友可以参考下
    2022-12-12
  • 手把手教你导入Go语言第三方库

    手把手教你导入Go语言第三方库

    本文主要介绍了手把手教你导入Go语言第三方库,通过导入gin包来深入学习,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2021-08-08
  • go语言开发中如何优雅得关闭协程方法

    go语言开发中如何优雅得关闭协程方法

    这篇文章主要为大家介绍了go语言开发中如何优雅得关闭协程方法详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-05-05
  • go调用shell命令两种方式实现(有无返回值)

    go调用shell命令两种方式实现(有无返回值)

    本文主要介绍了go调用shell命令两种方式实现(有无返回值),主要用于执行shell命令,并且返回shell的标准输出,具有一定的参考价值,感兴趣的可以了解一下
    2021-12-12
  • Go语言学习之链表的使用详解

    Go语言学习之链表的使用详解

    链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。本文将详细为大家介绍Go语言中链表的使用,感兴趣的可以了解一下
    2022-04-04
  • 部署Go语言项目的 N 种方法(小结)

    部署Go语言项目的 N 种方法(小结)

    这篇文章主要介绍了部署Go语言项目的 N 种方法(小结),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-11-11
  • Golang极简入门教程(三):并发支持

    Golang极简入门教程(三):并发支持

    这篇文章主要介绍了Golang极简入门教程(三):并发支持,本文讲解了goroutine线程、channel 操作符等内容,需要的朋友可以参考下
    2014-10-10
  • golang中map增删改查的示例代码

    golang中map增删改查的示例代码

    在Go语言中,map是一种内置的数据结构,用于存储键值对,本文主要介绍了golang中map增删改查的示例代码,具有一定的参考价值,感兴趣的可以了解一下
    2023-11-11

最新评论

?


http://www.vxiaotou.com