刚作完一个基于motan的项目不久,便去看看dubbo的新特性了,dubbo自上年9月恢复更新到如今大概半年多,发现已经有和spring boot集成的配置了。我的喜欢的配置方式优先级通常都是资源文件>Bean>xml,因感受而言Bean的配置方式更适合让人了解源码,而资源文件则是最简便,看了下还好以前写dubbo的demo时没有写文章,由于我通常都比较喜欢写能够经过最简易的方式达到目的的demo文章,并且也不会也重复的文章,若是以前写了就只有经过Bean配置而没有application资源文件配置dubbo的文章了。java
该文章主要参考自 dubbo官方Demo,dubbo的基本特性能够看用户指南,dubbo的概念能够看用户指南的第一章入门便可,我就不转述了,想要在dubbo基础上扩展的能够看开发指南。建议能够先尝试本身根据官方去搭一下,出问题了能够再对比一下本文章代码找出问题,成功了就能够省略代码部分阅览了。react
如今能够经过添加dubbo-spring-boot-starter依赖实现dubbo与spring boot的整合,简化dubbo的配置,具体文档以下图:
web
文档所属项目是dubbo-spring-boot-parent,因为0.2.x版本还没正式release,因此该demo用的依旧是0.1.0的依赖,但还未发现与Spring boot 2.0的版本继承有问题。spring
我的demo分了3个模块-consumer、provider、service,这3个模块的父模块则是dubbo-demo。service里存放服务接口和POJO(按正常划分POJO该新建一个entity模块存放,但demo随便了一下),provider存放DAO、service接口实现暴露service,consumer存放controller(controller从暴露服务的位置获取所需服务,如consule、zookeeper为分布式服务进行管理的注册中心,也能够是特定url)。dubbo-motan的maven依赖以下:
mybatis
dubbo-service-demo模块存服务接口与POJO:app
package per.dubbo.demo.postgres.service;
import per.dubbo.demo.postgres.model.Student;
import com.baomidou.mybatisplus.service.IService;
/**
* @author Wilson
* @since 2018-05-25
*/
public interface StudentService extends IService<Student> {
} maven
dubbo-provider-demo存放服务、DAO与dubbo配置资源文件properties.yml(@Service需用dubbo的):分布式
StudentServiceImpl:ide
package per.dubbo.demo.postgres.service.impl;
import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import per.dubbo.demo.postgres.dao.StudentDAO;
import per.dubbo.demo.postgres.model.Student;
import per.dubbo.demo.postgres.service.StudentService;
import javax.annotation.Resource;
/**
* @author Wilson
* @since 2018-05-25
*/
@Service
public class StudentServiceImpl extends ServiceImpl<StudentDAO, Student> implements StudentService {
@Resource
private StudentDAO studentDAO;
}spring-boot
dubbo-provider-demo模块下的application.xml:
server:
port: 8081
spring:
application:
name: provider-demo
dubbo:
scan:
base-packages: per.dubbo.demo.postgres.service.impl
application:
name: dubbo-provider-demo
id: dubbo-provider-demo
protocol:
id: dubbo
name: dubbo
port: 33333
registry:
address: multicast://224.5.6.7:1234
check: false
启动进程ProviderApplicationDemo(@EnableDubbo同@DubboComponentScan与@EnableDubboConfig,虽然application.yml已配置了扫描包但实际启动却没有起效,大概是我使用的spring boot版本是2.0但dubbo-spring-boot是0.1.x的缘由,因此要加@EnableDubbo才能起效):
package per.dubbo.demo.postgres;
import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import io.swagger.annotations.Api;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* ProviderApplicationDemo
*
* @author Wilson
* @date 18-4-12
*/
@Api("ProviderApplicationDemo")
@EnableDubbo
@SpringBootApplication
public class ProviderApplicationDemo {
public static void main(String[] args) {
SpringApplication.run(ProviderApplicationDemo.class);
}
}dubbo-consumer-demo存放controller:
package per.dubbo.demo.controller.impl;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.RestController;
import per.dubbo.demo.common.ServerResponse;
import per.dubbo.demo.controller.UserBaseController;
import per.dubbo.demo.postgres.service.StudentService;
import reactor.core.publisher.Mono;
/**
* UserBaseControllerImpl
*
* @author Wilson
* @date 18-5-25
*/
@RestController
public class UserBaseControllerImpl implements UserBaseController {
@Reference
private StudentService studentService;
@Override
public Mono<ServerResponse> login(String username, String password) {
return Mono.just(ServerResponse.ok());
}
@Override
public Mono<ServerResponse> list() {
return Mono.just(ServerResponse.ok(studentService.selectList(null)));
}
}
启动程序ConsumerDemoApplication(@PostConstruct在这只是用来判断有没有成功发现服务):
package per.dubbo.demo;
import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.dubbo.config.spring.context.annotation.DubboComponentScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import per.dubbo.demo.postgres.service.StudentService;
import javax.annotation.PostConstruct;
/**
* ConsumerDemoApplication
*
* @author Wilson
* @date 18-5-15
*/
@SpringBootApplication
@DubboComponentScan
public class ConsumerDemoApplication {
@Reference
private StudentService studentService;
@PostConstruct
public void init() {
System.err.println("studentService:" + studentService);
}
public static void main(String[] args) {
SpringApplication.run(ConsumerDemoApplication.class);
}
}
consumer配置文件application.yml:
spring:
application:
name: dubbo-consumer-demo
server:
port: 7979
dubbo:
consumer:
check: false
application:
id: dubbo-consumer
name: dubbo-consumer
protocol:
id: consumer
name: consumer
port: 33333
registry:
address: multicast://224.5.6.7:1234
check: false
scan:
base-packages: per.dubbo.demo.controller
项目起跑时均可以看到控制台输出application.yml的dubbo配置信息,以下图:
这一切都部署好后就把每个Consumer模块都看成一个系统开发就行了,其它无关dubbo配置的代码就不贴浪费位置了,毕竟最重要的仍是思想。分布式就至关于把一个大系统划分为一个个小系统进行开发,而dubbo就是划分的代码实现。如电商就可分为用户Consumer、用户Provider、商品Consumer、商品Provider、订单Consumer、订单Provider等等,不一样的consumer能够放到一个consumer大模块进行管理,provider丢到一个provider大模块进行管理,按照该文章里的例子则dubbo-demo则为全部模块的祖宗,根据功能划分存放common、provider-demo、consumer-demo、service-demo、entity等子模块,consumer-demo里放user-consumer、order-consumer、store-consumer等子模块,provider-demo放user-provider、order-provider、store-provider等子模块