当前位置: 首页 > news >正文

网站维护工作的基本内容google免费入口

网站维护工作的基本内容,google免费入口,微擎如何做网站,wordpress点击退出图片SpringBoot基于gRPC进行RPC调用 一、gRPC1.1 什么是gRPC?1.2 如何编写proto1.3 数据类型及对应关系1.4 枚举1.5 数组1.6 map类型1.7 嵌套对象 二、SpringBoot gRPC2.1 工程目录2.2 jrpc-api2.2.1 引入gRPC依赖2.2.2 编写 .proto 文件2.2.3 使用插件机制生产proto相关…

SpringBoot基于gRPC进行RPC调用

  • 一、gRPC
    • 1.1 什么是gRPC?
    • 1.2 如何编写proto
    • 1.3 数据类型及对应关系
    • 1.4 枚举
    • 1.5 数组
    • 1.6 map类型
    • 1.7 嵌套对象
  • 二、SpringBoot gRPC
    • 2.1 工程目录
    • 2.2 jrpc-api
      • 2.2.1 引入gRPC依赖
      • 2.2.2 编写 .proto 文件
      • 2.2.3 使用插件机制生产proto相关文件
    • 2.2 jrpc-server
      • 2.2.1 引入 `jrpc-api` 依赖
      • 2.2.2 编写impl
      • 2.2.3 编写Config
      • 2.2.4 yaml
    • 2.3 jrpc-client
      • 2.3.1 引入 `jrpc-api` 依赖
      • 2.3.2 编写config
      • 2.3.3 yaml
      • 2.3.4 测试验证

一、gRPC

1.1 什么是gRPC?

In gRPC, a client application can directly call a method on a server application on a different machine as if it were a local object, making it easier for you to create distributed applications and services. As in many RPC systems, gRPC is based around the idea of defining a service, specifying the methods that can be called remotely with their parameters and return types. On the server side, the server implements this interface and runs a gRPC server to handle client calls. On the client side, the client has a stub (referred to as just a client in some languages) that provides the same methods as the server.

在 gRPC 中,客户端应用程序可以直接调用服务器应用程序上的方法 在另一台机器上,就好像它是本地对象一样,使你更容易 创建分布式应用程序和服务。与许多 RPC 系统一样,gRPC 是 基于定义服务的思想,指定可以 使用其参数和返回类型进行远程调用。在服务器端, server 实现此接口并运行 gRPC 服务器来处理客户端调用。 在客户端,客户端有一个存根(在某些客户端中称为客户端 languages),它提供与服务器相同的方法。
在这里插入图片描述

gRPC 客户端和服务器可以在各种 环境 - 从 Google 内部的服务器到您自己的桌面 - 并且可以 用 gRPC 支持的任何语言编写。因此,例如,您可以轻松地 在 Java 中创建一个 gRPC 服务器,客户端使用 Go、Python 或 Ruby。另外 最新的 Google API 将具有其接口的 gRPC 版本,让您 轻松将 Google 功能构建到您的应用程序中。

gRPC 使用 proto buffers 作为服务定义语言,编写 proto 文件,即可完成服务的定义

在这里插入图片描述

1.2 如何编写proto

syntax = "proto3";option java_multiple_files = true;
// 生成位置
option java_package = "com.lizq.jrpc.api";
option java_outer_classname = "UserService";package user;service User {rpc SayHello (UserRequest) returns (UserResponse) {}
}message UserRequest {string name = 1;int32 age = 2;string addr = 3;
}message UserResponse {string name = 1;int32 age = 2;string addr = 3;OtherMsg otherMsg = 4;map<string, string> otherMap = 5;// 嵌套对象message OtherMsg {string ext1 = 1;string ext2 = 2;}
}
  • syntax = "proto3";:指定使用的protobuf版本;
  • option java_multiple_files = true;:如果为 false,则只会.java为此文件生成一个.proto文件,以及所有 Java 类/枚举/等。为顶级消息、服务和枚举生成的将嵌套在外部类中。如果为 true,.java将为每个 Java 类/枚举/等生成单独的文件。为顶级消息、服务和枚举生成,并且为此.proto文件生成的包装 Java 类将不包含任何嵌套类/枚举/等。 如果不生成 Java 代码,则此选项无效。
  • package user;:定义本服务的包名,避免不同服务相同消息类型产生冲突;
  • option java_package = "com.lizq.jrpc.api";:生成java文件包名;
  • option java_outer_classname = "UserService";:生成java文件类名称。如果文件中没有明确 java_outer_classname指定,.proto则将通过将.proto文件名转换为驼峰式来构造类名(因此 foo_bar.proto变为FooBar.java)
  • message UserResponse:定义服务的接口名称;
  • rpc SayHello (UserRequest) returns (UserResponse) {}:远程调用方法名,参数及响应类型;
  • message XXXXX{}:定义数据类型;

1.3 数据类型及对应关系

.proto类型NotesC++ TypeJava/KotlinPython
doubledoubledoublefloat
floatfloatfloatfloat
int32使用可变长度编码。对负数进行编码效率低下——如果您的字段可能有负值,请改用 sint32。int32intint
int64使用可变长度编码。对负数进行编码效率低下——如果您的字段可能有负值,请改用 sint64。int64longint/long
uint32使用可变长度编码。uint32intint/long
uint64使用可变长度编码。uint64longint/long
sint32使用可变长度编码。带符号的 int 值。这些比常规 int32 更有效地编码负数。int32intint
sint64使用可变长度编码。带符号的 int 值。这些比常规 int64 更有效地编码负数。int64longint/long
fixed32总是四个字节。如果值通常大于 228,则比 uint32 更有效uint32intint/long
fixed64总是八个字节。如果值通常大于 256,则比 uint64 更有效。uint64longint/long
sfixed32总是四个字节。int32intint
sfixed64总是八个字节。int64longint/long
boolboolbooleanbool
string字符串必须始终包含 UTF-8 编码或 7 位 ASCII 文本,并且不能超过 232。stringStringstr/unicode
bytes可能包含不超过 2 32的任意字节序列。stringByteStringstr (Python 2)、bytes (Python 3)

1.4 枚举

enum Sex {NONE = 0;MAN = 1;WOMAN = 2;
}message UserRequest {string name = 1;int32 age = 2;string addr = 3;Sex sex = 4;
}

**注意:**第一个枚举的值必须为0,因为0 是默认值,0 必须是第一个,保持和proto2 兼容

1.5 数组

使用 repeated 关键字来定义数组。

message UserRequest {string name = 1;int32 age = 2;string addr = 3;Sex sex = 4;// 定义一个数组repeated string cellphones = 5;
}

1.6 map类型

在开发的过程中经常需要使用关联字段,很自然的想到使用map,protobuf也提供了map的类型。

message UserResponse {string name = 1;map<string, string> otherMap = 2;
}

注意: map 字段前面不能是repeated

1.7 嵌套对象

message UserResponse {string name = 1;int32 age = 2;string addr = 3;OtherMsg otherMsg = 4;map<string, string> otherMap = 5;// 嵌套对象message OtherMsg {string ext1 = 1;string ext2 = 2;}
}

二、SpringBoot gRPC

2.1 工程目录

在这里插入图片描述

2.2 jrpc-api

2.2.1 引入gRPC依赖

<dependency><groupId>io.grpc</groupId><artifactId>grpc-all</artifactId><version>1.28.1</version>
</dependency>

2.2.2 编写 .proto 文件

syntax = "proto3";option java_multiple_files = true;
// 生成位置
option java_package = "com.lizq.jrpc.api";
option java_outer_classname = "UserService";package user;service User {rpc SayHello (UserRequest) returns (UserResponse) {}
}message UserRequest {string name = 1;int32 age = 2;string addr = 3;
}message UserResponse {string name = 1;int32 age = 2;string addr = 3;OtherMsg otherMsg = 4;map<string, string> otherMap = 5;// 嵌套对象message OtherMsg {string ext1 = 1;string ext2 = 2;}
}

2.2.3 使用插件机制生产proto相关文件

在 jrpc-api pom.xml 中添加如下:

<build><extensions><extension><groupId>kr.motd.maven</groupId><artifactId>os-maven-plugin</artifactId><version>1.6.2</version></extension></extensions><plugins><plugin><groupId>org.xolstice.maven.plugins</groupId><artifactId>protobuf-maven-plugin</artifactId><version>0.6.1</version><configuration><!--&lt;!&ndash; ${os.detected.classifier} 变量由${os.detected.name} 和 ${os.detected.arch} 组成--><protocArtifact>com.google.protobuf:protoc:3.12.0:exe:${os.detected.classifier}</protocArtifact><pluginId>grpc-java</pluginId><pluginArtifact>io.grpc:protoc-gen-grpc-java:1.28.1:exe:${os.detected.classifier}</pluginArtifact><!--protoSourceRoot 默认src/main/proto--><protoSourceRoot>src/main/proto</protoSourceRoot></configuration><executions><execution><goals><goal>compile</goal><goal>compile-custom</goal></goals></execution></executions></plugin></plugins>
</build>

执行命令生产proto相关文件

在这里插入图片描述
将生成的文件拷贝到工程中,如下:

在这里插入图片描述

2.2 jrpc-server

jrpc-server 为 springboot 项目。

2.2.1 引入 jrpc-api 依赖

<dependency><groupId>com.example</groupId><artifactId>jrpc-api</artifactId><version>1.0.0-SNAPSHOT</version>
</dependency>

2.2.2 编写impl

@Service
public class UserServiceImpl extends UserGrpc.UserImplBase {@Overridepublic void sayHello(UserRequest request, StreamObserver<UserResponse> responseObserver) {Map<String, String> otherMap = new HashMap<>();otherMap.put("test", "testmap");UserResponse response = UserResponse.newBuilder().setName("server:" + request.getName()).setAddr("server:" + request.getAddr()).setAge(request.getAge()).setOtherMsg(UserResponse.OtherMsg.newBuilder().setExt1("ext1").setExt2("ext2").build()).putAllOtherMap(otherMap).build();responseObserver.onNext(response);responseObserver.onCompleted();}
}

2.2.3 编写Config

@Configuration
public class GrpcServerConfiguration {@Value("${grpc.server-port}")private int port;@Beanpublic Server server() throws Exception {System.out.println("Starting gRPC on port {}." + port);// 构建服务端ServerBuilder<?> serverBuilder = ServerBuilder.forPort(port);// 添加需要暴露的接口this.addService(serverBuilder);// startServer server = serverBuilder.build().start();System.out.println("gRPC server started, listening on {}." + port);// 添加服务端关闭的逻辑Runtime.getRuntime().addShutdownHook(new Thread(() -> {System.out.println("Shutting down gRPC server.");if (server != null) {// 关闭服务端server.shutdown();}System.out.println("gRPC server shut down successfully.");}));if (server != null) {// 服务端启动后直到应用关闭都处于阻塞状态,方便接收请求server.awaitTermination();}return server;}@Autowiredprivate UserServiceImpl userService;/*** 添加需要暴露的接口* @param serverBuilder*/private void addService(ServerBuilder<?> serverBuilder) {serverBuilder.addService(userService);}
}

2.2.4 yaml

server:port: 8081
spring:application:name: spring-boot-jrpc-server
grpc:server-port: 18081

2.3 jrpc-client

2.3.1 引入 jrpc-api 依赖

<dependency><groupId>com.example</groupId><artifactId>jrpc-api</artifactId><version>1.0.0-SNAPSHOT</version>
</dependency>

2.3.2 编写config

@Configuration
public class GrpcClientConfiguration {@Value("${server-host}")private String host;/*** gRPC Server的端口*/@Value("${server-port}")private int port;@Beanpublic ManagedChannel managedChannel() {// 开启gRPC客户端ManagedChannel managedChannel = ManagedChannelBuilder.forAddress(host, port).usePlaintext().build();System.out.println("gRPC client started, server address: " + host + " , " + port);// 添加客户端关闭的逻辑Runtime.getRuntime().addShutdownHook(new Thread(() -> {try {// 调用shutdown方法后等待1秒关闭channelmanagedChannel.shutdown().awaitTermination(1, TimeUnit.SECONDS);System.out.println("gRPC client shut down successfully.");} catch (InterruptedException e) {e.printStackTrace();}}));return managedChannel;}@Autowiredprivate ManagedChannel managedChannel;@Beanpublic UserGrpc.UserBlockingStub userBlockingStub(ManagedChannel channel) {// 通过channel获取到服务端的stubreturn UserGrpc.newBlockingStub(managedChannel);}
}

2.3.3 yaml

server:port: 8080
spring:application:name: spring-boot-jrpc-client# 本地测试
server-host: 127.0.0.1
server-port: 18081

2.3.4 测试验证

@RestController("/user")
public class UserController {@Autowiredprivate UserGrpc.UserBlockingStub userBlockingStub;@GetMapping("/sayHello")public String sayHello(String name, String addr, int age) {UserRequest request = UserRequest.newBuilder().setName(name).setAddr(addr).setAge(age).build();UserResponse response;try {response = userBlockingStub.sayHello(request);} catch (StatusRuntimeException e) {e.printStackTrace();return e.getMessage();}return response.toString();}
}

浏览器访问:http://localhost:8080/user/sayHello?name=test&addr=addr&age=99 返回:

name: "server:test" age: 99 addr: "server:addr" otherMsg { ext1: "ext1" ext2: "ext2" } otherMap { key: "test" value: "testmap" }

文章转载自:
http://pomposity.rgxf.cn
http://sodium.rgxf.cn
http://firearms.rgxf.cn
http://gasman.rgxf.cn
http://viviparity.rgxf.cn
http://upheld.rgxf.cn
http://sopite.rgxf.cn
http://faunus.rgxf.cn
http://flush.rgxf.cn
http://heliskiing.rgxf.cn
http://belvedere.rgxf.cn
http://fairway.rgxf.cn
http://brickkiln.rgxf.cn
http://cymoscope.rgxf.cn
http://haneda.rgxf.cn
http://riouw.rgxf.cn
http://tranquility.rgxf.cn
http://howdy.rgxf.cn
http://everyhow.rgxf.cn
http://aesthete.rgxf.cn
http://devastator.rgxf.cn
http://apiece.rgxf.cn
http://trepidant.rgxf.cn
http://saccharoidal.rgxf.cn
http://pandarus.rgxf.cn
http://depreciable.rgxf.cn
http://grantsman.rgxf.cn
http://flamingo.rgxf.cn
http://falcula.rgxf.cn
http://ural.rgxf.cn
http://xanthine.rgxf.cn
http://vis.rgxf.cn
http://solarium.rgxf.cn
http://valuation.rgxf.cn
http://checkerman.rgxf.cn
http://marked.rgxf.cn
http://gesticulate.rgxf.cn
http://trackwalker.rgxf.cn
http://zootheism.rgxf.cn
http://oscillograph.rgxf.cn
http://ultramilitant.rgxf.cn
http://orthowater.rgxf.cn
http://tympanal.rgxf.cn
http://iridous.rgxf.cn
http://unido.rgxf.cn
http://fiddlestick.rgxf.cn
http://hyperaphia.rgxf.cn
http://duro.rgxf.cn
http://enrobe.rgxf.cn
http://etesian.rgxf.cn
http://uptown.rgxf.cn
http://lasher.rgxf.cn
http://murther.rgxf.cn
http://debrett.rgxf.cn
http://jagged.rgxf.cn
http://pansy.rgxf.cn
http://physicist.rgxf.cn
http://purpose.rgxf.cn
http://jeth.rgxf.cn
http://thermidorean.rgxf.cn
http://parosmia.rgxf.cn
http://optional.rgxf.cn
http://fuse.rgxf.cn
http://marjoram.rgxf.cn
http://talea.rgxf.cn
http://megabar.rgxf.cn
http://threepenny.rgxf.cn
http://donizettian.rgxf.cn
http://atrophy.rgxf.cn
http://prefabricate.rgxf.cn
http://uprisen.rgxf.cn
http://astigmometer.rgxf.cn
http://cavitation.rgxf.cn
http://frustulum.rgxf.cn
http://pillow.rgxf.cn
http://pietermaritzburg.rgxf.cn
http://freon.rgxf.cn
http://chubasco.rgxf.cn
http://neoarsphenamine.rgxf.cn
http://nundine.rgxf.cn
http://recreative.rgxf.cn
http://geogonic.rgxf.cn
http://ussr.rgxf.cn
http://denotatum.rgxf.cn
http://karpinskyite.rgxf.cn
http://gemeinschaft.rgxf.cn
http://zygoid.rgxf.cn
http://proteinous.rgxf.cn
http://genocidal.rgxf.cn
http://delusive.rgxf.cn
http://colligable.rgxf.cn
http://faintish.rgxf.cn
http://bingy.rgxf.cn
http://hosting.rgxf.cn
http://photodisintegration.rgxf.cn
http://cadaver.rgxf.cn
http://armoured.rgxf.cn
http://turboprop.rgxf.cn
http://swigger.rgxf.cn
http://kmps.rgxf.cn
http://www.dt0577.cn/news/115634.html

相关文章:

  • 网站建设com合肥做网站的公司有哪些
  • 网页搜索关键词seo网站诊断价格
  • 网上接单做网站微信指数怎么看
  • 大型 网站 建设 公司许昌正规网站优化公司
  • 南开大学 网站开发技术 刘冲关键词优化的策略有哪些
  • 网站检测器临沂色度广告有限公司
  • 网站建设中代码seo全网营销
  • 网站中全景是怎么做的高报师培训机构排名
  • 关于公司网站的建设的问卷百度快照怎么没有了
  • 网站建设系统全球网站排名前100
  • 手机wap网站如何建设千万不要去电商公司上班
  • 网站建好了 如何推广网站推广途径
  • 廊坊手机网站网站推广优化c重庆
  • 专用车网站建设哪家专业网络营销策略的内容
  • 黄山网站建设免费咨询线下推广方案
  • 江苏省苏州市相城区最新疫情需要优化的网站有哪些
  • wordpress怎么修改关键字标题优化方法
  • wordpress的文章写好后无法访问自己怎么优化网站
  • wordpress主题赚钱湖南优化公司
  • 怎样做 网站的快捷链接百度问答兼职怎么做
  • 网站的风格分析宁波专业seo服务
  • 做动图的网站免费推广网站2023mmm
  • 卢松松的网站seo网络推广方法
  • 做网站爬闪网站建站系统
  • 一级a做爰片免费网站 新闻现在百度推广有用吗
  • 集团微信网站方案策划直播:韩国vs加纳直播
  • 做网站什么费用百度seo关键词排名技术
  • 做公司网站的公司市场营销策划方案案例
  • mp3链接地址制作网站seo优化搜索结果
  • 做网站难不难搜索引擎收录查询工具