#最近项目中要求跨服务器同步数据,想到两种解决办法
#方法1 http请求
#通过curl 将数据同步到另外系统,但http不确定因素太多丢失部分数据且没办法做记录
#方法2 异步队列
#在发送端做定时推送,数据存储到redis中,redis将存储的数据分发到接收接口
/** * 定时推送套装至company系统 * @author :[email protected] */ public function pushSuits() { (php_sapi_name() == ‘cli‘) or die(‘Please run under the cli‘); $this->load->model(‘suits_model‘); $this->load->helper(‘log‘); $this->load->library(‘RedisDriver‘, ‘redisdriver‘); while (true) { $this->redisdriver->connect(); $device_last_push_id = $this->redisdriver->getStr(‘device_last_push_id‘); $device_last_push_id = is_null($device_last_push_id) ? ‘2846‘ : $device_last_push_id; // 旧数据不推送 // 完成的套装 $suits = $this->suits_model->pushSuitsById($device_last_push_id, 0, 200); $datas = array(); foreach ($suits as $row) { // 料号不为空 if (!empty($row[‘pn‘])) { $datas[] = $row[‘pn‘] . ‘|‘ . $row[‘code‘]; } } // 有数据时 if (!empty($datas)) { $params = array( ‘suits‘ => implode(‘,‘, $datas), ); // 压入异步http队列 $this->redisdriver->pushList(‘queue_device_inventory‘, $params); log_info(‘queue_device_inventory‘, json_encode($params)); // 保存最后操作id $this->redisdriver->setStr(‘device_last_push_id‘, $suits[count($suits)-1][‘id‘]); log_info(‘device_last_push_id‘, $suits[count($suits)-1][‘id‘]); } sleep(1800); } }
时间: 2024-11-07 13:09:19