pub_func.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. function kafkaProducer( $topic, $msg_data ){
  3. if (!extension_loaded('rdkafka')){
  4. echo 'rdkafka extension is not installed!!'.PHP_EOL;
  5. return false;
  6. }
  7. /********************* 初始化生产者配置项 start **************************/
  8. //考勤记录分析结果生产者
  9. $rk = new RdKafka\Producer();
  10. $rk->setLogLevel(LOG_DEBUG);
  11. $rk->addBrokers(C('KAFKA_BROKER_LIST'));
  12. $start = microtime(true);
  13. $topic_obj = $rk->newTopic($topic);
  14. /********************* 初始化生产者配置项 end **************************/
  15. if( empty($msg_data) ){
  16. return;
  17. }
  18. if( is_array($msg_data) || is_object($msg_data) ){ // 对象、数组转换
  19. $msg_data = json_encode($msg_data);
  20. }
  21. $topic_obj->produce(RD_KAFKA_PARTITION_UA, 0, $msg_data);
  22. $rk->poll(0);
  23. echo 'produce a msg to topic: '.$topic. PHP_EOL;
  24. }
  25. function mockProduce( ){
  26. /*
  27. $msg_data = '{
  28. "type":9,
  29. "title":"群推消息",
  30. "content":"这是一个广播"
  31. }';
  32. kafkaProducer('gps_location_data',$msg_data);
  33. // jyzl gps
  34. $msg_data = array(
  35. 'DeviceId' => FFFFFF123122,
  36. 'State' => 1,
  37. 'Speed' => 1.2,
  38. 'Longitude' => 121.20638,
  39. 'Latitude' => 30.18852,
  40. 'DeviceTime' => date('Y-m-d H:i:s'),
  41. 'LBS' => 'LBS',
  42. 'Direction' => 's',
  43. );
  44. kafkaProducer('gps_location_data',$msg_data);*/
  45. // fly 轨迹数据
  46. $msg_data = array(
  47. 'StationCode' => '30B5F101237D',
  48. "Longitude" => "120.600889",
  49. "Latitude" => "30.191478",
  50. "Address" => "华城·和瑞科技广场(长河路475号)",
  51. 'StationType' => 0,
  52. "CityId" => "2902",
  53. "StationName" => "基站名称",
  54. "AddTime" => date('Y-m-d H:i:s',strtotime('-3 minutes')),
  55. "VehicleNumber" => "86412717AC",
  56. "VehicleStatus" => "1",
  57. "SignalCount" => 88,
  58. "OnlineTime" => date('Y-m-d H:i:s',strtotime('-2 minutes'))
  59. );
  60. kafkaProducer('fly_vroute_data',$msg_data);
  61. }
  62. function debug_log( $filename, $data ){
  63. if(!APP_DEBUG){
  64. return;
  65. }
  66. $file = SOLUTION_LOG_PATH .APP_PREFIX .'/'.date("Ymd", time()) ."/".$filename.".log";
  67. $folder=dirname($file);
  68. if (!is_dir($folder)){
  69. mkdir($folder,0777,true);
  70. }
  71. echo '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL;
  72. file_put_contents($file, '[' . date('Y-m-d H:i:s') . ']' . $data . PHP_EOL,FILE_APPEND);
  73. }