剛做完一個基於motan的項目不久,便去看看dubbo的新特性了,dubbo自上年9月恢復更新到現在大概半年多,發現已經有和spring boot集成的配置了。個人喜歡的配置方式優先級一般都是資源文件>Bean>xml,因感覺而言Bean的配置方式更適合讓人瞭解源碼,而資源文件則是最簡便,看了下還好之前寫dubbo的demo時沒有寫文章,因爲我一般都比較喜歡寫可以通過最簡易的方式達到目的的demo文章,而且也不會也重複的文章,如果之前寫了就只有通過Bean配置而沒有application資源文件配置dubbo的文章了。
該文章主要參考自 dubbo官方Demo,dubbo的基本特性可以看用戶指南,dubbo的概念可以看用戶指南的第一章入門即可,我就不轉述了,想要在dubbo基礎上擴展的可以看開發指南。建議可以先嚐試自己根據官方去搭一下,出問題了可以再對比一下本文章代碼找出問題,成功了就可以省略代碼部分閱覽了。
現在可以通過添加dubbo-spring-boot-starter依賴實現dubbo與spring boot的整合,簡化dubbo的配置,具體文檔如下圖:
文檔所屬項目是dubbo-spring-boot-parent,由於0.2.x版本還沒正式release,所以該demo用的依舊是0.1.0的依賴,但還未發現與Spring boot 2.0的版本繼承有問題。
個人demo分了3個模塊-consumer、provider、service,這3個模塊的父模塊則是dubbo-motan。service裏存放服務接口和POJO(按正常劃分POJO該新建一個entity模塊存放,但demo隨便了一下),provider存放DAO、service接口實現暴露service,consumer存放controller(controller從暴露服務的位置獲取所需服務,如consule、zookeeper爲分佈式服務進行管理的註冊中心,也可以是特定url)。dubbo-motan的maven依賴如下:
dubbo-service-demo模塊存服務接口與POJO:
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> { }
dubbo-provider-demo存放服務、DAO與dubbo配置資源文件properties.yml(@Service需用dubbo的):
StudentServiceImpl:
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; }
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等子模塊