namoshek/laravel-redis-sentinel
Composer 安装命令:
composer require namoshek/laravel-redis-sentinel
包简介
An extension of Laravels Redis driver which supports connecting to a Redis master through Redis Sentinel.
README 文档
README
This package provides a Laravel Redis driver which allows connecting to a Redis master through a Redis Sentinel instance. The package is intended to be used in a Kubernetes environment or similar, where connecting to Redis Sentinels is possible through a load balancer.
This driver is an alternative to monospice/laravel-redis-sentinel-drivers.
The primary difference is that this driver supports the phpredis/phpredis PHP extension
and has significantly simpler configuration, due to a simpler architecture.
In detail this means that this package does not override the entire Redis subsystem of Laravel, it only adds an additional driver.
By default, Laravel supports the predis and phpredis drivers. This package adds a third phpredis-sentinel driver,
which is an extension of the phpredis driver for Redis Sentinel.
An extension for predis is currently not available and not necessary, since predis/predis already supports
connecting to Redis through one or more Sentinels.
Installation
You can install the package via composer:
composer require namoshek/laravel-redis-sentinel
The service provider which comes with the package is registered automatically.
Configuration
The package requires no extra configuration and does therefore not provide an additional configuration file.
Usage
To use the Redis Sentinel driver, the redis section in config/database.php needs to be adjusted:
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis-sentinel'), 'default' => [ 'sentinel_host' => env('REDIS_SENTINEL_HOST', '127.0.0.1'), 'sentinel_port' => (int) env('REDIS_SENTINEL_PORT', 26379), 'sentinel_service' => env('REDIS_SENTINEL_SERVICE', 'mymaster'), 'sentinel_timeout' => (float) env('REDIS_SENTINEL_TIMEOUT', 0), 'sentinel_persistent' => env('REDIS_SENTINEL_PERSISTENT'), 'sentinel_retry_interval' => (int) env('REDIS_SENTINEL_RETRY_INTERVAL', 0), 'sentinel_read_timeout' => (float) env('REDIS_SENTINEL_READ_TIMEOUT', 0), 'sentinel_username' => env('REDIS_SENTINEL_USERNAME'), 'sentinel_password' => env('REDIS_SENTINEL_PASSWORD'), 'connector_retry_attempts' => env('REDIS_CONNECTOR_RETRY_ATTEMPTS'), 'connector_retry_delay' => env('REDIS_CONNECTOR_RETRY_DELAY'), 'password' => env('REDIS_PASSWORD'), 'database' => (int) env('REDIS_DB', 0), ] ]
Instead of changing redis.client in the configuration file directly, you can also set REDIS_CLIENT=phpredis-sentinel in the environment variables.
As you can see, there are also a few new sentinel_* options available for each Redis connection.
Most of them work very similar to the normal Redis options, except that they are used for the connection to Redis Sentinel.
Noteworthy is the sentinel_service, which represents the instance name of the monitored Redis master.
All other options are the same for the Redis Sentinel driver, except that url is not supported and host and port are ignored.
SSL/TLS Support
If you want to use SSL/TLS to connect to Redis Sentinel, you need to add an additional configuration option sentinel_ssl next to the other sentinel_* settings:
'sentinel_ssl' => [ // ... SSL settings ... ],
Available SSL context options can be found in the official PHP documentation. Please note that SSL support for the Sentinel connection was added to the phpredis extension starting in version 6.1.
Also note that if your Redis Sentinel resolves SSL connections to Redis, you potentially need to add additional context options for your Redis connection:
'context' => [ 'stream' => [ // ... SSL settings ... ] ], 'scheme' => 'tls',
A full configuration example using SSL for Redis Sentinel as well as Redis looks like this if authentication is also enabled (environment variables omitted for clarity):
'redis' => [ 'client' => 'phpredis-sentinel', 'redis_with_tls' => [ 'sentinel_host' => 'tls://sentinel_host', 'sentinel_port' => 26379, 'sentinel_service' => 'mymaster', 'sentinel_timeout' => 0, 'sentinel_persistent' => false, 'sentinel_retry_interval' => 0, 'sentinel_read_timeout' => 0, 'sentinel_username' => 'sentinel_username', 'sentinel_password' => 'sentinel_password', 'sentinel_ssl' => [ 'cafile' => '/path/to/sentinel_ca.crt', ], 'context' => [ 'stream' => [ 'cafile' => '/path/to/redis_ca.crt', ], ], 'scheme' => 'tls', 'username' => 'redis_username', 'password' => 'redis_password', 'database' => 1, ] ]
The important parts are the tls:// protocol in sentinel_host as well as the tls in scheme, plus the sentinel_ssl and context.stream options.
Because Redis Sentinel resolves Redis instances by IP and port, your Redis certificate needs to have the IP as SAN. Alternatively, you can set verify_peer and maybe also verify_peer_name to false.
Multiple Sentinel instances
If your environment is composed of multiple Redis Sentinel instances, you can use the sentinel_hosts configuration instead of sentinel_host and sentinel_port.
sentinel_hosts is an array where each entry contains a host and a port key corresponding to a Sentinel instance.
Example configuration using multiple Redis Sentinel instances
'redis' => [ 'client' => env('REDIS_CLIENT', 'phpredis-sentinel'), 'default' => [ 'sentinel_hosts' => [ ['host' => '127.0.0.1', 'port' => 26379], ['host' => '127.0.0.1', 'port' => 26380], ['host' => '127.0.0.1', 'port' => 26381], ], 'sentinel_service' => 'mymaster', 'sentinel_timeout' => 0, 'sentinel_persistent' => false, 'sentinel_retry_interval' => 0, 'sentinel_read_timeout' => 0, 'sentinel_username' => 'sentinel_username', 'sentinel_password' => 'sentinel_password', 'password' => 'password', 'database' => 1, ] ]
How does it work?
An additional Laravel Redis driver is added (phpredis-sentinel), which resolves the currently declared master instance of a replication
cluster as active Redis instance. Under the hood, this driver relies on the framework driver for phpredis/phpredis,
it only wraps the connection part of it and adds some error handling which forcefully reconnects in case of a failover.
Please be aware that this package does not manage load balancing between Sentinels (which is supposed to be done on an infrastructure level) and does also not load balance read/write calls to replica/master nodes. All traffic is sent to the currently reported master.
Developing
To run the tests locally, a Redis cluster needs to be running.
The repository contains a script (thanks to monospice/laravel-redis-sentinel-drivers)
which can be used to start one by running sh start-redis-cluster.sh.
The script requires that Redis is installed on your machine. To install Redis on Ubuntu or Debian,
you can use sudo apt update && sudo apt install redis-server. For other operating systems, please see redis.io.
License
The MIT License (MIT). Please see License File for more information.
namoshek/laravel-redis-sentinel 适用场景与选型建议
namoshek/laravel-redis-sentinel 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 785.08k 次下载、GitHub Stars 达 39, 最近一次更新时间为 2021 年 07 月 18 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「redis」 「laravel」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 namoshek/laravel-redis-sentinel 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 namoshek/laravel-redis-sentinel 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 namoshek/laravel-redis-sentinel 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Microservice RPC through message queues.
The CodeIgniter Redis package
贝嘟分布式缓存扩展
Redis distributed locks for laravel
Redis backed library for creating background jobs and processing them later.
Alfabank REST API integration
统计信息
- 总下载量: 785.08k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 39
- 点击次数: 19
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2021-07-18