zoukankan      html  css  js  c++  java
  • 多节点,多线程下发订单,使用zookeeper分布式锁机制保证订单正确接入oms系统

     假设订单下发, 采用单机每分钟从订单OrderEntry接口表中抓100单, 接入订单oms系统中. 由于双十一期间, 订单量激增, 导致订单单机每分钟100单造成, 订单积压. 所以采用多节点多线程处理三方商家的订单接入系统.

    流程图:

    如图所示:

    采用多节点, 多线程, 使用curator操作zk的方式实现, 订单的下发.

    以前的方式每分钟下发100单, 之后采用现在的方式可以每分钟2000单, 大大增加系统的订单下发的处理速度, 系统的吞吐量, 提高了效率.

    1 操作zookeeper的工具类:

    import org.apache.curator.RetryPolicy;
    import org.apache.curator.framework.CuratorFramework;
    import org.apache.curator.framework.CuratorFrameworkFactory;
    import org.apache.curator.framework.recipes.locks.InterProcessMutex;
    import org.apache.curator.retry.ExponentialBackoffRetry;
     
    public class ZkUtils {
     
        private static final String zkStr = ConfUtil.getProperty("zkStr");
        private static final String zkPath = ConfUtil.getProperty("zkPath");
        
        public static InterProcessMutex createMutexLock(){
            
            RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10); //1 重试策略:初试时间为1s 重试10次
            
            CuratorFramework client = CuratorFrameworkFactory.builder()//2 通过工厂创建连接
                        .connectString(zkStr)
                        .sessionTimeoutMs(5000)
                        .retryPolicy(retryPolicy)
                        .build();
            client.start(); // 开启连接
            InterProcessMutex mutexLock = new InterProcessMutex(client, zkPath); // 创建分布式锁
            return mutexLock;
            
        }
    }

    2 使用该工具类处理订单下发业务逻辑

    public class Job {
        private final static org.slf4j.Logger logger=LoggerFactory.getLogger(HnJob.class);
     
        @Autowired
        private IHNOrderService iHNOrderService;
        
        public void execute(){
            
            InterProcessMutex mutexLock = ZkUtils.createMutexLock();
            try{
                mutexLock.acquire();
                bizHandler(); //执行订单下发业务处理逻辑
                
            }catch (Exception e) {
                e.printStackTrace();
            }finally{
                try {
                    mutexLock.release();
                } catch (Exception e) {
                    e.printStackTrace();
                } 
            }
        }
        
        void bizHandler(){
            long start=System.currentTimeMillis();
            logger.info("====================订单下发开始");
            iHNOrderService.hnOrderQuery();
            long end=System.currentTimeMillis();
            logger.info("=================订单下发结束,耗时={}秒",(end-start)/(1000));
        }
        
        
        
    }
  • 相关阅读:
    Path Sum II
    Convert Sorted Array to Binary Search Tree
    Construct Binary Tree from Inorder and Postorder Traversal
    Construct Binary Tree from Preorder and Inorder Traversal
    Maximum Depth of Binary Tree
    Binary Tree Zigzag Level Order Traversal
    Binary Tree Level Order Traversal
    Same Tree
    Validate Binary Search Tree
    Binary Tree Inorder Traversal
  • 原文地址:https://www.cnblogs.com/diaozhaojian/p/10582700.html
Copyright © 2011-2022 走看看