grpc双向流

news/2024/7/4 13:25:07 标签: java, c#, c/c++
简介

grpc是一个高性能、开源和通用的 RPC 框架,面向移动和 HTTP/2 设计。目前提供 C、Java 和 Go 语言版本,分别是:grpc, grpc-java, grpc-go. 其中 C 版本支持 C, C++, Node.js, Python, Ruby, Objective-C, PHP 和 C# 支持.

gRPC 基于 HTTP/2 标准设计,带来诸如双向流、流控、头部压缩、单 TCP 连接上的多复用请求等特。这些特性使得其在移动设备上表现更好,更省电和节省空间占用。

开发环境配置

首先配置maven引用jar包,导入全部grpc用的包,也可不全部导入,我这里求方便。

<dependency>
  <groupId>io.grpc</groupId>
  <artifactId>grpc-all</artifactId>
  <version>1.17.1</version>
</dependency>
<dependency>复制代码

然后引入protobuf文件解析和代码生成:

<extensions>
   <extension>
      <groupId>kr.motd.maven</groupId>
      <artifactId>os-maven-plugin</artifactId>
      <version>1.5.0.Final</version>
   </extension>
</extensions>
<plugins>
   <plugin>
      <groupId>org.xolstice.maven.plugins</groupId>
      <artifactId>protobuf-maven-plugin</artifactId>
      <version>0.5.1</version>
      <configuration>
         <protocArtifact>com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}</protocArtifact>
         <pluginId>grpc-java</pluginId>
         <pluginArtifact>io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}</pluginArtifact>
      </configuration>
      <executions>
         <execution>
            <goals>
               <goal>compile</goal>
               <goal>compile-custom</goal>
            </goals>
         </execution>
      </executions>
   </plugin>
   <plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-enforcer-plugin</artifactId>
      <version>1.4.1</version>
      <executions>
         <execution>
            <id>enforce</id>
            <goals>
               <goal>enforce</goal>
            </goals>
            <configuration>
               <rules>
                  <requireUpperBoundDeps/>
               </rules>
            </configuration>
         </execution>
      </executions>
   </plugin>
</plugins>复制代码

引入后,在idea中maven projects中的plugins中就会看到protobuf,在这个里面就能生成所需要的java文件。到“lifecycle”中执行compile或者执行maven命令“mvn compile”,就能生成需要的java文件。


注:定义的protobuf文件要与“java,resources”同级。


入门实例

grpc Client:

import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder;
import java.util.concurrent.TimeUnit;


public class HelloWorldClient {

  private final ManagedChannel channel;
  
  private static final GreeterGrpc.GreeterBlockingStub blockingStub;
  
  HelloWorldClient(String host, int port) {
    this.channel =  ManagedChannelBuilder.forAddress(host, port)
            .usePlaintext()
            .build();
    blockingStub = GreeterGrpc.newBlockingStub(channel);
  }

  public void shutdown() throws InterruptedException {
    channel.shutdown().awaitTermination(5, TimeUnit.SECONDS);
  }

  public static void main(String[] args) throws Exception {
    HelloWorldClient client = new HelloWorldClient("localhost", 50051);
    try {
      blockingStub.sayHello(HelloRequest.newBuilder().setName("hello").build();) 
    } finally {
      client.shutdown();
    }
  }
}
复制代码

grpc-server


import io.grpc.Server;
import io.grpc.ServerBuilder;
import io.grpc.stub.StreamObserver;
import java.io.IOException;
import java.util.logging.Logger;


public class HelloWorldServer {
  private static final Logger logger = Logger.getLogger(HelloWorldServer.class.getName());

  private Server server;

  private void start() throws IOException {
    int port = 50051;
    server = ServerBuilder.forPort(port)
        .addService(new GreeterImpl())
        .build()
        .start();
    Runtime.getRuntime().addShutdownHook(new Thread() {
      @Override
      public void run() {
        HelloWorldServer.this.stop();
      }
    });
  }

  private void stop() {
    if (server != null) {
      server.shutdown();
    }
  }

  /**
   * 等待主线程终止,因为GRPC库使用守护进程线程。
   */
  private void blockUntilShutdown() throws InterruptedException {
    if (server != null) {
      server.awaitTermination();
    }
  }

  public static void main(String[] args) throws IOException, InterruptedException {
    final HelloWorldServer server = new HelloWorldServer();
    server.start();
    server.blockUntilShutdown();
  }

  static class GreeterImpl extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
      HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
      responseObserver.onNext(reply);
      responseObserver.onCompleted();
    }
  }
}
复制代码


官方入门实例详见:https://github.com/grpc/grpc-java/tree/master/examples/src/main/java/io/grpc/examples/helloworld

双向流

server :

    Map<String,StreamObserver<Object> > streamObserverMap = new HashMap();
 static class GreeterImpl extends GreeterGrpc.GreeterImplBase {

    @Override
    public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
      HelloReply reply = HelloReply.newBuilder().setMessage("Hello " + req.getName()).build();
          //客户端调用时,将observer保存起
      streamObserverMap.put("sayHello",responseObserver);
    }
  }    /**
    * 发送消息
    */
    public void sendMsgToclient() {
        //使用时,从保存的streamObserver调用onNext()
        StreamObserver<Object> streamObserver = streamObserverMap.get("sayHello");
        streamObserver.onNext();
        //这里注意不要调用onCompleted()方法,onCompleted()会将流关闭。当然如果这个流不用了你可以关闭
//        responseObserver.onCompleted();
    }
复制代码

Client:

//客户端要在连接上服务端的时候要显示的调用sendMsg方法,作为流的注册
  public static void main(String[] args) throws Exception {
    HelloWorldClient client = new HelloWorldClient("localhost", 50051);
    try {
      //注册StreamObserver
      blockingStub.sayHello(HelloRequest.newBuilder().setName("").build()) 
    } finally {
      client.shutdown();
    }
  }复制代码


参考:grpc官方文档(http://doc.oschina.net/grpc?t=58008)


如有写的不对地方欢迎在评论指出。





http://www.niftyadmin.cn/n/1818989.html

相关文章

vue项目对axios的全局配置

转载&#xff1a;https://www.cnblogs.com/djjlovedjj/p/9050841.html 标准的vue-cli项目结构&#xff08;httpConfig文件夹自己建的&#xff09;&#xff1a; api.js: //const apiUrl http://test;//测试域名&#xff0c;自己改成自己的 const apiUrl http://xxoo;//线上域名…

final修饰和StringBuffer的几个案例(拼接,反转,对称操作)

final关键字修饰时如果是基本数据类型的变量&#xff0c;则其数值一旦在初始化之后便不能更改&#xff1b;如果是引用类型的变量&#xff0c;则在对其初始化之后便不能再让其指向另一个对象&#xff0c;但引用变量不能变&#xff0c;引用变量所指向的对象中的内容还是可以改变的…

模糊查询于聚合函数

通配符&#xff1a; _ 单个字符  %任意字符  []范围内的字符  [^]范围外的字符 SELECT * FROM StudentUPDATE Student SET StudentName王波 WHERE StudentNoY21003011--模糊查询&#xff1a;查询学生表中姓’冯‘的学生记录SELECT * FROM Student WHERE StudentName L…

html导出excel

function base64 (content) {return window.btoa(unescape(encodeURIComponent(content)))}function exportTab () { // 导出let tath thislet table document.getElementById(tableID) //tableID为dom层 table IDlet excelContent table.innerHTMLlet excelFile "&…

云评论

http://changyan.kuaizhan.com/static/help/index.html 转载于:https://www.cnblogs.com/guochen/p/11081671.html

十分钟构建双十一交互分析大盘

2019独角兽企业重金招聘Python工程师标准>>> 提到双十一很多人第一印象是一张成交金额跳动的炫酷大屏&#xff0c;的确大盘在阿里双十一中几乎是每个团队标配&#xff0c;例如&#xff1a; CEO看业务&#xff1a;把重要数据放到一张大屏上&#xff0c;简洁而有冲击力…

移动端开发-Day1

什么是Node&#xff1f; 它是一个基于Chrome v8引擎的js运行环境&#xff0c;采用高效轻量级的事件驱动&#xff0c;非阻塞式的I/O模型。 非阻塞I/O&#xff1f; 例如&#xff0c;当程序运行到某一函数时&#xff0c;调用后立即返回&#xff0c;不需要等待函数执行完成&#xf…

建议是在公司发不出工资前换好工作

在移动互联网兴起的这几年&#xff0c;无论是从网上还是身边都看到了很多程序员被无良公司拖欠工资&#xff0c;可能是公司倒闭&#xff0c;也可能是老板跑路。我个人感觉程序员是一个相对单纯的群体&#xff08;很好骗&#xff09;&#xff0c;尤其是刚工作的年轻人。油腻的创…