温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

maven grpc整合springboot demo的方法

发布时间:2022-04-27 14:32:58 来源:亿速云 阅读:213 作者:iii 栏目:开发技术

这篇文章主要介绍了maven grpc整合springboot demo的方法的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇maven grpc整合springboot demo的方法文章都会有所收获,下面我们一起来看看吧。

    1. 说明

    GRPC基于protobuf来定义接口。分为server端和client端。其中server端提供接口实现,client通过调用server端接口从而获取期望数据。

    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无法自动引入,可以现在依赖中引入,以便于依赖的下载,然后在删除依赖坐标即可)

    <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>
                        <!--默认值-->
                        <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,并将目录设置为Source Root,然后在目录src/main/proto下添加文件hello.proto,内容如下

    syntax = "proto3"; //指定proto版本
    package com.server;
    // 生成的Java代码的包名
    option java_package = "com.grpc.server";
    // 请求参数
    message HelloReq{
        string name = 1;
    }
    // 返回参数
    message HelloResp{
        string ret = 1;
    }
    // rpc service
    service HelloService{
    	// service中需要进行调用的具体方法
        rpc hello(HelloReq) returns (HelloResp){}
    }

    2.3 通过protobuf生成Java代码

    插件导入成功后,点击下图选中的protobuf:compileprotbuf:compile-custom 依次生成对应的Java代码(也就是接口依赖代码)

    maven grpc整合springboot demo的方法

    3. server端接口具体实现

    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端接口具体实现

    client端测试调用代码如下

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

    关于“maven grpc整合springboot demo的方法”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“maven grpc整合springboot demo的方法”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI