directory

  • What is distributed system?
  • How do distributed systems call each other?
  • How does Dubbo implement RPC calls based on dynamic proxies?
  • conclusion

Today, I would like to tell you a knowledge point, that is, many of our brothers are not the simple single block system developed 10 years ago, a project package deployment start, the system connected to MySQL, and then the WHOLE CRUD is enough, we are developing a very high level of distributed system.

What is distributed system?

Means you write system receives a request, your own code to run is not enough, you’ll have to call other brother writing system, let his system also do some things, then his work is done, after you finish the request processing is the thing, just because you’re dealing with requests to call other brothers system run together, A request involves multiple systems distributed across multiple machines, so it is called distributed, as shown below.

How do distributed systems call each other?

Now brothers usually develop distributed system, is to call other systems, generally what framework is used? Simple, now brothers usually use Spring Cloud or Dubbo, both of which are used by some people. Most of them used Spring Cloud for more than two years, but in the recent two years, everyone has switched to Spring Cloud Alibaba.

When you use Spring Cloud, you usually use Feign to call other systems, and now when you use Spring Cloud Alibaba, you usually use Dubbo. Today, we will take dubbo as an example to illustrate how we usually call each other.

To provide an interface for others to call, the system must write an interface that defines which methods you want to allow others to call. It might look something like this:

public interface Service {  
    String sayHello(String name); } Next, you need to develop an implementation class for the interface that implements the logic of the method, and add to the implementation class@DubboServiceThis annotation, which Dubbo identifies as an external service interface, looks like this:@dubboService (version = "1.0.0", interfaceClass = service.class)
public class ServiceImpl implements Service {    
    public String sayHello(String name) {   
        // Run some code
      return "hello, "+ name; }}Copy the code

So what happens when your business system B starts up with the interface and implementation classes developed and annotated with ** @dubboService **? In simple terms, the Dubbo framework starts with your business system B. It starts a network server that listens for a port number you specify, usually 20880, as shown below.

This time on business system B dubbo has begun A web server to monitor A port number, ready to receive you send over the call request, then let’s turn the business system played A, the business system of A hypothesis to invoke business system B Service interface defined in the method, what will he do? The code would look something like this:

@RestController 
public class Controller {  
    // Note that Service is the interface defined by business system B
    @ DubboReference (version = "1.0.0")  
    private Service service;    
    
   
    @RequestMapping("/hello")  
    public Response sayHello(String name) {          
        String result = service.sayHello(name);    
        returnResponse.success(result); }}Copy the code

The Service interface of system B is defined as Service Service, and then the ** @dubboreference ** annotation is added. When the Service interface of system B is started, the ** @dubboreference ** annotation is added. What would Dubbo do?

How does Dubbo implement RPC calls based on dynamic proxies

The important point here is that Dubbo will use our design proxy mode to create a dynamic proxy object and inject this dynamic proxy object into the Service Service variable, which will reference Dubbo’s dynamic proxy object.

So what is this dynamic proxy object? In simple terms, Dubbo can dynamically generate a class that implements the Service interface, and all methods have their own implementation logic. We’ll talk about the implementation logic later, but it looks like this.

So what’s really important here is that you have to understand this concept of Dubbo dynamic proxy, which is a classic use of the proxy pattern in design mode, which is that once Dubbo generates a dynamic proxy object for the interface, and injects it into the Service Service variable, The Dubbo dynamic proxy object is called by the Dubbo dynamic proxy object.

@RestController 
public class Controller {     
    
    // Note that Service is the interface defined by business system B
    // This interface variable is actually injected into the dynamic proxy object generated by Dubbo
    @ DubboReference (version = "1.0.0")   
    private Service service;   
    
    @RequestMapping("/hello")   
    public Response sayHello(String name) { 
        // Note that when you call the interface method, you are actually calling the method of the Dubbo dynamic proxy object
        String result = service.sayHello(name);    
     returnResponse.success(result); }}Copy the code

What happens when the method of the Dubbo dynamic proxy object is called? In fact, it will establish a network connection with our business system B’s machine, and then send a call request through this network connection, and the Dubbo network server in business system B will call the local interface implementation method based on the request, and get the return value. The return value is then returned to the Dubbo dynamic proxy object of business System A over the network connection, and finally the Dubbo dynamic proxy object gives the return value to us, as shown in the following figure.

conclusion

Well, today to share with you based on dubbo implementation of inter-system call principle here, I hope you usually use Dubbo to do development, to his underlying principle also have a certain understanding.

pleasantly surprised