spring boot集成dubbo,Spring boot +Dubbo,簡易的配置方式

剛作完一個基於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等子模塊  

相關文章
相關標籤/搜索