SpringBoot快速集成Dubbo(附带源码)

举报
长路 发表于 2022/11/28 20:19:49 2022/11/28
【摘要】 刚开始搜了许多springboot集成dubbo案例,试了两三个都不行,之后我是根据慕课网的一个Dubbo案例来进行跟着学习搭建。当前项目是Dubbo+Zookeeper的一个简洁版集成案例,以Zookeeper作为注册中心,所以请提前进行Zookeeper的安装配置。配套代码:github-springboot-dubbo、gitee-springboot-dubbo所有博客文件目录索引:博客目

@[toc]

前言

刚开始搜了许多springboot集成dubbo案例,试了两三个都不行,之后我是根据慕课网的一个Dubbo案例来进行跟着学习搭建。

当前项目是Dubbo+Zookeeper的一个简洁版集成案例,以Zookeeper作为注册中心,所以请提前进行Zookeeper的安装配置。

配套代码github-springboot-dubbogitee-springboot-dubbo

所有博客文件目录索引博客目录索引(持续更新)


项目代码

项目目录

采用模块化进行开发:

image-20220607153104993

  • dubbo-consumer:消费者模块,这个模块就是对外暴露api接口的,其中接口的service会去进行一个RPC调用。
  • dubbo-interface:rpc接口调用模块,主要来定义service接口与一些实体类。
  • dubbo-producer:生产者模块,这个模块会在启动时注册到zookeeper。

pom.xml:父pom依赖

<?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.changlu</groupId>
    <artifactId>springboot-dubbo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <modules>
        <module>dubbo-producer</module>
        <module>dubbo-interface</module>
        <module>dubbo-consumer</module>
    </modules>
    <packaging>pom</packaging>

    <!-- 父模块依赖   -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.12.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <java.version>1.8</java.version>
        <spring-boot.version>2.1.12.RELEASE</spring-boot.version>
        <dubbo.version>2.7.4.1</dubbo.version>
    </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>

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

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

            <!--      引入自己的依赖模块      -->
            <dependency>
                <groupId>com.changlu</groupId>
                <artifactId>dubbo-interface</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
            <dependency>
                <groupId>com.changlu</groupId>
                <artifactId>dubbo-producer</artifactId>
                <version>1.0-SNAPSHOT</version>
            </dependency>
        </dependencies>
    </dependencyManagement>
    
</project>

dubbo-interface

首先进行接口模块的定义,主要是定义其中的service接口:

image-20220607153727718

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</artifactId>
        <groupId>com.changlu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-interface</artifactId>

</project>

pojo/user.java

package com.changlu.pojo;

import java.io.Serializable;

/**
 * @ClassName User
 * @Author ChangLu
 * @Date 6/7/2022 2:43 PM
 * @Description 用户类
 */
public class User implements Serializable {  //注意进行序列化

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

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

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                '}';
    }
}

/service/UserService.java

package com.changlu.service;

import com.changlu.pojo.User;

/**
 * @ClassName UserService
 * @Author ChangLu
 * @Date 6/7/2022 2:44 PM
 * @Description 用户业务接口
 */
public interface UserService {
    
    User getUserById(Integer id);
    
}

dubbo-producer

image-20220607153610237

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</artifactId>
        <groupId>com.changlu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-producer</artifactId>

    <dependencies>
        <!--   SpringBoot依赖     -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <!-- Web 功能 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.4.1</version>
        </dependency>

        <!--   dubbo依赖     -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>

        <!-- Zookeeper dependencies -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--    引入自定义模块:接口   -->
        <dependency>
            <groupId>com.changlu</groupId>
            <artifactId>dubbo-interface</artifactId>
        </dependency>
    </dependencies>


</project>

application.yml

# 手动指定版本号,分别表示:大版本.中版本.小版本。【大版本一般以年为单位,中版本一般是指某个功能大的迭代,小版本则是小功能的升级】
demo:
  service:
    version: 1.0.0

server:
  port: 8801

spring:
  application:
    name: producer

dubbo:
  # 协议
  protocol:
    name: dubbo
    port: -1
  # 注册配置
  registry:
    # 注册地址
    address: zookeeper://192.168.10.40:2181
    # 缓存信息
    file: C:\Users\93997\Desktop\upload\/dubbo-cache\${spring.application.name}\dubbo.cache
  # 扫描指定的service包
  scan:
    base-packages: com.changlu.service

service/UserServiceImpl.java

package com.changlu.service;

import com.changlu.pojo.User;
import org.apache.dubbo.config.annotation.Service;

/**
 * @ClassName UserServiceImpl
 * @Author ChangLu
 * @Date 6/7/2022 2:51 PM
 * @Description 用户业务实现类
 */
//注意:这里的service是dubbo中的service注解
@Service(version = "${demo.service.version}")
public class UserServiceImpl implements UserService{
    @Override
    public User getUserById(Integer id) {
        return new User("changlu", "男", 18);
    }
}

DubboProducerApplication.java

package com.changlu;

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

/**
 * @ClassName DubboProducerApplication
 * @Author ChangLu
 * @Date 6/7/2022 2:52 PM
 * @Description 启动器
 */
@EnableAutoConfiguration
public class DubboProducerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DubboProducerApplication.class, args);
    }
}

dubbo-consumer

image-20220607154343955

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</artifactId>
        <groupId>com.changlu</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>dubbo-consumer</artifactId>

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

        <!-- Web 功能 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- Dubbo Spring Boot Starter -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-spring-boot-starter</artifactId>
            <version>2.7.4.1</version>
        </dependency>

        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo</artifactId>
        </dependency>
        <!-- Zookeeper dependencies -->
        <dependency>
            <groupId>org.apache.dubbo</groupId>
            <artifactId>dubbo-dependencies-zookeeper</artifactId>
            <version>${dubbo.version}</version>
            <type>pom</type>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!--   自定义依赖     -->
        <dependency>
            <groupId>com.changlu</groupId>
            <artifactId>dubbo-interface</artifactId>
        </dependency>
    </dependencies>
</project>

application.yml

# 手动指定版本号,分别表示:大版本.中版本.小版本。【大版本一般以年为单位,中版本一般是指某个功能大的迭代,小版本则是小功能的升级】
demo:
  service:
    version: 1.0.0


server:
  port: 8802

spring:
  application:
    name: consumer

dubbo:
  # 通信协议
  protocol:
    name: dubbo
    port: -1   # -1表示随机使用一个端口号进行监听,对于服务发现并不是靠端口号,无需特别关注
  # 注册配置
  registry:
    # 注册地址
    address: zookeeper://192.168.10.40:2181
    # 缓存信息
    file: C:\Users\93997\Desktop\upload\/dubbo-cache\${spring.application.name}\dubbo.cache

controller/UserController.java·:

package com.changlu.controller;

import com.changlu.pojo.User;
import com.changlu.service.UserService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @ClassName UserController
 * @Author ChangLu
 * @Date 6/7/2022 3:04 PM
 * @Description 用户控制器
 */
@RestController
public class UserController {

    //使用dubbo提供依赖注入注解,指定版本号
    @Reference(version = "${demo.service.version}")
    private UserService userService;

    @GetMapping("/user")
    public User userInfo(){
        return userService.getUserById(123);
    }

}

DubboConsumerApplication.java

package com.changlu;

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

/**
 * @ClassName DubboConsumerApplication
 * @Author ChangLu
 * @Date 6/7/2022 3:06 PM
 * @Description 启动器
 */
@SpringBootApplication
public class DubboConsumerApplication {

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

}

测试演示

image-20220607154743182

1、首先启动生产者也就是先让对应的服务进行注册到zookeeper注册中心上:注意若是出现以下信息表示注册成功

image-20220607154838995

image-20220607154906524

2、启动消费者,也就是我们的web模块

若是出现以下log信息同样也表示连接zookeeper成功

image-20220607155004752

3、最后打开浏览器来进行接口测试:出现以下信息就表示RPC调用成功,你也可以自己通过debug来进行调试

image-20220607155056995

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。