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

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

1. 引入依赖

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

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>

2. 配置Dubbo

application.yml 中配置Dubbo和Nacos:

yaml 复制代码
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. 定义服务接口

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

java 复制代码
package com.example.dubbo;

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

4. 实现服务提供者

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

java 复制代码
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的启动类:

java 复制代码
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

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

yaml 复制代码
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

服务消费者代码:

java 复制代码
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"));
    }
}

服务消费者启动类:

java 复制代码
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,实现分布式服务的开发和调用。

相关推荐
tan180°8 小时前
MySQL表的操作(3)
linux·数据库·c++·vscode·后端·mysql
优创学社29 小时前
基于springboot的社区生鲜团购系统
java·spring boot·后端
why技术9 小时前
Stack Overflow,轰然倒下!
前端·人工智能·后端
幽络源小助理9 小时前
SpringBoot基于Mysql的商业辅助决策系统设计与实现
java·vue.js·spring boot·后端·mysql·spring
ai小鬼头10 小时前
AIStarter如何助力用户与创作者?Stable Diffusion一键管理教程!
后端·架构·github
简佐义的博客10 小时前
破解非模式物种GO/KEGG注释难题
开发语言·数据库·后端·oracle·golang
Code blocks11 小时前
使用Jenkins完成springboot项目快速更新
java·运维·spring boot·后端·jenkins
追逐时光者11 小时前
一款开源免费、通用的 WPF 主题控件包
后端·.net