您好,登錄后才能下訂單哦!
本篇內容介紹了“GoLang與Java各自生成grpc代碼怎么實現”的有關知識,在實際案例的操作過程中,不少人都會遇到這樣的困境,接下來就讓小編帶領大家學習一下如何處理這些情況吧!希望大家仔細閱讀,能夠學有所成!
由于公司的日志系統使用的是plumelog,最近生產環境老是報 jedis連接池不夠,導致丟失日志,而且服務老是重啟,懷疑跟日志系統有關,于是自己改造plumelog,使用go grpc生成server端,使用java grpc生成客戶端,將日志以grpc服務形式傳遞到server端。
protc
選擇自己所需版本,解壓后將protoc.exe拷貝至go環境的bin目錄下
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 }
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插件:
// 這個就是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代碼怎么實現”的內容就介紹到這里了,感謝大家的閱讀。如果想了解更多行業相關的知識可以關注億速云網站,小編將為大家輸出更多高質量的實用文章!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。