Spring Boot 和 Dubbo整合(注解方式配置)

注:1. Dubbo的注解方式需要升级到 2.5.7 及以上版本

  2. 本工程结构为Spring Boot + Maven多模块的方式,其中:

    "xxx-api"模块为接口类定义模块,

    "xxx-service"为服务提供方模块;

    "xxx-web"为服务消费方模块

  

一、服务提供商(xxx-service模块):

1.提供方全局配置:

复制代码
@Configuration
public class DubboConfiguration {

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("provider-test");
        return applicationConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");return registryConfig;
    }
}
复制代码

2.service注解暴露服务:

import com.alibaba.dubbo.config.annotation.Service;

@Component //此注解不要忘记
@Service(timeout = 5000) public class AnnotateServiceImpl implements AnnotateService { 
  // 具体实现方法
}

 

3.Spring Boot启动类添加服务扫描:

@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service.impl")
public class ProviderTestApp {
    // 启动boot的main方法
}

 

二、服务消费方(xxx-web模块):

1.消费方全局配置

复制代码
@Configuration
public class DubboConfiguration {

    @Bean
    public ApplicationConfig applicationConfig() {
        ApplicationConfig applicationConfig = new ApplicationConfig();
        applicationConfig.setName("consumer-test");
        return applicationConfig;
    }

    @Bean
    public ConsumerConfig consumerConfig() {
        ConsumerConfig consumerConfig = new ConsumerConfig();
        consumerConfig.setTimeout(3000);
        return consumerConfig;
    }

    @Bean
    public RegistryConfig registryConfig() {
        RegistryConfig registryConfig = new RegistryConfig();
        registryConfig.setAddress("zookeeper://127.0.0.1:2181");
        registryConfig.setClient("curator");
        return registryConfig;
    }
}
复制代码

2.消费方注解引用服务:

public class AnnotationConsumeService {

    @com.alibaba.dubbo.config.annotation.Reference
    public AnnotateService annotateService;
    
    // ...
}

3.Spring Boot启动类添加服务扫描:

@SpringBootApplication
@DubboComponentScan(basePackages = "com.alibaba.dubbo.test.service")
public class ConsumerTestApp {
    // ...
}

 

至此,即可完成了springboot和Dubbo的基本整合。

posted @   MalcolmFeng  阅读(8304)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
点击右上角即可分享
微信分享提示