Introduction to Dubbo

Posted by irkevin on Tue, 20 Aug 2019 04:23:23 +0200

Are you like me, making progress every day and getting stronger

introduce

Dubbo is a high-performance open source framework for Java RPC. It was originally opened-source by Ali on GitHub and later hosted by Apache. Dubbo's development documentation is very detailed and user-friendly. Dubbo website , Core Diagram:

  • RPC:Remote Procedure Call, remote call, Specific introduction
  • Provider: Provider of the service, exposing the service
  • Registry: A registry responsible for registering services and publishing and subscribing to service events
  • Consumer: Consumer, caller
  • Monitor: Service Monitoring Center, not required

Download the registry, recommend ZooKeeper

Development Documentation
After downloading the ZooKeeper package, unzip it, enter the bin path, and run zkServer.cmd under CMD
Error: Enter conf folder, copy zoo_sample.cfg, paste, rename zoo.cfg, run again


As shown in the figure, the startup succeeded, verifying that CMD runs zkCli.cmd in the bin directory

install dubbo-ops

  1. Download the zip package under github, unzip it, and enter dubbo-admin, a project under Spring Boot
  2. Use maven to package jar s in the project path to start the project
  3. Open the port in the browser and enter the account password, as shown in the figure


Note: This operation requires ZooKeeper startup to succeed, do not know the port configuration to view the project file, note that dubbo.registry.address configuration under application.properties file should be the same as the previous step

Getting Started Development (under idea)

New maven parent-child project, 3 sub-projects, Provider, Consumer and Intermediate

The middle side mainly stores entity classes and interfaces

**Note: Entity classes need to inherit serialization or run tests will fail**

Provider, the provider of the service

  1. Introducing dependencies in pom.xml also requires introducing intermediate dependencies because of the need to implement intermediate interfaces, which is why maven's parent-child project is used

    <dependency>
                <groupId>denny.dubbo</groupId>
                <artifactId>gmall</artifactId>
                <version>1.0-SNAPSHOT</version>
                <scope>compile</scope>
            </dependency>
    
            <!-- Introduce dubbo -->
            <!-- https://mvnrepository.com/artifact/com.alibaba/dubbo -->
            <dependency>
                <groupId>com.alibaba</groupId>
                <artifactId>dubbo</artifactId>
                <version>2.6.2</version>
            </dependency>
            <!-- The registry uses zookeeper,Introducing operations zookeeper Client -->
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>2.12.0</version>
            </dependency>
  2. Configure provider.xml file to expose services with Spring configuration declaration
    The development document describes this:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
        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">
     
        <!-- Provider applies information to calculate dependencies -->
        <dubbo:application name="hello-world-app"  />
     
        <!-- Use multicast Broadcast Registry Exposure Service Address -->
        <dubbo:registry address="multicast://224.5.6.7:1234" />
     
        <!-- use dubbo Protocol exposes services on port 20880 -->
        <dubbo:protocol name="dubbo" port="20880" />
     
        <!-- Declare service interfaces that need to be exposed -->
        <dubbo:service interface="org.apache.dubbo.demo.DemoService" ref="demoService" />
     
        <!-- And local bean Implement services as well -->
        <bean id="demoService" class="org.apache.dubbo.demo.provider.DemoServiceImpl" />
    </beans>

    My Configuration:

    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo"
           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">
    
        <!-- 1,Specify the current service/Name of the application (same service name, do not have the same name as other services) -->
        <dubbo:application name="provider"></dubbo:application>
    
        <!-- 2,Specify the location of the registry -->          
        <dubbo:registry protocol="zookeeper" address="127.0.0.1:2181"></dubbo:registry>
    
        <!-- 3,Specify communication rules (communication protocols?Communication Port) -->
        <dubbo:protocol name="dubbo" port="20882"></dubbo:protocol>
    
        <!-- 4,Exposure Services   ref: Real implementation objects pointing to services -->
        <dubbo:service interface="service.UserService" ref="userServiceImp" ></dubbo:service>
    
        <!-- Implementation of services -->
        <bean id="userServiceImp" class="service.impl.UserServiceImpl"></bean>
    
        <!-- Monitoring Center Auto Registration -->
        <dubbo:monitor protocol="registry"></dubbo:monitor>
    </beans>
  3. test
    New MainApplication.java, Start Project

    public class MainApplication {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("provider.xml");
            classPathXmlApplicationContext.start();
            System.in.read();
        }
    }


Configuration Successful

Consumer, consumer of services

  1. Similarly, introduce dependency
  2. Configure consumer.xml to reference remote services through Spring configuration

    <?xml version="1.0" encoding="UTF-8" ?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:dubbo="http://dubbo.apache.org/schema/dubbo" xmlns:context="http://www.springframework.org/schema/context"
           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 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
    
        <context:component-scan base-package="service.impl"></context:component-scan>
    
        <!-- Consumer application name, used to calculate dependencies, not matching criteria, not like provider -->
        <dubbo:application name="consumer"></dubbo:application>
    
        <!-- Use multicast Broadcast Registry Exposure Discovery Service Address -->
        <dubbo:registry address="zookeeper://127.0.0.1:2181"></dubbo:registry>
    
        <!-- Build a remote service proxy that can be local bean Use the same demoService -->
        <dubbo:reference id="userService" interface="service.UserService" />
    
        <dubbo:monitor protocol="registry"></dubbo:monitor>
    </beans>
  3. test
    Create a new MainApplication.java, mainly like the above test, but here you need to call your Service method to start the project

    public class MainApplication {
        public static void main(String[] args) throws IOException {
            ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("consumer.xml");
            OrderService orderService = applicationContext.getBean(OrderService.class);
            orderService.initOrder();
            System.out.println("Call complete....");
            System.in.read();
        }
    }
    Success is achieved with output from the console, which can also be viewed in the registry  
    

Spring Boot Configuration

  1. rely on

    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
  2. service Annotation Configuration in provider


    @Service is a dubbo comment
    @Component instead of Spring boot's Service comment

  3. Active introduction of service in consumer

  4. The @EnableDubbo annotation is required in the startup file to open Dubbo

Document only the beginning of Dubbo, definitely the development documentation

Topics: Java Dubbo Apache Spring xml