@@ -6,7 +6,7 @@ Laravel Drivers for Redis Sentinel
6
6
7
7
#### Redis Sentinel integration for Laravel and Lumen.
8
8
9
- [ Redis Sentinel] [ sentinel ] provides high-availability, monitoring, and
9
+ [ Redis Sentinel] [ sentinel ] facilitates high-availability, monitoring, and
10
10
load-balancing for Redis servers configured for master-slave replication.
11
11
[ Laravel] [ laravel ] includes built-in support for Redis, but we cannot configure
12
12
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
18
18
Sentinel. This causes issues when we need to clear the cache, because Laravel
19
19
erases stored session information as well.
20
20
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.
28
28
29
29
Contents
30
30
--------
@@ -78,13 +78,21 @@ composer require monospice/laravel-redis-sentinel-drivers
78
78
composer require monospice/laravel-redis-sentinel-drivers:^1.0
79
79
```
80
80
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
+
81
86
If the project does not already use Redis with Laravel, this will install the
82
87
[ Predis] [ predis ] package as well.
83
88
84
89
#### Register the Service Provider
85
90
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* :
88
96
89
97
``` php
90
98
'providers' => [
@@ -162,8 +170,8 @@ Environment-Based Configuration
162
170
For suitable applications, the package's ability to configure itself from the
163
171
environment eliminates the need to create or modify configuration files in many
164
172
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.
167
175
168
176
Developers may still configure the package [ through standard Laravel
169
177
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
184
192
that we can access through the [ ` RedisSentinel ` facade] [ s-facade ] (or by
185
193
resolving ` app('redis-sentinel') ` from the container) like we would for
186
194
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:
189
197
190
198
``` shell
199
+ BROADCAST_DRIVER=redis-sentinel
191
200
CACHE_DRIVER=redis-sentinel
192
201
SESSION_DRIVER=redis-sentinel
193
202
QUEUE_DRIVER=redis-sentinel
@@ -196,10 +205,11 @@ QUEUE_DRIVER=redis-sentinel
196
205
#### Connection-Specific Configuration
197
206
198
207
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):
203
213
204
214
``` shell
205
215
REDIS_CACHE_DATABASE=1
@@ -214,9 +224,9 @@ the value of any `*_HOST` variable to a comma-seperated string of hostnames or
214
224
IP addresses:
215
225
216
226
``` shell
217
- REDIS_HOST=sentinel1.example.com, sentinel2.example.com
227
+ REDIS_HOST=sentinel1.example.com, sentinel2.example.com, ...
218
228
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, ...
220
230
```
221
231
222
232
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:
378
388
],
379
389
```
380
390
381
- ### Cache, Session, and Queue Drivers
391
+ ### Broadcasting, Cache, Session, and Queue Drivers
382
392
383
393
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.
385
395
Remember that we don't need to set Sentinel connections for all of these
386
396
services. We could select a standard Redis connection for one and a Sentinel
387
397
connection for another, if desired, but we likely want to take advantage of
388
398
Sentinel for all of our Redis connections if it's available.
389
399
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
+
390
427
#### Cache
391
428
392
429
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.
474
511
A custom package config file need only contain the top-level elements that
475
512
developer wishes to customize: in the code shown above, the custom config file
476
513
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.
479
516
480
517
Hybrid Configuration
481
518
--------------------
@@ -485,7 +522,7 @@ configuration methods provided by this package. For example, an application may
485
522
contain a [ standard] [ s-standard-config ] or [ package] [ s-package-config ] config
486
523
file that defines the Redis Sentinel connections, but rely on the package's
487
524
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.
489
526
490
527
The package uses configuration data in this order of precedence:
491
528
@@ -548,7 +585,7 @@ local environments and switch to a full Sentinel set of servers in production.
548
585
Executing Redis Commands (RedisSentinel Facade)
549
586
-------------------------------------------------
550
587
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
552
589
directly, such as we can through the ` Redis ` facade, but we don't want to
553
590
[ override Laravel's Redis API] [ s-override-redis-api ] as above, we can use the
554
591
` RedisSentinel ` facade provided by this package or resolve the ` redis-sentinel `
@@ -567,11 +604,12 @@ app('redis')->get('some-key');
567
604
This provides support for uncommon use cases for which an application may need
568
605
to connect to both standard Redis servers and Sentinel clusters in the same
569
606
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.
572
609
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* :
575
613
576
614
``` php
577
615
'aliases' => [
@@ -684,71 +722,53 @@ how it handles connections to Sentinel servers.
684
722
Set the value of this variable to ` redis-sentinel ` to [ override Laravel's
685
723
standard Redis API] [ s-override-redis-api ] .
686
724
687
- ### ` CACHE_DRIVER ` , ` SESSION_DRIVER ` , ` QUEUE_DRIVER `
725
+ ### ` BROADCAST_DRIVER ` , ` CACHE_DRIVER ` , ` SESSION_DRIVER ` , ` QUEUE_DRIVER `
688
726
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.
692
730
693
- ### ` REDIS_{CACHE,SESSION,QUEUE}_{HOST,PORT,PASSWORD,DATABASE,SERVICE} `
731
+ ### ` REDIS_{BROADCAST, CACHE,SESSION,QUEUE}_{HOST,PORT,PASSWORD,DATABASE,SERVICE} `
694
732
695
733
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:
698
736
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.
701
739
- ` REDIS_CACHE_PORT ` - Overrides ` REDIS_PORT ` or ` REDIS_SENTINEL_PORT ` for
702
740
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.
715
741
- ` REDIS_SESSION_PASSWORD ` - Overrides ` REDIS_PASSWORD ` or
716
742
` 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.
729
743
- ` REDIS_QUEUE_DATABASE ` - Overrides ` REDIS_DATABASE ` or
730
744
` REDIS_SENTINEL_DATABASE ` for the default * queue* connection.
731
745
- ` REDIS_QUEUE_SERVICE ` - Overrides ` REDIS_SENTINEL_SERVICE ` for the default
732
746
* queue* connection.
733
747
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.
734
753
735
754
### ` CACHE_REDIS_CONNECTION ` , ` CACHE_REDIS_SENTINEL_CONNECTION `
736
755
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
738
757
` CACHE_DRIVER ` equals ` redis-sentinel ` . It defaults to the package's internal,
739
758
auto-configured * cache* connection when unset.
740
759
741
760
### ` QUEUE_REDIS_CONNECTION ` , ` QUEUE_REDIS_SENTINEL_CONNECTION `
742
761
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
744
763
` QUEUE_DRIVER ` equals ` redis-sentinel ` . It defaults to the package's internal,
745
764
auto-configured * queue* connection when unset.
746
765
747
766
### ` SESSION_CONNECTION `
748
767
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
750
769
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 ` .
752
772
753
773
Appendix: Configuration Examples
754
774
--------------------------------
@@ -943,6 +963,7 @@ Sentinel Documentation][sentinel].
943
963
[ s-standard-config-examples ] : #configuration-file-examples
944
964
945
965
[ laravel-env-docs ] : https://laravel.com/docs/configuration#environment-configuration
966
+ [ laravel-package-discovery-docs ] : https://laravel.com/docs/packages#package-discovery
946
967
[ laravel-redis-api-docs ] : https://laravel.com/docs/redis#interacting-with-redis
947
968
[ laravel-redis-docs ] : https://laravel.com/docs/redis
948
969
[ laravel ] : https://laravel.com
0 commit comments