Skip to content

Commit 1267b25

Browse files
committed
Add automatic configuration and documentation for broadcasting
Signed-off-by: Cy Rossignol <[email protected]>
1 parent 6cf2d08 commit 1267b25

File tree

8 files changed

+312
-106
lines changed

8 files changed

+312
-106
lines changed

Diff for: README.md

+88-67
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Laravel Drivers for Redis Sentinel
66

77
#### Redis Sentinel integration for Laravel and Lumen.
88

9-
[Redis Sentinel][sentinel] provides high-availability, monitoring, and
9+
[Redis Sentinel][sentinel] facilitates high-availability, monitoring, and
1010
load-balancing for Redis servers configured for master-slave replication.
1111
[Laravel][laravel] includes built-in support for Redis, but we cannot configure
1212
Sentinel setups flexibly out-of-the-box. This limits configuration of Sentinel
@@ -18,13 +18,13 @@ types of data like we can in a standard, single-server Redis setup without
1818
Sentinel. This causes issues when we need to clear the cache, because Laravel
1919
erases stored session information as well.
2020

21-
This package wraps the configuration of Laravel's caching, session, and queue
22-
APIs for Sentinel with the ability to set options for our Redis services
23-
independently. We configure the package separately from Laravel's standard
24-
Redis configuration so we can choose to use the Sentinel connections as needed
25-
by the environment. A developer may use a standalone Redis server in their
26-
local environment, while production environments operate a Redis Sentinel set
27-
of servers.
21+
This package wraps the configuration of Laravel's broadcasting, cache, session,
22+
and queue APIs for Sentinel with the ability to set options for our Redis
23+
services independently. We configure the package separately from Laravel's
24+
standard Redis configuration so we can choose to use the Sentinel connections
25+
as needed by the environment. A developer may use a standalone Redis server in
26+
their local environment, while production environments operate a Redis Sentinel
27+
set of servers.
2828

2929
Contents
3030
--------
@@ -78,13 +78,21 @@ composer require monospice/laravel-redis-sentinel-drivers
7878
composer require monospice/laravel-redis-sentinel-drivers:^1.0
7979
```
8080

81+
**Note:** According to the Laravel release schedule, all Laravel versions prior
82+
to 5.4 exited their active development and support periods in August of 2017.
83+
After December, 2017, this package will no longer provide feature releases on
84+
the `1.x` branch for Laravel versions earlier than 5.4.
85+
8186
If the project does not already use Redis with Laravel, this will install the
8287
[Predis][predis] package as well.
8388

8489
#### Register the Service Provider
8590

86-
To use the drivers in Laravel, add the package's service provider to
87-
*config/app.php*:
91+
Laravel 5.5 brings [package discovery][laravel-package-discovery-docs]! *No
92+
service provider registration required in Laravel 5.5+.*
93+
94+
To use the drivers in Laravel 5.4 and below, add the package's service provider
95+
to *config/app.php*:
8896

8997
```php
9098
'providers' => [
@@ -162,8 +170,8 @@ Environment-Based Configuration
162170
For suitable applications, the package's ability to configure itself from the
163171
environment eliminates the need to create or modify configuration files in many
164172
scenarios. The package automatically configures Redis Sentinel connections and
165-
the application cache, session, and queue services for these connections using
166-
environment variables.
173+
the application broadcasting, cache, session, and queue services for these
174+
connections using environment variables.
167175

168176
Developers may still configure the package [through standard Laravel
169177
configuration files][s-standard-config] when the application requirements
@@ -184,10 +192,11 @@ This sets up the *default* Redis Sentinel connection for the package's services
184192
that we can access through the [`RedisSentinel` facade][s-facade] (or by
185193
resolving `app('redis-sentinel')` from the container) like we would for
186194
Laravel's [standard Redis API][laravel-redis-api-docs]. To use this Sentinel
187-
connection for Laravel's cache, session, or queue services, change the
188-
following values as well:
195+
connection for Laravel's broadcasting, cache, session, or queue services,
196+
change the following values as well:
189197

190198
```shell
199+
BROADCAST_DRIVER=redis-sentinel
191200
CACHE_DRIVER=redis-sentinel
192201
SESSION_DRIVER=redis-sentinel
193202
QUEUE_DRIVER=redis-sentinel
@@ -196,10 +205,11 @@ QUEUE_DRIVER=redis-sentinel
196205
#### Connection-Specific Configuration
197206

198207
In many cases, we'd set different connection parameters for the application
199-
cache, session, and queue. We may configure different Redis databases for our
200-
cache and session (so that clearing the cache doesn't erase our user session
201-
information), and the Redis servers that contain the application queue may
202-
reside behind a different Sentinel service (master group name):
208+
broadcasts, cache, session, and queue. We may configure different Redis
209+
databases for our cache and session (so that clearing the cache doesn't erase
210+
our user session information), and the Redis servers that contain the
211+
application queue may reside behind a different Sentinel service (master group
212+
name):
203213

204214
```shell
205215
REDIS_CACHE_DATABASE=1
@@ -214,9 +224,9 @@ the value of any `*_HOST` variable to a comma-seperated string of hostnames or
214224
IP addresses:
215225

216226
```shell
217-
REDIS_HOST=sentinel1.example.com, sentinel2.example.com
227+
REDIS_HOST=sentinel1.example.com, sentinel2.example.com, ...
218228
REDIS_CACHE_HOST=10.0.0.1, 10.0.0.2, 10.0.0.3
219-
REDIS_QUEUE_HOST=tcp://10.0.0.4:26379, tcp://10.0.0.4:26380
229+
REDIS_QUEUE_HOST=tcp://10.0.0.4:26379, tcp://10.0.0.4:26380, ...
220230
```
221231

222232
Hosts share the port set for the connection unless we explicitly include the
@@ -378,15 +388,42 @@ for a single connection. The default values are shown below:
378388
],
379389
```
380390

381-
### Cache, Session, and Queue Drivers
391+
### Broadcasting, Cache, Session, and Queue Drivers
382392

383393
After configuring the Sentinel database connections, we can instruct Laravel to
384-
use these connections for the application's cache, session, and queue services.
394+
use these connections for the application's other redis-enabled services.
385395
Remember that we don't need to set Sentinel connections for all of these
386396
services. We could select a standard Redis connection for one and a Sentinel
387397
connection for another, if desired, but we likely want to take advantage of
388398
Sentinel for all of our Redis connections if it's available.
389399

400+
**Note:** We can omit (or remove) the following configuration blocks entirely
401+
and the package will configure these services for us. If we created custom
402+
Sentinel connections as above, we may need to [declare those connection
403+
names](#broadcast_redis_connection-broadcast_redis_sentinel_connection).
404+
405+
#### Broadcasting
406+
407+
Add the following connection definition to *config/broadcasting.php* in the
408+
`'connections'` array:
409+
410+
```php
411+
'connections' => [
412+
...
413+
'redis-sentinel' => [
414+
'driver' => 'redis-sentinel',
415+
'connection' => 'default',
416+
],
417+
],
418+
```
419+
420+
...and change the `BROADCAST_DRIVER` environment variable to `redis-sentinel`
421+
in *.env*.
422+
423+
If the application contains a specific connection in the `'redis-sentinel'`
424+
database configuration for the event broadcasting, replace `'default'` with its
425+
name.
426+
390427
#### Cache
391428

392429
Add the following store definition to *config/cache.php* in the `'stores'`
@@ -474,8 +511,8 @@ as needed. Lumen users may need to create this directory if it doesn't exist.
474511
A custom package config file need only contain the top-level elements that
475512
developer wishes to customize: in the code shown above, the custom config file
476513
only overrides the package's default configuration for Redis Sentinel
477-
connections, so the package will still automatically configure the cache,
478-
session, and queue services using environment variables.
514+
connections, so the package will still automatically configure the broadcasting,
515+
cache, session, and queue services using environment variables.
479516

480517
Hybrid Configuration
481518
--------------------
@@ -485,7 +522,7 @@ configuration methods provided by this package. For example, an application may
485522
contain a [standard][s-standard-config] or [package][s-package-config] config
486523
file that defines the Redis Sentinel connections, but rely on the package's
487524
automatic [environment-based configuration][s-env-config] to set up the cache,
488-
session, and queue services for Sentinel.
525+
session, queue, and broadcasting services for Sentinel.
489526

490527
The package uses configuration data in this order of precedence:
491528

@@ -548,7 +585,7 @@ local environments and switch to a full Sentinel set of servers in production.
548585
Executing Redis Commands (RedisSentinel Facade)
549586
-------------------------------------------------
550587

551-
If a we need to send Redis commands to Redis instances behind a Sentinel server
588+
If we need to send Redis commands to Redis instances behind a Sentinel server
552589
directly, such as we can through the `Redis` facade, but we don't want to
553590
[override Laravel's Redis API][s-override-redis-api] as above, we can use the
554591
`RedisSentinel` facade provided by this package or resolve the `redis-sentinel`
@@ -567,11 +604,12 @@ app('redis')->get('some-key');
567604
This provides support for uncommon use cases for which an application may need
568605
to connect to both standard Redis servers and Sentinel clusters in the same
569606
environment. When possible, follow the approach described in the previous
570-
section to uniformly connect to Sentinel throughout application to decouple the
571-
code from the Redis implementation.
607+
section to uniformly connect to Sentinel throughout the application to decouple
608+
the code from the Redis implementation.
572609

573-
To enable the facade in Laravel, add the following alias to the `'aliases'`
574-
array in *config/app.php*:
610+
The facade is not auto-aliased in Laravel 5.5+ for future compatibility with
611+
the PhpRedis extension. To enable the facade in Laravel, add the following
612+
alias to the `'aliases'` array in *config/app.php*:
575613

576614
```php
577615
'aliases' => [
@@ -684,71 +722,53 @@ how it handles connections to Sentinel servers.
684722
Set the value of this variable to `redis-sentinel` to [override Laravel's
685723
standard Redis API][s-override-redis-api].
686724

687-
### `CACHE_DRIVER`, `SESSION_DRIVER`, `QUEUE_DRIVER`
725+
### `BROADCAST_DRIVER`, `CACHE_DRIVER`, `SESSION_DRIVER`, `QUEUE_DRIVER`
688726

689-
Laravel uses these to select the backends for the application cache, session,
690-
and queue services. Set the value to `redis-sentinel` for each service that the
691-
application should use Sentinel connections for.
727+
Laravel uses these to select the backends for the application broadcasting,
728+
cache, session, and queue services. Set the value to `redis-sentinel` for each
729+
service that the application should use Sentinel connections for.
692730

693-
### `REDIS_{CACHE,SESSION,QUEUE}_{HOST,PORT,PASSWORD,DATABASE,SERVICE}`
731+
### `REDIS_{BROADCAST,CACHE,SESSION,QUEUE}_{HOST,PORT,PASSWORD,DATABASE,SERVICE}`
694732

695733
These variables configure service-specific connection parameters when they
696-
differ from the default Sentinel connection parameters for the cache, session,
697-
and queue connections.
734+
differ from the default Sentinel connection parameters for the broadcasting,
735+
cache, session, and queue connections. For example:
698736

699-
- `REDIS_CACHE_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for
700-
the default *cache* connection.
737+
- `REDIS_BROADCAST_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for
738+
the default *broadcasting* connection.
701739
- `REDIS_CACHE_PORT` - Overrides `REDIS_PORT` or `REDIS_SENTINEL_PORT` for
702740
the default *cache* connection.
703-
- `REDIS_CACHE_PASSWORD` - Overrides `REDIS_PASSWORD` or
704-
`REDIS_SENTINEL_PASSWORD` for the default *cache* connection.
705-
- `REDIS_CACHE_DATABASE` - Overrides `REDIS_DATABASE` or
706-
`REDIS_SENTINEL_DATABASE` for the default *cache* connection.
707-
- `REDIS_CACHE_SERVICE` - Overrides `REDIS_SENTINEL_SERVICE` for the default
708-
*cache* connection.
709-
710-
711-
- `REDIS_SESSION_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for
712-
the default *session* connection.
713-
- `REDIS_SESSION_PORT` - Overrides `REDIS_PORT` or `REDIS_SENTINEL_PORT` for
714-
the default *session* connection.
715741
- `REDIS_SESSION_PASSWORD` - Overrides `REDIS_PASSWORD` or
716742
`REDIS_SENTINEL_PASSWORD` for the default *session* connection.
717-
- `REDIS_SESSION_DATABASE` - Overrides `REDIS_DATABASE` or
718-
`REDIS_SENTINEL_DATABASE` for the default *session* connection.
719-
- `REDIS_SESSION_SERVICE` - Overrides `REDIS_SENTINEL_SERVICE` for the default
720-
*session* connection.
721-
722-
723-
- `REDIS_QUEUE_HOST` - Overrides `REDIS_HOST` or `REDIS_SENTINEL_HOST` for
724-
the default *queue* connection.
725-
- `REDIS_QUEUE_PORT` - Overrides `REDIS_PORT` or `REDIS_SENTINEL_PORT` for
726-
the default *queue* connection.
727-
- `REDIS_QUEUE_PASSWORD` - Overrides `REDIS_PASSWORD` or
728-
`REDIS_SENTINEL_PASSWORD` for the default *queue* connection.
729743
- `REDIS_QUEUE_DATABASE` - Overrides `REDIS_DATABASE` or
730744
`REDIS_SENTINEL_DATABASE` for the default *queue* connection.
731745
- `REDIS_QUEUE_SERVICE` - Overrides `REDIS_SENTINEL_SERVICE` for the default
732746
*queue* connection.
733747

748+
### `BROADCAST_REDIS_CONNECTION`, `BROADCAST_REDIS_SENTINEL_CONNECTION`
749+
750+
The name of the Sentinel connection to select for application broadcasting when
751+
`BROADCAST_DRIVER` equals `redis-sentinel`. It defaults to the package's
752+
internal, auto-configured *broadcasting* connection when unset.
734753

735754
### `CACHE_REDIS_CONNECTION`, `CACHE_REDIS_SENTINEL_CONNECTION`
736755

737-
The name of the Sentinel connection to set for the application cache when
756+
The name of the Sentinel connection to select for the application cache when
738757
`CACHE_DRIVER` equals `redis-sentinel`. It defaults to the package's internal,
739758
auto-configured *cache* connection when unset.
740759

741760
### `QUEUE_REDIS_CONNECTION`, `QUEUE_REDIS_SENTINEL_CONNECTION`
742761

743-
The name of the Sentinel connection to set for the application queue when
762+
The name of the Sentinel connection to select for the application queue when
744763
`QUEUE_DRIVER` equals `redis-sentinel`. It defaults to the package's internal,
745764
auto-configured *queue* connection when unset.
746765

747766
### `SESSION_CONNECTION`
748767

749-
The name of the Sentinel connection to set for storing application sessions
768+
The name of the Sentinel connection to select for storing application sessions
750769
when `SESSION_DRIVER` equals `redis-sentinel`. It defaults to the package's
751-
internal, auto-configured *session* connection when unset.
770+
internal, auto-configured *session* connection when unset unless the
771+
application configuration already contains a value for `session.connection`.
752772

753773
Appendix: Configuration Examples
754774
--------------------------------
@@ -943,6 +963,7 @@ Sentinel Documentation][sentinel].
943963
[s-standard-config-examples]: #configuration-file-examples
944964

945965
[laravel-env-docs]: https://laravel.com/docs/configuration#environment-configuration
966+
[laravel-package-discovery-docs]: https://laravel.com/docs/packages#package-discovery
946967
[laravel-redis-api-docs]: https://laravel.com/docs/redis#interacting-with-redis
947968
[laravel-redis-docs]: https://laravel.com/docs/redis
948969
[laravel]: https://laravel.com

Diff for: config/redis-sentinel.php

+41-4
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,11 @@
9595
| array that defines options for all connections.
9696
|
9797
| We can individually configure each of the application service connections
98-
| ("cache", "session", and "queue") with environment variables by setting
99-
| the variables named for each connection. If more than one connection
100-
| shares a common configuration value, we can instead set the environment
101-
| variable that applies to all of the Sentinel connections.
98+
| ("broadcasting", "cache", "session", and "queue") with environment
99+
| variables by setting the variables named for each connection. If more
100+
| than one connection shares a common configuration value, we can instead
101+
| set the environment variable that applies to all of the Sentinel
102+
| connections.
102103
|
103104
| For example, we may set the following configuration in ".env" for a setup
104105
| that uses the same Sentinel hosts for the application's cache and queue,
@@ -145,6 +146,20 @@
145146
],
146147
],
147148

149+
'broadcasting' => [
150+
[
151+
'host' => env('REDIS_BROADCAST_HOST', $host),
152+
'port' => env('REDIS_BROADCAST_PORT', $port),
153+
],
154+
'options' => [
155+
'service' => env('REDIS_BROADCAST_SERVICE', $service),
156+
'parameters' => [
157+
'password' => env('REDIS_BROADCAST_PASSWORD', $password),
158+
'database' => env('REDIS_BROADCAST_DATABASE', $database),
159+
],
160+
],
161+
],
162+
148163
'cache' => [
149164
[
150165
'host' => env('REDIS_CACHE_HOST', $host),
@@ -216,6 +231,28 @@
216231

217232
],
218233

234+
/*
235+
|--------------------------------------------------------------------------
236+
| Redis Sentinel Broadcasting Connection
237+
|--------------------------------------------------------------------------
238+
|
239+
| Defines the broadcasting connection that uses a Redis Sentinel connection
240+
| for the application event broadcasting services.
241+
|
242+
*/
243+
244+
'broadcasting' => [
245+
'connections' => [
246+
'redis-sentinel' => [
247+
'driver' => 'redis-sentinel',
248+
'connection' => env(
249+
'BROADCAST_REDIS_SENTINEL_CONNECTION',
250+
env('BROADCAST_REDIS_CONNECTION', 'broadcasting')
251+
),
252+
],
253+
],
254+
],
255+
219256
/*
220257
|--------------------------------------------------------------------------
221258
| Redis Sentinel Cache Store

0 commit comments

Comments
 (0)