Dubbo(53)如何在Spring Boot中集成Dubbo?

news2025/4/15 21:01:21

在Spring Boot中集成Dubbo可以通过Spring Boot Starter来简化配置,以下是详细的步骤和相关代码示例。

1. 引入依赖

首先,在Spring Boot项目的 pom.xml 中添加Dubbo相关的依赖:

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- Spring Boot Starter for Dubbo -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version> <!-- 使用合适的版本 -->
    </dependency>

    <!-- Spring Boot Starter for Nacos (作为配置中心和注册中心) -->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>0.2.10</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-discovery-spring-boot-starter</artifactId>
        <version>0.2.10</version>
    </dependency>
</dependencies>

2. 配置Dubbo

application.yml 中配置Dubbo和Nacos:

spring:
  application:
    name: dubbo-demo-provider

  # 配置Nacos作为注册中心和配置中心
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml

dubbo:
  application:
    name: dubbo-demo-provider
  registry:
    address: nacos://127.0.0.1:8848
  protocol:
    name: dubbo
    port: 20880
  scan:
    base-packages: com.example.dubbo.provider

3. 定义服务接口

定义一个服务接口,例如:

package com.example.dubbo;

public interface MyService {
    String sayHello(String name);
}

4. 实现服务提供者

在Spring Boot项目中实现服务提供者,并使用 @DubboService 注解:

package com.example.dubbo.provider;

import com.example.dubbo.MyService;
import org.apache.dubbo.config.annotation.DubboService;

@DubboService
public class MyServiceImpl implements MyService {
    @Override
    public String sayHello(String name) {
        return "Hello, " + name;
    }
}

5. 启动类

创建Spring Boot的启动类:

package com.example.dubbo;

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

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

6. 服务消费者

在另一个Spring Boot项目中配置Dubbo和Nacos,并编写服务消费者代码。

服务消费者 pom.xml

<dependencies>
    <!-- Spring Boot Starter -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>

    <!-- Spring Boot Starter for Dubbo -->
    <dependency>
        <groupId>org.apache.dubbo</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>2.7.8</version> <!-- 使用合适的版本 -->
    </dependency>

    <!-- Spring Boot Starter for Nacos (作为配置中心和注册中心) -->
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-config-spring-boot-starter</artifactId>
        <version>0.2.10</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>nacos-discovery-spring-boot-starter</artifactId>
        <version>0.2.10</version>
    </dependency>
</dependencies>

服务消费者 application.yml

spring:
  application:
    name: dubbo-demo-consumer

  # 配置Nacos作为注册中心和配置中心
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: yaml

dubbo:
  application:
    name: dubbo-demo-consumer
  registry:
    address: nacos://127.0.0.1:8848
  scan:
    base-packages: com.example.dubbo.consumer

服务消费者代码:

package com.example.dubbo.consumer;

import com.example.dubbo.MyService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

@Component
public class MyServiceConsumer implements CommandLineRunner {

    @DubboReference
    private MyService myService;

    @Override
    public void run(String... args) throws Exception {
        System.out.println(myService.sayHello("Dubbo"));
    }
}

服务消费者启动类:

package com.example.dubbo;

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

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

7. 启动Nacos

确保Nacos配置中心和注册中心在本地或远程服务器上运行。可以从Nacos官网下载并启动Nacos。

8. 启动服务提供者和消费者

  1. 启动服务提供者:运行 DubboProviderApplication 类,确保服务成功注册到Nacos。
  2. 启动服务消费者:运行 DubboConsumerApplication 类,调用服务并检查结果。

总结

通过以上步骤,我们成功地在Spring Boot中集成了Dubbo,并实现了服务提供者和消费者的示例。关键步骤包括:

  1. 引入依赖:在Spring Boot项目的 pom.xml 中添加Dubbo相关依赖。
  2. 配置Dubbo:在 application.yml 中配置Dubbo和Nacos。
  3. 定义服务接口:定义服务接口。
  4. 实现服务提供者:在Spring Boot项目中实现服务提供者,并使用 @DubboService 注解。
  5. 编写启动类:创建Spring Boot的启动类。
  6. 配置服务消费者:在另一个Spring Boot项目中配置Dubbo和Nacos,并编写服务消费者代码。
  7. 启动Nacos:确保Nacos配置中心和注册中心在本地或远程服务器上运行。
  8. 启动服务提供者和消费者:运行服务提供者和消费者的启动类,调用服务并检查结果。

通过这些步骤,可以有效地在Spring Boot中集成Dubbo,实现分布式服务的开发和调用。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2334768.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

XDocument和XmlDocument的区别及用法

因为这几天用到了不熟悉的xml统计数据&#xff0c;啃了网上的资料解决了问题&#xff0c;故总结下xml知识。 1.什么是XML?2.XDocument和XmlDocument的区别3.XDocument示例1示例2&#xff1a;示例3&#xff1a; 4.XmlDocument5.LINQ to XML6.XML序列化(Serialize)与反序列化(De…

Blender安装基础使用教程

本博客记录安装Blender和基础使用&#xff0c;可以按如下操作来绘制标靶场景、道路标识牌等。 目录 1.安装Blender 2.创建面板资源 步骤 1: 设置 Blender 场景 步骤 2: 创建一个平面 步骤 3: 将 PDF 转换为图像 步骤 4-方法1: 添加材质并贴图 步骤4-方法2&#xff1a;创…

【Git】从零开始使用git --- git 的基本使用

哪怕是野火焚烧&#xff0c;哪怕是冰霜覆盖&#xff0c; 依然是志向不改&#xff0c;依然是信念不衰。 --- 《悟空传》--- 从零开始使用git 了解 Gitgit创建本地仓库初步理解git结构版本回退 了解 Git 开发场景中&#xff0c;文档可能会经历若干版本的迭代。假如我们不进行…

Android 中支持旧版 API 的方法(API 30)

Android 中最新依赖库的版本支持 API 31 及以上版本&#xff0c;若要支持 API30&#xff0c;则对应的依赖库的版本就需要使用旧版本。 可通过修改模块级 build.gradle 文件来进行适配。 1、android 标签的 targetSdk 和 compileSdk 版本号 根据实际目标设备的 android 版本来…

[特殊字符] Hyperlane:Rust 高性能 HTTP 服务器库,开启 Web 服务新纪元!

&#x1f680; Hyperlane&#xff1a;Rust 高性能 HTTP 服务器库&#xff0c;开启 Web 服务新纪元&#xff01; &#x1f31f; 什么是 Hyperlane&#xff1f; Hyperlane 是一个基于 Rust 语言开发的轻量级、高性能 HTTP 服务器库&#xff0c;专为简化网络服务开发而设计。它支…

RIP V2路由协议配置实验CISCO

1.RIP V2简介&#xff1a; RIP V2&#xff08;Routing Information Protocol Version 2&#xff09;是 RIP 路由协议的第二版&#xff0c;属于距离矢量路由协议&#xff0c;主要用于中小型网络环境。相较于 RIP V1&#xff0c;RIP V2 在功能和性能上进行了多项改进&#xff0c…

《LNMP架构+Nextcloud私有云超维部署:量子级安全与跨域穿透实战》

项目实战-使用LNMP搭建私有云存储 准备工作 恢复快照&#xff0c;关闭安全软件 [rootserver ~]# setenforce 0[rootserver ~]# systemctl stop firewalld搭建LNMP环境 [rootserver ~]# yum install nginx mariadb-server php* -y# 并开启nginx服务并设置开机自启 [r…

3DMAX笔记-UV知识点和烘焙步骤

1. 在展UV时&#xff0c;如何点击模型&#xff0c;就能选中所有这个模型的uv 2. 分多张UV时&#xff0c;不同的UV的可以设置为不同的颜色&#xff0c;然后可以通过颜色进行筛选。 3. 烘焙步骤 摆放完UV后&#xff0c;要另存为一份文件&#xff0c;留作备份 将模型部件全部分成…

【新人系列】Golang 入门(十三):结构体 - 下

✍ 个人博客&#xff1a;https://blog.csdn.net/Newin2020?typeblog &#x1f4dd; 专栏地址&#xff1a;https://blog.csdn.net/newin2020/category_12898955.html &#x1f4e3; 专栏定位&#xff1a;为 0 基础刚入门 Golang 的小伙伴提供详细的讲解&#xff0c;也欢迎大佬们…

Spring Boot 自定义商标(Logo)的完整示例及配置说明( banner.txt 文件和配置文件属性信息)

Spring Boot 自定义商标&#xff08;Logo&#xff09;的完整示例及配置说明 1. Spring Boot 商标&#xff08;Banner&#xff09;功能概述 Spring Boot 在启动时会显示一个 ASCII 艺术的商标 LOGO&#xff08;默认为 Spring 的标志&#xff09;。开发者可通过以下方式自定义&a…

Ubuntu虚拟机Linux系统入门

目录 一、安装 Ubuntu Linux 20.04系统 1.1 安装前准备工作 1.1.1 镜像下载 1.1.2 创建新的虚拟机 二、编译内核源码 2.1 下载源码 2.2 指定编译工具 2.3 将根文件系统放到源码根目录 2.4 配置生成.config 2.5 编译 三、安装aarch64交叉编译工具 四、安装QEMU 五、…

【蓝桥杯】2025省赛PythonB组复盘

前言 昨天蓝桥杯python省赛B组比完&#xff0c;今天在洛谷上估了下分&#xff0c;省一没有意外的话应该是稳了。这篇博文是对省赛试题的复盘&#xff0c;所给代码是省赛提交的代码。PB省赛洛谷题单 试题 A: 攻击次数 思路 这题目前有歧义&#xff0c;一个回合到底是只有一个…

【数据结构_4下篇】链表

一、链表的概念 链表&#xff0c;不要求在连续的内存空间&#xff0c;链表是一个离散的结构。 链表的元素和元素之间&#xff0c;内存是不连续的&#xff0c;而且这些元素的空间之间也没有什么规律&#xff1a; 1.顺序上没有规律 2.内存空间上也没有规律 *如何知道链表中包…

音视频 五 看书的笔记 MediaCodec

MediaCodec 用于访问底层媒体编解码器框架&#xff0c;编解码组件。通常与MediaExtractor(解封装,例如Mp4文件分解成 video和audio)、MediaSync、MediaMuxer(封装 例如音视频合成Mp4文件)、MediaCrypto、Image(cameraX 回调的ImageReader对象可以获取到Image帧图像,可转换成YU…

ubuntu 系统安装Mysql

安装 mysql sudo apt update sudo apt install mysql-server 启动服务 sudo systemctl start mysql 设置为开机自启 sudo systemctl enable mysql 查看服务状态 &#xff08;看到类似“active (running)”的状态信息代表成功&#xff09; sudo systemctl status mysql …

selenium快速入门

一、操作浏览器 from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By# 设置选项 q1 Options() q1.add_argument("--no-sandbo…

Redis:线程模型

单线程模型 Redis 自诞生以来&#xff0c;一直以高性能著称。很多人好奇&#xff0c;Redis 为什么早期采用单线程模型&#xff0c;它真的比多线程还快吗&#xff1f; 其实&#xff0c;Redis 的“快”并不在于并发线程&#xff0c;而在于其整体架构设计极致简单高效&#xff0c;…

Transformer模型解析与实例:搭建一个自己的预测语言模型

目录 1. 前言 2. Transformer 的核心结构 2.1 编码器&#xff08;Encoder&#xff09; 2.2 解码器&#xff08;Decoder&#xff09; 2.3 位置编码&#xff08;Positional Encoding&#xff09; 3. 使用 PyTorch 构建 Transformer 3.1 导入所需的模块&#xff1a; 3.2 定…

springboot框架集成websocket依赖实现物联网设备、前端网页实时通信!

需求&#xff1a; 最近在对接一个物联网里设备&#xff0c;他的通信方式是 websocket 。所以我需要在 springboot框架中集成websocket 依赖&#xff0c;从而实现与设备实时通信&#xff01; 框架&#xff1a;springboot2.7 java版本&#xff1a;java8 好了&#xff0c;还是直接…

ES6学习03-字符串扩展(unicode、for...of、字符串模板)和新方法()

一、字符串扩展 1. eg: 2.for...of eg: 3. eg: 二。字符串新增方法 1. 2. 3. 4. 5.