配置依赖
<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