Dart-gPRC

  1. 创建项目并运行
    1. 编译成正式成品
  2. 安装dart-server环境
  3. 修改代码重新运行
  4. 实现gRPC
  5. 创建web应用
  6. demo

创建项目并运行

1
2
3
dart create -t console-full dart_cli_sample
cd dart_cli_sample
dart run

编译成正式成品

1
dart compile exe bin/dart_cli_sample.dart

安装dart-server环境

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
brew install dart
flutter
brew install protobuf
protoc --version
# Dart plugin for the protocol compiler
pub global activate protoc_plugin
# 此处建议将其加到环境变量,我的在.bash_profile写着
export PATH="$PATH:$HOME/.pub-cache/bin"
# 获取grpc-sample
git clone https://github.com/grpc/grpc-dart
cd grpc-dart/example/helloworld
pub get
dart bin/server.dart
# 在另一个终端运行
dart bin/client.dart

修改代码重新运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# nano protos/helloworld.proto
// The greeting service definition.
service Greeter {
// Sends a greeting
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends another greeting
// 定义方法
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}

// The response message containing the greetings
message HelloReply {
string message = 1;
}

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

# nano bin/server.dart
class GreeterService extends GreeterServiceBase {
@override
Future<HelloReply> sayHello(ServiceCall call, HelloRequest request) async {
return HelloReply()..message = 'Hello, ${request.name}!';
}

@override
Future<HelloReply> sayHelloAgain(ServiceCall call, HelloRequest request) async {
return HelloReply()..message = 'Hello again, ${request.name}!';
}
}

# nano bin/client.dart
Future<void> main(List<String> args) async {
final channel = ClientChannel(
'localhost',
port: 50051,
options: const ChannelOptions(credentials: ChannelCredentials.insecure()),
);
final stub = GreeterClient(channel);

final name = args.isNotEmpty ? args[0] : 'world';

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

dart bin/server.dart
# 在另一个终端运行
dart bin/client.dart Alice

实现gRPC

1
2
3
4
5
cd grpc-dart/example/route_guide
protoc -I protos/ protos/route_guide.proto --dart_out=grpc:lib/src/generated
dart /bin/server.dart
# 在另一个终端运行
dart /bin/client.dart

创建web应用

1
2
3
4
5
6
7
8
mkdir dart-web
cd dart-web
stagehand web-simple
pub get
# 启动app
webdev serve
# 编译app
webdev build

demo


转载请注明来源,欢迎对文章中的引用来源进行考证,欢迎指出任何有错误或不够清晰的表达。可以在下面评论区评论,也可以邮件至 nathanwriting@126.com

文章标题:Dart-gPRC

字数:408

本文作者:Nathaniel

发布时间:2020-11-19, 15:28:59

最后更新:2023-11-06, 22:59:18

原始链接:http://example.com/2020/11/19/dart-gRPC/

版权声明: "署名-非商用-相同方式共享 4.0" 转载请保留原文链接及作者。

×

喜欢就点赞,疼爱就打赏