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

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

在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,实现分布式服务的开发和调用。

相关文章:

  • 批量给dwg显示略缩图_c#插件实现(com)
  • Tkinter图像和多媒体处理
  • 【深度学习】PyTorch实现VGG16模型及网络层数学原理
  • OpenCV 图像拼接
  • 使用U盘安装 ubuntu 系统
  • SpringBoot 动态路由菜单 权限系统开发 菜单权限 数据库设计 不同角色对应不同权限
  • 量化交易 - 聚宽joinquant - 多因子入门研究 - 源码开源
  • 高效数据拷贝方法总结
  • 第16届蓝桥杯c++省赛c组个人题解
  • 基于spring boot的交通旅游订票系统
  • 输入输出系统(I/O系统)
  • 记一次项目上线404--Nginx配置文件
  • 【mllm】——qnn后端解读
  • Linux多线程同步与互斥:从互斥锁原理到死锁防范的深度实践
  • Tkinter事件与绑定
  • 计算机组成原理笔记(十五)——3.5指令系统的发展
  • 使用FormData格式上传图片
  • zk(Zookeeper)实现分布式锁
  • Java基本数据类型与包装类的区别
  • Linux安装开源版MQTT Broker——EMQX服务器环境从零到一的详细搭建教程
  • 马上评丨全民定制公交,打开城市出行想象空间
  • 教育部答澎湃:2025世界数字教育大会将发布系列重磅成果
  • 明明睡够了,怎么还有黑眼圈?可能是身体在求救
  • 纪念|古文字学泰斗裘锡圭:“还有很多事情要做”
  • 马上评|让“贾宝玉是长子长孙”争议回归理性讨论
  • 英国和美国就关税贸易协议条款达成一致