preface

Text has been included to my lot warehouse, welcome Star:https://github.com/bin392328206 plant a tree is the best time ten years ago, followed by now

six-finger-web

The wheels of a Web backend framework roll themselves (easy) from handling Http requests [Netty request-level Web servers] to MVC [interface encapsulating and forwarding], to IOC [dependency injection], to AOP [aspect], to RPC [remote procedure calls] and finally to ORM [database operations].

github

Why the wheel

Actually is such, small six six oneself peacetime? Sometimes like to look at the source code such as Spring, but the level may not how, every time dazed at all, and then feel the inside of the details too difficult, then I can only view the overall thought, then I think if I can according to each elder some thought, rolled out a simple wheel, Is it easier for me to understand the author’s ideas? So six-Finger-Web came out, and it was really a learning process for me, and THEN I opened it up to help those of you who are having trouble learning the source code. In addition, we can exercise our coding ability, because we usually use Java API for CRUD. As time goes by, we are not familiar with many framework API classes, so we take this opportunity to exercise.

The characteristics of

  • Built-in HTTP server written by Netty, without additional dependence on Web services such as Tomcat.
  • Code is simple to understand (small 66 can not write their own framework big guy that kind of high clustering, low coupling code), ability a little bit stronger to see the code can understand, weakness also does not matter, small 66 has a supporting from 0 build tutorial.
  • Support MVC-related annotations to ensure that they are used similarly to SpringMVC
  • Support for Spring IOC and Aop related features
  • Support similar to Mybatis related functions
  • Supports RPC-related functionality similar to Dubbo
  • For data returns, only the Json format is supported

omg

The front is already written chapters, I will give you one by one to go through the construction process

  • Suitable for beginners and intermediate Java programmer training manual to build the entire Web project from 0 (A)
  • Build the whole Web project from 0 (2)
  • Build the whole Web project from 0 (3)
  • Build the whole Web project from 0 (4)
  • Build the whole Web project from 0 (5)

Let me summarize what we have done before. We have completed Netty based Http server, and springMVC, Spring IOC,spring AOP related functions. Xiao Liu feels good about himself, at least some people add me to wechat to learn with Xiao Liu, so I continue today. Today we first to simply understand RPC bai!

Introduction of the RPC

Remote Procedure Call Protocol (RPC) Remote Call: Remote procedure call (RPC) is a commonly used distributed network communication protocol that allows a program running on one computer to call a subroutine on another computer while hiding the details of network communication so that the user does not have to program for this interaction. Communication between distributed systems is mostly achieved through RPC

Basically a lot of the bottom of the framework are using RPC communication, such as we are familiar with ES RocktMq and distributed framework almost all are, so their RPC framework that is very necessary!

Today small 66 would like to say, we do not say that we eat a fat, we first to achieve the most simple case, to see a general process of HIS RPC, and then we according to our Dubbo to write an RPC framework, may be a lot better, so today is to give you a small case, Let’s take a look at the main ideas of RPC framework.

RPC request procedure

  • Register the client service

  • Start the RPC server

  • The client invokes the server locally

  • The client proxy finds the server address, connects to the server, and passes parameters, methods, and so on to the server

  • The server receives the request and decodes the data, then executes the local program based on the decoded information and returns the results to the client

  • The client decodes and retrieves the returned result

code

IHello

package com.xiaoliuliu.six.finger.web.demo.rpc.simple.test;

/ * ** @author little six six* @ version 1.0 * @date 2020/10/30 17:46 * Remote service interface* /public interface IHello {  String sayHello(String info); } Copy the code

HelloService

package com.xiaoliuliu.six.finger.web.demo.rpc.simple.test;

import lombok.Data;

/ * ** @author little six six* @ version 1.0 * @date 2020/10/30 17:47 * Remote Service Interface Implementation Class (Server)* /@Data public class HelloService implements IHello {   @Override  public String sayHello(String info) {  String result = "hello : " + info;  System.out.println(result);  return result;  } } Copy the code

RpcProxyServer is also a service exposure process

package com.xiaoliuliu.six.finger.web.demo.rpc.simple.test;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.Method;
import java.net.ServerSocket; import java.net.Socket;  / * ** @author little six six* @ version 1.0 * @date 2020/10/30 17:47 * Server proxy implementation* /public class RpcProxyServer {  private IHello hello = new HelloService();   public void publisherServer(int port) {  try (ServerSocket ss = new ServerSocket(port)) {  while (true) {  try (Socket socket = ss.accept()) {  try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {  String method = ois.readUTF();  Object[] objs = (Object[]) ois.readObject(); Class<? >[] types = new Class[objs.length]; for (int i = 0; i < types.length; i++) {  types[i] = objs[i].getClass();  }  Method m = HelloService.class.getMethod(method, types);  Object obj = m.invoke(hello, objs);   try (ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream())) {  oos.writeObject(obj);  oos.flush();  }  }  } catch (Exception e) {  e.printStackTrace();  }  }  } catch (Exception e) {  e.printStackTrace();  }  } }  Copy the code

RpcProxyClient

package com.xiaoliuliu.six.finger.web.demo.rpc.simple.test;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.net.Socket;  / * ** @author little six six* @ version 1.0 * @date 2020/10/31 10:24 * RPC client proxy implementation* /public class RpcProxyClient<T> {  public static Object proxyClient(Class clazz) {   return Proxy.newProxyInstance(clazz.getClassLoader(),new Class[]{clazz},new InvocationHandler() {  @Override  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {   try (Socket socket = new Socket("localhost", 8000)) {  try (ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream())) {  oos.writeUTF(method.getName());  oos.writeObject(args);  oos.flush();   try (ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {  return ois.readObject();  }  }  }  }  });  }  }  Copy the code

RpcServer test

package com.xiaoliuliu.six.finger.web.demo.rpc.simple.test;

/ * ** @author little six six* @ version 1.0 * @date 2020/10/31 10:26 * /public class RpcServer {  // Publish the service public static void main(String[] args) {  RpcProxyServer server = new RpcProxyServer();  server.publisherServer(8000);  } }  Copy the code

RpcClient

package com.xiaoliuliu.six.finger.web.demo.rpc.simple.test;

/ * ** @author little six six* @ version 1.0 * @date 2020/10/31 10:26 * /public class RpcClient {  // Invoke the service public static void main(String[] args) {  IHello hello = (IHello) RpcProxyClient.proxyClient(IHello.class);  String s = hello.sayHello("Little Six wrote RPC demo.");  System.out.println(s);   } }  Copy the code

The results of



At the end

Ok, the beginning of RPC, we have written, in fact very simple, through the proxy and network communication to mask the implementation details of the code, do call remote services and local services, no perception. Dubbo is not a simple RPC framework. It is dedicated to service governance. This is a very broad framework that we will look at later.

Daily for praise

Ok, everybody, that’s all for this article, you can see people here, they are real fans.

Creation is not easy, your support and recognition, is the biggest power of my creation, our next article

Six pulse excalibur | article “original” if there are any errors in this blog, please give criticisms, be obliged!