Rumah >Java >javaTutorial >Cara springboot menyepadukan dubbo untuk mencapai pengagregatan kumpulan

Cara springboot menyepadukan dubbo untuk mencapai pengagregatan kumpulan

王林
王林ke hadapan
2023-05-11 20:43:04735semak imbas

Pengguna

konfigurasi fail yml:

dubbo:
  application:
    name: dubbo-gateway
  registry:
    address: zookeeper://127.0.0.1:2181
  server: true
  provider:
    timeout: 3000
  protocol:
    name: dubbo
    port: 20881

kelas pengawal:

@RestController
@RequestMapping(value = "/order")
@Slf4j
public class OrderController {

/**
     * dubbo 的分组特性:group(指定将要聚合的分组)
     * dubbo 的聚合特性:merger(指定聚合策略)
     * 自定义策略申明文件名为:org.apache.dubbo.rpc.cluster.Merger(不可变),文件夹名:META-INF.dubbo(不可变)
     */
    @DubboReference(check = false, group = "2017,2018", merger = "page")
    private OrderService orderService;

/**
     * 查看订单信息
     *
     * @param nowPage
     * @param pageSize
     * @return
     */
    @PostMapping("/getOrderInfo")
    public ResponseVO getOrderInfo(@RequestParam(name = "nowPage", required = false, defaultValue = "1") Integer nowPage,
                                   @RequestParam(name = "pageSize", required = false, defaultValue = "5") Integer pageSize) {

        // 获取当前登陆人的信息
        String userId = CurrentUser.getUserId();

        // 使用当前登陆人获取已经购买的订单
        Page<OrderVO> page = new Page<>(nowPage,pageSize);
        if(userId != null && userId.trim().length()>0){
            Page<OrderVO> result = orderService.getOrderByUserId(Integer.parseInt(userId), page);

            return ResponseVO.success(nowPage, (int) result.getPages(),"",result.getRecords());

        }else{
            return ResponseVO.serviceFail("用户未登陆");
        }
    }

Strategi pengagregatan tersuai

Terdapat fail org.apache.dubbo.rpc.cluster.Merger dalam direktori dubbo-3.0.9.jar!/META-INF/dubbo/internal/. Kandungan fail adalah seperti berikut:

map=org.apache.dubbo.rpc.cluster.merger.MapMerger
set=org.apache.dubbo.rpc.cluster.merger.SetMerger
list=org.apache.dubbo.rpc.cluster.merger.ListMerger
byte=org.apache.dubbo.rpc.cluster.merger.ByteArrayMerger
char=org.apache.dubbo.rpc.cluster.merger.CharArrayMerger
short=org.apache.dubbo.rpc.cluster.merger.ShortArrayMerger
int=org.apache.dubbo.rpc.cluster.merger.IntArrayMerger
long=org.apache.dubbo.rpc.cluster.merger.LongArrayMerger
float=org.apache.dubbo.rpc.cluster.merger.FloatArrayMerger
double=org.apache.dubbo.rpc.cluster.merger.DoubleArrayMerger
boolean=org.apache.dubbo.rpc.cluster.merger.BooleanArrayMerger

Ia mengisytiharkan strategi pengagregatan yang ditakrifkan oleh dubbo. Apabila menentukan strategi pengagregatan dubbo, anda boleh menggunakan strategi pengagregatan yang disediakan oleh dubbo atau menggunakan strategi pengagregatan tersuai.

Bagaimana untuk menyesuaikan strategi pengagregatan dubbo?

Buat direktori dan fail berikut dalam direktori sumber (nota: direktori dan nama fail tidak boleh diubah).

Cara springboot menyepadukan dubbo untuk mencapai pengagregatan kumpulan

org.apache.dubbo.rpc.cluster.Kandungan fail penggabungan adalah seperti berikut:

# 自定义聚合策略
page=com.stylefeng.guns.gateway.config.PageMerger

Penggabungan tersuai Kelas strategi:

package com.stylefeng.guns.gateway.config;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.dubbo.rpc.cluster.Merger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

public class PageMerger implements Merger<Page> {


    @Override
    public Page merge(Page... items) {
        Page<Object> page = new Page<>();

        List<Object> records = new ArrayList<>();
        AtomicInteger total = new AtomicInteger();

        Arrays.stream(items).forEach(item -> {
            records.addAll(item.getRecords());
            total.addAndGet((int) item.getPages());
        });
        page.setRecords(records);
        page.setSize(total.get());
        return page;
    }
}

Pembekal

konfigurasi fail yml:

dubbo:
  application:
    name: dubbo-order
  registry:
    address: zookeeper://127.0.0.1:2181
  server: true
  provider:
    timeout: 3000
  protocol:
    name: dubbo
    port: 20885

Antara muka dan pelaksanaannya

Antara muka OrderService:

public interface OrderService {

    /**
     * 使用当前登陆人获取已经购买的订单
     * @param userId
     * @param page
     * @return
     */
    Page<OrderVO> getOrderByUserId(Integer userId, Page<OrderVO> page);

}

Kelas pelaksanaan OrderServiceImplA:

@DubboService(group = "2017")
@Slf4j
public class OrderServiceImplA implements OrderService {

    @Autowired
    private MoocOrder2017TMapper moocOrder2017TMapper;

    /**
     * 使用当前登陆人获取已经购买的订单
     *
     * @param userId
     * @param page
     * @return
     */
    @Override
    public Page<OrderVO> getOrderByUserId(Integer userId, Page<OrderVO> page) {
        Page<OrderVO> result = new Page<>();
        if(userId == null){
            log.error("订单查询业务失败,用户编号未传入");
            return null;
        }else{
            List<OrderVO> ordersByUserId = moocOrder2017TMapper.getOrdersByUserId(userId,page);
            if(ordersByUserId==null && ordersByUserId.size()==0){
                result.setTotal(0);
                result.setRecords(new ArrayList<>());
                return result;
            }else{
                // 获取订单总数
                EntityWrapper<MoocOrder2017T> entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("order_user",userId);
                Integer counts = moocOrder2017TMapper.selectCount(entityWrapper);
                // 将结果放入Page
                result.setTotal(counts);
                result.setRecords(ordersByUserId);

                return result;
            }
        }
    }
}

Kelas pelaksanaan OrderServiceImplB:

struktu
@DubboService(group = "2018")
@Slf4j
public class OrderServiceImplB implements OrderService {

    @Autowired
    private MoocOrder2018TMapper moocOrder2018TMapper;

    /**
     * 使用当前登陆人获取已经购买的订单
     *
     * @param userId
     * @param page
     * @return
     */
    @Override
    public Page<OrderVO> getOrderByUserId(Integer userId, Page<OrderVO> page) {
        Page<OrderVO> result = new Page<>();
        if(userId == null){
            log.error("订单查询业务失败,用户编号未传入");
            return null;
        }else{
            List<OrderVO> ordersByUserId = moocOrder2018TMapper.getOrdersByUserId(userId,page);
            if(ordersByUserId==null && ordersByUserId.size()==0){
                result.setTotal(0);
                result.setRecords(new ArrayList<>());
                return result;
            }else{
                // 获取订单总数
                EntityWrapper<MoocOrder2018T> entityWrapper = new EntityWrapper<>();
                entityWrapper.eq("order_user",userId);
                Integer counts = moocOrder2018TMapper.selectCount(entityWrapper);
                // 将结果放入Page
                result.setTotal(counts);
                result.setRecords(ordersByUserId);

                return result;
            }
        }
    }
}
Data

Struktur jadual:

CREATE TABLE `mooc_order_2017_t` (
  `UUID` varchar(100) DEFAULT NULL COMMENT &#39;主键编号&#39;,
  `cinema_id` int DEFAULT NULL COMMENT &#39;影院编号&#39;,
  `field_id` int DEFAULT NULL COMMENT &#39;放映场次编号&#39;,
  `film_id` int DEFAULT NULL COMMENT &#39;电影编号&#39;,
  `seats_ids` varchar(50) DEFAULT NULL COMMENT &#39;已售座位编号&#39;,
  `seats_name` varchar(200) DEFAULT NULL COMMENT &#39;已售座位名称&#39;,
  `film_price` double DEFAULT NULL COMMENT &#39;影片售价&#39;,
  `order_price` double DEFAULT NULL COMMENT &#39;订单总金额&#39;,
  `order_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT &#39;下单时间&#39;,
  `order_user` int DEFAULT NULL COMMENT &#39;下单人&#39;,
  `order_status` int DEFAULT &#39;0&#39; COMMENT &#39;0-待支付,1-已支付,2-已关闭&#39;
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=&#39;订单信息表&#39;;

CREATE TABLE `mooc_order_2018_t` (
  `UUID` varchar(100) DEFAULT NULL COMMENT &#39;主键编号&#39;,
  `cinema_id` int DEFAULT NULL COMMENT &#39;影院编号&#39;,
  `field_id` int DEFAULT NULL COMMENT &#39;放映场次编号&#39;,
  `film_id` int DEFAULT NULL COMMENT &#39;电影编号&#39;,
  `seats_ids` varchar(50) DEFAULT NULL COMMENT &#39;已售座位编号&#39;,
  `seats_name` varchar(200) DEFAULT NULL COMMENT &#39;已售座位名称&#39;,
  `film_price` double DEFAULT NULL COMMENT &#39;影片售价&#39;,
  `order_price` double DEFAULT NULL COMMENT &#39;订单总金额&#39;,
  `order_time` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT &#39;下单时间&#39;,
  `order_user` int DEFAULT NULL COMMENT &#39;下单人&#39;,
  `order_status` int DEFAULT &#39;0&#39; COMMENT &#39;0-待支付,1-已支付,2-已关闭&#39;
) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=DYNAMIC COMMENT=&#39;订单信息表&#39;;

Data jadual:

INSERT INTO `guns_rest`.`mooc_order_2017_t`(`UUID`, `cinema_id`, `field_id`, `film_id`, `seats_ids`, `seats_name`, `film_price`, `order_price`, `order_time`, `order_user`, `order_status`) VALUES (&#39;329123812gnfn31&#39;, 1, 1, 2, &#39;1,2,3,4&#39;, &#39;第一排1座,第一排2座,第一排3座,第一排4座&#39;, 63.2, 126.4, &#39;2017-05-03 12:13:42&#39;, 2, 0);
INSERT INTO `guns_rest`.`mooc_order_2017_t`(`UUID`, `cinema_id`, `field_id`, `film_id`, `seats_ids`, `seats_name`, `film_price`, `order_price`, `order_time`, `order_user`, `order_status`) VALUES (&#39;310bb3c3127a4551ad72f2f3e53333c7&#39;, 1, 1, 2, &#39;9,10&#39;, &#39;第一排9座,第一排10座&#39;, 60, 120, &#39;2022-07-20 14:25:42&#39;, 2, 0);

INSERT INTO `guns_rest`.`mooc_order_2018_t`(`UUID`, `cinema_id`, `field_id`, `film_id`, `seats_ids`, `seats_name`, `film_price`, `order_price`, `order_time`, `order_user`, `order_status`) VALUES (&#39;124583135asdf81&#39;, 1, 1, 2, &#39;1,2,3,4&#39;, &#39;第一排1座,第一排2座,第一排3座,第一排4座&#39;, 63.2, 126.4, &#39;2018-02-12 11:53:42&#39;, 2, 0);

Demo:

Cara springboot menyepadukan dubbo untuk mencapai pengagregatan kumpulan

Atas ialah kandungan terperinci Cara springboot menyepadukan dubbo untuk mencapai pengagregatan kumpulan. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Artikel ini dikembalikan pada:yisu.com. Jika ada pelanggaran, sila hubungi admin@php.cn Padam