1. 程式人生 > 其它 >dubbo整合springboot入門案例

dubbo整合springboot入門案例

dubbo整合springboot入門案例

建立server-common-api專案

server-common-api中建立消費者和提供者都需要的類,實現程式碼複用

使用者地址實體類

package com.yl.entity;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;

/**
 * 使用者地址
 *
 * @author Y-wee
 */
@AllArgsConstructor
@Getter
@Setter
@ToString
public class UserAddress implements Serializable {
    /**
     * 主鍵
     */
    private Integer id;
    /**
     * 使用者地址
     */
    private String userAddress;
    /**
     * 使用者id
     */
    private String userId;
    /**
     * 收貨人
     */
    private String consignee;
    /**
     * 電話號碼
     */
    private String phoneNum;
    /**
     * 是否為預設地址:Y-是,N-否
     */
    private String isDefault;

}

訂單服務介面

package com.yl.service;

import com.yl.entity.UserAddress;

import java.util.List;

/**
 * 訂單
 *
 * @author Y-wee
 */
public interface OrderService {

    /**
     * 初始化訂單
     *
     * @param userId
     */
    List<UserAddress> initOrder(String userId);

}

使用者服務介面

package com.yl.service;

import com.yl.entity.UserAddress;

import java.util.List;


/**
 * 使用者
 *
 * @author Y-wee
 */
public interface UserService {

    /**
     * 按照使用者id返回所有的收貨地址
     *
     * @param userId
     * @return
     */
    List<UserAddress> getUserAddressList(String userId);

}

建立boot-server-user-provider專案

boot-server-user-provider實現使用者相關業務,作為服務提供者

匯入依賴

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.yl</groupId>
        <artifactId>server-common-api</artifactId>
        <version>1.0-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.alibaba.boot</groupId>
        <artifactId>dubbo-spring-boot-starter</artifactId>
        <version>0.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
</dependencies>

使用者業務實現類

package com.yl.boot.server.user.provider.service.impl;

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

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.yl.entity.UserAddress;
import com.yl.service.UserService;
import org.springframework.stereotype.Service;

/**
 * 使用者
 *
 * @author Y-wee
 */
@Service
// 暴露服務,注意包名是dubbo的
@com.alibaba.dubbo.config.annotation.Service
public class UserServiceImpl implements UserService {

    /**
     * 獲取使用者地址
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> getUserAddressList(String userId) {
        System.out.println("---UserServiceImpl..3.....");

        UserAddress address1 = new UserAddress(1, "北京市昌平區巨集福科技園綜合樓3層", "1", "李老師", "010-56253825", "Y");
        UserAddress address2 = new UserAddress(2, "深圳市寶安區西部矽谷大廈B座3層(深圳分校)", "1", "王老師", "010-56253825", "N");

        if (Math.random() > 0.5) {
            throw new RuntimeException();
        }

        return Arrays.asList(address1, address2);
    }

}

配置檔案

server:
  port: 8091

dubbo:
  application:
    # 當前服務註冊名稱
    name: boot-server-user-provider
  registry:
    # 註冊地址
    address: 127.0.0.1:2181
    # 註冊中心協議型別
    protocol: zookeeper
  protocol:
    # 通訊協議
    name: dubbo
    # 通訊埠
    port: 20081
  monitor:
    # 連線監控中心方式
    protocol: registry

主啟動類新增@EnableDubbo啟用dubbo註解

package com.yl.boot.server.user.provider;

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

@SpringBootApplication
@EnableDubbo
public class BootServerUserProviderApplication {

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

}

啟動服務,將提供者以boot-server-user-provider名稱註冊到zookeeper,可以通過dubbo-admin控制檯檢視

建立boot-server-order-consumer專案

匯入依賴,因為consumer用到web功能所以匯入spring-boot-starter-web依賴,其他與boot-server-user-provider一致

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>

訂單業務實現類

package com.yl.boot.server.order.consumer.service.impl;

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

import com.yl.entity.UserAddress;
import com.yl.service.OrderService;
import com.yl.service.UserService;
import org.springframework.stereotype.Service;

import com.alibaba.dubbo.config.annotation.Reference;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;

/**
 * 訂單
 *
 * @author Y-wee
 */
@Service
public class OrderServiceImpl implements OrderService {
    /**
     * 宣告需要呼叫的遠端服務介面
     */
    @Reference
    private UserService userService;

    /**
     * 呼叫遠端使用者服務獲取使用者地址
     *
     * @param userId
     * @return
     */
    @Override
    public List<UserAddress> initOrder(String userId) {
        System.out.println("使用者id:" + userId);
        // 查詢使用者的收貨地址
        List<UserAddress> addressList = userService.getUserAddressList(userId);
        return addressList;
    }

}

訂單contoller介面

package com.yl.boot.server.order.consumer.controller;

import java.util.List;

import com.yl.entity.UserAddress;
import com.yl.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 訂單
 *
 * @author Y-wee
 */
@RestController
public class OrderController {
    @Resource
    private OrderService orderService;

    /**
     * 呼叫使用者服務
     *
     * @param userId
     * @return
     */
    @GetMapping("/initOrder")
    public List<UserAddress> initOrder(@RequestParam("uid") String userId) {
        return orderService.initOrder(userId);
    }

}

配置檔案

server:
  port: 8092

dubbo:
  application:
    name: boot-server-order-consumer
  registry:
    address: zookeeper://127.0.0.1:2181
  monitor:
    protocol: registry

主啟動類新增@EnableDubbo啟用dubbo註解

package com.yl.boot.server.order.consumer;

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

@SpringBootApplication
@EnableDubbo
public class BootServerOrderConsumerApplication {

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

}

啟動服務,訪問:http://localhost:8092/initOrder?uid=1

實現通過dubbo遠端呼叫使用者服務