1. 程式人生 > >Dubbo整合SpringBoot

Dubbo整合SpringBoot

spring 結構 body map logs main protoc tro width

目前的dubbo已支持和springboot集成,還是之前的例子,這次我們通過springboot容器來實現。借此了解一下基於springboot容器啟動的dubbo的配置及使用。

1. 準備工作

創建一個Maven空項目,作為項目的父工程,此工程的子項目基於Spring Boot 2.0.5 實現

技術分享圖片

在父工程的pom.xml引入之後要創建的子工程

    <modules>
        <module>gmall-interface</module>
        <module>user-service-provider</
module> <module>order-service-consumer</module> </modules>

可以提前看一下工程結構

技術分享圖片

下面分別來實現子工程:(子工程的實現方式都是在gmall工程下新建Module)

技術分享圖片

2. 公共API

項目中共用的接口和POJO類,代碼和之前一樣,這裏不再展開

技術分享圖片

3. 服務提供者

工程結構如下

技術分享圖片

引入依賴

<!-- 引入公共API,以實現其接口 -->
        <dependency>
            <
groupId>com.zang</groupId> <artifactId>gmall-interface</artifactId> <version>1.0-SNAPSHOT</version> </dependency> <!-- 引入spring-boot-starter以及dubbo和curator的依賴 --> <dependency> <groupId>
com.alibaba.boot</groupId> <artifactId>dubbo-spring-boot-starter</artifactId> <version>0.2.0</version> </dependency> <!-- Spring Boot相關依賴 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>

需要註意的是,根據jdk和Spring Boot版本的不同,dubbo-spring-boot-starter的版本需要有根據的選擇

技術分享圖片

dubbo提供了@Service註解,可將類聲明為提供方,省去了大量配置的麻煩

import com.alibaba.dubbo.config.annotation.Service;
import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.UserService;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Service   //屬於Dubbo的@Service註解,非Spring  作用:暴露服務
@Component
public class UserServiceImpl implements UserService {

    @Override
    public List<UserAddress> getUserAddressList(String userId) {
    //省略
}}

通過屬性配置的方式設置application.properties

#當前服務/應用的名字
dubbo.application.name=user-service-provider
#註冊中心的協議和地址 dubbo.registry.protocol=zookeeper dubbo.registry.address=127.0.0.1:2181
#通信規則(通信協議和接口) dubbo.protocol.name=dubbo dubbo.protocol.port=20880
#連接監控中心 dubbo.monitor.protocol=registry #開啟包掃描,可替代 @EnableDubbo 註解 ##dubbo.scan.base-packages=com.zang.gmall

springboot容器根據配置啟動服務提供方,這裏需要添加 @EnableDubbo 註解

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

// 開啟基於註解的dubbo功能(主要是包掃描@DubboComponentScan)
// 也可以在配置文件中使用dubbo.scan.base-package來替代   @EnableDubbo
@EnableDubbo

@SpringBootApplication
public class UserServiceProviderApplication {

    public static void main(String[] args) {
        SpringApplication.run(UserServiceProviderApplication.class, args);
    }
}

提供方順利啟動

技術分享圖片

4. 服務消費者

消費者工程在初始化時設置為web項目,結構如下

技術分享圖片

引入和服務提供方同樣的依賴(代碼略)。

dubbo提供了@Reference註解,可替換@Autowired註解,用於引入遠程服務

import com.alibaba.dubbo.config.annotation.Reference;
import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.OrderService;
import com.zang.gmall.service.UserService;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Reference
    UserService userService;

    @Override
    public List<UserAddress> initOrder(String userId) {
  //代碼省略
   }
}

配置文件application.properties

#避免和監控中心端口沖突,設為8081端口訪問
server.port=8081  

dubbo.application.name=order-service-consumer
dubbo.registry.address=zookeeper://127.0.0.1:2181
dubbo.monitor.protocol=registry

啟動類同樣加上@EnableDubbo註解

import com.alibaba.dubbo.config.spring.context.annotation.EnableDubbo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@EnableDubbo
@SpringBootApplication
public class OrderServiceConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(OrderServiceConsumerApplication.class, args);
    }
}

為查看調用是否成功,新增控制層用於訪問

import com.zang.gmall.bean.UserAddress;
import com.zang.gmall.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class OrderController {

    @Autowired
    OrderService orderService;

    @ResponseBody   //以json格式返回
    @RequestMapping("/initOrder")
    public List<UserAddress> initOrder(@RequestParam("uid") String userId){

       return orderService.initOrder(userId);
    }

}

5. 測試調用

啟動消費方,在瀏覽器訪問

技術分享圖片

調用成功

技術分享圖片

Dubbo整合SpringBoot