作者:幽人贞吉--幽若涵轩_721 | 来源:互联网 | 2023-08-29 10:50
环境背景某广告系统日志上报服务,每天接受亿级别接受数据,常用消息队列有awssqskiness,rabbitmq,kafka,还有鸡肋版本的redisstream,个人事后建议使用
环境背景
某广告系统日志上报服务,每天接受亿级别接受数据, 常用消息队列有 aws sqs kiness,rabbitmq,kafka,还有鸡肋版本的redis stream,个人事后建议使用(rabbitmq,kafka)、但是由于对消息队列的不了解 为了快速开发用了redis 简单轻小
- 异步消息队列redis stream
- 数据库 tidb存储,tidb是个好东西 不用分表分库,但是对高可用这方面还是差点 需要对tidb捣鼓好长时间调优才可用
- laravelS 自定义进程
- laravelS HTTP服务
namespace App\Processes;
use Hhxsv5\LaravelS\Swoole\Process\CustomProcessInterface;
use Illuminate\Support\Facades\Redis;
use Swoole\Http\Server;
use Swoole\Process;
class LoggerProcess implements CustomProcessInterface
{
private static $quit = false;
public static $group_name = '';
public static $consumer_name = '';
public static function callback(Server $swoole, Process $process){
self::$group_name = "logger";
self::$consumer_name = "logger_".rand(1,100);
self::groupInit();
self::handle();
}
public static function onReload(Server $swoole, Process $process)
{
self::$quit = true;
}
public static function onStop(Server $swoole, Process $process)
{
self::$quit = true;
}
public static function handle(){
$cache_key = 'LOGGER_STREAM';
$cache_retry_key = 'LOGGER_RETRY_STREAM';
while (!self::$quit){
$data = [];
try {
$get_result = Redis::xReadGroup(self::$group_name,self::$consumer_name,[$cache_key=>">"],1000,1000);
if(!$get_result || !isset($get_result[$cache_key])){
continue;
}
$data = $get_result[$cache_key];
self::insertData($data);
Redis::xAck($cache_key,self::$group_name,array_keys($data));
Redis::xDel($cache_key,array_keys($data));
}catch (\Exception $exception){
if ($data){
foreach ($data as $item){
Redis::xAdd($cache_retry_key,"*",$item);
}
Redis::xAck($cache_key,self::$group_name,array_keys($data));
}
$str = "[".date('Y-m-d H:i:s')."] ".class_basename(__CLASS__).":系统异常,".catchErr($exception);
}
}
}
public static function groupInit(){
$cache_key = 'LOGGER_STREAM';
$group_result = Redis::xInfo('GROUPS', $cache_key);
$exist_group=false;
if ($group_result){
foreach($group_result as $group){
if($group['name'] === self::$group_name) {
$exist_group = true;
break;
}
}
}
if(!$exist_group){
Redis::xGroup('CREATE', $cache_key,self::$group_name, "0");
}
}
}
php