Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of integrating springboot demo by maven grpc

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of the method of maven grpc integration of springboot demo, the content is detailed and easy to understand, the operation is simple and fast, and it has a certain reference value. I believe you will gain something after reading this maven grpc integration springboot demo method article. Let's take a look at it.

1. Description

GRPC defines interfaces based on protobuf. It is divided into server side and client side. The server side provides the interface implementation, and the client obtains the desired data by calling the server side interface.

two。 Public part 2.1Adding dependency net.devh grpc-spring-boot-starter 2.12.0.RELEASE javax.annotation javax.annotation-api

Add plug-ins (Note: if wagon-provider-api cannot be introduced automatically, you can introduce dependencies now to facilitate dependent downloads, and then delete dependency coordinates)

Org.xolstice.maven.plugins protobuf-maven-plugin 0.6.1 com.google.protobuf:protoc:3.17.3:exe:$ {os.detected.classifier} grpc-java io.grpc:protoc-gen-grpc-java:1.39. 0src/main/proto exeVR ${os.detected.classifier} ${project.basedir} / src/main/proto ${project.basedir} / src/main/java false Compile compile-custom 2.2 add proto dependent files

Add the directory src/main/proto, and set the directory to Source Root, and then add the file hello.proto under the directory src/main/proto, as follows

Syntax = "proto3"; / / specify the package name of the Java code generated by the proto version of package com.server;// option java_package = "com.grpc.server"; / / request parameter message HelloReq {string name = 1;} / / return parameter message HelloResp {string ret = 1;} / / specific method rpc hello (HelloReq) returns (HelloResp) to be called in / / rpc serviceservice HelloService {/ / service generate Java code through protobuf

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

3. Specific implementation of server port interface

The service code is as follows

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

The client side test call code is as follows

Import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestpublic class GrpcTest {@ Autowired private HelloSerivce helloSerivce; @ Testpublic void test1 () throws Exception {helloSerivce. ("Niu ");}} this is the end of the article on "maven grpc's method of integrating springboot demo". Thank you for reading! I believe you all have a certain understanding of "maven grpc's method of integrating springboot demo". If you want to learn more, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report