GoLang与Java各自生成grpc代码怎么实现
本篇内容介绍了“GoLang与Java各自生成grpc代码怎么实现”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
1.背景
由于公司的日志系统使用的是plumelog,最近生产环境老是报 jedis连接池不够,导致丢失日志,而且服务老是重启,怀疑跟日志系统有关,于是自己改造plumelog,使用go grpc生成server端,使用java grpc生成客户端,将日志以grpc服务形式传递到server端。
2.go生成grpc代码
2.1 安装
protc
选择自己所需版本,解压后将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";// 指定等会文件生成出来的packagepackage server;option go_package = "plumelog/rpc;server";// 定义request modelmessage PlumelogRequest{ string message = 1; // 1代表顺序}// 定义response modelmessage 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 serverimport ( "context" "plumelog/rpc")type RpcServer struct {}var pushProducer *plumelog.Producerfunc (*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和3syntax = "proto3";// 指定等会文件生成出来的packagepackage server;option java_package = "com.plumelog.logback.rpc";option java_multiple_files = false;option java_outer_classname = "RpcClient";// 定义request modelmessage PlumelogRequest{ string message = 1; // 1代表顺序}// 定义response modelmessage 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代码怎么实现”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注编程网网站,小编将为大家输出更多高质量的实用文章!
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341