溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

maven?grpc整合springboot?demo的方法

發(fā)布時間:2022-04-27 14:32:58 來源:億速云 閱讀:222 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹了maven grpc整合springboot demo的方法的相關(guān)知識,內(nèi)容詳細(xì)易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇maven grpc整合springboot demo的方法文章都會有所收獲,下面我們一起來看看吧。

    1. 說明

    GRPC基于protobuf來定義接口。分為server端和client端。其中server端提供接口實現(xiàn),client通過調(diào)用server端接口從而獲取期望數(shù)據(jù)。

    2. 公共部分

    2.1 添加依賴

            <dependency>
                <groupId>net.devh</groupId>
                <artifactId>grpc-spring-boot-starter</artifactId>
                <version>2.12.0.RELEASE</version>
            </dependency>
            <dependency>
                <!-- Java 9+ compatibility -->
                <groupId>javax.annotation</groupId>
                <artifactId>javax.annotation-api</artifactId>
            </dependency>

    添加插件(注意:如果wagon-provider-api無法自動引入,可以現(xiàn)在依賴中引入,以便于依賴的下載,然后在刪除依賴坐標(biāo)即可)

    <plugin>
                    <!--                    protobuf生成插件-->
                    <groupId>org.xolstice.maven.plugins</groupId>
                    <artifactId>protobuf-maven-plugin</artifactId>
                    <version>0.6.1</version>
                    <configuration>
                        <protocArtifact>com.google.protobuf:protoc:3.17.3:exe:${os.detected.classifier}
                        </protocArtifact>
                        <pluginId>grpc-java</pluginId>
                        <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.39.0:exe:${os.detected.classifier}
                        </pluginArtifact>
                        <!--默認(rèn)值-->
                        <protoSourceRoot>${project.basedir}/src/main/proto</protoSourceRoot>
                        <outputDirectory>${project.basedir}/src/main/java</outputDirectory>
                        <clearOutputDirectory>false</clearOutputDirectory>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>compile</goal>
                                <goal>compile-custom</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>

    2.2 添加proto依賴文件

    添加目錄src/main/proto,并將目錄設(shè)置為Source Root,然后在目錄src/main/proto下添加文件hello.proto,內(nèi)容如下

    syntax = "proto3"; //指定proto版本
    package com.server;
    // 生成的Java代碼的包名
    option java_package = "com.grpc.server";
    // 請求參數(shù)
    message HelloReq{
        string name = 1;
    }
    // 返回參數(shù)
    message HelloResp{
        string ret = 1;
    }
    // rpc service
    service HelloService{
    	// service中需要進(jìn)行調(diào)用的具體方法
        rpc hello(HelloReq) returns (HelloResp){}
    }

    2.3 通過protobuf生成Java代碼

    插件導(dǎo)入成功后,點擊下圖選中的protobuf:compileprotbuf:compile-custom 依次生成對應(yīng)的Java代碼(也就是接口依賴代碼)

    maven?grpc整合springboot?demo的方法

    3. server端接口具體實現(xiàn)

    service代碼如下

    import io.grpc.stub.StreamObserver;
    import net.devh.boot.grpc.server.service.GrpcService;
    @GrpcService
    public class HelloService extends HelloServiceGrpc.HelloServiceImplBase {
        @Override
        public void hello(Hello.HelloReq request, StreamObserver<Hello.HelloResp> responseObserver) {
            Hello.HelloResp resp = Hello.HelloResp.newBuilder().setRet("你好-->"+request.getName()).build();
            responseObserver.onNext(resp);
            responseObserver.onCompleted();
        }
    }

    4 client端接口具體實現(xiàn)

    client端測試調(diào)用代碼如下

    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    @SpringBootTest
    public class GrpcTest {
        @Autowired
        private HelloSerivce helloSerivce;
        @Test
        public void test1() throws  Exception{
            helloSerivce.haha("牛哈哈");
        }
    }

    關(guān)于“maven grpc整合springboot demo的方法”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“maven grpc整合springboot demo的方法”知識都有一定的了解,大家如果還想學(xué)習(xí)更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI