欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

GoLang與Java各自生成grpc代碼流程介紹

 更新時(shí)間:2023年03月15日 11:16:04   作者:Json_Marz  
這篇文章主要介紹了GoLang與Java各自生成grpc代碼流程,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧

1.背景

由于公司的日志系統(tǒng)使用的是plumelog,最近生產(chǎn)環(huán)境老是報(bào) jedis連接池不夠,導(dǎo)致丟失日志,而且服務(wù)老是重啟,懷疑跟日志系統(tǒng)有關(guān),于是自己改造plumelog,使用go grpc生成server端,使用java grpc生成客戶端,將日志以grpc服務(wù)形式傳遞到server端。

2.go生成grpc代碼

2.1 安裝

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

選擇自己所需版本,解壓后將protoc.exe拷貝至go環(huán)境的bin目錄下

2.2 安裝對(duì)應(yīng)插件

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環(huán)境的bin目錄下

編寫(xiě).proto文件:

syntax = "proto3";
// 指定等會(huì)文件生成出來(lái)的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代表順序
}
// 定義服務(wù)主體
service PlumelogService{
  // 定義方法
  rpc GetPlumelog(PlumelogRequest) returns(PlumelogResponse);
}

項(xiàng)目結(jié)構(gòu)圖:

在終端cd到proto目錄下,執(zhí)行如下命令生成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一個(gè)grpc的server
    rpcServer := grpc.NewServer()
    // 2. 將剛剛我們新建的ProdService注冊(cè)進(jìn)去
    rpc.RegisterPlumelogServiceServer(rpcServer, new(server.RpcServer))
    // 3. 新建一個(gè)listener,以tcp方式監(jiān)聽(tīng)8899端口
    listener, err := net.Listen("tcp", ":8899")
    if err != nil {
        log.Fatal("服務(wù)監(jiān)聽(tīng)端口失敗", err)
    }
    // 4. 運(yùn)行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 創(chuàng)建maven項(xiàng)目

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代碼

// 這個(gè)就是protobuf的中間文件
// 指定的當(dāng)前proto語(yǔ)法的版本,有2和3
syntax = "proto3";
// 指定等會(huì)文件生成出來(lái)的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代表順序
}
// 定義服務(wù)主體
service PlumelogService{
  // 定義方法
  rpc GetPlumelog(PlumelogRequest) returns(PlumelogResponse);
}

ps:指定的package要與go 那邊的proto指定的package一致,否則啟動(dòng)報(bào)找不到rpc服務(wù)

雙擊maven插件的protobuf:complie生成rpc代碼,雙擊maven插件的protobuf:custom生成grpc代碼

調(diào)用:

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

服務(wù)引入log在本地 maven倉(cāng)庫(kù)的坐標(biāo),啟動(dòng)即可,前提是go 服務(wù)先啟動(dòng)。

到此這篇關(guān)于GoLang與Java各自生成grpc代碼流程介紹的文章就介紹到這了,更多相關(guān)Go生成grpc內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Go語(yǔ)言防范SQL注入CSRF及XSS攻擊實(shí)例探究

    Go語(yǔ)言防范SQL注入CSRF及XSS攻擊實(shí)例探究

    在本文中,我們將會(huì)介紹幾種最常見(jiàn)的攻擊類型,并且介紹如何使用Golang來(lái)防范這些攻擊,本文會(huì)涉及XSS攻擊、CSRF攻擊、SQL注入等,如果你想學(xué)習(xí)Golang和網(wǎng)絡(luò)安全的相關(guān)知識(shí),那么這篇文章會(huì)是一個(gè)很好的開(kāi)始
    2024-01-01
  • Go語(yǔ)言學(xué)習(xí)之WaitGroup用法詳解

    Go語(yǔ)言學(xué)習(xí)之WaitGroup用法詳解

    Go語(yǔ)言中的?WaitGroup?和?Java?中的?CyclicBarrier、CountDownLatch?非常類似。本文將詳細(xì)為大家講講WaitGroup的用法,感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2022-06-06
  • 淺談go中defer的一個(gè)隱藏功能

    淺談go中defer的一個(gè)隱藏功能

    這篇文章主要介紹了淺談go中defer的一個(gè)隱藏功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • golang image圖片處理示例

    golang image圖片處理示例

    這篇文章主要介紹了golang image圖片處理的方法,結(jié)合實(shí)例形式分析了Go語(yǔ)言針對(duì)圖片的打開(kāi)、讀取、轉(zhuǎn)換等相關(guān)操作技巧,需要的朋友可以參考下
    2016-07-07
  • Go語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單留言板的方法

    Go語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單留言板的方法

    這篇文章主要介紹了Go語(yǔ)言實(shí)現(xiàn)簡(jiǎn)單留言板的方法,涉及數(shù)據(jù)庫(kù)、模板頁(yè)面元素等留言板相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-02-02
  • golang 使用time包獲取時(shí)間戳與日期格式化操作

    golang 使用time包獲取時(shí)間戳與日期格式化操作

    這篇文章主要介紹了golang 使用time包獲取時(shí)間戳與日期格式化操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • Go?Java算法之單詞搜索示例詳解

    Go?Java算法之單詞搜索示例詳解

    這篇文章主要為大家介紹了Go?Java算法之單詞搜索示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-08-08
  • Golang并發(fā)編程之調(diào)度器初始化詳解

    Golang并發(fā)編程之調(diào)度器初始化詳解

    這篇文章主要為大家詳細(xì)介紹了Golang并發(fā)編程中關(guān)于調(diào)度器初始化的相關(guān)知識(shí),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解一下
    2023-03-03
  • GO語(yǔ)言函數(shù)(func)的聲明與使用詳解

    GO語(yǔ)言函數(shù)(func)的聲明與使用詳解

    這篇文章主要介紹了GO函數(shù)(func)的聲明與使用,包括了GO語(yǔ)言函數(shù)聲明與使用,GO語(yǔ)言遞歸函數(shù),GO語(yǔ)言內(nèi)置函數(shù),GO語(yǔ)言函數(shù)defer應(yīng)用,GO語(yǔ)言函數(shù)可變長(zhǎng)度參數(shù)需要的朋友可以參考下
    2022-12-12
  • Go語(yǔ)言基礎(chǔ)模板設(shè)計(jì)模式示例詳解

    Go語(yǔ)言基礎(chǔ)模板設(shè)計(jì)模式示例詳解

    這篇文章主要為大家介紹了Go語(yǔ)言基礎(chǔ)設(shè)計(jì)模式之模板模式的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步
    2021-11-11

最新評(píng)論