Complete Dubbo's Spring Boot integration in 10 minutes to realize multiple registration centers

Hello, I'm Lao Tian

Last time, we shared the use of Dubbo's introductory case.

Dubbo source code analysis: introduction to Xiaobai

In the previous case, we used Zookeeper as the registry.

But what if you need more than one registry? It's all right. Brother Tian will arrange it for you.

Let's start with a Dubbo architecture diagram:

Multi registry

Dubbo currently supports almost all registration centers on the market:

  • Consul
  • Zookeeper
  • Eureka
  • Redis
  • Nacos
  • ....

In fact, in our actual development, most of the Dubbo registry uses Zookeeper and Nacos.

Therefore, this sharing is a demonstration using Zookeeper and Nacos.

Nacos preparation

We first download Nacos locally from the official website, and then install it.

Introduction to the official website: https://nacos.io/zh-cn/docs/what-is-nacos.html

Download address: https://github.com/alibaba/nacos/releases/tag/1.4.0

Operation mode

Nacos Server has two operation modes:

  • standalone mode
  • Cluster cluster mode

This paper adopts the standalone model

If the Nacos cluster is not deployed, change the mode to stand-alone mode

Open the bin/startup.cmd file and change the MODE from cluster to standalone as follows:

data storage

We register the service on Nacos, and the service related information needs to be saved.

Where is it kept? Nacos provides two ways:

  • Local memory (default)
  • database

We have a configuration file under / conf in the Nacos installation directory: application.properties, which can be adjusted accordingly.

Here, I have configured the database. By default, the database is not configured. In addition, I need to execute the database script in this directory: nacos-mysql.sql

Finally, change the database connection information into your own database connection information.

In addition, if you want to learn more about Nacos, the database script is basically annotated in Chinese, which is very helpful.

Start Nacos

Under Linux environment: sh bin/startup.sh

Under Windows Environment: cmd bin/startup.cmd, or double-click startup.cmd directly.

The startup log based on Windows environment is as follows:

visit: http://localhost:8848/nacos/index.html

Enter user name / password to access nacos/nacos

Well, after finishing the installation of Nacos, let's stop for the time being and continue.

Zookeeper registry is not repeated here. We used zookeeper as the registry in the last article.

Integrated Spring Boot

The overall project is as follows:

The project module will not be introduced here, just like the article we shared last time.

The parent pom.xml is as follows:

<?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>

    <groupId>com.tian.dubbo</groupId>
    <artifactId>spring-boot-dubbo-demo</artifactId>
    <packaging>pom</packaging>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>spring-boot-dubbo-demo-api</module>
        <module>spring-boot-dubbo-demo-provider</module>
        <module>spring-boot-dubbo-demo-consumer</module>
    </modules>

    <properties>
        <spring-boot.version>2.2.1.RELEASE</spring-boot.version>
    </properties>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>
</project>

spring-boot-dubbo-demo-api

The API project is simply an interface class:

package com.tian.dubbo.service;

public interface DemoService {
    String sayHello(String msg);
}

spring-boot-dubbo-demo-provider

The code in the service provider project is explained as follows:

<?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">
    <parent>
        <artifactId>spring-boot-dubbo-demo</artifactId>
        <groupId>com.tian.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-dubbo-demo-provider</artifactId>

    <properties>
        <dubbo.version>3.0.4</dubbo.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.0.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>3.0.2.1</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.tian.dubbo</groupId>
            <artifactId>spring-boot-dubbo-demo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
    </dependencies>

</project>

Zookeeper, Nacos and related starter s are integrated.

application.properties configuration item

application.properties configuration content:

## Random port
server.port=-1
spring.application.name=spring-boot-dubbo-demo-provider


dubbo.protocol.name=dubbo
dubbo.protocol.port=-1
## dubbo.registries.name.address
## Name can be understood as the name of the registry
dubbo.registries.shanghai.address=zookeeper://127.0.0.1:2181
dubbo.registries.shanghai.timeout=10000 

dubbo.registries.beijing.address=nacos://127.0.0.1:8848

We matched the Zookeeper and Nacos addresses.

DemoServiceImpl business implementation class

package com.tian.dubbo.service;

import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class DemoServiceImpl implements DemoService {
    public String sayHello(String msg) {
        System.out.println("provider receive msg : " + msg);
        return "provider return " + msg;
    }
}

In Spring related projects, we usually use the annotation @ Service to indicate that we are a Service Bean, so Dubbo also made a set, but at this time, the annotation @ DubboService is used.

If my Service only wants to register in one registry, we can use:

@DubboService(registry = {"beijing"})

The @ DubboService annotation defines the following contents:

@DubboService

  • Version service version number
  • Timeout timeout
  • Retries retries
  • Registry registry
  • ...

ProviderMain project startup class

package com.tian.dubbo;

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

@DubboComponentScan(basePackages = "com.tian.dubbo.service")
@SpringBootApplication
public class ProviderMain {

    public static void main(String[] args) {
        SpringApplication.run(ProviderMain.class, args);
    }

}

Above, we have annotated the business implementation class @ DubboService. Like Spring, we need to scan ComponentScan.

Start the provider project

After the project is started, we return to the Nacos registry:

Prove that our project has been registered on Nacos

By the way, let's check out providers:com.tian.dubbo.service.DemoService:: for details.

Take another look at the Zookeeper registry:

As can be seen from the figure, our service has been successfully registered.

Here, our service provider has registered the service in the registry.

Let's look at consumers.

spring-boot-dubbo-demo-consumer

pom.xml is similar to the service provider:

<?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">
    <parent>
        <artifactId>spring-boot-dubbo-demo</artifactId>
        <groupId>com.tian.dubbo</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>spring-boot-dubbo-demo-consumer</artifactId>

    <dependencies>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>3.0.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>3.0.2.1</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba.nacos</groupId>
            <artifactId>nacos-client</artifactId>
            <version>2.0.2</version>
        </dependency>
        <dependency>
            <groupId>com.tian.dubbo</groupId>
            <artifactId>spring-boot-dubbo-demo-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>

A web starter is added.

application.properties configuration file

spring.application.name=spring-boot-dubbo-sample-consumer


dubbo.registries.shanghai.address=zookeeper://127.0.0.1:2181
dubbo.registries.shanghai.timeout=10000 

dubbo.registries.beijing.address=nacos://127.0.0.1:8848

server.port=20882

We also configure two registries.

controller test class

We write a Controller on the consumer side:

package com.tian.dubbo.controller;


import com.tian.dubbo.service.DemoService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {

    @DubboReference(registry = {"shanghai","beijing"})
    private DemoService demoService;

    @GetMapping("/sayHello")
    public String sayHello(){
        return demoService.sayHello("tian");
    }

}

In the Spring project, we usually use the annotation @ Autowired or @ Service to inject a Service, but when using Dubbo Service injection, the annotation is @ DubboReference.

Similarly, @ DubboService can also specify one or more registries.

About the @ DubboReference annotation definition method:

@DubboReference

In fact, we can basically guess what these are used for. For example:

  • Version is used to upgrade the service version
  • Check is to check whether the service is available when the service is started
  • url is basically used when developing joint debugging, bypassing the registry and directly connecting to the service
  • timaout timeout setting
  • Registry registry
  • ...

The official websites are all Chinese documents, so we won't introduce them one by one here.

Startup class

package com.tian.dubbo;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class ConsumerMain {
    public static void main(String[] args) {
        SpringApplication.run(ConsumerMain.class, args);
    }
}

The startup class is nothing special, just like the startup of an ordinary Spring project.

Next, let's verify it.

verification

We visit: http://localhost:20882/sayHello

Return: provider return tian

It proves that our consumers have successfully called the service and returned the correct information.

Well, this demonstration has been successfully completed.

Simple summary

In this demonstration case, we simply configured the registry information about the contents of the Dubbo configuration file. In fact, we can also configure the default registration center, weight and other information. Please refer to the official website for relevant configuration suggestions.

Official document address:

https://dubbo.apache.org/zh/docs/advanced/multi-registry/

Dubbo integrates Spring Boot to realize multi registration. The steps are as follows:

  • Construction of Nacos and Zookeeper registration centers
  • Create an api project and print it into a jar package
  • Create a jar package for the api that the service provider project depends on
  • Introduce corresponding Nacos and Zookeeper dependent jar packages
  • Both consumers and providers add spring boot starter dependencies
  • Both consumers and providers add Dubbo spring boot starter dependencies
  • Both consumers and providers add the application.properties configuration file and configure the project configuration items
  • The annotation @ DubboServie needs to be added in the Service implementation class of the provider
  • The consumer needs to inject Service and use the annotation @ DubboReference

Well, that's what we're sharing today.

Next: Dubbo source code analysis series: Dubbo advanced features!

Like friends, I'll see you next time!

Posted on Fri, 26 Nov 2021 07:30:35 -0500 by satya61229