To talk about Dubbo, let’s start with RPC:

Concept:

Remote Procedure Call (RPC) : a protocol that requests services from Remote computer programs over the network without understanding the underlying network technology. It makes calling a remote program locally as if it were calling a local program.

The RPC protocol assumes the existence of some transport protocol, such as TCP or UDP, to carry information data between communication programs. In the OSI network communication model (seven-layer model), RPC spans both the transport layer and the application layer. RPC makes it easier to develop applications including network distributed multiprograms.

RPC call procedure:

  1. The client invokes the service as a local service

  2. The Client stub acts as the proxy and then handles the call and its parameters

  3. The Client stub sends calls to the remote system over TCP or UDP

  4. The Server Stub processes the calls and parameters sent by the Client stub

  5. The Server Stub invokes the service that is actually provided

  6. The Server Stub processes the reply and sends it to the client

Dubbo

What is dubbo?

Dubbo is the core framework of Alibaba’s SOA servitization governance scheme. It is a distributed service framework and is committed to providing high-performance and transparent RPC remote service invocation scheme and SOA service governance scheme.

Soa: “Service-oriented architecture”, which provides an architectural style and concept rather than a technology or product. SOA advocates encapsulating and hosting the business functions of different applications as “services”, usually in the form of interfaces and contracts that are exposed and made available to external applications (by exchanging messages) to make different systems reusable.

2. The technical architecture diagram of Dubbo is as follows:

Let’s explain the architecture diagram:

  • A) Consumer B) Provider C) Container D) Container

  • B. The service provider starts and then registers the service.

  • C, Monitor is a Monitor. The dotted line in the figure shows that the Consumer and Provider send messages asynchronously to the Monitor. The Consumer and Provider store the information to the local disk and send messages once every 1 minute on average. Monitor is optional throughout the architecture.

  • D. The Monitor function needs to be configured separately. If it is not configured or configured, the failure of Monitor does not affect service invocation.

3. Dubbo service consumer calls the process

The figure above shows the main process of service consumption:

First, the private void init() method of ReferenceConfig class will check and initialize all configuration information, and then call the private T createProxy(Map Map) to create the proxy. The consumer finally gets the proxy of the service. CreateProxy then calls the Invoker refer(Class Type, URL URL) method of the Protocol interface implementation to generate the Invoker instance (shown in red in the figure above), which is key to service consumption. Next, the Invoker is converted to the interface (such as HelloWorld) required by the client through the ProxyFactory ProxyFactory, the service proxy is created and returned.

Initialization process on the consumer side

  • 1. Encapsulate the information referenced by the service into a URL and register it in the ZK registry;

  • 2. Listen for services in the registry to go up and down;

  • 3. Connect to the service provider and create the NettyClient object;

  • 4. Wrap this information into a call chain on the consumer side of DubboInvoker, create a service proxy for the consumer side of Invoker instance and return it;

The service reference process on the consumer side

  • 1. Call the provider through the load balancing policy;

  • 2. Select the URL of one of the services to establish a connection with the provider Netty, use ProxyFactory to create remote communication, or local communication, Invoker sent to the Netty server;

  • 3. After receiving the Invoker message, the server finds the local Invoker and processes the Invocation.

  • 4. Obtain asynchronous or synchronous processing results;

    • Asynchrony does not require a return value: call the exchangeclient.send () method directly;

    • Synchronization requires a return value: use the exchangeclient.request () method to return a ResponseFuture that blocks until the server returns the response;

The simplest instance of Dubbo

Packages required by both service providers and consumers (here I create a New Maven project as a POM project, writing common project dependencies to POM.xml)

The overall project structure is

The pom. XML file contains the following contents


     
  1. The < project XMLNS = "http://maven.apache.org/POM/4.0.0" XMLNS: xsi = "http://www.w3.org/2001/XMLSchema-instance" Xsi: schemaLocation = "http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" >

  2. The < modelVersion > 4.0.0 < / modelVersion >

  3.  <groupId>com.test</groupId>

  4.  <artifactId>dubbo-demo</artifactId>

  5. < version > 0.0.1 - the SNAPSHOT < / version >

  6.  <packaging>pom</packaging>

  7.   <properties>

  8. < motan version > 0.3.0 < / motan. Version >

  9. <! -- GA version is widely used inside Alibaba: 2.4.9, strongly recommended -->

  10. < dubbo version > 2.5.3 < / dubbo version >

  11. < dubbox version > 2.8.4 < / dubbox version >

  12. < spring version > 4.3.6. RELEASE < / spring. Version >

  13. < Java version > 1.7 < / Java. Version >

  14.        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

  15.    </properties>

  16.    <dependencies>

  17.      <dependency>

  18.            <groupId>com.alibaba</groupId>

  19.            <artifactId>dubbo</artifactId>

  20. The < version > 2.5.3 < / version >

  21.            <exclusions>

  22.                <exclusion>

  23.                    <groupId>org.springframework</groupId>

  24.                    <artifactId>spring</artifactId>

  25.                </exclusion>

  26.            </exclusions>

  27.        </dependency>

  28.        <dependency>

  29.            <groupId>com.github.sgroschupf</groupId>

  30.            <artifactId>zkclient</artifactId>

  31. < version > 0.1 < / version >

  32.        </dependency>

  33. <! -- Spring -->

  34.        <dependency>

  35.            <groupId>org.springframework</groupId>

  36.            <artifactId>spring-core</artifactId>

  37.            <version>${spring.version}</version>

  38.        </dependency>

  39.        <dependency>

  40.            <groupId>org.springframework</groupId>

  41.            <artifactId>spring-beans</artifactId>

  42.            <version>${spring.version}</version>

  43.        </dependency>

  44.        <dependency>

  45.            <groupId>org.springframework</groupId>

  46.            <artifactId>spring-context</artifactId>

  47.            <version>${spring.version}</version>

  48.        </dependency>

  49.        <dependency>

  50.            <groupId>org.springframework</groupId>

  51.            <artifactId>spring-jdbc</artifactId>

  52.            <version>${spring.version}</version>

  53.        </dependency>

  54.        <dependency>

  55.            <groupId>org.springframework</groupId>

  56.            <artifactId>spring-web</artifactId>

  57.            <version>${spring.version}</version>

  58.        </dependency>

  59.        <dependency>

  60.            <groupId>org.springframework</groupId>

  61.            <artifactId>spring-webmvc</artifactId>

  62.            <version>${spring.version}</version>

  63.        </dependency>

  64.        <dependency>

  65.            <groupId>org.springframework</groupId>

  66.            <artifactId>spring-aop</artifactId>

  67.            <version>${spring.version}</version>

  68.        </dependency>

  69.        <dependency>

  70.            <groupId>org.springframework</groupId>

  71.            <artifactId>spring-tx</artifactId>

  72.            <version>${spring.version}</version>

  73.        </dependency>

  74.        <dependency>

  75.            <groupId>org.springframework</groupId>

  76.            <artifactId>spring-orm</artifactId>

  77.            <version>${spring.version}</version>

  78.        </dependency>

  79.        <dependency>

  80.            <groupId>org.springframework</groupId>

  81.            <artifactId>spring-context-support</artifactId>

  82.            <version>${spring.version}</version>

  83.        </dependency>

  84.        <dependency>

  85.            <groupId>org.springframework</groupId>

  86.            <artifactId>spring-test</artifactId>

  87.            <version>${spring.version}</version>

  88.        </dependency>

  89.        <dependency>

  90.            <groupId>org.springframework</groupId>

  91.            <artifactId>spring-jms</artifactId>

  92.            <version>${spring.version}</version>

  93.        </dependency>

  94.        <dependency>

  95.            <groupId>org.aspectj</groupId>

  96.            <artifactId>aspectjrt</artifactId>

  97. The < version > 1.6.11 < / version >

  98.        </dependency>

  99.        <dependency>

  100.            <groupId>org.aspectj</groupId>

  101.            <artifactId>aspectjweaver</artifactId>

  102. The < version > 1.6.11 < / version >

  103.        </dependency>

  104.    </dependencies>

  105.   <modules>

  106.    <module>demo-api</module>

  107.    <module>dubbo-consumer</module>

  108.    <module>dubbo-provider</module>

  109.   </modules>

  110. </project>

Copy the code

Define the service interface in demo-API (note that both service providers and consumers need to rely on this project)


     
  1. package com.test;

  2. public interface DemoService{  

  3.     String sayHello(String name);  

  4. }

Copy the code

Service provider implementation

The project structure

Implementing an interface


     
  1. package com.test;

  2. import org.springframework.stereotype.Service;

  3. import com.test.DemoService;

  4. @Service("demoService")

  5. public class DemoServiceImpl implements DemoService{

  6.    @Override

  7.    public String sayHello(String name) {

  8.        // TODO Auto-generated method stub

  9.        return name;

  10.    }  

  11. }

Copy the code

Claim exposure service:


     
  1. <? The XML version = "1.0" encoding = "utf-8"? >

  2. <beans xmlns="http://www.springframework.org/schema/beans"

  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  4.    xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"

  5.    xsi:schemaLocation="http://www.springframework.org/schema/beans

  6.    http://www.springframework.org/schema/beans/spring-beans.xsd

  7.    http://code.alibabatech.com/schema/dubbo

  8.    http://code.alibabatech.com/schema/dubbo/dubbo.xsd">

  9. <! -- Provider application information for calculating dependencies -->

  10.    <dubbo:application name="dubbo_provider"  />  

  11. <! -- Use zooKeeper registry to expose service address -->

  12. "Dubbo: registry address =" zookeeper: / / 127.0.0.1:2181 "/ >

  13. <! Expose service on port 20880 with dubbo

  14.    <dubbo:protocol name="dubbo" port="20880" />  

  15. <! Declare the service interface to be exposed -->

  16.    <dubbo:service interface="com.test.DemoService" ref="demoService" />  

  17. </beans>

Copy the code

Scan the service annotations in springMVC.xml and import the relevant Dubbo configuration in dubbo-provider.xml


     
  1. <? The XML version = "1.0" encoding = "utf-8"? >

  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"

  3.    xmlns:context="http://www.springframework.org/schema/context"

  4.    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  5.    xsi:schemaLocation="http://www.springframework.org/schema/aop  

  6.        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  

  7.        http://www.springframework.org/schema/beans  

  8.        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  

  9.        http://www.springframework.org/schema/context

  10.        http://www.springframework.org/schema/context/spring-context-4.0.xsd  

  11.        http://www.springframework.org/schema/util

  12.        http://www.springframework.org/schema/util/spring-util-4.0.xsd"

  13.    default-autowire="byName">

  14.    <aop:aspectj-autoproxy />

  15.    <context:component-scan base-package="com.test" />

  16.    <import resource="classpath:dubbo-provider.xml" />

  17. </beans>

Copy the code

Load the Spring configuration and start the service:


     
  1. package com.test;

  2. import java.io.IOException;

  3. import org.springframework.context.support.ClassPathXmlApplicationContext;

  4. public class Test {

  5.    public static void main(String[] args) throws Exception {

  6.        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("classpath:springmvc.xml");

  7.        context.start();

  8. System.out.println("Dubbo provider start..." );

  9.        try {

  10. System.in.read(); // Press any key to exit

  11.        } catch (IOException e) {

  12.            e.printStackTrace();

  13.        }

  14.    }

  15. }

Copy the code

Service consumer implementation

The project structure

Declare the services that need to be consumed in dubo-consumer.xml


     
  1. <? The XML version = "1.0" encoding = "utf-8"? >

  2. <beans xmlns="http://www.springframework.org/schema/beans"    

  3.    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"    

  4.    xsi:schemaLocation="http://www.springframework.org/schema/beans    

  5.        http://www.springframework.org/schema/beans/spring-beans.xsd    

  6.        http://code.alibabatech.com/schema/dubbo    

  7.        http://code.alibabatech.com/schema/dubbo/dubbo.xsd ">          

  8. <! -- Consumer application name, used to calculate dependencies, not matching conditions, not the same as provider -->

  9.    <dubbo:application name="dubbo_consumer" />      

  10. <! Expose discovery service addresses with multicast broadcast registries -->

  11. "Dubbo: registry protocol =" zookeeper "address =" zookeeper: / / 127.0.0.1:2181 "/ >

  12. <! Create a remote service proxy that can use demoService as a local bean -->

  13.    <dubbo:reference id="demoService" interface="com.test.DemoService" />    

  14. </beans>  

Copy the code

Scan the service annotations in springMVC.xml and bring in the relevant Dubbo configuration in dubbo-consumer.xml


     
  1. <? The XML version = "1.0" encoding = "utf-8"? >

  2. <beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"

  3.    xmlns:context="http://www.springframework.org/schema/context"

  4.    xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

  5.    xsi:schemaLocation="http://www.springframework.org/schema/aop  

  6.        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd  

  7.        http://www.springframework.org/schema/beans  

  8.        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd  

  9.        http://www.springframework.org/schema/context

  10.        http://www.springframework.org/schema/context/spring-context-4.0.xsd  

  11.        http://www.springframework.org/schema/util

  12.        http://www.springframework.org/schema/util/spring-util-4.0.xsd"

  13.    default-autowire="byName">

  14.    <aop:aspectj-autoproxy />

  15.    <context:component-scan base-package="com.test" />

  16.    <import resource="classpath:/dubbo-consumer.xml" />

  17. </beans>

Copy the code

Load the Spring configuration and invoke the service:


     
  1. package com.test;

  2. import java.io.IOException;

  3. import org.springframework.context.support.ClassPathXmlApplicationContext;

  4. public class Test {

  5.    public static void main(String[] args) {

  6.        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(new String[] { "classpath:springmvc.xml" });

  7.        context.start();

  8.        DemoService demoService = (DemoService) context.getBean("demoService");

  9. System.out.println(demoservice.sayHello (" ha ha ha "));

  10.        try {

  11.            System.in.read();

  12.        } catch (IOException e) {

  13.            e.printStackTrace();

  14.        }

  15.    }

  16. }

Copy the code

The call succeeds if the following results occur

Service providers and consumers can be seen through the Dubo-admin management background

The disclosing party:

Consumer:

Recommended reading

  • [JVM] class loading, connection, and initialization procedures

  • The full set of learning video resources for 2018 has been organized! Free to share!

  • Docker Core Technology Video tutorial

  • Ant Class 2 without encryption -Java video tutorial

  • Java logging system summary in detail

  • 【 resources sharing 】Spring Cloud micro-service combat video course

  • Concurrent programming ThreadLocal keyword

  • Rounding BlockingQueue

  • Springmvc source code analysis summary

  • MyBatis architecture source code interpretation

Our official account will give you benefits from time to time, including learning resources, etc. Please look forward to it!

If you can’t use the content in your work now, you can forward it to your friend circle or favorite first, so that you can find it easily when you use it.

In addition, you are welcome to pay attention to the public account or add wechat friends to learn and communicate with each other.