SpringBoot integrates Dubbo

1. Build SpringBoot environment

1.1 Create a duubo-spring-boot-demo project

  • duubo-spring-boot-demo: is the parent project to facilitate dependency management
  • duubo-spring-boot-demo-consumer: is the service consumer and inherits the parent project
  • duubo-spring-boot-demo-provider: is the service provider and inherits the parent project
  • dubbo-interface: stores the interface, because dubbo is discovered based on service registration based on the interface, so we need to take out the interface separately, the provider implements the interface and registers the service, and the consumer needs to inject based on the interface

1.2 Introducing dependent coordinates

1.2.1 dubbo-parent
<?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>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<dubbo.version>3.2.0-beta.4</dubbo.version>
<spring-boot.version>2.7.8</spring-boot.version>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

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

<!-- Dubbo -->
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-bom</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>

<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
<version>${dubbo.version}</version>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>


<build>
<pluginManagement>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>${spring-boot.version}</version>
</plugin>
</plugins>
</pluginManagement>
</build>


</project>

1.2.2 dubbo-consumer
<?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>
        <groupId>com.itheima</groupId>
        <artifactId>dubbo-spring-boot-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.itheima</groupId>
    <artifactId>dubbo-spring-boot-demo-consumer</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>dubbo-spring-boot-demo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>



    </dependencies>


</project>
1.2.3 dubbo-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">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.itheima</groupId>
        <artifactId>dubbo-spring-boot-demo</artifactId>
        <version>1.0-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <groupId>com.itheima</groupId>
    <artifactId>dubbo-spring-boot-demo-provider</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>

    <dependencies>
        <dependency>
            <groupId>com.itheima</groupId>
            <artifactId>dubbo-spring-boot-demo-interface</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>

        <!-- dubbo -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper-curator5</artifactId>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <artifactId>slf4j-reload4j</artifactId>
                    <groupId>org.slf4j</groupId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- spring boot starter -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    </dependencies>

</project>
1.2.4 dubbo-interface
<?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>
<groupId>com.itheima</groupId>
<artifactId>dubbo-spring-boot-demo-interface</artifactId>
<version>0.0.1-SNAPSHOT</version>

</project>

2. Linux environment configuration

2.1 Install docker

Reference https://blog.csdn.net/qq_41428418/article/details/133786336

2.2 Install zookeeper

Reference https://blog.csdn.net/qq_41428418/article/details/133857185

3. yml configuration file

3.1 provider yml configuration

server:
  port: 9000
Dubbo:
  application:
    name: dubbo-provider
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://${zookeeper.address:192.168.56.10}:2181

Add the @EnableDubbo annotation to the startup class

3.2 consumer provider yml configuration

server:
  port: 8000
Dubbo:
  application:
    name: dubbo-consumer
    qos-port: 33333
  protocol:
    name: dubbo
    port: -1
  registry:
    address: zookeeper://${zookeeper.address:192.168.56.10}:2181

If the consumer does not provide services, that is, does not register the interface to the registration center, it does not need to add the @EnableDubbo annotation.

4. Service testing

4.1 New interface

package com.itheima.service;
public interface UserService {<!-- -->
    public String sayHello();
}

4.2 Interface implementation

Implement the UserService created in dubbo-interface in dubbo-provider

package com.itheima.service.impl;

import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;
import org.apache.dubbo.config.annotation.Service;


//@Service defines bean
//Publish the methods (services) provided by this class to the outside world. Register the accessed address IP, port, and path to the registration center
@DubboService
public class UserServiceImpl implements UserService {<!-- -->
    @Override
    public String sayHello() {<!-- -->
        return "hello dubbo!~welcome!";
    }
}

4.3 Service Reference

Create a controller in dubbo-consumer and reference UserService.

package com.itheima.controller;


import com.itheima.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class UserController {<!-- -->
    //1. Obtain the access url of userService from the zookeeper registration center
    //2. Make remote RPC calls
    //3. Encapsulate the result into a proxy object and assign a value to the variable
    @DubboReference
    private UserService userService;
    @GetMapping("/sayHello")
    public String sayHello() {<!-- -->
        return userService.sayHello();
    }

}

4.4 Service Test

4.3.1 Let’s first check the information in zookeeper on the server

1. Execute docker ps to view the status of the zookeeper container, and copy the CONTAINER ID corresponding to zookeeper

2. Execute docker exec -it ${CONTAINER ID} /bin/bash

3. After entering, run ls to view the directory and execute cd bin to enter the bin directory.

4. Execute the ./zkCli.sh script to connect to the zookeeper client

5. Execute ls / to view the directory. We can see that there is only one zookeeper now.

4.3.2 Start dubbo-provider and dubbo-consumer
4.3.3 View zookeeper content after startup

1. After the service is started, execute ls / to view the content and you will see that there is an extra dubbo.

2. Execute ls /dubbo to view and you can see the path of the interface we registered.

3. Continue to check. You can see that there are still consumers and providers. Check the consumers and providers and you can see the specific registration center. At this point, it means that we have completed the registration.

4.3.4 Browser access test

Official Chinese documentation