Spring Boot整合Dubbo

配置依赖 

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

服务端的的配置(生产者)

spring:
  dubbo:
    application:
      name: provider
    registry:
      address: zookeeper://127.0.0.1:2181
    protocol:
      name: dubbo
      prot: 20880
    scan: com.java4all

客户端配置(消费者)

spring:
  dubbo:
    application:
      name: consumer
    registry:
      address: zookeeper://127.0.0.1:2181
    scan:com.java4all.controller
  redis:
    host: localhost

提供生产者方法 
在实现类上添加达博服务(使用达博包下的@Service注解,不是春天的注解)

package com.java4all.impl;


import com.alibaba.dubbo.config.annotation.Service;
import com.java4all.api.UserService;
import com.java4all.dao.UserDao;
import com.java4all.entity.User;
import org.springframework.beans.factory.annotation.Autowired;


@Service
public class UserServiceImpl implements UserService{


    @Autowired
    private UserDao userDao;


    @Override
    public User findByMobile(String mobile) {
        return userDao.findByMobile(mobile);
    }
}

需要注意的是,这个类必须放到达博扫描的包中(在配置中写的扫描配置扫描:com.java4all)

使用消费者方法 
需要使用多宝的注解进行自动注入(使用多宝包下的@Reference注解注入)

package com.java4all.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.java4all.api.UserService;
import com.java4all.entity.User;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


@RequestMapping(value = "user")
@RestController
public class UserController {


    @Reference
    private UserService userService;


    @RequestMapping(value = "showName",method = RequestMethod.GET)
    public String showName(String mobile){
        User user = userService.findByMobile(mobile);
        String name = user.getName();
        return name;
    }
}

这个类也是必须要被多宝扫描的(即客户端配置扫描:com.java4all.controller)

项目地址:

https://github.com/ShiLinDang/springBoot-dubbo.git
https://github.com/ShiLinDang/springBoot-dubbo.git
https://github.com/ShiLinDang/springBoot-dubbo.git
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值