Spring Boot配置Dubbo

本文介绍如何使用Spring Boot集成Dubbo实现微服务架构,包括配置依赖、Zookeeper注册中心配置及服务提供者与消费者的实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先理解一下下面依赖
配置1.

<dependency>
    <groupId>io.dubbo.springboot</groupId>
    <artifactId>spring-boot-starter-dubbo</artifactId>
</dependency>

这个配置依赖三个maven依赖
配置2.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>dubbo</artifactId>
</dependency>
<dependency>
    <groupId>com.101tec</groupId>
    <artifactId>zkclient</artifactId>
</dependency>

java版本要求1.6+
我们只需要添加配置1就可以了,然后添加zk的配置
服务端配置(生产者)

spring:
  dubbo:
    application:
      name: provider
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      prot: 20880
    scan: store.bymin.user

客户端配置(消费者)

spring:
  dubbo:
    application:
      name: consumer
    registry:
      address: zookeeper://127.0.0.1:2181
    scan: store.bymin.backend
  redis:
    host: localhost

配置完成后我们就可以使用了
生产者提供方法
在实现类上添加dubbo包中的Service(它也会把实现类送给spring管理)

import com.alibaba.dubbo.config.annotation.Service;
@Service(version="1.0.0")
public class DictServiceImpl implements DictService {
    @Override
    public void test() {
        System.out.println("test-------------");
    }
}

需要注意的是;这个类必须放到dubbo扫描的包中(在配置中写的scan配置)

消费者使用方法
需要使用dubbo包中的Reference进行自动注入

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

import store.bymin.common.data.ApplicationResult;
import store.bymin.user.service.DictService;

@RestController
@RequestMapping("dict")
public class DictController {
    @Reference(version = "1.0.0")
    DictService dictService;
    @RequestMapping("getDict")
    public ApplicationResult printCity() {
        dictService.test();
        return ApplicationResult.SUCCESS;
    }
}

这个类也是必须要被dubbo扫描到的
配置完成后就可以启动测试了

再说一下:必须去掉springboot的自动部署插件,不然一直不成功,还不知道为什么

如果有问题或者BUG请发邮件到:bandaotixi@hotmail.com
感谢

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值