当前位置:网站首页>Dart: about grpc (I)
Dart: about grpc (I)
2022-07-03 12:01:00 【J_ D_ Chi】
List of articles
Write it at the front
Recently I learned that gRPC Related content of , This article is about usage and concepts , With Dart Language as a code example .
Content
Start with an example
Configuration environment
- install Dart SDK or Flutter SDK, Then configure their environment variables
- install protobuf
brew install protobuf
- install protoc_plugin
pub global activate protoc_plugin- stay bash_profile (M1 It should be zshrc in ) Add below
export PATH="$PATH":"$HOME/.pub-cache/bin"
- I'm using IDEA Development , You can install one called Protocol Buffer Editor Plug in for , To help us edit better .proto file .
Demo
Let's build a new Dart engineering , In its pusepc.yaml Add dependency :
dependencies:
protobuf: ^2.0.0
grpc: ^3.0.0
Then we can start .
stay lib Create under folder protos ( Name at will ) Folder , Then create helloworld.proto Used to write our code :
// helloworld.proto
syntax = "proto3";
package helloworld;
service Greeter{
rpc SayHello(HelloRequest) returns (HelloReply){}
}
message HelloRequest{
string name = 1;
}
message HelloReply{
string message = 1;
}
At the beginning, we declared that proto3 edition , And then defined Greeter service , And declared a rpc Interface SayHello, The requested parameter is HelloRequest, The returned parameter is HelloReply.
stay lib Under the table of contents , We build a new one src/generated Folder , It is used to store the files we will generate later .
Then the command line enters our lib Under the table of contents , Carry out orders :
protoc --dart_out=grpc:src/generated -Iprotos protos/helloworld.proto
such generated The corresponding file will be generated under the folder :
helloworld.pbjson.dart
helloworld.pbgrpc.dart
helloworld.pbenum.dart
helloworld.pb.dart
It's the same in lib Under the directory, we create src/bin Directory to write our client and server .
client :
// 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 side :
// 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}...');
}
Next, we can start two terminals , To run the client and server respectively , Use dart server.dart and dart client.dart To execute .
Execution results :
// Server side
Server listening on port 50051...
// client
Greeter client received: I got it world
This completes a gRPC The client communicates with the server Demo.
Reference resources
边栏推荐
- PHP导出word方法(一phpword)
- Is BigDecimal safe to calculate the amount? Look at these five pits~~
- Nestjs configuration service, configuring cookies and sessions
- Download address and installation tutorial of vs2015
- 抓包整理外篇fiddler———— 会话栏与过滤器[二]
- Why can't my MySQL container start
- [learning notes] DP status and transfer
- 为什么我的mysql容器启动不了呢
- Hongmeng fourth training
- Dynamically monitor disk i/o with ZABBIX
猜你喜欢

PHP export word method (one MHT)

Momentum of vulnhub

QT OpenGL rotate, pan, zoom

Vulnhub's presidential

Duplicate numbers in the array of sword finger offer 03

Socket TCP for network communication (I)

Symlink(): solution to protocol error in PHP artisan storage:link on win10

Unity3d learning notes 5 - create sub mesh

836. 合并集合(DAY 63)并查集

vulnhub之Ripper
随机推荐
STL教程10-容器共性和使用场景
OPenGL 基本知识(根据自己理解整理)
利用Zabbix动态监控磁盘I/O
typeScript
Xml的(DTD,xml解析,xml建模)
vulnhub之cereal
Go语言实现静态服务器
Visual Studio 2022下载及配置OpenCV4.5.5
《剑指offer 03》数组中重复的数字
(构造笔记)GRASP学习心得
Use of QT OpenGL camera
Extrapolated scatter data
安裝electron失敗的解决辦法
Is BigDecimal safe to calculate the amount? Look at these five pits~~
2022年湖南工学院ACM集训第二次周测题解
Interview experience in summer camp of Central South University in 2022
MySQL union和union all区别
vulnhub之pyexp
Optimize interface performance
Wrong arrangement (lottery, email)