Dubbo I. Introduction to Dubbo framework and handwriting simulation Dubbo

Posted by taldos on Sun, 16 Jan 2022 05:41:56 +0100

What is RPC

Wikipedia defines RPC as follows:

In distributed computing, Remote Procedure Call (RPC) is a computer communication protocol. The protocol allows a program running on one computer to call a subroutine in another address space (usually a computer on an open network), and the programmer does not need to program the interaction (without paying attention to details) like calling a local program. RPC is a Client/Server mode. The classic implementation is a system that communicates information by sending request and receiving response.
If the software involved adopts object-oriented programming, remote procedure call can also be called remote call or remote method call, such as Java RMI.

Therefore, for Java programmers, RPC is a remote method call.
Remote method call and local method call are two relative concepts. Local method call refers to the method call inside the process, while remote method call refers to the mutual call of methods in two processes.

To realize remote method call, the basic is to call through the network and transfer data.

So there is:

  1. RPC over Http: transmits data based on Http protocol
  2. PRC over Tcp: transmit data based on Tcp protocol

The transmitted data can be defined by both parties of RPC through negotiation, but it basically includes:

  1. Which class or interface is called
  2. Which method is called, method name and method parameter type (considering method overload)
  3. Input parameter of calling method

Therefore, we can actually see that RPC is highly customizable. Each company can implement its own RPC framework, and Dubbo is a set of RPC framework opened by Ali.

Second, what is Dubbo

Official website address: http://dubbo.apache.org/zh/

At present, it is introduced on the official website as follows: Apache Dubbo is a high-performance and lightweight open source Java service framework
Not long ago, the official website introduced that Apache Dubbo is a high-performance and lightweight open source Java RPC framework

Why change RPC to service?

Dubbo started with RPC, focusing on the call between two services. However, with the prevalence of microservices, in addition to service invocation, Dubbo is also gradually involved in service governance, service monitoring, service gateway, etc., so now Dubbo's goal is not just RPC framework, but like Spring Cloud, it wants to become a service framework.

Dubbo gateway reference: https://github.com/apache/dubbo-proxy (the community is not very active)

2.1 basic principles

2.2 comparison of open source RPC frameworks

Online screenshot, for reference only (data needs to be updated)

Handwritten analog Dubbo code address: https://gitee.com/archguide/rpc
git clone address: https://gitee.com/archguide/rpc.git

III. difference between Dubbo and direct remote call Controller

If the remote Controller is called directly using Httpclient or restTemplate, the following problems exist

  1. When calling the service provider, the address of the service provider may be written incorrectly in the code
  2. Request methods such as get/post may also be written incorrectly
  3. After obtaining the return value of String type, the consumer also needs to manually convert it to an object

In short, using remote direct calls has a high probability of various errors

With dubbo, you only need to write a service/serviceImpl class for the service provider, as follows

public interface DemoService {
    public String sayHello(String name) ;
}
import org.apache.dubbo.demo.DemoService;
import org.apache.dubbo.rpc.RpcContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

public class DemoServiceImpl implements DemoService {   // service
    private static final Logger logger = LoggerFactory.getLogger(DemoServiceImpl.class);

    @Override
    public String sayHello(String name) {
        logger.info("Hello " + name + ", request from consumer: " + RpcContext.getContext().getRemoteAddress());
        return "Hello " + name + ", response from provider: " + RpcContext.getContext().getLocalAddress();
    }

}

For service consumers, you only need to define the same interface as the service provider, and the interface volume is very small, so consumers rely on very little other code

import org.apache.dubbo.demo.DemoService;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.util.concurrent.CompletableFuture;

public class Application {
    public static void main(String[] args) {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring/dubbo-consumer.xml");
        context.start();


        // Proxy object for interface
        DemoService demoService = context.getBean("demoService", DemoService.class);
        String hello = demoService.sayHello("world");  // Execution services
        System.out.println("result Zhou Yu: " + hello);
    }
}

IV. spring integration dubbo

spring integrates dubbo in two ways: xml configuration file and annotation

4.1 xml configuration file integration dubbo

Service provider

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    
    <!--Application name-->
    <dubbo:application name="demo-provider"/>
     <!--Registration Center, you can not use the registration center-->
    <dubbo:registry  address="zookeeper://127.0.0.1:2181"/>
    
    <!--The service agreement can also be configured as http-->
    <dubbo:protocol name="dubbo"/>
    <!--The service agreement is http The default is tomcat´╝îThis can be changed manually to jetty-->
    <!--<dubbo:protocol name="http" server="jetty"/>-->

    <!--definition springBean-->
    <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl"/>

    <!--definition dubbo service  interface: Interface   ref: Specific implementation of interface-->
    <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
</beans>

Service consumers

<?xml version="1.0" encoding="UTF-8"?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
  -->
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
       xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
       http://dubbo.apache.org/schema/dubbo http://dubbo.apache.org/schema/dubbo/dubbo.xsd">
    <!--Application name-->
    <dubbo:application name="demo-consumer"/>
    <!--Registration Center-->
    <dubbo:registry address="zookeeper://127.0.0.1:2181"/>
   <!--definition dubbo service  reference: Introduction Service   id: bean Name (use this in the code) bean)  interface: Which service do you want to use-->
    <dubbo:reference id="demoService" check="false"  interface="org.apache.dubbo.demo.DemoService"/>

</beans>

Topics: Java Distribution rpc IDE