RPC implementation principle (HSF, dubbo) from scratch (1), hsfdubbo

Source: Internet
Author: User

RPC implementation principle (HSF, dubbo) from scratch (1), hsfdubbo

Preface

 

I have been away from the blog Park for a long time. Although I have read a lot of things I wrote before, I still feel that I should share some things that I think are useful with you. Now let's get started.

After six years of work experience in some large companies, everyone who has been in large companies should know that they are constantly adding business and functions in a large application, in addition, performance-based considerations make it necessary to modularize many basic services so that subsystems can be conveniently used instead of implementing a set of basic services, also, the basic functions that may become bottlenecks can be expanded separately, such as (E-commerce system examples) User Information Management, transaction management center, and commodity management center. At the beginning of rpc development, after a service is implemented as a module block, various subsystems and modules implement a wide variety of technologies, such as hessian, WebService, Socket, and http, to call each other, the interaction methods and methods between subsystems are inconsistent, making it difficult to integrate systems. These methods also involve timeout, encryption and decryption, parameter transmission, and other issues. In this case, high-performance rpc middleware such as hsf and dubbo emerged. Now I have explained the principles from the very beginning in the simplest way.

I will be divided into a series of anatomy for everyone

I. RPC implementation principle (HSF and dubbo) from scratch (I)

II,RPC implementation principle (HSF, dubbo)Publish and subscribe to a service(3)

III,RPC implementation principle (HSF, dubbo)Zookeeper for cluster configuration management(2)

IV,RPC implementation principle (HSF, dubbo)Replacing java socket with netty(4)

5. To be supplemented

NO.1 TCP transmission protocol

Why use TCP as the transmission protocol? HTTP is located at the upper layer of TCP, at the application layer, and at the network layer. The faster the underlying layer, the more I will explain why I chose TCP as the transmission protocol. So how can we use tcp for calling in the project? The code of the previous example is as follows:

Socket server:

 

Import java.net. *; import java. io. *;/*** socket programming: simple socket server ** @ author chengwei. lcw */public class SocketServer {private ServerSocket serverSocket; private Socket socket; private BufferedReader in; private PrintWriter out; public SocketServer () {try {serverSocket = new ServerSocket (9999 ); while (true) {// blocking will occur here. The role of nio socket = serverSocket will be discussed later. accept (); in = new BufferedReader (new InputStreamReader (socket. getInputStream (); out = new PrintWriter (socket. getOutputStream (), true); String line = in. readLine (); // print out and check the result System. out. println ("line:" + line); // return to the client to notify me that the data has been received. println ("you input is:" + line); out. close (); in. close (); socket. close () ;}} catch (IOException e) {e. printStackTrace () ;}} public static void main (String [] args) {new SocketServer ();}}

 

Scoket client:

 

Import java. io. *; import java.net. *;/*** socket programming: simple socket client ** @ author chengwei. lcw */public class SocketClient {private Socket socket; private BufferedReader in; private PrintWriter out; public SocketClient () {try {socket = new Socket ("127.0.0.1", 9999 ); in = new BufferedReader (new InputStreamReader (socket. getInputStream (); out = new PrintWriter (socket. getOutputStream (), true); // write data to the server BufferedReader line = new BufferedReader (new InputStreamReader (System. in); out. println (line. readLine (); line. close (); // print the receipt System sent back by the server. out. println (in. readLine (); in. close (); out. close (); socket. close ();} catch (IOException e) {e. printStackTrace () ;}} public static void main (String [] args) {new SocketClient ();}}

Start the server, start the client, enter the parameter, and press Enter. The first session is complete.

Summary:

In the current example, we use standard I/O socket, Which is blocked in many cases, such as accept () and read. During the test, the client can sleep for several seconds. During this period, the second client is started. Before the first client is completed, the second client is blocked in accept. In this case, you can allocate a single thread to each client. However, creating too many threads may seriously affect the server performance. The second solution is to use NIO non-blocking communication mode. After jdk1.4, this function has been introduced so that the server can process all client socket requests as long as a thread is started. Netty is a high-performance framework based on NIO. Compared with jdk nio, netty has made many improvements and fixed some defects. (I will not repeat netty and jdk nio too much here. We will not discuss the principles here. If you are interested in this aspect, I will write an article to explain it in depth)

 

No. 2 serialization Method

In real projects, we often upload classes defined by ourselves. In remote communication, class transmission requires class serialization and deserialization. There are multiple serialization methods, such as binary, xml, and soap. Let's take the most binary as an example:

Socket server:

Import java. io. IOException; import java. io. objectInputStream; import java. io. objectOutputStream; import java.net. serverSocket; import java.net. socket;/*** socket programming: Transmission Object server ** @ author chengwei. lcw */public class SocketObjectSever {private ServerSocket serverSocket; private ObjectInputStream in; private ObjectOutputStream out; public SocketObjectSever () {try {serverSocket = new ServerSocket (9999); while (true) {// blocking will be performed here. The functions of nio Socket = serverSocket will be discussed later. accept (); in = new ObjectInputStream (socket. getInputStream (); out = new ObjectOutputStream (socket. getOutputStream (); // receives data from the server and converts it to Student student = (Student) in. readObject (); // overwrite the toString () method and print it out to see the System. out. println ("Server:" + student); // return to the client to notify me that the data has been received. writeObject ("yes client, I receive"); out. flush () ;}} catch (IOException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace () ;}} public static void main (String [] args) {new SocketObjectSever ();}}

 

Socket Client:

Import java. io. IOException; import java. io. objectInputStream; import java. io. objectOutputStream; import java.net. socket; import java.net. unknownHostException;/*** socket programming: Transmission object client ** @ author chengwei. lcw */public class SocketObjectClient {private Socket socket; private ObjectInputStream in; private ObjectOutputStream out; public SocketObjectClient () {try {socket = new Socket ("127.0.0.1", 9999 ); out = new ObjectOutputStream (socket. getOutputStream (); in = new ObjectInputStream (socket. getInputStream ();/** create a student object for transmission */Student s = new Student ("chengwei. lcw ", 28); // write the object to the pipeline, and the client receives the output. writeObject (s); out. flush (); String receive = (String) in. readObject (); System. out. println ("Client Receive:" + receive); in. close (); out. close (); socket. close ();} catch (UnknownHostException e) {e. printStackTrace ();} catch (IOException e) {e. printStackTrace ();} catch (ClassNotFoundException e) {e. printStackTrace () ;}} public static void main (String [] args) {new SocketObjectClient ();}}

 

Define another class to be transmitted:

Import java. io. serializable;/*** socket programming: the class to be transmitted must inherit the Serializable interface ** @ author chengwei. lcw 2016-11-27 **/public class Student implements Serializable {private static final long serialVersionUID = 1L; private String name; private int age; public Student (String name, int age) {this. name = name; this. age = age;} public String toString () {return "name =" + this. name + ", age =" + this. age ;}}

Start the server first, and then start the client. The server console outputs:

Server: name=chengwei.lcw, age=28

So far, our socket can transmit objects.

Here we use the serialization Method for java to directly serialize, while hessian serialization is much more efficient than Java serialization, And the generated byte stream is much shorter, because hessian compresses byte streams during serialization. In subsequent upgrades, I will use hessian serialization for serialization.

 

There are still things in the company, and I don't know if these are what my friends want to see. I will continue to add them today. I hope you can correct the mistakes.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.