当前位置:   article > 正文

SpringBoot 集成gRpc Demo_springboot rpc demo

springboot rpc demo
1.gRpc简介

    gRpc是google开源的一个高性能、跨语言的rpc框架,基于HTTP2协议,基于protobuf 3.x;对于开发者,使用gRpc需要做的事情:

1>使用protobuf定义接口,即.proto文件

2>compile生成特定语言的代码,比如JAVA,Python等,解决跨语言的问题。

3>服务端实现上述生成的接口,然后启动,等待客户端进行连接;

4>启动一个或者多个客户端,与service建立连接并发送请求;request和response都被封装成Http2的stream Frame,通过channel进行交互;

2.SpringBoot对于gRpc的封装

    SpringBoot提供了grpc-spring-boot-starter对gRpc进行集成;具体的代码地址https://github.com/xiaoguangtouqiang/grpc-demo.git

项目目录结构如图


grpc-lib存放定义的proto文件,作为公共的依赖,给grpc-client和grpc-server调用;

1>创建grpc-lib文件夹,新建目录src/main/proto,再下面新建helloworld.proto文件,代码如下

  1. syntax = "proto3";
  2. option java_multiple_files = true;
  3. //定义输出的目录,生成的目录就是“net/devh/examples/grpc/lib”下面
  4. option java_package = "net.devh.examples.grpc.lib";
  5. //定义输出的文件名称,生成在lib下的就是HelloWorldProto.class
  6. option java_outer_classname = "HelloWorldProto";
  7. // The greeting service definition.
  8. //定义的接口的类,这里会生成一个SimpleGrpc.class,服务端需要来实现的
  9. service Simple {
  10. //定义接口方法
  11. rpc SayHello (HelloRequest) returns (HelloReply) {
  12. }
  13. }
  14. //请求参数
  15. message HelloRequest {
  16. string name = 1;
  17. }
  18. //返回结果
  19. message HelloReply {
  20. string message = 1;
  21. }

然后创建build.gradle,需要添加protobuf将上面的文件编译成java代码,具体内容如下

  1. apply plugin: 'java'
  2. apply plugin: 'com.google.protobuf'
  3. apply plugin: 'idea'
  4. repositories {
  5. maven { url "https://plugins.gradle.org/m2/" }
  6. }
  7. dependencies {
  8. compile "io.grpc:grpc-netty:1.10.0"
  9. compile "io.grpc:grpc-protobuf:1.10.0"
  10. compile "io.grpc:grpc-stub:1.10.0"
  11. }
  12. protobuf {
  13. protoc {
  14. // The artifact spec for the Protobuf Compiler
  15. artifact = 'com.google.protobuf:protoc:3.0.0'
  16. }
  17. plugins {
  18. // Optional: an artifact spec for a protoc plugin, with "grpc" as
  19. // the identifier, which can be referred to in the "plugins"
  20. // container of the "generateProtoTasks" closure.
  21. grpc {
  22. artifact = 'io.grpc:protoc-gen-grpc-java:1.0.0-pre2'
  23. }
  24. }
  25. generateProtoTasks {
  26. ofSourceSet('main')*.plugins {
  27. // Apply the "grpc" plugin whose spec is defined above, without
  28. // options. Note the braces cannot be omitted, otherwise the
  29. // plugin will not be added. This is because of the implicit way
  30. // NamedDomainObjectContainer binds the methods.
  31. grpc { }
  32. }
  33. }
  34. }
  35. buildscript {
  36. repositories {
  37. maven { url "https://plugins.gradle.org/m2/" }
  38. }
  39. dependencies {
  40. classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.4'
  41. }
  42. }

然后执行gradle compileJava生成对应的java代码,生成的结果如图所示


2>服务端实现

    通过start.spring.io生成springBoot的服务端,build.gradle文件添加依赖项,代码如下

  1. buildscript {
  2. ext {
  3. springBootVersion = '2.0.2.RELEASE'
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  10. }
  11. }
  12. apply plugin: 'java'
  13. apply plugin: 'eclipse'
  14. apply plugin: 'org.springframework.boot'
  15. apply plugin: 'io.spring.dependency-management'
  16. group = 'com.example'
  17. version = '0.0.1-SNAPSHOT'
  18. sourceCompatibility = 1.8
  19. repositories {
  20. mavenCentral()
  21. }
  22. dependencies {
  23. compile('org.springframework.boot:spring-boot-starter-web')
  24. compile 'net.devh:grpc-server-spring-boot-starter:1.4.0.RELEASE'
  25. //注意,需要依赖grpc-lib项目
  26. compile project(':grpc-lib')
  27. testCompile('org.springframework.boot:spring-boot-starter-test')
  28. }
  29. buildscript {
  30. dependencies {
  31. classpath("org.springframework.boot:spring-boot-gradle-plugin:0.8.4")
  32. }
  33. }

然后创建GrpcServerService实现上面的接口,代码如图所示

  1. package com.example.gRpc;
  2. import io.grpc.stub.StreamObserver;
  3. import net.devh.examples.grpc.lib.HelloReply;
  4. import net.devh.examples.grpc.lib.HelloRequest;
  5. import net.devh.examples.grpc.lib.SimpleGrpc;
  6. import net.devh.springboot.autoconfigure.grpc.server.GrpcService;
  7. @GrpcService(SimpleGrpc.class)
  8. public class GrpcServerService extends SimpleGrpc.SimpleImplBase{
  9. @Override
  10. public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
  11. HelloReply reply = HelloReply.newBuilder().setMessage("Hello =============> " + req.getName()).build();
  12. responseObserver.onNext(reply);
  13. responseObserver.onCompleted();
  14. }
  15. }

另外,需要配置rpc服务端的名称,端口等信息,application.properties配置如图

  1. #服务端名称
  2. spring.application.name=local-grpc-server
  3. #服务端运行端口
  4. server.port=8888
  5. #grpc通信端口
  6. grpc.server.port=9898

3>客户端

与服务端类似,build.gradle文件添加依赖

  1. buildscript {
  2. ext {
  3. springBootVersion = '2.0.2.RELEASE'
  4. }
  5. repositories {
  6. mavenCentral()
  7. }
  8. dependencies {
  9. classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
  10. }
  11. }
  12. apply plugin: 'java'
  13. apply plugin: 'eclipse'
  14. apply plugin: 'org.springframework.boot'
  15. apply plugin: 'io.spring.dependency-management'
  16. group = 'com.example'
  17. version = '0.0.1-SNAPSHOT'
  18. sourceCompatibility = 1.8
  19. repositories {
  20. mavenCentral()
  21. }
  22. dependencies {
  23. compile project(':grpc-lib')
  24. compile("net.devh:grpc-client-spring-boot-starter:1.4.0.RELEASE")
  25. compile('org.springframework.boot:spring-boot-starter-web')
  26. }

然后创建GrpcClientService,调用服务端提供的接口,代码如下

  1. package com.example.gRpcclient;
  2. import io.grpc.Channel;
  3. import net.devh.examples.grpc.lib.HelloReply;
  4. import net.devh.examples.grpc.lib.HelloRequest;
  5. import net.devh.examples.grpc.lib.SimpleGrpc;
  6. import net.devh.springboot.autoconfigure.grpc.client.GrpcClient;
  7. import org.springframework.stereotype.Service;
  8. @Service
  9. public class GrpcClientService {
  10. @GrpcClient("local-grpc-server")
  11. private Channel serverChannel;
  12. public String sendMessage(String name) {
  13. SimpleGrpc.SimpleBlockingStub stub = SimpleGrpc.newBlockingStub(serverChannel);
  14. HelloReply response = stub.sayHello(HelloRequest.newBuilder().setName(name).build());
  15. return response.getMessage();
  16. }
  17. }

application.properties中配置grpc服务端的信息

  1. server.port=8080
  2. spring.application.name=local-grpc-client
  3. grpc.client.local-grpc-server.host=127.0.0.1
  4. grpc.client.local-grpc-server.port=9898
  5. grpc.client.local-grpc-server.enableKeepAlive=true
  6. grpc.client.local-grpc-server.keepAliveWithoutCalls=true

最后写个简单的请求来测试下

  1. package com.example.gRpcclient;
  2. import org.springframework.beans.factory.annotation.Autowired;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. public class GrpcClientController {
  8. @Autowired
  9. private GrpcClientService grpcClientService;
  10. @RequestMapping("/")
  11. public String printMessage(@RequestParam(defaultValue = "Michael") String name) {
  12. return grpcClientService.sendMessage(name);
  13. }
  14. }

运行结果如图所示



参考内容:

https://github.com/yidongnan/grpc-spring-boot-starter/blob/master/README-zh.md

https://blog.csdn.net/qq_28423433/article/details/79108976

https://blog.csdn.net/lyjshen/article/details/52238234

protobuf-gradle-plugin


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/145975
推荐阅读
相关标签
  

闽ICP备14008679号