脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|shell|

服务器之家 - 脚本之家 - Golang - GoLang与Java各自生成grpc代码流程介绍

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

2023-05-30 10:24Json_Marz Golang

这篇文章主要介绍了GoLang与Java各自生成grpc代码流程,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

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文件:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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);
}

项目结构图:

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

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

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

server端:

main.go:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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

?
1
2
3
4
5
6
7
8
9
10
11
12
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插件

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

 

3.2 创建maven项目

pom.xml:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
<?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插件:

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

 

3.3 生成grpc代码

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// 这个就是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代码

调用:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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);
    }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!

原文链接:https://blog.csdn.net/Json_Marz/article/details/129508053

延伸 · 阅读

精彩推荐
  • GolangGo日志框架zap增强及源码解读

    Go日志框架zap增强及源码解读

    这篇文章主要为大家介绍了Go日志框架zap增强及源码解读,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    捉虫大师8752022-07-29
  • Golang示例剖析golang中的CSP并发模型

    示例剖析golang中的CSP并发模型

    这篇文章主要为大家介绍了示例剖析golang中的CSP并发模型,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    ThomasYuan11442022-10-10
  • GolangGin 框架快速创建静态文件下载Web服务

    Gin 框架快速创建静态文件下载Web服务

    本文主要介绍了Gin 框架快速创建静态文件下载Web服务,文中通过示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...

    尹东勋11532022-01-22
  • GolangGolang 字符串与字节数组互转的实现

    Golang 字符串与字节数组互转的实现

    在Go语言中,我们经常在字符串和切片之间进行转换,本文就详细的介绍一下Golang 字符串与字节数组互转的实现,文中通过示例代码介绍的非常详细,具有...

    头秃猫轻王5472022-09-04
  • Golang一文带你深入探索Golang操作mongodb的方法

    一文带你深入探索Golang操作mongodb的方法

    这篇文章主要为大家详细介绍了Golang操作mongodb的相关知识,包括:初始化项目工程、容器方式安装mongo和调试运行和编译运行,感兴趣的小伙伴可以了解一...

    金色旭光6212023-02-09
  • Golanggolang的强制类型转换实现

    golang的强制类型转换实现

    这篇文章主要介绍了golang的强制类型转换实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随...

    紫葡萄010322021-03-29
  • GolangGO的range具体使用

    GO的range具体使用

    GO语言的for…range 能做什么呢?golang的for…range是go 身的语法,可以用来遍历数据结构,本文就详细的来介绍一下具体使用,感兴趣的可以了解一下...

    小魔童哪吒4012021-11-25
  • Golanggo高并发时append方法偶现错误解决分析

    go高并发时append方法偶现错误解决分析

    这篇文章主要为大家介绍了go高并发时append方法偶现错误解决分析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪...

    gokingliu5522022-12-01