栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

grpc基础教程之protocol-buffers

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

grpc基础教程之protocol-buffers

protocol buffers:

Basics tutorial | Java | gRPC

一、包路径配置:

option java_package = "io.grpc.examples.routeguide";

二、grpc请求与返回:

 simple rpc

// Obtains the feature at a given position.
rpc GetFeature(Point) returns (Feature) {}

 server-side streaming

// Obtains the Features available within the given Rectangle.  Results are
// streamed rather than returned at once (e.g. in a response message with a
// repeated field), as the rectangle may cover a large area and contain a
// huge number of features.
rpc ListFeatures(Rectangle) returns (stream Feature) {}

  client-side streaming

// Accepts a stream of Points on a route being traversed, returning a
// RouteSummary when traversal is completed.
rpc RecordRoute(stream Point) returns (RouteSummary) {}

  server-side streaming & client-side streaming

// Accepts a stream of RouteNotes sent while a route is being traversed,
// while receiving other RouteNotes (e.g. from other users).
rpc RouteChat(stream RouteNote) returns (stream RouteNote) {}

三、message type define

// Points are represented as latitude-longitude pairs in the E7 representation
// (degrees multiplied by 10**7 and rounded to the nearest integer).
// Latitudes should be in the range +/- 90 degrees and longitude should be in
// the range +/- 180 degrees (inclusive).
message Point {
  int32 latitude = 1;
  int32 longitude = 2;
}

四、生成客户端和服务代码

POM.xml

  
        
            io.grpc
            grpc-netty-shaded
            1.30.2
        
        
            io.grpc
            grpc-protobuf
            1.30.2
        
        
            io.grpc
            grpc-stub
            1.30.2
        
         
            org.apache.tomcat
            annotations-api
            6.0.53
            provided
        
    
    
        
            
                kr.motd.maven
                os-maven-plugin
                1.6.2
            
        
        
            
                org.xolstice.maven.plugins
                protobuf-maven-plugin
                0.6.1
                
                    com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}
                    grpc-java
                    io.grpc:protoc-gen-grpc-java:1.30.2:exe:${os.detected.classifier}
                
                
                    
                        
                            compile
                            compile-custom
                        
                    
                
            
        
    

编译:

服务端代码生成:

 客户端代码生成:

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/862978.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号