当前位置:网站首页>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
边栏推荐
- Laravel time zone timezone
- Unity3D学习笔记5——创建子Mesh
- MySQL uses the method of updating linked tables with update
- Symlink(): solution to protocol error in PHP artisan storage:link on win10
- PHP导出word方法(一phpword)
- Sheet1$. Output [excel source output] Error in column [xxx]. The returned column status is: "the text is truncated, or one or more characters have no matches in the target code page.".
- [MySQL special] read lock and write lock
- Mysql根据时间搜索常用方法整理
- Hongmeng fourth training
- Momentum of vulnhub
猜你喜欢

Raven2 of vulnhub

Vulnhub's cereal

Why can't my MySQL container start

同事写了一个责任链模式,bug无数...
![[learning notes] DP status and transfer](/img/5e/59c64d2fe08b89fba2d7e1e6de2761.png)
[learning notes] DP status and transfer

PHP Basics

MCDF Experiment 1

Groovy test class and JUnit test

win10 上PHP artisan storage:link 出现 symlink (): Protocol error的解决办法

After watching the video, AI model learned to play my world: cutting trees, making boxes, making stone picks, everything is good
随机推荐
网络通讯之Socket-Tcp(一)
并发编程-单例
DNS multi-point deployment IP anycast+bgp actual combat analysis
STL教程8-map
PHP導出word方法(一mht)
vulnhub之raven2
[official MySQL document] deadlock
Unity3D学习笔记5——创建子Mesh
DEJA_VU3D - Cesium功能集 之 053-地下模式效果
DEJA_VU3D - Cesium功能集 之 054-模拟火箭发射全过程
PHP导出word方法(一phpword)
Hongmeng fourth training
Ripper of vulnhub
Qt OpenGL相机的使用
Why can't my MySQL container start
剑指offer专项32-96题做题笔记
(数据库提权——Redis)Redis未授权访问漏洞总结
Qt OpenGL 旋转、平移、缩放
OpenGL 索引缓存对象EBO和线宽模式
ftp登录时,报错“530 Login incorrect.Login failed”