https://github.com/zq2599/blog_demos
内容:所有原创文章分类汇总及配套源码,涉及Java、Docker、Kubernetes、DevOPS等;
本文是《java版gRPC实战》系列的第三篇,前文咱们实战体验了简单的RPC请求和响应,那种简单的请求响应方式其实只是gRPC定义的四种类型之一,这里给出《gRPC 官方文档中文版》对这四种gRPC类型的描述:
本篇是服务端流类型的gRPC服务实战,包括以下内容:
名称 | 链接 | 备注 |
---|---|---|
项目主页 | https://github.com/zq2599/blog_demos | 该项目在GitHub上的主页 |
git仓库地址(https) | https://github.com/zq2599/blog_demos.git | 该项目源码的仓库地址,https协议 |
git仓库地址(ssh) | git@github.com:zq2599/blog_demos.git | 该项目源码的仓库地址,ssh协议 |
syntax = "proto3"; option java_multiple_files = true; // 生成java代码的package option java_package = "com.bolingcavalry.grpctutorials.lib"; // 类名 option java_outer_classname = "MallProto"; // gRPC服务,这是个在线商城的订单查询服务 service OrderQuery { // 服务端流式:订单列表接口,入参是买家信息,返回订单列表(用stream修饰返回值) rpc ListOrders (Buyer) returns (stream Order) {} } // 买家ID message Buyer { int32 buyerId = 1; } // 返回结果的数据结构 message Order { // 订单ID int32 orderId = 1; // 商品ID int32 productId = 2; // 交易时间 int64 orderTime = 3; // 买家备注 string buyerRemark = 4; }
// 使用springboot插件 plugins { id 'org.springframework.boot' } dependencies { implementation 'org.projectlombok:lombok' implementation 'org.springframework.boot:spring-boot-starter' // 作为gRPC服务提供方,需要用到此库 implementation 'net.devh:grpc-server-spring-boot-starter' // 依赖自动生成源码的工程 implementation project(':grpc-lib') }
spring: application: name: server-stream-server-side # gRPC有关的配置,这里只需要配置服务端口号 grpc: server: port: 9899
package com.bolingcavalry.grpctutorials; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ServerStreamServerSideApplication { public static void main(String[] args) { SpringApplication.run(ServerStreamServerSideApplication.class, args); } }
package com.bolingcavalry.grpctutorials; import com.bolingcavalry.grpctutorials.lib.Buyer; import com.bolingcavalry.grpctutorials.lib.Order; import com.bolingcavalry.grpctutorials.lib.OrderQueryGrpc; import io.grpc.stub.StreamObserver; import net.devh.boot.grpc.server.service.GrpcService; import java.util.ArrayList; import java.util.List; @GrpcService public class GrpcServerService extends OrderQueryGrpc.OrderQueryImplBase { /** * mock一批数据 * @return */ private static List<Order> mockOrders(){ List<Order> list = new ArrayList<>(); Order.Builder builder = Order.newBuilder(); for (int i = 0; i < 10; i++) { list.add(builder .setOrderId(i) .setProductId(1000+i) .setOrderTime(System.currentTimeMillis()/1000) .setBuyerRemark(("remark-" + i)) .build()); } return list; } @Override public void listOrders(Buyer request, StreamObserver<Order> responseObserver) { // 持续输出到client for (Order order : mockOrders()) { responseObserver.onNext(order); } // 结束输出 responseObserver.onCompleted(); } }
plugins { id 'org.springframework.boot' } dependencies { implementation 'org.projectlombok:lombok' implementation 'org.springframework.boot:spring-boot-starter' implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'net.devh:grpc-client-spring-boot-starter' implementation project(':grpc-lib') }
server: port: 8081 spring: application: name: server-stream-client-side grpc: client: # gRPC配置的名字,GrpcClient注解会用到 server-stream-server-side: # gRPC服务端地址 address: 'static://127.0.0.1:9899' enableKeepAlive: true keepAliveWithoutCalls: true negotiationType: plaintext
package com.bolingcavalry.grpctutorials; import lombok.AllArgsConstructor; import lombok.Data; import java.io.Serializable; @Data @AllArgsConstructor public class DispOrder { private int orderId; private int productId; private String orderTime; private String buyerRemark; }
package com.bolingcavalry.grpctutorials; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ServerStreamClientSideApplication { public static void main(String[] args) { SpringApplication.run(ServerStreamClientSideApplication.class, args); } }
package com.bolingcavalry.grpctutorials; import com.bolingcavalry.grpctutorials.lib.Buyer; import com.bolingcavalry.grpctutorials.lib.Order; import com.bolingcavalry.grpctutorials.lib.OrderQueryGrpc; import io.grpc.StatusRuntimeException; import lombok.extern.slf4j.Slf4j; import net.devh.boot.grpc.client.inject.GrpcClient; import org.springframework.stereotype.Service; import java.time.Instant; import java.time.LocalDateTime; import java.time.ZoneOffset; import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Iterator; import java.util.List; @Service @Slf4j public class GrpcClientService { @GrpcClient("server-stream-server-side") private OrderQueryGrpc.OrderQueryBlockingStub orderQueryBlockingStub; public List<DispOrder> listOrders(final String name) { // gRPC的请求参数 Buyer buyer = Buyer.newBuilder().setBuyerId(101).build(); // gRPC的响应 Iterator<Order> orderIterator; // 当前方法的返回值 List<DispOrder> orders = new ArrayList<>(); // 通过stub发起远程gRPC请求 try { orderIterator = orderQueryBlockingStub.listOrders(buyer); } catch (final StatusRuntimeException e) { log.error("error grpc invoke", e); return new ArrayList<>(); } DateTimeFormatter dtf = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"); log.info("start put order to list"); while (orderIterator.hasNext()) { Order order = orderIterator.next(); orders.add(new DispOrder(order.getOrderId(), order.getProductId(), // 使用DateTimeFormatter将时间戳转为字符串 dtf.format(LocalDateTime.ofEpochSecond(order.getOrderTime(), 0, ZoneOffset.of("+8"))), order.getBuyerRemark())); log.info(""); } log.info("end put order to list"); return orders; } }
package com.bolingcavalry.grpctutorials; import com.bolingcavalry.grpctutorials.lib.Order; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import java.util.List; @RestController public class GrpcClientController { @Autowired private GrpcClientService grpcClientService; @RequestMapping("/") public List<DispOrder> printMessage(@RequestParam(defaultValue = "will") String name) { return grpcClientService.listOrders(name); } }
至此,服务端流类型的gRPC接口的开发和使用实战就完成了,接下来的章节还会继续学习另外两种类型;
微信搜索「程序员欣宸」,我是欣宸,期待与您一同畅游Java世界…
https://github.com/zq2599/blog_demos