SpringBoot整合Dubbo

xiondun發表於2024-11-14

Dubbo教程(二) | SpringBoot整合Dubbo

  • 一、Dubbo Spring Boot 版本關係
    • Dubbo 官方提供的 對應關係:
    • github 官方提供的 對應關係(該版本說明很久沒有更新了):
  • 二、引入maven
  • 三、專案結構
    • aip模組 構建
    • provider模組 構建
    • customer模組 構建

一、Dubbo Spring Boot 版本關係

在使用 Dubbo 和 Spring Boot 進行微服務開發時,版本的選擇是非常重要的。
不同版本之間可能會存在相容性問題,導致服務無法正常執行。
因此,瞭解 Dubbo Spring Boot 版本關係是非常必要的。

目前網上有兩個方式可以看匹配關係
方式一:Dubbo 官方提供
方式二: github 官方提供(已經過時了,就不要看了)

Dubbo 官方提供的 對應關係:

https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/versions/

Dubbo分支 最新版本 JDK Spring Boot 詳細說明
3.3.x (當前文件) 3.3.0 8, 17, 21 2.x、3.x 生產可用(推薦,長期維護)! 最新Triple協議升級,內建Metrics、Tracing、GraalVM支援等
3.2.x 3.2.10 8, 17 2.x、3.x 生產可用(長期維護)!
3.1.x 3.1.11 8, 17 2.x、3.x 僅修復安全漏洞!
3.0.x 3.0.15 8 2.x 停止維護!
2.7.x 2.7.23 8 2.x 停止維護!
2.6.x 2.6.20 6, 7 - 停止維護!
2.5.x 2.5.10 6, 7 - 停止維護!

github 官方提供的 對應關係(該版本說明很久沒有更新了):

https://cn.dubbo.apache.org/zh-cn/overview/mannual/java-sdk/versions/

舊版本
如果您仍然使用版本低於 2.7.0 的舊版 Dubbo,請使用以下 Spring Boot 啟動器:

Dubbo Spring Boot Dubbo Spring Boot
0.2.1.釋出 2.6.5+ 2.x
0.1.2.釋出 2.6.5+ 1.x

去看manven 官網 倉庫裡面你就會發現。工件已經遷移了
在這裡插入圖片描述

二、引入maven

Dubbo 起步依賴

<!--dubbo整合springboot依賴-->
<dependency>
    <groupId>org.apache.dubbo</groupId>
    <artifactId>dubbo-spring-boot-starter</artifactId>
    <version>2.7.15</version>
</dependency>
123456

ZooKeeper

<dependency>
    <groupId>org.apache.zookeeper</groupId>
    <artifactId>zookeeper</artifactId>
    <version>3.7.0</version>
</dependency>
12345

ZooKeeper API 管理依賴(ZooKeeper的高階操作工具包)

<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-recipes</artifactId>
    <version>5.1.0</version>
</dependency>
12345

三、專案結構

在這裡插入圖片描述
對目錄進行分析:

分為三個模組:

  • api 介面 :該模組必須要有,這裡放置資料和介面)
  • provider 生成者:業務介面提供者,實現介面,實現api模組中的 介面,讓 customer 消費)
  • customer 消費者:呼叫提供者中的介面

父模組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>
    <groupId>com.example</groupId>
    <artifactId>springboot-dubbo-demo</artifactId>

    <packaging>pom</packaging>

    <version>0.0.1-SNAPSHOT</version>

    <modules>
        <module>api</module>
        <module>provider</module>
        <module>customer</module>
    </modules>


    <name>springboot-dubbo-demo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.6.13</spring-boot.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <scope>provided</scope>
        </dependency>

        <!--dubbo整合springboot依賴-->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.15</version>
        </dependency>
        <!-- Zookeeper 客戶端依賴 -->
        <dependency>
            <groupId>org.apache.zookeeper</groupId>
            <artifactId>zookeeper</artifactId>
            <version>3.7.0</version>
        </dependency>
        <!-- ZooKeeper的高階操作工具包-->
        <dependency>
            <groupId>org.apache.curator</groupId>
            <artifactId>curator-recipes</artifactId>
            <version>5.1.0</version>
        </dependency>
    </dependencies>
    
    <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>

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778

aip模組 構建

aip 模組 目錄結構 如下:

在這裡插入圖片描述
實體類

package com.example.api.model;

import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;

@Data
@NoArgsConstructor
public class User implements Serializable {

    private Integer id;
    private String name;
    private String sex;
    private String age;

    public User(Integer id, String name, String sex, String age) {
        this.id = id;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
}

123456789101112131415161718192021222324

介面

package com.example.api.service;

import com.example.api.model.User;

import java.util.List;

public interface UserService {

    List<User> findAll();

    String sayHello(String name);
}

12345678910111213

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">
    <parent>
        <artifactId>springboot-dubbo-demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>api</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>
    
</project>
12345678910111213141516171819

provider模組 構建

provider模組目錄結構 如下:
在這裡插入圖片描述

業務實現類

關鍵註解:
@DubboService:主要用於標記一個服務提供者,使其能夠被 Dubbo 框架識別並註冊到服務註冊中心。

package com.example.provider.service.impl;

import com.example.api.model.User;
import com.example.api.service.UserService;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.LinkedList;
import java.util.List;

@DubboService
public class UserServiceImpl implements UserService {

    @Override
    public List<User> findAll() {

        LinkedList users = new LinkedList();
        User user = new User(1, "小明(來自消費者)", "男", "20");
        User user1 = new User(2, "小紅(來自消費者)", "女", "22");
        users.add(user);
        users.add(user1);
        return users;
    }

    @Override
    public String sayHello(String name) {
        return "hello,"+name;
    }
}
12345678910111213141516171819202122232425262728

啟動類

關鍵註解:
@DubboComponentScan 掃描 Dubbo相關注解

package com.example.provider;

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

@SpringBootApplication
@DubboComponentScan(basePackages = "com.example.provider.*")
public class ProviderApplication {

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

123456789101112131415

application.yml

server:
  port: 8001
#生成者模組
dubbo:
  application:
    name: dubbo-provider
  registry:
    address: zookeeper://127.0.0.1:2181  # zookeeper 註冊中心地址
  protocol:
    name: dubbo
    port: 20880  # dubbo 協議埠,預設為20880
1234567891011

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">
    <parent>
        <artifactId>springboot-dubbo-demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>provider</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.provider.ProviderApplication</mainClass>
                    <!--                    <skip>true</skip>-->
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>


</project>
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162

customer模組 構建

customer模組目錄結構 如下:
在這裡插入圖片描述

控制層

關鍵註解:
@DubboReference :主要作用是在服務消費者端引用遠端服務提供者的服務

package com.example.customer.controller;

import com.example.api.model.User;
import com.example.api.service.UserService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/user")
public class TestController {

    @DubboReference
    private UserService userService;

    @RequestMapping("/findAll")
    public List<User> findAll(){
        return userService.findAll();
    }

    @RequestMapping("sayHello")
    public String sayHello(String name){
        return userService.sayHello(name);
    }

    @RequestMapping("hello/{name}")
    public String hello(@PathVariable String name){
        return "hello"+name;
    }
}

12345678910111213141516171819202122232425262728293031323334

啟動類

關鍵註解:
@DubboComponentScan 掃描 Dubbo相關注解

package com.example.customer;

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

@SpringBootApplication
@DubboComponentScan(basePackages = "com.example.customer.*")
public class CustomerApplication {

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

123456789101112131415

application.yml

server:
  port: 8002
#消費者模組
dubbo:
  application:
    name: dubbo-customer
  registry:
    address: zookeeper://127.0.0.1:2181 # zookeeper 註冊中心地址
12345678

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">
    <parent>
        <artifactId>springboot-dubbo-demo</artifactId>
        <groupId>com.example</groupId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>customer</artifactId>

    <properties>
        <maven.compiler.source>8</maven.compiler.source>
        <maven.compiler.target>8</maven.compiler.target>
    </properties>


    <dependencies>
        <dependency>
            <groupId>com.example</groupId>
            <artifactId>api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
    </dependencies>


    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>${spring-boot.version}</version>
                <configuration>
                    <mainClass>com.example.customer.CustomerApplication</mainClass>
<!--                    <skip>true</skip>-->
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061

參考文章
【1】SpringBoot整合Dubbo實現RPC遠端過程呼叫
【2】springboot 整合 dubbo 教程(註解方式) + 新版 dubbo admin 使用教程
【3】dubbo學習三:springboot整合dubbo+zookeeper,並使用dubbo管理介面監控服務是否註冊到zookeeper上。

相關文章