SpringBoot integration org.apache.Dubbo

Posted by zysac on Sat, 18 Jan 2020 02:54:04 +0100

SpringBoot integration org.apache.Dubbo

1. Preface

As an excellent RPC framework, Dubbo has a large number of users in China. Since Dubbo was donated to the Apache Foundation by Ali in February 2018, it seems that Dubbo has brought new life to Apache Dubbo under its new name.Although the Spring Clould framework has the same excellence and advantages, it is also a good thing that Dubbo can be revitalized, looking forward to Dubbo perfecting its mechanism and updating its iteration to a better one.

All code I uploaded to github, self-test as needed, welcome to start, portal
https://github.com/AggerChen/spring-boot-dubbo

2. Project introduction

Looking at the data on Dubbo's new official website, although many demo s have found that either the old package name, com.alibaba.dubbo, or the integrated SpringBoot 1.x, does not meet the new requirements at all. Now that they are all Apache Dubbo, of course, they will use the latest org.apache.dubbo and the latest SpringBoot 2.x version~
This article presents examples in the following version:

SpringBoot 2.2.2
org.apache.dubbo 2.7.5
zookeeper 3.4.14

3. Project Development

Now that it's a Dubbo project, the scenario we're using is distributed microservices, multiple services and a consumer, and a common project, common, to write common classes and interfaces.

3.1 Create project spring-boot-dubbo

  1. Create a project in IDEA named spring-boot-dubbo.This is a root project where pom.xml contains dependencies as follows:

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <modelVersion>4.0.0</modelVersion>

    <!-- Parent Reference -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <!-- Essential information -->
    <groupId>com.agger</groupId>
    <artifactId>spring-boot-dubbo</artifactId>
    <version>1.0.0-SNAPSHOT</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>
    <description>dubbo Root Project</description>

    <!--To configure-->
    <properties>
        <skip_maven_deploy>true</skip_maven_deploy>
        <dubbo.version>2.7.5</dubbo.version>
        <zookeeper.version>3.4.14</zookeeper.version>
        <spring.version>5.2.2.RELEASE</spring.version>
        <dubbo.common>0.0.1-SNAPSHOT</dubbo.common>
    </properties>


    <modules>
        <module>dubbo-common</module>
        <module>dubbo-consumer</module>
        <module>dubbo-provider</module>
    </modules>

    <!--Declare global dependencies (subprojects need to display references to inherit dependencies)-->
    <dependencyManagement>
        <dependencies>

            <!-- dubbo rely on -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo</artifactId>
                <version>${dubbo.version}</version>
                <exclusions>
                    <exclusion>
                        <groupId>org.springframework</groupId>
                        <artifactId>spring</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>javax.servlet</groupId>
                        <artifactId>servlet-api</artifactId>
                    </exclusion>
                    <exclusion>
                        <groupId>log4j</groupId>
                        <artifactId>log4j</artifactId>
                    </exclusion>
                </exclusions>
            </dependency>

            <!-- dubbo-start rely on -->
            <dependency>
                <groupId>org.apache.dubbo</groupId>
                <artifactId>dubbo-spring-boot-starter</artifactId>
                <version>${dubbo.version}</version>
            </dependency>

            <!-- zookeeper rely on -->
            <dependency>
                <groupId>org.apache.zookeeper</groupId>
                <artifactId>zookeeper</artifactId>
                <version>${zookeeper.version}</version>
                <exclusions>
                    <exclusion>
                        <artifactId>slf4j-log4j12</artifactId>
                        <groupId>org.slf4j</groupId>
                    </exclusion>
                </exclusions>
            </dependency>
            <dependency>
                <groupId>com.github.sgroschupf</groupId>
                <artifactId>zkclient</artifactId>
                <version>0.1</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-recipes</artifactId>
                <version>4.2.0</version>
            </dependency>
            <dependency>
                <groupId>org.apache.curator</groupId>
                <artifactId>curator-framework</artifactId>
                <version>4.2.0</version>
            </dependency>

        </dependencies>
    </dependencyManagement>

    <!--Declare global dependencies (subprojects do not need to display references, automatically inherit dependencies)-->
    <dependencies>

        <!--spring-boot rely on -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- lombook rely on -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>

        <!-- Public Project Dependency -->
        <dependency>
            <groupId>com.agger</groupId>
            <artifactId>dubbo-common</artifactId>
            <version>${dubbo.common}</version>
        </dependency>

    </dependencies>

    <!-- Packaging Plugins -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. Through IDEA, three module s are created in the project spring-boot-dubbo, dubbo-common, dubbo-consumer, and dubbo-privider.The resulting directory structure is:

3.2 dubbo-comm Project

This project is a public project and does not require any additional dependencies; it just writes interfaces and public class uses.
Write an interface named HelloService:

HelloService.java

package com.agger.dubbocommon.service;
/**
 * @classname: HelloService
 * @description: Hello Service Interface
 * @author chenhx
 * @date 2020-01-14 13:55:38
 */
public interface HelloService {
    String sayHello(String name);
}

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.agger</groupId>
        <artifactId>spring-boot-dubbo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.agger</groupId>
    <artifactId>dubbo-common</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-common</name>
    <packaging>jar</packaging>
    <description>dubbo Public Projects</description>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

3.3 dubbo-provider project

This project is a service-type project that registers interface services with the zookeeper registry for consumption.

  1. Write pom File Dependencies

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.agger</groupId>
        <artifactId>spring-boot-dubbo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <!-- Essential information -->
    <groupId>com.agger</groupId>
    <artifactId>dubbo-provider</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-provider</name>
    <packaging>jar</packaging>
    <description>dubbo Project Server</description>

    <dependencies>
        <!-- dubbo rely on -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <!-- zookeeper rely on -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>
  1. Write a service class to implement the interface
    Note: @Service comment is a dubbo comment, not a spring comment

HelloServiceImpl.java

package com.agger.dubboprovider.impl;

import com.agger.dubbocommon.service.HelloService;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.stereotype.Component;

/**
 * @classname: HelloServiceImpl
 * @description: HelloService Realization
 * @author chenhx
 * @date 2020-01-14 14:11:39
 */

@Service
@Component
public class HelloServiceImpl implements HelloService {

    @Override
    public String sayHello(String name) {
        System.out.println("dubbo Service invocation:" + name);
        return "Good morning~" + name;
    }
}
  1. Add Configuration on springBoot Startup Class
    The @EnableDubboConfig annotation indicates opening the configuration associated with Dubbo
    @DubboComponentScan is used to scan the provided interface implementation location

DubboProviderApplication.java

package com.agger.dubboprovider;

import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubboConfig
@DubboComponentScan("com.agger.dubboprovider.impl")
@SpringBootApplication
public class DubboProviderApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProviderApplication.class, args);
    }
}
  1. Add dubbo configuration
    dubbo adds configurations in a variety of ways, from xml and properties to API.Now that we're using SpringBoot, we should eliminate the way xml is configured, using both properties and yml, and use YML here as an example.

application.yml

server:
  port: 8087    # Service Port

dubbo:
  application:
    id: dubbo-provider
    name: dubbo-provider  #apply name
    owner: aggerChen      #Apply Owner
    organization: agger   #Apply Owned Organization
  registry:
    id: zookeeper-registry #Registry id
    protocol: zookeeper    #Registry Agreement
    address: zookeeper://127.0.0.1:2181 #Registry Address
  metadata-report:
    address: zookeeper://127.0.0.1:2181
  protocol:
    name: dubbo   #Protocol Name
    port: 20880   #Protocol Port
    accesslog: dubbo-access.log #Protocol Access log
  provider:
    retries: 0    #retry count
    timeout: 3000 #timeout
  monitor:
    protocol: registry # Registration Monitoring Center

3.4 dubbo-consumer project

  1. This project is a web consuming project, or of course not a web project. For a convenient demonstration, use a web project. First, look at the dependent files.Just like relying on a provider project, there is only one more spring-boot-starter-web

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.agger</groupId>
        <artifactId>spring-boot-dubbo</artifactId>
        <version>1.0.0-SNAPSHOT</version>
    </parent>

    <groupId>com.agger</groupId>
    <artifactId>dubbo-consumer</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>dubbo-consumer</name>
    <packaging>jar</packaging>
    <description>dubbo Project Client</description>

    <dependencies>
        <!-- dubbo rely on -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>

        <!-- dubbo Of zookeeper rely on -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>${zookeeper.version}</version>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-log4j12</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.github.sgroschupf</groupId>
            <artifactId>zkclient</artifactId>
            <version>0.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>4.2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-framework</artifactId>
            <version>4.2.0</version>
        </dependency>

        <!-- web Project Dependency -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
  1. Write a controller to call a method
    Use the @Reference annotation to inject the interface

HelloController.java

package com.agger.dubboconsumer.controller;

import com.agger.dubbocommon.service.HelloService;
import com.agger.dubboconsumer.vo.ResultVO;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @classname: HelloController
 * @description: Hello Controller
 * @author chenhx
 * @date 2020-01-14 13:57:48
 */
@RestController
@RequestMapping("/hello")
public class HelloController {

    @Reference
    HelloService helloService;

    @GetMapping("/morning/{name}")
    public ResultVO morning(@PathVariable("name") String name){
        System.out.println(name);
        String hello = helloService.sayHello(name);
        System.out.println(hello);
        return ResultVO.builder().flag(true).msg("Call succeeded").data(hello).build();
    }
}

Of course, you can encapsulate the method that calls the interface with a service layer, change the version number of the service, and so on.
3. Add Configuration on Project Startup Class
Add the Dubbo startup and scan annotations to the project's startup class, DubboConsumerApplication.

DubboConsumerApplication.java

package com.agger.dubboconsumer;

import org.apache.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.apache.dubbo.config.spring.context.annotation.EnableDubboConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubboConfig
@DubboComponentScan("com.agger.dubboconsumer.controller")
@SpringBootApplication
public class DubboConsumerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboConsumerApplication.class, args);
    }
}
  1. Fill in the configuration file

application.yml

server:
  port: 8088

dubbo:
  application:
    name: dubbo-consumer  # apply name
    owner: aggerChen      # Apply Owner
    organization: agger   # Apply Owned Organization
  registry:
    id: zookeeper-registry #Registry id
    protocol: zookeeper    #Registry Agreement
    address: zookeeper://127.0.0.1:2181 #Registry Address
  monitor:
    protocol: registry # Registration Monitoring Center

3.5 Test

The coding section is now complete and the test is started.

  1. Start zookeeper
    The zookeeper client needs to be started before starting the project, which can be downloaded from the official website Portal
    Select a stable version and download it.


    When the download is complete, remember to rename the file zoo_sample.cfg under the conf folder to zoo.cfg and start bin/zkServer.cmd.

  2. Start dubbo-provider
    If you have multiple machines or virtual machines on your intranet, you can package the dubbo-provider project to start several more on different machines.And use the java-jar dubbo-provider command to start the run.I run View Console info in IDEA: I have successfully registered the interface HelloService with zookeeper

  3. Start dubbo-cosumer
    After starting, you can see that consumer has subscribed to the interface it needs from the registry

  4. Test Access
    Access address in browser: http://localhost:8088/hello/morning/tom
    Or HTTP Client, a test tool for IDEA, is very useful
    See the test results as follows, OK, test success!

4. Summary

  • All of the above code I uploaded to github, self-test as needed, welcome to start, portal
    https://github.com/AggerChen/spring-boot-dubbo
  • You can visit the blog of Ali Middleware Team. Portal , download the Ali plug-in to quickly build the project and generate the required common, consumer, provider packages.
    The plug-ins are as follows: Alibaba Cloud Toolkit



    Once you've finished all the way, you'll automatically build the projects you need, so you can test them~
  • This article doesn't explain much of Dubbo's principles, it's just a quick demo. To learn more about other aspects of Dubbo, you need to bite more on the website.
17 original articles published. 31% praised. 110,000 visits+
Private letter follow

Topics: Dubbo Apache Spring Zookeeper