Table of Contents
1. Description
2. Public part
2.1 Add dependencies
2.2 Add proto dependency file
2.3 Generate Java code through protobuf
3. The specific implementation of the server-side interface
4 The specific implementation of the client-side interface
Home Java javaTutorial How to integrate maven grpc with springboot demo

How to integrate maven grpc with springboot demo

May 13, 2023 pm 01:01 PM
grpc maven springboot

    1. Description

    GRPC defines the interface based on protobuf. Divided into server side and client side. The server side provides interface implementation, and the client obtains the expected data by calling the server side interface.

    2. Public part

    2.1 Add dependencies

            <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>
    Copy after login

    Add plug-in (Note: If wagon-provider-api cannot be automatically introduced, you can introduce it now in the dependencies to facilitate dependencies Download, and then delete the dependency coordinates)

    <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>
    Copy after login

    2.2 Add proto dependency file

    Add directorysrc/main/proto, and set the directory to Source Root, and then add the file hello.proto under the directory src/main/proto, with the following content

    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){}
    }
    Copy after login

    2.3 Generate Java code through protobuf

    After the plug-in is imported successfully, click on the protobuf:compile and protbuf:compile-custom selected in the figure below to generate the corresponding Java code (that is, the interface dependency code) in sequence

    How to integrate maven grpc with springboot demo

    3. The specific implementation of the server-side interface

    service code is as follows

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

    4 The specific implementation of the client-side interface

    client The terminal test calling code is as follows

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

    The above is the detailed content of How to integrate maven grpc with springboot demo. For more information, please follow other related articles on the PHP Chinese website!

    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Notepad++7.3.1

    Notepad++7.3.1

    Easy-to-use and free code editor

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    Zend Studio 13.0.1

    Zend Studio 13.0.1

    Powerful PHP integrated development environment

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    SublimeText3 Mac version

    SublimeText3 Mac version

    God-level code editing software (SublimeText3)

    Hot Topics

    Java Tutorial
    1663
    14
    PHP Tutorial
    1266
    29
    C# Tutorial
    1239
    24
    Java Maven build tool advancement: optimizing compilation speed and dependency management Java Maven build tool advancement: optimizing compilation speed and dependency management Apr 17, 2024 pm 06:42 PM

    Optimize Maven build tools: Optimize compilation speed: Take advantage of parallel compilation and incremental compilation. Optimize dependencies: Analyze dependency trees and use BOM (bill of materials) to manage transitive dependencies. Practical case: illustrate optimizing compilation speed and dependency management through examples.

    How to use gRPC to implement file upload in Golang? How to use gRPC to implement file upload in Golang? Jun 03, 2024 pm 04:54 PM

    How to implement file upload using gRPC? Create supporting service definitions, including request and response messages. On the client, the file to be uploaded is opened and split into chunks, then streamed to the server via a gRPC stream. On the server side, file chunks are received and stored into a file. The server sends a response after the file upload is completed to indicate whether the upload was successful.

    Avoid common mistakes in Maven environment configuration: Solve configuration problems Avoid common mistakes in Maven environment configuration: Solve configuration problems Feb 19, 2024 pm 04:56 PM

    Maven is a Java project management and build tool that is widely used in the development of Java projects. In the process of using Maven to build projects, you often encounter some common environment configuration problems. This article will answer these common questions and provide specific code examples to help readers avoid common configuration errors. 1. Maven environment variables are incorrectly configured. Problem description: When using Maven, if the environment variables are incorrectly configured, Maven may not work properly. Solution: Make sure

    Guide to setting up Maven local libraries: efficiently manage project dependencies Guide to setting up Maven local libraries: efficiently manage project dependencies Feb 19, 2024 am 11:47 AM

    Maven local warehouse configuration guide: Easily manage project dependencies. With the development of software development, project dependency package management has become more and more important. As an excellent build tool and dependency management tool, Maven plays a vital role in the project development process. Maven will download project dependencies from the central warehouse by default, but sometimes we need to save some specific dependency packages to the local warehouse for offline use or to avoid network instability. This article will introduce how to configure Maven local warehouse for easy management

    Basic tutorial: Create a Maven project using IDEA Basic tutorial: Create a Maven project using IDEA Feb 19, 2024 pm 04:43 PM

    IDEA (IntelliJIDEA) is a powerful integrated development environment that can help developers develop various Java applications quickly and efficiently. In Java project development, using Maven as a project management tool can help us better manage dependent libraries, build projects, etc. This article will detail the basic steps on how to create a Maven project in IDEA, while providing specific code examples. Step 1: Open IDEA and create a new project Open IntelliJIDEA

    Best practices and recommended methods for setting Java versions in Maven Best practices and recommended methods for setting Java versions in Maven Feb 22, 2024 pm 03:18 PM

    When using Maven to build a Java project, you often encounter situations where you need to set the Java version. Correctly setting the Java version can not only ensure that the project runs normally in different environments, but also avoid some compatibility issues and improve the stability and maintainability of the project. This article will introduce the best practices and recommended methods for setting Java versions in Maven, and provide specific code examples for reference. 1. Set the Java version in the pom.xml file. In the pom.xml file of the Maven project, you can

    Smooth build: How to correctly configure the Maven image address Smooth build: How to correctly configure the Maven image address Feb 20, 2024 pm 08:48 PM

    Smooth build: How to correctly configure the Maven image address When using Maven to build a project, it is very important to configure the correct image address. Properly configuring the mirror address can speed up project construction and avoid problems such as network delays. This article will introduce how to correctly configure the Maven mirror address and give specific code examples. Why do you need to configure the Maven image address? Maven is a project management tool that can automatically build projects, manage dependencies, generate reports, etc. When building a project in Maven, usually

    How to disable test cases in Maven? How to disable test cases in Maven? Feb 26, 2024 am 09:57 AM

    Maven is an open source project management tool that is commonly used for tasks such as construction, dependency management, and document release of Java projects. When using Maven for project build, sometimes we want to ignore the testing phase when executing commands such as mvnpackage, which will improve the build speed in some cases, especially when a prototype or test environment needs to be built quickly. This article will detail how to ignore the testing phase in Maven, with specific code examples. Why you should ignore testing During project development, it is often

    See all articles