12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
-
- function kafkaProducer( $topic, $msg_data ){
- if (!extension_loaded('rdkafka')){
- echo 'rdkafka extension is not installed!!'.PHP_EOL;
- return false;
- }
- /********************* 初始化生产者配置项 start **************************/
- //考勤记录分析结果生产者
- $rk = new RdKafka\Producer();
- $rk->setLogLevel(LOG_DEBUG);
- $rk->addBrokers(C('KAFKA_BROKER_LIST'));
- $start = microtime(true);
- $topic_obj = $rk->newTopic($topic);
- /********************* 初始化生产者配置项 end **************************/
- if( empty($msg_data) ){
- return;
- }
- if( is_array($msg_data) || is_object($msg_data) ){ // 对象、数组转换
- $msg_data = json_encode($msg_data);
- }
- $topic_obj->produce(RD_KAFKA_PARTITION_UA, 0, $msg_data);
- $rk->poll(0);
- echo 'produce a msg to topic: '.$topic. PHP_EOL;
-
- }
-
-
- function mockProduce( ){
- /*
- $msg_data = '{
- "type":9,
- "title":"群推消息",
- "content":"这是一个广播"
- }';
- kafkaProducer('gps_location_data',$msg_data);
-
- // jyzl gps
- $msg_data = array(
- 'DeviceId' => FFFFFF123122,
- 'State' => 1,
- 'Speed' => 1.2,
- 'Longitude' => 121.20638,
- 'Latitude' => 30.18852,
- 'DeviceTime' => date('Y-m-d H:i:s'),
- 'LBS' => 'LBS',
- 'Direction' => 's',
- );
- kafkaProducer('gps_location_data',$msg_data);*/
-
- // fly 轨迹数据
- $msg_data = array(
- 'StationCode' => '30B5F101237D',
- "Longitude" => "120.600889",
- "Latitude" => "30.191478",
- "Address" => "华城·和瑞科技广场(长河路475号)",
- 'StationType' => 0,
- "CityId" => "2902",
- "StationName" => "基站名称",
- "AddTime" => date('Y-m-d H:i:s',strtotime('-3 minutes')),
- "VehicleNumber" => "86412717AC",
- "VehicleStatus" => "1",
- "SignalCount" => 88,
- "OnlineTime" => date('Y-m-d H:i:s',strtotime('-2 minutes'))
- );
- kafkaProducer('fly_vroute_data',$msg_data);
- }
-
-
- function debug_log( $filename, $data ){
- if(!APP_DEBUG){
- return;
- }
- $file = SOLUTION_LOG_PATH .APP_PREFIX .'/'.date("Ymd", time()) ."/".$filename.".log";
- $folder=dirname($file);
- if (!is_dir($folder)){
- mkdir($folder,0777,true);
- }
- echo '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL;
- file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL,FILE_APPEND);
- }
-
|