最新消息:欢迎加入小松的QQ群一起讨论一起学习,搜索页面修改成lua+ElasticSearch,使用php这个页面速度相对慢一点,优化之后速度杠杠的,如有问题请加群联系我

电商网站订单同步到内部系统架构

PHP 7浏览 0评论

电商网站的订单怎么同步到公司内部系统,之前的做法,网站下完单就会有邮件发到客服的邮箱,邮件内容中包含了订单号,在内部系统中粘贴订单号从网站上获取订单信息,如果订单取消了,也是根据邮件,来取消内部系统的订单,这种半自动的方式还是比较麻烦,经常客服漏看了邮件,订单进不到内部系统后面的配货发货影响时效,对用户体验不好。
方式改进之后
通过rabbitma队列来处理,支付之后把订单信息写入到队列里面,内部系统写一个php脚本不停的去跑,这样时效的文件解决了,也不会发生漏单的情况,系统更新也比较及时。
下面是队列的主要代码实例

<?php  
/* 
 * amqp协议操作类,可以访问rabbitMQ 
 * 需先安装php_amqp扩展 
 */  
class RabbitMQCommand{  
  
    public $configs = array();  
    //交换机名称  
    public $exchange_name = '';  
    //队列名称  
    public $queue_name = '';  
    //路由名称  
    public $route_key = '';  
    /* 
     * 持久化,默认True 
     */  
    public $durable = True;  
    /* 
     * 自动删除 
     * exchange is deleted when all queues have finished using it 
     * queue is deleted when last consumer unsubscribes 
     *  
     */  
    public $autodelete = False;  
    /* 
     * 镜像 
     * 镜像队列,打开后消息会在节点之间复制,有master和slave的概念 
     */  
    public $mirror = False;  
      
    private $_conn = Null;  
    private $_exchange = Null;  
    private $_channel = Null;  
    private $_queue = Null;  
  
    /* 
     * @configs array('host'=>$host,'port'=>5672,'username'=>$username,'password'=>$password,'vhost'=>'/') 
     */  
  
    public function __construct($configs = array(), $exchange_name = '', $queue_name = '', $route_key = '') {  
        $this->setConfigs($configs);  
        $this->exchange_name = $exchange_name;  
        $this->queue_name = $queue_name;  
        $this->route_key = $route_key;  
    }  
      
    private function setConfigs($configs) {  
        if (!is_array($configs)) {  
            throw new Exception('configs is not array');  
        }  
        if (!($configs['host'] && $configs['port'] && $configs['username'] && $configs['password'])) {  
            throw new Exception('configs is empty');  
        }  
        if (empty($configs['vhost'])) {  
            $configs['vhost'] = '/';  
        }  
        $configs['login'] = $configs['username'];  
        unset($configs['username']);  
        $this->configs = $configs;  
    }  
  
    /* 
     * 设置是否持久化,默认为True 
     */  
  
    public function setDurable($durable) {  
        $this->durable = $durable;  
    }  
  
    /* 
     * 设置是否自动删除 
     */  
  
    public function setAutoDelete($autodelete) {  
        $this->autodelete = $autodelete;  
    }  
    /* 
     * 设置是否镜像 
     */  
    public function setMirror($mirror) {  
        $this->mirror = $mirror;  
    }  
  
    /* 
     * 打开amqp连接 
     */  
  
    private function open() {  
        if (!$this->_conn) {  
            try {  
                $this->_conn = new AMQPConnection($this->configs);  
                $this->_conn->connect();  
                $this->initConnection();  
            } catch (AMQPConnectionException $ex) {  
                throw new Exception('cannot connection rabbitmq',500);  
            }  
        }  
    }  
  
    /* 
     * rabbitmq连接不变 
     * 重置交换机,队列,路由等配置 
     */  
  
    public function reset($exchange_name, $queue_name, $route_key) {  
        $this->exchange_name = $exchange_name;  
        $this->queue_name = $queue_name;  
        $this->route_key = $route_key;  
        $this->initConnection();  
    }  
  
    /* 
     * 初始化rabbit连接的相关配置 
     */  
  
    private function initConnection() {  
        if (empty($this->exchange_name) || empty($this->queue_name) || empty($this->route_key)) {  
            throw new Exception('rabbitmq exchange_name or queue_name or route_key is empty',500);  
        }  
        $this->_channel = new AMQPChannel($this->_conn);  
        $this->_exchange = new AMQPExchange($this->_channel);  
        $this->_exchange->setName($this->exchange_name);  
  
        $this->_exchange->setType(AMQP_EX_TYPE_DIRECT);  
        if ($this->durable)  
            $this->_exchange->setFlags(AMQP_DURABLE);  
        if ($this->autodelete)  
            $this->_exchange->setFlags(AMQP_AUTODELETE);  
        $this->_exchange->declare();  
  
        $this->_queue = new AMQPQueue($this->_channel);  
        $this->_queue->setName($this->queue_name);  
        if ($this->durable)  
            $this->_queue->setFlags(AMQP_DURABLE);  
        if ($this->autodelete)  
            $this->_queue->setFlags(AMQP_AUTODELETE);  
        if ($this->mirror)   
            $this->_queue->setArgument('x-ha-policy', 'all');  
        $this->_queue->declare();  
          
        $this->_queue->bind($this->exchange_name, $this->route_key);  
    }  
  
    public function close() {  
        if ($this->_conn) {  
            $this->_conn->disconnect();  
        }  
    }  
      
    public function __sleep() {  
        $this->close();  
        return array_keys(get_object_vars($this));  
    }  
  
    public function __destruct() {  
        $this->close();  
    }  
      
    /* 
     * 生产者发送消息 
     */  
    public function send($msg) {  
        $this->open();  
        if(is_array($msg)){  
            $msg = json_encode($msg);  
        }else{  
            $msg = trim(strval($msg));  
        }  
        return $this->_exchange->publish($msg, $this->route_key);  
    }  
    /* 
     * 消费者 
     * $fun_name = array($classobj,$function) or function name string 
     * $autoack 是否自动应答 
     *  
     * function processMessage($envelope, $queue) { 
            $msg = $envelope->getBody();  
            echo $msg."\n"; //处理消息 
            $queue->ack($envelope->getDeliveryTag());//手动应答 
        } 
     */  
    public function run($fun_name, $autoack = True){  
        $this->open();  
        if (!$fun_name || !$this->_queue) return False;    
        while(True){  
            if ($autoack) $this->_queue->consume($fun_name, AMQP_AUTOACK);     
            else $this->_queue->consume($fun_name);      
        }  
    }  
  
}

订单支付成功,写入队列代码

<?php  
set_time_limit(0);  
include_once('RabbitMQCommand.php');  
  
$configs = array('host'=>'127.0.0.1','port'=>5672,'username'=>'asdf','password'=>'123456','vhost'=>'/');  
$exchange_name = 'class-e-1';  
$queue_name = 'class-q-1';  
$route_key = 'class-r-1';  
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);  
for($i=0;$i<=100;$i++){  
    $ra->send(date('Y-m-d H:i:s',time()));  
}  
exit();

处理订单队列脚本代码

<?php  
error_reporting(0);  
include_once('RabbitMQCommand.php');  
  
  
$configs = array('host'=>'127.0.0.1','port'=>5672,'username'=>'asdf','password'=>'123456','vhost'=>'/');  
$exchange_name = 'class-e-1';  
$queue_name = 'class-q-1';  
$route_key = 'class-r-1';  
$ra = new RabbitMQCommand($configs,$exchange_name,$queue_name,$route_key);  
  
  
class A{  
    function processMessage($envelope, $queue) {  
        $msg = $envelope->getBody();  
        $envelopeID = $envelope->getDeliveryTag();  
        $pid = posix_getpid();  
        file_put_contents("log{$pid}.log", $msg.'|'.$envelopeID.''."\r\n",FILE_APPEND);  
        $queue->ack($envelopeID);  
    }  
}  
$a = new A();  
  
  
$s = $ra->run(array($a,'processMessage'),false);

PS.我这里用的是rabbitma队列来处理,所有php运行需要amq的扩展

 

QQ交流群:136351212

如无特别说明,本站文章皆为原创,若要转载,务必请注明以下原文信息:
转载保留版权:小松博客» 电商网站订单同步到内部系统架构
本文链接地址:https://www.phpsong.com/3273.html

发表我的评论
取消评论
表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
木有头像就木有JJ!点这里按步骤申请Gravatar头像吧!

网友最新评论 (1)

加载中,请稍候...
  1. 非常不错!!!!电商网站必备啊。