欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

Netty实现一个简单的RPC框架

程序员文章站 2022-06-27 20:59:00
微服务微服务通讯API构建需要考虑的因素通讯协议文本协议或者二进制协议支持的调用方式:单向、双向、StreamingAPI定义与声明API容错、可伸缩性RPC框架RESThttp://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htmREST即Representational State Transfer的缩写,可译为“表现层状态转化”,是一种软件体系结构架构风格。REST最大的几个特点为:资源、统一接口...

微服务

Netty实现一个简单的RPC框架

微服务通讯
Netty实现一个简单的RPC框架

API构建需要考虑的因素

通讯协议

文本协议或者二进制协议

支持的调用方式:单向、双向、Streaming

API定义与声明

API容错、可伸缩性

RPC框架

Netty实现一个简单的RPC框架

REST

http://www.ics.uci.edu/~fielding/pubs/dissertation/rest_arch_style.htm

REST即Representational State Transfer的缩写,可译为“表现层状态转化”,是一种软件体系结构架构风格。REST最大的几个特点为:资源、统一接口、URI和无状态。

符合REST的系统具有下述特点:

Client-Server Architecture
Statelessness
Cacheability
Layered System
Code on Demand (Optional)
Uniform Interface

Restful API

HTTP + JSON

RESTFUL API

对于简单的服务, HTTP REST 能够满足要求

HTTP 谓词(GET、POST、PUT、DELETE 等) 能够满足调用需求
REST 语义清晰

对复杂服务系统,RESTful接口不足之处

性能无法满足海量并发请求的需求
    文本消息JSON
    HTTP协议

接口强类型的需求

RPC

远程过程调用(Remote Procedure Call,简称 RPC)是一个计算机通信协议。 该协议允许运行于一台计算机的程序调用另一台计算机的子程序,而程序员无需额外地为这个交互作用编程。 如果涉及的软件采用面向对象编程,那么远程过程调用亦可称作远程调用或远程方法调用,比如 Java RMI。

Netty实现一个简单的RPC框架

gRPC

Netty实现一个简单的RPC框架

gRPC发展历史

Google open sourced in Feb 2015

Transport Protocol: HTTP/2

Message format: Protocol Buffers v3 (Binary)

Service definition: Protocol Buffers v3 IDL

Libraries in ~10 languages (native C, C#, Go, Java)

RPC framework

gRPC背景

Google has an internal RPC system, called Stubby

    All production applications use RPCs
    Over 10^10 RPCs per second in total
    4 generations over 13 years (since 2003)
    APIs for C++, Java, Python, Go
    
What's missing

    Not suitable for external use (tight coupling with internal tools & infrastructure)
    Limited language & platform support
    Proprietary protocol and security
    No mobile support

gRPC特点

Google has an internal RPC system, called Stubby

    All production applications use RPCs
    Over 10^10 RPCs per second in total
    4 generations over 13 years (since 2003)
    APIs for C++, Java, Python, Go
    
What's missing

    Not suitable for external use (tight coupling with internal tools & infrastructure)
    Limited language & platform support
    Proprietary protocol and security
    No mobile support

Netty实现一个简单的RPC框架

Netty实现一个简单的RPC框架

Netty实现一个简单的RPC框架

Netty实现一个简单的RPC框架

Netty实现一个简单的RPC框架

gRPC原理

A high level service definition to describe the API using Protocol Buffers PB定义服务 – 自动生成代码

Client and server code generated from the service definition in 10+ languages

Efficiency in serialization with PB and connection with HTTP/2 HTTP/2 PB高性能

Connection options: Unary, server-side streaming, client-side Streaming, bi-direction-streaming

Authentication Options: SSL/TLS, token based authentication

Netty实现一个简单的RPC框架

Netty实现一个简单的RPC框架

HTTP 1.x: Limited Parallelism

每次HTTP请求一个TCP连接
HTTP请求的并发数 = TCP连接数
Keep-alive

Netty实现一个简单的RPC框架

Netty实现一个简单的RPC框架

HTTP/2历史

Released in 2015. Extend (not replace) the semantics of HTTP/1.1
Improve end-user perceived latency
Address the “head of line blocking”
Not require multiple connections
Minimize protocol overhead

HTTP/2主要特性

Binary Protocol – no more Text
Native Stream Support – no need for WebSocket
Stream Multiplexing – single connection
Header compression – saves more bandwidth

HTTP/2 Binary Framing

Stream is a bidirectional flow of bytes within an established connection, which may carry one or more messages.
Message is a complete sequence of frames that map to a logical request or response message.
Frame is the smallest unit of communication in HTTP/2, each containing a frame header, which at a minimum identifies the stream to which the frame belong

Netty实现一个简单的RPC框架

Multipexing多路复用

Netty实现一个简单的RPC框架

Interleave multiple requests and responses in parallel without blocking on any one
Use a single TCP connection to deliver multiple requests and responses in parallel.
Enable flow-control, server push, etc

HPACK: Header compression for HTTP/2

Client and server maintain and update an indexed list of previously seen header fields
Indexes are sent for already seen headers
Values are encoded with a static Huffman code

Netty实现一个简单的RPC框架

Protocol Buffers

Goole开源的,一个平台无关,语言无关,可扩展的序列化结构数据格式
定义了一种  IDL语言来描述数据格式
支持多种语言,可根据IDL自动生成多种语言的接口
二进制编码

Netty实现一个简单的RPC框架

Goole开源的,一个平台无关,语言无关,可扩展的序列化结构数据格式

定义了一种  IDL语言来描述数据格式
    支持多种语言,可根据IDL自动生成多种语言的接口
    二进制编码

Goole开源的,一个平台无关,语言无关,可扩展的序列化结构数据格式

定义了一种  IDL语言来描述数据格式
    支持多种语言,可根据IDL自动生成多种语言的接口
    二进制编码

Netty实现一个简单的RPC框架

gRPC开发流程

Netty实现一个简单的RPC框架

Simple RPC:  Request - Response
Server-side streaming RPC
Client-side streaming RPC
Bi-directional streaming RPC

本文地址:https://blog.csdn.net/qq_31977367/article/details/107157717