dubbo与springboot的集成和使用dubbo-spring-boot-starter
dubbo最近更新了dubbo-spring-boot-starter的jar包,废话不多少,看看如何使用。
首先我们可以通过github编译打包最新的starter,
Git地址为:https://github.com/alibaba/dubbo-spring-boot-starter.git
在springboot项目的pom.xml中新增,添加maven的依赖。
-
<!--dubbo-springBoot依赖-->
-
<dependency>
-
<groupId>com.alibaba.spring.boot</groupId>
-
<artifactId>dubbo-spring-boot-starter</artifactId>
-
<version>2.0.1-SNAPSHOT</version>
-
</dependency>
可以新建一个springboot-dubbo-api工程,用来做接口定义,方便provider和consumer端使用,避免重复编码
接口定义示例:
-
import com.xq.live.dubbo.common.BaseResp;
-
import com.xq.live.dubbo.common.Pager;
-
import com.xq.live.dubbo.model.Shop;
-
import com.xq.live.dubbo.service.BaseSerivce;
-
import com.xq.live.dubbo.vo.ShopInVo;
-
/**
-
* @package: com.xq.live.dubbo.api
-
* @description: 商家接口定义
-
* @author: zhangpeng32
-
* @date: 2018/3/25 12:18
-
* @version: 1.0
-
*/
-
public interface ShopApi extends BaseSerivce<Shop>{
-
/**
-
* 分页查询
-
* @param shopInVo
-
* @return
-
*/
-
public BaseResp<Pager<Shop>> queryWithPg(ShopInVo shopInVo);
-
}
在这里我定义了一个BaseService,用来定义一些通用的接口方法,包含了简单的增删改查,也可以把分页的方法写在里面。
-
/**
-
* @package: com.xq.live.dubbo.common
-
* @description: 基础接口类
-
* @author: zhangpeng32
-
* @date: 2018/3/25 12:27
-
* @version: 1.0
-
*/
-
public interface BaseSerivce<T> {
-
/**
-
* 新增记录
-
* @param t
-
* @return
-
*/
-
public BaseResp<Long> create(T t);
-
/**
-
* 根据主键删除
-
* @param pk
-
* @return
-
*/
-
public BaseResp<Integer> delete(Long pk);
-
/**
-
* 根据主键更新记录
-
* @param t
-
* @return
-
*/
-
public BaseResp<Integer> update(T t);
-
/**
-
* 根据主键查询
-
* @param pk
-
* @return
-
*/
-
public BaseResp<T> get(Long pk);
-
}
在springboot-dubbo-provider项目中,首先做好dubbo和springboot的集成配置:
-
## dubbo springboot 配置
-
spring.dubbo.application.id=live-dubbo-provider
-
spring.dubbo.application.name=live-dubbo-provider
-
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
-
spring.dubbo.server=true
-
spring.dubbo.protocol.name=dubbo
-
spring.dubbo.protocol.port=20880
在springboot的启动类添加@EnableDubboConfiguration注解:
-
@MapperScan("com.xq.live.dubbo.dao")
-
@EnableDubboConfiguration
-
@SpringBootApplication
-
public class DubboServerApplication {
-
public static void main(String[] args) {
-
SpringApplication.run(DubboServerApplication.class, args);
-
}
-
}
ShopApi接口实现类,示例如下:
-
import com.alibaba.dubbo.config.annotation.Service;
-
import com.xq.live.dubbo.api.ShopApi;
-
import com.xq.live.dubbo.common.BaseResp;
-
import com.xq.live.dubbo.common.Pager;
-
import com.xq.live.dubbo.common.ResultEnums;
-
import com.xq.live.dubbo.dao.ShopDao;
-
import com.xq.live.dubbo.model.Shop;
-
import com.xq.live.dubbo.vo.ShopInVo;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.stereotype.Component;
-
/**
-
* @package: com.xq.live.dubbo.handler
-
* @description: 商家dubbo服务实现类
-
* @author: zhangpeng32
-
* @date: 2018/3/25 12:40
-
* @version: 1.0
-
*/
-
@Service(interfaceClass = ShopApi.class)
-
@Component
-
public class ShopHandler implements ShopApi {
-
@Autowired
-
private ShopDao shopDao;
-
@Override
-
public BaseResp<Long> create(Shop shop) {
-
return null;
-
}
-
@Override
-
public BaseResp<Integer> delete(Long pk) {
-
return null;
-
}
-
@Override
-
public BaseResp<Integer> update(Shop shop) {
-
return null;
-
}
-
@Override
-
public BaseResp<Shop> get(Long pk) {
-
Shop shop = shopDao.selectByPrimaryKey(pk);
-
return new BaseResp<Shop>(ResultEnums.SUCCESS, shop);
-
}
-
@Override
-
public BaseResp<Pager<Shop>> queryWithPg(ShopInVo shopInVo) {
-
return null;
-
}
-
}
启动本地的zookeeper: 在 \zookeeper-3.4.11\bin目录下执行zkServer.bat,启动zookeeper。
启动dubbo-admin可以用户查看服务注册情况以及权重调整等。
源码可以在这里获取:https://github.com/alibaba/dubbo.git
启动后界面如下:
启动springboot-dubbo-provider应用,可以通过dubbo-admin看到服务注册成功。
消费端
消费端application.properties配置为:
-
## 避免和 server 工程端口冲突
-
server.port=8081
-
## Dubbo 服务消费者配置
-
spring.dubbo.application.name=live-dubbo-consumer
-
spring.dubbo.application.id=live-dubbo-consumer
-
spring.dubbo.protocol.port=20800
-
spring.dubbo.protocol.name=dubbo
-
spring.dubbo.registry.address=zookeeper://127.0.0.1:2181
接口实例化和调用示例:
-
import com.alibaba.dubbo.config.annotation.Reference;
-
import com.xq.live.dubbo.api.ShopApi;
-
import com.xq.live.dubbo.common.BaseResp;
-
import com.xq.live.dubbo.model.Shop;
-
import org.springframework.stereotype.Component;
-
/**
-
* @package: com.xq.live.dubbo.service
-
* @description: TODO
-
* @author: zhangpeng32
-
* @date: 2018/3/25 12:58
-
* @version: 1.0
-
*/
-
@Component
-
public class ShopDubboService {
-
@Reference
-
private ShopApi shopApi;
-
public BaseResp<Shop> get(Long id){
-
return shopApi.get(id);
-
}
-
}
下面是消费端的springboot启动类配置:
-
import com.alibaba.dubbo.spring.boot.annotation.EnableDubboConfiguration;
-
import com.xq.live.dubbo.common.BaseResp;
-
import com.xq.live.dubbo.model.Shop;
-
import com.xq.live.dubbo.service.ShopDubboService;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.boot.SpringApplication;
-
import org.springframework.boot.autoconfigure.SpringBootApplication;
-
import org.springframework.web.bind.annotation.RequestMapping;
-
import org.springframework.web.bind.annotation.RestController;
-
@SpringBootApplication
-
@RestController
-
@EnableDubboConfiguration
-
public class DubboClientApplication {
-
@Autowired
-
private ShopDubboService shopDubboService;
-
public static void main(String[] args) {
-
SpringApplication.run(DubboClientApplication.class, args);
-
}
-
@RequestMapping(value = "/")
-
public String hello(){
-
BaseResp<Shop> baseResp = shopDubboService.get(1L);
-
return "hello "+ baseResp.getCode()+": NAME : "+baseResp.getData().getShopName();
-
}
-
}
启动消费端,我这里使用的端口是8081,服务器采用的springboot内置的tomcat.启动后可以在dubbo-admin看到消费端注册成功。
总结:
1、dubbo和springboot的结合非常简单,引入dubbo的starter即可。
2、application.properties配置清晰简单,各配置都非常完整。
3、本示例中用到的各种工具类可以忽略,我只是为了统一返回需要做的一个定义。
4、目前没遇到什么问题,如果有,可以发消息我一起探讨。