Dart : 关于 gRPC(一)

写在前面

最近了解到了 gRPC 的相关内容, 这一篇就写在用法和概念上的相关东西,以 Dart 语言来做代码示例。

内容

从一个示例开始

配置环境

  1. 安装 Dart SDK 或是 Flutter SDK,然后配置它们的环境变量
  2. 安装 protobuf
  • brew 安装
    • brew install protobuf
  • 压缩包安装
    • 有遇到过上面的 brew 安装失败,于是可以直接去官方 GitHub,下载对应的压缩包,如我是 mac,就下载 osx-x86_64.zip。然后解压该压缩包,将 bin 路径写入到 PATH 即可。
  1. 安装protoc_plugin
    • pub global activate protoc_plugin
    • 在 bash_profile (M1的话应该是 zshrc里)下添加export PATH="$PATH":"$HOME/.pub-cache/bin"
  2. 我使用的是 IDEA 进行开发,可以安装一个叫 Protocol Buffer Editor 的插件,来帮助我们更好地编辑 .proto 文件。

Demo

让我们新建一个 Dart 工程,在其pusepc.yaml里添加依赖:

dependencies:
  protobuf: ^2.0.0
  grpc: ^3.0.0

然后我们就可以开始了。

在 lib 文件夹下创建 protos (名称随意)文件夹,然后创建helloworld.proto用于写我们的代码:

// helloworld.proto
syntax = "proto3";

package helloworld;

service Greeter{
  rpc SayHello(HelloRequest) returns (HelloReply){}
}

message HelloRequest{
  string name = 1;
}

message HelloReply{
  string message = 1;
}

开头我们声明了使用的是 proto3 版本,然后定义了Greeter服务,并声明了一个 rpc 接口SayHello,请求的参数是HelloRequest,返回的参数是HelloReply

在 lib 目录下,我们新建一个 src/generated 文件夹,用于存放我们待会要生成的文件。

然后命令行进入我们的 lib 目录下,执行命令:

protoc --dart_out=grpc:src/generated -Iprotos protos/helloworld.proto

这样 generated 文件夹下就会生成相应的文件:

helloworld.pbjson.dart
helloworld.pbgrpc.dart
helloworld.pbenum.dart
helloworld.pb.dart

同样地在 lib 目录下我们创建 src/bin 目录来写我们的客户端和服务端。

客户端:

// client.dart
import 'package:grpc/grpc.dart';
import 'package:grpc_learning/src/generated/helloworld.pbgrpc.dart';

void main() async {
  final channel = ClientChannel(
    'localhost',
    port: 50051,
    options: ChannelOptions(
      credentials: ChannelCredentials.insecure(),
      codecRegistry:
          CodecRegistry(codecs: const [GzipCodec(), IdentityCodec()]),
    ),
  );
  final stub = GreeterClient(channel);

  final name = 'world';

  try {
    final response = await stub.sayHello(HelloRequest()..name = name);
    print('Greeter client received: ${response.message}');
  } catch (e) {
    print('error  = ${e.toString()}');
  }
  await channel.shutdown();
}

服务端:

// server.dart
import 'package:grpc/grpc.dart';
import 'package:grpc/src/server/call.dart';
import 'package:grpc_learning/src/generated/helloworld.pbgrpc.dart';

class GreeterService extends GreeterServiceBase {
  @override
  Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) async {
    return HelloReply()..message = 'I got it ${request.name}';
  }
}

void main() async {
  final server = Server([GreeterService()]);
  await server.serve(port: 50051);
  print('Server listening on port ${server.port}...');
}

接下来我们就可以开启两个终端,来分别运行客户端和服务端了,使用dart server.dartdart client.dart来执行。
执行结果:

// 服务端
Server listening on port 50051...

// 客户端
Greeter client received: I got it world

这样就完成了一个 gRPC 的客户端与服务端通信的 Demo。

参考

Dart : Quick start

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值