# hyperf-tenancy **Repository Path**: cmslz/hyperf-tenancy ## Basic Information - **Project Name**: hyperf-tenancy - **Description**: hyperf多租户模式 - **Primary Language**: PHP - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 2 - **Forks**: 0 - **Created**: 2023-09-07 - **Last Updated**: 2024-04-16 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # hyperf-tenancy 安装使用 ```shell composer require cmslz/hyperf-tenancy ``` https://github.com/cmslz/hyperf-tenancy ## 介绍 > 1. 可通过header参数或在GET参数绑定对应租户 >> Header:x-tenant-id:xxxx > > GET:tenant = xxx ## 配置 - amqp.php > \Cmslz\HyperfTenancy\Kernel\Tenant\AsyncQueue\RedisDriver::class - annotations.php ```PHP return [ 'scan' => [ 'class_map' => [ Hyperf\Coroutine\Coroutine::class => BASE_PATH . '/vendor/cmslz/hyperf-tenancy/src/Kernel/ClassMap/Coroutine.php', Hyperf\Database\Migrations\Migration::class => BASE_PATH . '/vendor/cmslz/hyperf-tenancy/src/Kernel/Migrations/Migration.php', ] ] ]; ``` - dependencies.php ```PHP return [ Psr\EventDispatcher\EventDispatcherInterface::class => \Cmslz\HyperfTenancy\Kernel\Event\EventDispatcherFactory::class, Hyperf\Database\ConnectionResolverInterface::class => \Cmslz\HyperfTenancy\Kernel\Tenant\ConnectionResolver::class, ]; ``` - tenancy.php > [config.tenancy](/publish/config.php) - cache.php ```PHP return [ ...[], // 中央域缓存 'central' => [ 'driver' => \Cmslz\HyperfTenancy\Kernel\Cache\RedisDriver::class, 'packer' => Hyperf\Codec\Packer\PhpSerializerPacker::class, 'prefix' => 'central:cache:', ], // 租户缓存 'tenant' => [ 'driver' => \Cmslz\HyperfTenancy\Kernel\Tenant\Cache\RedisDriver::class, 'packer' => Hyperf\Codec\Packer\PhpSerializerPacker::class, 'prefix' => 'tenant:cache:' ], ]; ``` - databases.php ```PHP return [ ...[], 'central' => [ 'driver' => env('DB_DRIVER', 'mysql'), 'host' => env('DB_HOST', 'localhost'), 'port' => env('DB_PORT', 3306), 'database' => env('DB_CENTRAL_DATABASE', 'central'), 'username' => env('DB_CENTRAL_USERNAME', 'root'), 'password' => env('DB_CENTRAL_PASSWORD', ''), 'charset' => env('DB_CHARSET', 'utf8mb4'), 'collation' => env('DB_COLLATION', 'utf8mb4_unicode_ci'), 'prefix' => env('DB_CENTRAL_PREFIX', ''), 'pool' => [ 'min_connections' => 1, 'max_connections' => 10, 'connect_timeout' => 10.0, 'wait_timeout' => 3.0, 'heartbeat' => -1, 'max_idle_time' => (float) env('DB_MAX_IDLE_TIME', 60), ], 'cache' => [ 'handler' => Hyperf\ModelCache\Handler\RedisHandler::class, 'cache_key' => '{mc:%s:m:%s}:%s:%s', 'prefix' => 'central', 'ttl' => 3600 * 24, 'empty_model_ttl' => 600, 'load_script' => true, ], 'commands' => [ 'gen:model' => [ 'path' => 'app/Model', 'force_casts' => true, 'inheritance' => 'Model', 'uses' => '', 'refresh_fillable' => true, 'table_mapping' => [], ], ], ] ]; ``` - redis.php ```PHP return [ ...[], // 中央域通用redis 'central' => [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', null), 'port' => (int)env('REDIS_PORT', 6379), 'db' => (int)env('REDIS_DB', 0), 'pool' => [ 'min_connections' => 1, 'max_connections' => 32, 'connect_timeout' => 10.0, 'wait_timeout' => 3.0, 'heartbeat' => -1, 'max_idle_time' => (float)env('REDIS_MAX_IDLE_TIME', 60), ] ], // 租户通用redis 'tenant' => [ 'host' => env('REDIS_HOST', 'localhost'), 'auth' => env('REDIS_AUTH', null), 'port' => (int)env('REDIS_PORT', 6379), 'db' => (int)env('REDIS_DB', 0), 'pool' => [ 'min_connections' => 1, 'max_connections' => 32, 'connect_timeout' => 10.0, 'wait_timeout' => 3.0, 'heartbeat' => -1, 'max_idle_time' => (float)env('REDIS_MAX_IDLE_TIME', 60), ] ] ]; ``` ## 使用 在需要使用路由添加中间件 `TenantMiddleware::class` ### 队列使用 #### 消费端 ```PHP params = $params; } public function handle() { var_dump($this->params, tenancy()->getId()); } } ``` #### 客户端 ```PHP use App\Job\TenantJob; queue_push(new TenantJob(['2131313']),5); ```