这是一个订单匹配服务。在测试中,我们观察到只要X订单与Y订单相匹配(深度不超过9),该服务就可以正常工作。要说明一下,如果有9个卖单,并且买方下了一个买单来一次交易全部9个订单,服务将完全执行。
但是,如果订单中有9个以上的订单,则问题开始。 Hystrix是在THREAD策略上配置的,超时为120000。但是,一旦执行的订单超过9个,就会出现“ SHORT-CIRCUIT”错误,整个应用程序将搁置5-10分钟。
调试了具有不同逻辑的代码,但一次最多可以处理9个订单。
import java.util.Optional;
@服务 @ Log4j
公共类MatchService {
@Autowired
OrderRepository orderRepository;
@Autowired
OrderSecureGatewayClient orderGatewayClient;
public void saveOrder(MatchOrderRequest matchOrderRequest) {
ExecuteOrderRequest executeOrderRequest = new ExecuteOrderRequest();
ExecuteOrderRequest.Order executedOrder = new ExecuteOrderRequest.Order();
synchronized (this) {
MatchOrder request = new MatchOrder();
request.setVolume(matchOrderRequest.getVolume());
request.setPrice(matchOrderRequest.getPrice());
request.setOrderType(matchOrderRequest.getOrderType());
request.setOrderId(matchOrderRequest.getOrderId());
request.setMarket(matchOrderRequest.getMarket());
request.setTimestamp(System.currentTimeMillis());
request = orderRepository.save(request);
Optional<MatchOrder> optionalMatchOrder;
log.info("Order Type: " + matchOrderRequest.getOrderType());
if (matchOrderRequest.getOrderType() == OrderType.BUY) {
optionalMatchOrder = orderRepository.
findTopByMarketAndOrderTypeAndPriceLessThanEqualOrderByPriceAsc(request.getMarket(), OrderType.SELL, request.getPrice());
log.info(">>>>>> Buy order found <<<<<< " + request.getMarket() + "Price: " + request.getPrice());
} else {
optionalMatchOrder = orderRepository.
findTopByMarketAndOrderTypeAndPriceGreaterThanEqualOrderByPriceDesc(request.getMarket(), OrderType.BUY, request.getPrice());
}
log.info(optionalMatchOrder.toString());
if (optionalMatchOrder.isPresent()) {
executedOrder.setOrderId(request.getOrderId());
ExecuteOrderRequest.Order pOrder = new ExecuteOrderRequest.Order();
pOrder.setOrderId(optionalMatchOrder.get().getOrderId());
executeOrderRequest.setExecutedOrder(executedOrder);
executeOrderRequest.setAgainstOrder(pOrder);
orderRepository.delete(request.getId());
orderRepository.delete(optionalMatchOrder.get().getId());
}
}
if (executeOrderRequest.getAgainstOrder() != null && executeOrderRequest.getExecutedOrder() != null) {
orderGatewayClient.orderExecuted(executeOrderRequest);
}
}
Application.YML
eureka:
client:
serviceUrl:
defaultZone: ${EUREKA_URI:http://localhost:8000/eureka/}
instance:
preferIpAddress: true
hystrix:
command:
default:
execution:
timeout:
enabled: false
isolation:
strategy: THREAD
thread:
timeoutInMilliseconds: 120000
ribbon:
eureka:
enabled: true
MaxAutoRetries: 0
MaxAutoRetriesNextServer: 0
ConnectTimeout: 120000
ReadTimeout: 120000
OkToRetryOnAllOperations: false
无论每次要执行的订单数量如何,我们都需要匹配服务才能正常工作。即使有人要买断整个订单,也不应触发短路。