队列和定时任务不同场景下指定不同配置文件运行

1. 运行环境

宝塔面板部署的laravel8 用的是nginx+php7.4

2. 问题描述?

我的laravel系统是多账号共用一套代码,每个账号有自己的env文件,有自己的数据库。如下图

Laravel

Laravel

  1. 我现在要使用队列执行任务,但是发现队列总是使用的是.env.env中配置的数据库,这个其实是默认的配置文件,并非我当前调用队列的用户对应的env文件和数据库
  2. 我要启用定时任务的话,和队列一样,也用的是默认的配置,怎么样才能做到在不同的env配置下运行

3. 您期望得到的结果?

  1. 针对队列,我当前的用户是aa 访问域名是aa.saas.test 配置文件是.env.aa_saas_test 。能不能在aa.saas.test域名下的所有队列都应用.env.aa_saas_test配置文件
  2. 针对定时任务,因为用户不确定会有多少,每个用户除了代码 啥都是独立的,目前没啥想法能解决这个。

请大佬赐教..

Beer
《L04 微信小程序从零到发布》
从小程序个人账户申请开始,带你一步步进行开发一个微信小程序,直到提交微信控制台上线发布。
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
最佳答案
// Detect if a custom environment file matching the APP_ENV exists.
// function name checkForSpecificEnvironmentFile 
if ($app->runningInConsole() &&
            ($input = new ArgvInput)->hasParameterOption('--env') &&
            $this->setEnvironmentFilePath($app, $app->environmentFile().'.'.$input->getParameterOption('--env'))) {
            return;
        }

The second way

export APP_ENV='env_name'
1年前 评论
sanlilin (楼主) 1年前
陈先生 (作者) 1年前
sanlilin (楼主) 1年前
讨论数量: 8
// Detect if a custom environment file matching the APP_ENV exists.
// function name checkForSpecificEnvironmentFile 
if ($app->runningInConsole() &&
            ($input = new ArgvInput)->hasParameterOption('--env') &&
            $this->setEnvironmentFilePath($app, $app->environmentFile().'.'.$input->getParameterOption('--env'))) {
            return;
        }

The second way

export APP_ENV='env_name'
1年前 评论
sanlilin (楼主) 1年前
陈先生 (作者) 1年前
sanlilin (楼主) 1年前

分库的saas方案?

不能,还有更多的坑在后边等着你。

关于你的问题,试试这个包吧:tenancyforlaravel.com

1年前 评论
sanlilin (楼主) 1年前

假设拥有环境:

  • testing_a
  • testing_b

需创建配置文件:

  • .env.testing_a
  • .env.testing_b

在命令行环境中区分方式,以启动队列命令为例:

php artisan queue:work --env=testing_a  // 会使用 .env.testing_a 配置
php artisan queue:work --env=testing_b  // 会使用 .env.testing_b 配置

在WEB访问中,如果你系统每个租户域名都是不同的,那么你可通过配置 nginx 来区分:

// 租户A配置,重点关注 fastcgi_param  APP_ENV
server {
    listen 80;
    server_name  租户A.com;
    已省略部分配置...

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  APP_ENV testing_a;
        include        fastcgi_params;
    }
}

// 租户B配置,重点关注 fastcgi_param  APP_ENV
server {
    listen 80;
    server_name  租户B.com;
    已省略部分配置...

    location ~ \.php$ {
        fastcgi_pass   127.0.0.1:9000;
        fastcgi_index  index.php;
        fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
        fastcgi_param  APP_ENV testing_b;
        include        fastcgi_params;
    }
}
1年前 评论
sanlilin (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!