当前位置:网站首页>Come on, handwritten RPC S2 serialization exploration
Come on, handwritten RPC S2 serialization exploration
2022-07-29 01:47:00 【In the misty rain of the Pavilion】
List of articles
SOA
Service Oriented Architecture (SOA) With the rapid development of the Internet A system architecture method . Service orientation is a design paradigm , Logical units for creating solutions , These logical units can be combined 、 Reusable , To support specific strategic goals and benefits of Service-oriented Computing .
( It feels like a micro service
Due to the split of many small service modules , Communication between modules is particularly important , Simple information transmission can be used MQ, Complex method calls must be robust RPC Support .
In actual production , We can't be like that in the last lesson , Just by “ Ask each other about the service name and method name offline ” The way to run our RPC function , We must have a completed system to ensure the health of the service . At least the following parts should be included :
- Service registry
- Service providers and related heartbeat detection mechanisms
- Stricter consumer calls 、 The ginseng 、 Exception handling mechanism
- Current limiting strategy
- Polling strategy, etc
The corresponding , The overall service process should be :
- The service provider starts the service , Register the service provider information to the service registry , Service provider information Generally, it includes the service host address 、 port 、 Service interface information, etc .
- The service consumer obtains the service provider information from the service registry to the local cache , At the same time, service consumption
Report the information of the applicant to the service registration center . - The service consumer selects a service provider to initiate service invocation according to a certain soft load policy , The service call First, adopt some data serialization scheme , Serialize the call data into an array of bytes that can be transmitted over the network , Adopt some NIO frame ( Such as Netty、 Mina、 Grizzy) Completion of invocation .
- Service governance ( Health monitoring 、 Current limiting the drop 、 Polling, etc )
Serialization and deserialization
Let's start with the most basic concepts .
serialize
serialize ( Serialization) It is the process of transforming the state information of an object into a form that can be stored or transmitted . in short , The process of converting an object into a sequence of bytes is called object serialization .
Deserialization
Deserialization ( Deserialization) It's the reverse of serialization . Deserialize a byte array into an object , The process of restoring a byte sequence to an object is called deserialization of the object .
sd The necessity of
- It enables the transmission of objects between systems that do not share memory and are connected through the network .
- Ensure that the transmission content is not distorted
Serialization instructions
- Serialization , Save only the state of the object , Regardless of the object's method .
- When a parent class implements serialization , Subclasses automatically implement serialization , You don't need to explicitly implement Serializable Interface .
- Call the instance variable of an object to reference other objects , When serializing the object, the reference object is also sequenced turn .
- When a field is declared as transient after , The default serialization mechanism ignores this field .
- For the above has been declared transient Yuduan of , Private methods can be added to classes writeObject() And readObject() Two methods to serialize .
Common serialization schemes
imitation Java Native serialization of
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
/** * @Desc **/
public class MineSerializer {
public <T> byte[] serialize(T obj) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
ObjectOutputStream objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
objectOutputStream.writeObject(obj);
objectOutputStream.close();
} catch (Exception e) {
throw new RuntimeException(e);
}
return byteArrayOutputStream.toByteArray();
}
@SuppressWarnings("unchecked")
public <T> T deserialize(byte[] data, Class<T> clazz) {
ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(data);
try {
ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream);
return (T) objectInputStream.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
XML and JSON serialize
JSON Serialization uses Alibaba fastjson,google Of gson All possible , Not much introduction . Here are XML serialize :
<dependencies>
<!-- https://mvnrepository.com/artifact/com.thoughtworks.xstream/xstream -->
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.19</version>
</dependency>
</dependencies>
Code :
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;
import java.nio.charset.StandardCharsets;
/** * @Desc **/
public class MineXmlSerialize {
private static final XStream X_STREAM =new XStream(new DomDriver()) ;
public <T> byte[] serialize(T obj) {
return X_STREAM.toXML(obj).getBytes(StandardCharsets.UTF_8);
}
@SuppressWarnings("unchecked")
public <T> T deserialize(byte[] data, Class<T> clazz) {
String xml = new String(data);
return (T) X_STREAM.fromXML(xml);
}
}
Hessian serialize
Is a cross language binary serialization Protocol , Strong performance and easy to use , The most important is cross language .
<!-- https://mvnrepository.com/artifact/com.caucho/hessian -->
<dependency>
<groupId>com.caucho</groupId>
<artifactId>hessian</artifactId>
<version>4.0.66</version>
</dependency>
Code :
import com.caucho.hessian.io.HessianInput;
import com.caucho.hessian.io.HessianOutput;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
/** * @Desc **/
public class MineHessianSerialize {
public byte[] serialize(Object obj) {
if (obj == null) {
throw new NullPointerException();
}
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
HessianOutput ho = new HessianOutput(os);
ho.writeObject(obj);
return os.toByteArray();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@SuppressWarnings("unchecked")
public <T> T deserialize(byte[] data, Class<T> clazz) {
if (data == null) {
throw new NullPointerException();
}
try {
ByteArrayInputStream is = new ByteArrayInputStream(data);
HessianInput hi = new HessianInput(is);
return (T) hi.readObject();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
Thrift serialize
Along with RPC Supporting serialization of the framework .
XML file :
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libfb303</artifactId>
<version>0.9.3</version>
</dependency>
<dependency>
<groupId>org.apache.thrift</groupId>
<artifactId>libthrift</artifactId>
<version>0.9.3</version>
</dependency>
Code
import org.apache.thrift.TBase;
import org.apache.thrift.TDeserializer;
import org.apache.thrift.TSerializer;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TBinaryProtocol.Factory;
/** * @Desc **/
public class MineThriftSerialize {
public <T> byte[] serialize(T obj) {
try {
if (!(obj instanceof TBase)) {
throw new UnsupportedOperationException("not support");
}
TSerializer serializer = new TSerializer(new Factory());
return serializer.serialize((TBase) obj);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public <T> T deserialize(byte[] data, Class<T> clazz) {
try {
if (!TBase.class.isAssignableFrom(clazz)) {
throw new UnsupportedOperationException("not support");
}
TBase o = (TBase) clazz.newInstance();
TDeserializer tDeserializer = new TDeserializer(new TBinaryProtocol.Factory());
tDeserializer.deserialize(o, data);
return (T) o;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}
other
And Google protobuf, But it's troublesome to use, and it's basically useless for big projects
Serialization scheme selection
- Performance metrics ( Basic ratio of binary xml and json fast )
- Easy to use ruler ( It's not easy to learn )
- Cross language / Platform ruler ( such as go-nodejs-java-python framework )
- Long term support ( The parent company went bankrupt …)
边栏推荐
- J9 number theory: what factors determine the value of NFT?
- Read the recent trends of okaleido tiger and tap the value and potential behind it
- JS事件简介
- [web technology] 1395 esbuild bundler HMR
- What is the ISO assessment? How to do the waiting insurance scheme
- 【HCIP】MGRE环境下OSPF实验,含多进程双向重发布及OSPF特殊区域
- It is found that the data of decimal type in the database can be obtained through resultset.getdouble, but this attribute cannot be obtained through GetObject.
- [SQL's 18 dragon subduing palms] 01 - Kang long regrets: introductory 10 questions
- Openpyxl border
- 【GoLang】同步锁 Mutex
猜你喜欢

What are the common cyber threats faced by manufacturers and how do they protect themselves

Analysis of Multi Chain use cases on moonbeam -- review of Derek's speech in Polkadot decoded 2022

How many of the top ten test tools in 2022 do you master

Introduction to Elmo, Bert and GPT

Making high-precision map based on autoware (V)

瑞吉外卖项目实战Day01

What is the ISO assessment? How to do the waiting insurance scheme

Cloud native application comprehensive exercise

matplotlib中文问题

The information security and Standardization Commission issued the draft for comments on the management guide for app personal information processing activities
随机推荐
JS event introduction
About df['a column name'] [serial number]
ELMO,BERT和GPT简介
MySQL execution order
J9 number theory: what factors determine the value of NFT?
Formal parameters, arguments, main function parameters, arrays or pointers as function parameters of the knowledge in every corner of C language
T-sne dimensionality reduction
[hcip] experiment of republishing and routing strategy
[机缘参悟-54]:《素书》-1-事物缘起[原始章第一]:大道至简。
Ruiji takeout project actual battle day01
[search] - DFS pruning and optimization
【观察】三年跃居纯公有云SaaS第一,用友YonSuite的“飞轮效应”
els 到底停止
什么是原码、反码和补码
Understand all the basic grammar of ES6 in one article
internship:用于类型判断的工具类编写
Openpyxl cell center
HCIA配置实例(eNSP)
ELS square movement
拼多多众多 API 接口皆可使用