当前位置:网站首页>Grpc message sending of vertx
Grpc message sending of vertx
2022-07-05 01:46:00 【Xiong Da believed what Xiong er said】
Introduce
Vert.x gRPC modular , It will Google gRPC Programming style and Vert.x Consistent style . gRPC It's a high performance 、 Open source 、 General purpose RPC frame , It is an interprocess communication technology , from Google Introduction , be based on HTTP2 Protocol standard design and development , By default Protocol Buffers Data serialization protocol , Support multiple development languages .gRPC Provides a simple way to accurately define Services , And automatically generate a reliable function library for the client and server . stay gRPC The client can directly call remote programs on different servers , Using gestures looks like calling a local program , Easily realize the connection of remote services 、 call 、 Operation and commissioning , It's easy to build distributed applications and services . And a lot of RPC The system is the same , The server is responsible for implementing the defined interface and processing the client's request , The client directly calls the required services according to the interface description . The client and server can use... Respectively gRPC Different language implementations supported .
1. maven Project dependence
<dependencies> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-core</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-grpc</artifactId> </dependency> <dependency> <groupId>io.vertx</groupId> <artifactId>vertx-config-yaml</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <dependency> <groupId>com.lance.common</groupId> <artifactId>vertx-common-core</artifactId> <version>0.0.1-SNAPSHOT</version> </dependency></dependencies>2.vertx Start public class
public class StartApplication { public static void runClustered(Class<? extends AbstractVerticle> clazz) { run(clazz, new VertxOptions(), null, true); } public static void run(Class<? extends AbstractVerticle> clazz) { run(clazz, new VertxOptions(), null, false); } public static void run(Class<? extends AbstractVerticle> clazz, DeploymentOptions options) { run(clazz, new VertxOptions(), options, false); } public static void run(Class<? extends AbstractVerticle> clazz, VertxOptions options, DeploymentOptions deploymentOptions, boolean clustered) { run(clazz.getName(), options, deploymentOptions, clustered); } public static void run(String className, VertxOptions options, DeploymentOptions deploymentOptions, boolean clustered) { if (options == null) { // Default parameter options = new VertxOptions(); } Consumer<Vertx> runner = vertx -> { try { if (deploymentOptions != null) { vertx.deployVerticle(className, deploymentOptions); } else { vertx.deployVerticle(className); } } catch (Throwable t) { log.error("Vertx deploy fail: ", t); } }; if (clustered) { Vertx.clusteredVertx(options, res -> { if (res.succeeded()) { Vertx vertx = res.result(); runner.accept(vertx); } else { res.cause().printStackTrace(); } }); } else { Vertx vertx = Vertx.vertx(options); runner.accept(vertx); } }}3.Grpc Server side
public class HelloServer extends AbstractVerticle { public static void main(String[] args) { StartApplication.run(HelloServer.class); } @Override public void start() { // Create the server VertxServer server = VertxServerBuilder.forAddress(vertx, RpcConst.HOST, RpcConst.PORT) .addService(new VertxGreeterGrpc.GreeterVertxImplBase() { @Override public Future<Hello.HelloReply> sayHello(Hello.HelloRequest request) { log.info("Hello {}", request.getName()); String message = "Thanks " + request.getName(); return Future.succeededFuture(Hello.HelloReply.newBuilder().setMessage(message).build()); } }) .build(); // start the server server.start(ar -> { if (ar.failed()) { log.error("Grpc server start fail: ", ar.cause()); } else { log.info("Grpc server[port: {}] start success.", server.getPort()); } }); }}4.Grpc client
public class HelloClient extends AbstractVerticle { public static void main(String[] args) { StartApplication.run(HelloClient.class); } @Override public void start() { // Create the channel ManagedChannel channel = VertxChannelBuilder .forAddress(vertx, RpcConst.HOST, RpcConst.PORT) .usePlaintext() .build(); VertxGreeterGrpc.GreeterVertxStub stub = VertxGreeterGrpc.newVertxStub(channel); Hello.HelloRequest request = Hello.HelloRequest.newBuilder().setName("Jim Green").build(); stub.sayHello(request).onComplete(asyncResponse -> { if (asyncResponse.succeeded()) { log.info("Got the server response: {}", asyncResponse.result().getMessage()); } else { log.error("Could not reach server fail: ", asyncResponse.cause()); } }); }}5.Proto file
syntax = "proto3";option java_multiple_files = false;option java_outer_classname = "Hello";package com.lance.grpc.hello.gen;// The greeting service definition.service Greeter { // Sends a greeting rpc SayHello (HelloRequest) returns (HelloReply) {}}// The request message containing the user's name.message HelloRequest { string name = 1;}// The response message containing the greetingsmessage HelloReply { string message = 1;}6. journal
2022-02-13 20:22:19.301 INFO 21 --- [ntloop-thread-1] com.lance.grpc.hello.HelloClient ---[ 38] : Got the server response: Thanks .Jim Green2022-02-13 20:21:42.852 INFO 21 --- [ntloop-thread-1] com.lance.grpc.hello.HelloServer ---[ 45] : Grpc server[port: 18006] start success.2022-02-13 20:22:19.254 INFO 21 --- [ntloop-thread-1] com.lance.grpc.hello.HelloServer ---[ 33] : Hello Jim Green7. Full address of the project
边栏推荐
- Win:使用 PowerShell 检查无线信号的强弱
- es使用collapseBuilder去重和只返回某个字段
- 流批一體在京東的探索與實踐
- MATLB | multi micro grid and distributed energy trading
- When the industrial Internet era is truly developed and improved, it will witness the birth of giants in every scene
- MATLB|多微电网及分布式能源交易
- Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
- Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
- After reading the average code written by Microsoft God, I realized that I was still too young
- Database postragesq BSD authentication
猜你喜欢

Wechat applet: independent background with distribution function, Yuelao office blind box for making friends

How to safely eat apples on the edge of a cliff? Deepmind & openai gives the answer of 3D security reinforcement learning

Nebula Importer 数据导入实践

The application and Optimization Practice of redis in vivo push platform is transferred to the end of metadata by

runc hang 导致 Kubernetes 节点 NotReady

R language uses logistic regression and afrima, ARIMA time series models to predict world population

Async/await you can use it, but do you know how to deal with errors?

Mysql database | build master-slave instances of mysql-8.0 or above based on docker

PowerShell: use PowerShell behind the proxy server
![[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation](/img/9e/4c8557bb4b75b1e74598dedb24af86.jpg)
[Digital IC hand tearing code] Verilog edge detection circuit (rising edge, falling edge, double edge) | topic | principle | design | simulation
随机推荐
220213c language learning diary
Restful Fast Request 2022.2.1发布,支持cURL导入
Express routing, express middleware, using express write interface
MySQL REGEXP:正则表达式查询
Exploration and Practice of Stream Batch Integration in JD
Talk about the things that must be paid attention to when interviewing programmers
Behind the cluster listing, to what extent is the Chinese restaurant chain "rolled"?
[swagger]-swagger learning
What sparks can applet container technology collide with IOT
Yyds dry inventory jetpack hit dependency injection framework Getting Started Guide
STM32 series - serial port UART software pin internal pull-up or external resistance pull-up - cause problem search
Wechat applet: the latest WordPress black gold wallpaper wechat applet two open repair version source code download support traffic main revenue
Interpretation of mask RCNN paper
Database postragesql lock management
Redis(1)之Redis简介
Valentine's Day flirting with girls to force a small way, one can learn
PHP Basics - detailed explanation of DES encryption and decryption in PHP
Yyds dry goods inventory kubernetes management business configuration methods? (08)
Remote control service
Wechat applet: Xingxiu UI v1.5 WordPress system information resources blog download applet wechat QQ dual end source code support WordPress secondary classification loading animation optimization