当前位置: 首页 > news >正文

Dubbo入门-通过spring-boot来动手实践

第一种--快速创建应用

可以直接下载示例项目,链接:https://github.com/apache/dubbo-samples/tree/master/11-quickstart

第二种--新建项目

新建 Java 空白 Maven 项目

  • jdk17

之后我们还需要创建 dubbo-spring-boot-demo-interfacedubbo-spring-boot-demo-provider dubbo-spring-boot-demo-consumer 三个子模块。

image

在父项目的pom.xml中 添加:

<groupId>org.example</groupId><artifactId>dubbo-spring-boot-demo</artifactId><version>1.0-SNAPSHOT</version><packaging>pom</packaging><modules><module>dubbo-spring-boot-demo-interface</module><module>dubbo-spring-boot-demo-provider</module><module>dubbo-spring-boot-demo-consumer</module></modules><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>

consumerprovider模块的pom.xml中 添加:

<dependencies><dependency><groupId>org.example</groupId><artifactId>dubbo-spring-boot-demo-interface</artifactId><version>${project.parent.version}</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>

在这份配置中,定义了 dubbo 和 zookeeper(以及对应的连接器 curator)的依赖。
添加了上述的配置以后,先通过mvn clean install构建interface模块,之后 可以通过 Mvn - Reload All Maven Projects 刷新依赖。

定义服务接口

服务接口 Dubbo 中沟通消费端和服务端的桥梁。

dubbo-spring-boot-demo-interface 模块的 org.apache.dubbo.samples.api 下建立 DemoService 接口

package org.apache.dubbo.springboot.demo;public interface DemoService {String sayHello(String name);
}

DemoService 中,定义了 sayHello 这个方法。后续服务端发布的服务通过实现 DemoService 接口,消费端订阅的服务也是围绕这个接口的。

实现接口服务

定义了服务接口之后,可以在服务端这一侧定义对应的实现,这部分的实现相对于消费端来说是远端的实现,本地没有相关的信息。

image

DemoServiceImpl 类中添加了 @DubboService 注解,通过这个配置可以基于 Spring Boot 去发布 Dubbo 服务

配置YAML文件

通过 Spring Boot 的方式配置 Dubbo 的一些基础信息。例如这个例子:定义了 Dubbo 的应用名、Dubbo 协议信息、Dubbo 使用的注册中心地址。

在服务端和消费端分别创建application.yml文件

# 服务端
dubbo:application:name: dubbo-springboot-demo-providerprotocol:name: dubboport: -1registry:address: zookeeper://${zookeeper.address:127.0.0.1}:2181
------------------------------------------------------------------
# 消费端
dubbo:application:name: dubbo-springboot-demo-consumerprotocol:name: dubboport: -1registry:address: zookeeper://${zookeeper.address:127.0.0.1}:2181

创建Spring启动类

基于 Spring 配置服务端启动类

package org.apache.dubbo.springboot.demo.provider;import org.apache.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
@EnableDubbo
public class ProviderApplication {public static void main(String[] args) {SpringApplication.run(ProviderApplication.class, args);}
}

这个服务端,消费端同理。在这启动类里,配置了ProviderApplication去读取刚才的YAML配置文件并启动应用。

写一个消费端的服务调用

package org.apache.dubbo.springboot.demo.consumer;import java.util.Date;import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.dubbo.springboot.demo.DemoService;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;@Component
public class Task implements CommandLineRunner {@DubboReferenceprivate DemoService demoService;@Overridepublic void run(String... args) throws Exception {String result = demoService.sayHello("xf");System.out.println("Receive result ======> " + result);new Thread(()-> {while (true) {try {Thread.sleep(1000);System.out.println(new Date() + " Receive result ======> " + demoService.sayHello("world"));} catch (InterruptedException e) {e.printStackTrace();Thread.currentThread().interrupt();}}}).start();}
}

在这个类中,通过@DubboReferenceDubbo 获取了一个 RPC 订阅,这个 demoService 可以像本地调用一样直接调用。在 run方法中创建了一个线程进行调用。

启动应用

  1. 在启动应用前,需要启动zookeeper当注册中心。
    在zookeeper的bin目录下使用命令行启动./zkServer.sh start
  2. 启动 org.apache.dubbo.samples.provider.Application ,等待一会出现如下图所示的日志(Current Spring Boot Application is await)即代表服务提供者启动完毕,标志着该服务提供者可以对外提供服务了。
    image
  3. 启动org.apache.dubbo.samples.client.Application ,等待一会出现如下图所示的日志(Hello world )即代表服务消费端启动完毕并调用到服务端成功获取结果。
    image
http://www.hskmm.com/?act=detail&tid=33167

相关文章:

  • 使用ceph rdb做k8s后端存储(ceph-csi storageclass
  • 2025 最新火烧板源头厂家推荐排行榜:自有矿山加持 + 品质卓越,芝麻灰 / 五莲花等石材采购优选指南
  • 【IEEE出版、吉首大学主办】第二届智能机器人与自动控制国际学术会议 (IRAC 2025)
  • 第三次课动手动脑合集
  • 2025 年火山石厂家最新推荐排行榜:聚焦自有矿藏与全自动生产,涵盖滤料填料等多品类企业权威指南人工湿地填料/人工湿地滤料/黑色/红色火山石厂家推荐
  • 【SPIE出版、往届已检索】第十届能源系统、电气与电力国际学术会议 (ESEP 2025)
  • 2025 防火隔断厂家最新推荐排行榜:甲级防火玻璃隔断厂家深度测评,精选优质品牌助力采购决策
  • 作业3
  • 2025-10-17
  • 2025 年最新推荐!国内优质球墨铸铁管厂家排行榜,涵盖市政 / 给水 / 水利工程用管,助力采购高效选材
  • Nexpose 8.24.0 for Linux Windows - 漏洞扫描
  • 用于博客美化的测试(后面再更新)
  • 2025 年最新留学中介机构推荐排行榜:覆盖英美澳加新等热门地,专业团队护航学子海外深造之选
  • 有趣评测小程序系统:开启视频与答题变现新创业风口
  • 看图猜成语达人小程序:一站式趣味与变现解决方案
  • Metasploit Pro 4.22.8-20251014 (Linux, Windows) - 专业渗透测试框架
  • 设备租赁归还小程序系统:免人工化租赁管理解决方案
  • Navcat如何上传数据大的sql文件?
  • 实测PaddleOCR-VL:文心4.5最强衍生模型如何重构文档处理效率
  • 高考语文 词类活用
  • 使用Scalar.AspNetCore来管理你的OpenApi
  • 2025年市面上景观灯品牌推荐排行榜:权威解析与选择指南
  • 设计社会意识算法的三大关键问题
  • 【转】[C#] 项目里的配置文件与选项对比
  • Windows定时任务 每隔一段时间(最小到秒级)执行一次指定的Python脚本
  • 深入解析米尔全志T536核心板的实时性技术突破
  • 2025年西安买房终极指南:十大热门楼盘排名揭秘
  • PHPMyAdmin上传SQL文件报错:413 Request Entity Too Large
  • Nessus Professional 10.10 Auto Installer for RHEL 10, AlmaLinux 10, Rocky Linux 10
  • 2025 年电动球阀厂家最新推荐榜:覆盖智能灌溉、物联网、远程控制等场景,深度解析行业优质企业及选择指南