栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

微服务 撮合引擎 撮合微服务搭建 案例 4

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

微服务 撮合引擎 撮合微服务搭建 案例 4

目录

一、rocketmq的使用

1、开启rocketmq

2、添加依赖

 3、springboot整合disruptor

4、集成SpringCloudStreamRocketmq


一、rocketmq的使用

1、开启rocketmq

2、添加依赖

        
            com.lmax
            disruptor
        


        
            net.openhft
            affinity
            ${affinity.version}
        
        

        
            com.alibaba.cloud
            spring-cloud-stream-binder-rocketmq
        

 3、springboot整合disruptor
package com.dragonwu.disruptor;


import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;

@Data
@ConfigurationProperties(prefix = "spring.disruptor")
public class DisruptorProperties {


    
    private Integer ringBufferSize = 1024 * 1024 ;

    
    private boolean isMultiProducer = false ;
}

异常处理:

package com.dragonwu.disruptor;


import com.lmax.disruptor.BatchEventProcessor;
import com.lmax.disruptor.EventHandler;
import com.lmax.disruptor.ExceptionHandler;
import com.lmax.disruptor.LifecycleAware;
import lombok.extern.slf4j.Slf4j;


@Slf4j
public class DisruptorHandlerException implements ExceptionHandler {


    
    @Override
    public void handleEventException(Throwable ex, long sequence, Object event) {
        log.info("handleEventException Exception===>{} , sequence==> {} ,event===>{}",ex.getMessage(),sequence,event);
    }

    
    @Override
    public void handleOnStartException(Throwable ex) {
        log.info("OnStartHandler Exception===>{} ",ex.getMessage());
    }

    
    @Override
    public void handleOnShutdownException(Throwable ex) {
        log.info("OnShutdownHandler Exception===>{} ",ex.getMessage());
    }
}

处理器:

package com.dragonwu.disruptor;

import com.lmax.disruptor.EventTranslatorOneArg;
import com.lmax.disruptor.RingBuffer;
import com.dragonwu.model.Order;


public class DisruptorTemplate {

    private static final EventTranslatorOneArg TRANSLATOR = new EventTranslatorOneArg() {

        public void translateTo(OrderEvent event, long sequence, Order input) {
            event.setSource(input);
        }
    };
    private final RingBuffer ringBuffer;

    public DisruptorTemplate(RingBuffer ringBuffer) {
        this.ringBuffer = ringBuffer;
    }

    
    public void onData(Order input) {
        ringBuffer.publishEvent(TRANSLATOR, input);
    }
}

自动配置装配:

package com.dragonwu.disruptor;

import com.lmax.disruptor.*;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;
import net.openhft.affinity.AffinityThreadFactory;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.ThreadFactory;

@Configuration
@EnableConfigurationProperties(value = DisruptorProperties.class)
public class DisruptorAutoConfiguration {

    public DisruptorProperties disruptorProperties;

    public DisruptorAutoConfiguration(DisruptorProperties disruptorProperties) {
        this.disruptorProperties = disruptorProperties;
    }


    @Bean
    public EventFactory eventEventFactory() {

        EventFactory orderEventEventFactory = new EventFactory() {
            @Override
            public OrderEvent newInstance() {
                return new OrderEvent();
            }
        };
        return orderEventEventFactory;
    }


    @Bean
    public ThreadFactory threadFactory() {
        return new AffinityThreadFactory("Match-Handler:") ;
    }


    
    @Bean
    public WaitStrategy waitStrategy() {
        return new YieldingWaitStrategy();
    }

    
    @Bean
    public RingBuffer ringBuffer(
            EventFactory eventFactory,
            ThreadFactory threadFactory,
            WaitStrategy waitStrategy,
            EventHandler[] eventHandlers
    ) {

        
        Disruptor disruptor = null;

        ProducerType producerType = ProducerType.SINGLE;

        if (disruptorProperties.isMultiProducer()) {
            producerType = ProducerType.MULTI;
        }

        disruptor = new Disruptor(eventFactory, disruptorProperties.getRingBufferSize(), threadFactory, producerType, waitStrategy);
        disruptor.setDefaultExceptionHandler(new DisruptorHandlerException());

        // 设置消费者---我们的每个消费者代表我们的一个交易对,有多少个交易对,我们就有多少个eventHandlers ,事件来了后,多个eventHandlers 是并发执行的

        disruptor.handleEventsWith(eventHandlers);

        RingBuffer ringBuffer = disruptor.getRingBuffer();
        disruptor.start();// 开始监听


        final Disruptor disruptorShutdown = disruptor;

        // 使用优雅的停机
        Runtime.getRuntime().addShutdownHook(new Thread(
                () -> {
                    disruptorShutdown.shutdown();
                }, "DisruptorShutdownThread"
        ));
        return ringBuffer;
    }


    
    @Bean
    public DisruptorTemplate disruptorTemplate(RingBuffer ringBuffer) {
        return new DisruptorTemplate(ringBuffer);
    }
}

订单类:

package com.dragonwu.disruptor;

import lombok.Data;

import java.io.Serializable;

@Data
public class OrderEvent implements Serializable {

    
    private final long timestamp;

    
    protected transient Object source;

    public OrderEvent() {
        this.timestamp = System.currentTimeMillis();
    }

    public OrderEvent(Object source) {
        this.timestamp = System.currentTimeMillis();
        this.source = source ;
    }
}

事件处理器:

package com.dragonwu.disruptor;

import com.lmax.disruptor.EventHandler;
import com.dragonwu.match.MatchServiceFactory;
import com.dragonwu.match.MatchStrategy;
import com.dragonwu.model.Order;
import com.dragonwu.model.OrderBooks;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;



@Data
@Slf4j
public class OrderEventHandler implements EventHandler {

    private OrderBooks orderBooks;

    private String symbol ;

    public OrderEventHandler(OrderBooks orderBooks) {
        this.orderBooks = orderBooks;
        this.symbol =  this.orderBooks.getSymbol() ;
    }

    
    @Override
    public void onEvent(OrderEvent event, long sequence, boolean endOfBatch) throws Exception {
        // 从ringbuffer 里面接收了某个数据
        Order order = (Order)event.getSource();
        if(!order.getSymbol().equals(symbol)){ // 我们接收到了一个不属于我们处理的数据,我们不处理
            return;
        }

//        log.info("开始接收订单事件============>{}", event);

        MatchServiceFactory.getMatchService(MatchStrategy.LIMIT_PRICE).match(orderBooks ,order);

        /// 处理逻辑是啥?
//        log.info("处理完成我们的订单事件===================>{}", event);
    }
}

4、集成SpringCloudStreamRocketmq

配置:

stream:
      bindings:
        order_in: {destination: order_in, content-type: application/plain, group: order-group, consumer.maxAttempts: 1}
        trade_plate_out: {destination: trade_plate_out, content-type: application/plain}
        completed_orders_out: {destination: completed_orders_out, content-type: application/plain}
        exchange_trades_out: {destination: exchange_trades_out, content-type: application/plain}
        cancel_order_out: {destination: cancel_order_out, content-type: application/plain}
      rocketmq:
        binder:
          name-server: rocket-server:9876

1、添加数据收发接口

package com.dragonwu.rocket;


import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.MessageChannel;

public interface Sink {

    @Input("order_in")
    public MessageChannel messageChannel() ;
}
package com.dragonwu.rocket;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface Source {


    
    @Output("trade_plate_out")
    MessageChannel plateOut() ;


    
    @Output("completed_orders_out")
    MessageChannel completedOrdersOut() ;


    
    @Output("exchange_trades_out")
    MessageChannel exchangeTradesOut() ;


    
    @Output("cancel_order_out")
    MessageChannel cancelOrderOut() ;
}

消息消费者:

package com.dragonwu.rocket;


import com.dragonwu.disruptor.DisruptorTemplate;
import com.dragonwu.domain.EntrustOrder;
import com.dragonwu.model.Order;
import com.dragonwu.util.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class MessageConsumerListener {

    @Autowired
    private DisruptorTemplate disruptorTemplate;

    @StreamListener("order_in")
    public void handleMessage(EntrustOrder entrustOrder) {
        Order order = null;
        if (entrustOrder.getStatus() == 2) { // 该单需要取消
            order = new Order();
            order.setOrderId(entrustOrder.getId().toString());
            order.setCancelOrder(true);
        } else {
            order = BeanUtils.entrustOrder2Order(entrustOrder);
        }
        log.info("接收到了委托单:{}", order);
        disruptorTemplate.onData(order);
    }
}

开启stream的开发:

package com.dragonwu.rocket;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Configuration;



@Configuration
@EnableBinding(value = {Sink.class,Source.class}) //
public class RocketStreamConfig {
}

交易微服务里的数据发送:

spring:
  cloud:  
    stream:
      rocketmq:
        binder:
          name-server: rocket-server:9876  #/RocketMQ Message hasn't been sent
        binders:
          order_out: {consumer.orderly: true}
      bindings:
        order_out: {destination: order_in, content-type: application/plain}
        cancel_order_in: {destination: cancel_order_out, content-type: application/plain, group: order-group, consumer.maxAttempts: 1}
        exchange_trade_in: {destination: exchange_trades_out, content-type: application/plain, group: order-group, consumer.maxAttempts: 1}

1、收发接口

package com.dragonwu.config.rocket;

import com.dragonwu.enums.MessageChannel;
import org.springframework.cloud.stream.annotation.Input;


public interface Sink {

    
    @Input("exchange_trade_in")
    MessageChannel exchangeTradeIn() ;



    
    @Input("cancel_order_in")
    MessageChannel cancelOrderIn() ;
}
package com.dragonwu.config.rocket;

import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;

public interface Source {

    @Output("order_out")
    MessageChannel outputMessage() ;
}

配置类:

package com.dragonwu.config.rocket;

import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.context.annotation.Configuration;

@Configuration
@EnableBinding(value = Source.class)
public class RocketMQConfig {
}

服务消费者:

package com.dragonwu.config.rocket;


import com.dragonwu.domain.ExchangeTrade;
import com.dragonwu.service.EntrustOrderService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;


@Component
@Slf4j
public class ExchangeTradeListener {

    @Autowired
    private EntrustOrderService entrustOrderService ;

    @Transactional
    @StreamListener("exchange_trade_in")
    public void receiveExchangeTrade(List exchangeTrades){
        if (CollectionUtils.isEmpty(exchangeTrades)){
            return;
        }
        for (ExchangeTrade exchangeTrade : exchangeTrades) {
            if (exchangeTrade != null) {
                //  交易完成后,去更新我们的数据库
                entrustOrderService.doMatch(exchangeTrade);
            }
        }
    }


    @Transactional
    @StreamListener("cancel_order_in")
    public void receiveCancelOrder(String orderId){
        entrustOrderService.cancleEntrustOrderToDb(orderId);
    }
}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/937257.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号