Skip to content

Commit f4b6918

Browse files
committed
Add ability to configure package using environment variables
Signed-off-by: Cy Rossignol <[email protected]>
1 parent 70dbe1d commit f4b6918

File tree

8 files changed

+2071
-307
lines changed

8 files changed

+2071
-307
lines changed

README.md

Lines changed: 725 additions & 264 deletions
Large diffs are not rendered by default.

config/redis-sentinel.php

Lines changed: 283 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,283 @@
1+
<?php
2+
3+
/*
4+
|------------------------------------------------------------------------------
5+
| Laravel Redis Sentinel Drivers Package Configuration
6+
|------------------------------------------------------------------------------
7+
|
8+
| This file describes the configuration used by the Redis Sentinel services
9+
| provided by this package. The default configuration structure below enables
10+
| simple, environment-based configuration, especially for Lumen applications,
11+
| without the need to create or modify application configuration files.
12+
|
13+
| Each of the "redis-sentinel" keys in this configuration will merge into the
14+
| main application configuration hierarchy at the same path. Please note that
15+
| this merge is not recursive--if the key already exists, the configuration
16+
| in memory takes precedence over this configuration file when the package
17+
| boots. This design allows developers to override the default configuration
18+
| when needed.
19+
|
20+
| For advanced configuration that exceeds the capacity of this configuration
21+
| file, developers may add the "redis-sentinel" configuration blocks to each
22+
| of the application configuration files with the same name as the top-level
23+
| key. Lumen users may copy this configuration file to the "config/" directory
24+
| in the project root directory (which may need to be created) to avoid adding
25+
| several application configuration files just to configure this package.
26+
| Because the package merges these settings into the application's main
27+
| configuration, it does not publish the "redis-sentinel.php" config file
28+
| automatically.
29+
|
30+
| For more configuration information, please see the package's README:
31+
|
32+
| https://github.com/monospice/laravel-redis-sentinel-drivers
33+
|
34+
*/
35+
36+
$host = env('REDIS_SENTINEL_HOST', env('REDIS_HOST', 'localhost'));
37+
$port = env('REDIS_SENTINEL_PORT', env('REDIS_PORT', 26379));
38+
$password = env('REDIS_SENTINEL_PASSWORD', env('REDIS_PASSWORD', null));
39+
$database = env('REDIS_SENTINEL_DATABASE', env('REDIS_DATABASE', 0));
40+
$service = env('REDIS_SENTINEL_SERVICE', 'mymaster');
41+
42+
return [
43+
44+
/*
45+
|--------------------------------------------------------------------------
46+
| Automatic Configuration Controls
47+
|--------------------------------------------------------------------------
48+
|
49+
| These values enable developers to control how the package loads and
50+
| manages its configuration.
51+
|
52+
| By default, this package attempts to load its configuration from the
53+
| environment and automatically merge it into the config entries for the
54+
| corresponding application components to relieve developers of the need
55+
| to set up config blocks in multiple configuration files. For advanced
56+
| configuration, such as when an application provides its own config files
57+
| for the package, developers can disable this behavior by setting the
58+
| value of "load_config" to FALSE so the package skips auto-configuration.
59+
|
60+
| After loading its configuration and merging the values for the other
61+
| components, the package no longer requires its own configuration, so it
62+
| removes entries under the "redis-sentinel" top-level config key. This
63+
| prevents the artisan "config:cache" command from saving the unnecessary
64+
| values to the cache file. If the application uses this configuration
65+
| key for other purposes, set the value of "clean_config" to FALSE.
66+
|
67+
*/
68+
69+
'load_config' => true,
70+
71+
'clean_config' => true,
72+
73+
/*
74+
|--------------------------------------------------------------------------
75+
| Redis Sentinel Database Driver
76+
|--------------------------------------------------------------------------
77+
|
78+
| The following block configures the Redis Sentinel connections for the
79+
| application.
80+
|
81+
| Each of the connections below contains one or more host definitions for
82+
| the Sentinel servers in the quorum. Each host definition is wrapped in
83+
| an unnamed array that contains the host's IP address or hostname and the
84+
| port number. To specify multiple Sentinel hosts for a connection, add a
85+
| sub-array to the connection's array with the address and port for each
86+
| Sentinel server. Configurations that place multiple Sentinel servers
87+
| behind one aggregate hostname, such as "sentinels.example.com", should
88+
| contain only one host definition per connection.
89+
|
90+
| The main "redis-sentinel" driver configuration array and each of the
91+
| connection arrays within may contain an "options" element that provides
92+
| additional configuration settings for the connections, such as the
93+
| password for the Redis servers behind Sentinel, if needed. Any options
94+
| specified for a connection override the options in the global "options"
95+
| array that defines options for all connections.
96+
|
97+
| 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.
102+
|
103+
| For example, we may set the following configuration in ".env" for a setup
104+
| that uses the same Sentinel hosts for the application's cache and queue,
105+
| but a different Redis database for each connection:
106+
|
107+
| REDIS_HOST=sentinels.example.com
108+
| REDIS_CACHE_DATABASE=1
109+
| REDIS_QUEUE_DATABASE=2
110+
|
111+
| Developers need only supply environment configuration variables for the
112+
| Sentinel connections used by the application.
113+
|
114+
| To simplify environment configuration, this script attempts to read both
115+
| the "REDIS_SENTINEL_*" and the "REDIS_*" environment variables that
116+
| specify values shared by multiple Sentinel connections. If an application
117+
| does not require both Redis Sentinel and standard Redis connections at
118+
| the same time, this feature allows developers to use the same environment
119+
| variale names in development (with a single Redis server) and production
120+
| (with a full set of Sentinel servers). These variables are:
121+
|
122+
| REDIS_SENTINEL_HOST (REDIS_HOST)
123+
| REDIS_SENTINEL_PORT (REDIS_PORT)
124+
| REDIS_SENTINEL_PASSWORD (REDIS_PASSWORD)
125+
| REDIS_SENTINEL_DATABASE (REDIS_DATABASE)
126+
|
127+
| The package supports environment-based configuration for connections with
128+
| multiple hosts by allowing a comma-seperated string of hosts in each of
129+
| the "*_HOST" environment variables. For example:
130+
|
131+
| REDIS_HOST=sentinel1.example.com, sentinel2.example.com
132+
| REDIS_CACHE_HOST=10.0.0.1, 10.0.0.2, 10.0.0.3
133+
| REDIS_QUEUE_HOST=tcp://10.0.0.3:26379, tcp://10.0.0.3:26380
134+
|
135+
*/
136+
137+
'database' => [
138+
139+
'redis-sentinel' => [
140+
141+
'default' => [
142+
[
143+
'host' => $host,
144+
'port' => $port,
145+
],
146+
],
147+
148+
'cache' => [
149+
[
150+
'host' => env('REDIS_CACHE_HOST', $host),
151+
'port' => env('REDIS_CACHE_PORT', $port),
152+
],
153+
'options' => [
154+
'service' => env('REDIS_CACHE_SERVICE', $service),
155+
'parameters' => [
156+
'password' => env('REDIS_CACHE_PASSWORD', $password),
157+
'database' => env('REDIS_CACHE_DATABASE', $database),
158+
],
159+
],
160+
],
161+
162+
'session' => [
163+
[
164+
'host' => env('REDIS_SESSION_HOST', $host),
165+
'port' => env('REDIS_SESSION_PORT', $port),
166+
],
167+
'options' => [
168+
'service' => env('REDIS_SESSION_SERVICE', $service),
169+
'parameters' => [
170+
'password' => env('REDIS_SESSION_PASSWORD', $password),
171+
'database' => env('REDIS_SESSION_DATABASE', $database),
172+
],
173+
],
174+
],
175+
176+
'queue' => [
177+
[
178+
'host' => env('REDIS_QUEUE_HOST', $host),
179+
'port' => env('REDIS_QUEUE_PORT', $port),
180+
],
181+
'options' => [
182+
'service' => env('REDIS_QUEUE_SERVICE', $service),
183+
'parameters' => [
184+
'password' => env('REDIS_QUEUE_PASSWORD', $password),
185+
'database' => env('REDIS_QUEUE_DATABASE', $database),
186+
],
187+
],
188+
],
189+
190+
// These options apply to all Redis Sentinel connections unless a
191+
// connection supplies a local options array that overrides the
192+
// values here:
193+
'options' => [
194+
'service' => $service,
195+
196+
'parameters' => [
197+
'password' => $password,
198+
'database' => $database,
199+
],
200+
201+
'sentinel_timeout' => env('REDIS_SENTINEL_TIMEOUT', 0.100),
202+
'retry_limit' => env('REDIS_SENTINEL_RETRY_LIMIT', 20),
203+
'retry_wait' => env('REDIS_SENTINEL_RETRY_WAIT', 1000),
204+
'update_sentinels' => env('REDIS_SENTINEL_DISCOVERY', false),
205+
],
206+
207+
],
208+
209+
// Set the value of "REDIS_DRIVER" to "redis-sentinel" to override
210+
// Laravel's standard Redis API ("Redis" facade and "redis" service
211+
// binding) so that these use the Redis Sentinel connections instead
212+
// of the Redis connections.
213+
'redis' => [
214+
'driver' => env('REDIS_DRIVER', 'default'),
215+
],
216+
217+
],
218+
219+
/*
220+
|--------------------------------------------------------------------------
221+
| Redis Sentinel Cache Store
222+
|--------------------------------------------------------------------------
223+
|
224+
| Defines the cache store that uses a Redis Sentinel connection for the
225+
| application cache.
226+
|
227+
*/
228+
229+
'cache' => [
230+
'stores' => [
231+
'redis-sentinel' => [
232+
'driver' => 'redis-sentinel',
233+
'connection' => env(
234+
'CACHE_REDIS_SENTINEL_CONNECTION',
235+
env('CACHE_REDIS_CONNECTION', 'cache')
236+
),
237+
],
238+
],
239+
],
240+
241+
/*
242+
|--------------------------------------------------------------------------
243+
| Redis Sentinel Session Connection
244+
|--------------------------------------------------------------------------
245+
|
246+
| Defines the Redis Sentinel connection used store and retrieve sessions
247+
| when "SESSION_DRIVER" ("session.driver") is set to "redis-sentinel".
248+
|
249+
| The package only uses this value if the application supports sessions
250+
| (Lumen applications > 5.2 typically don't).
251+
|
252+
*/
253+
254+
'session' => [
255+
'connection' => env('SESSION_CONNECTION', 'session'),
256+
],
257+
258+
/*
259+
|--------------------------------------------------------------------------
260+
| Redis Sentinel Queue Connector
261+
|--------------------------------------------------------------------------
262+
|
263+
| Defines the queue connector that uses a Redis Sentinel connection for the
264+
| application queue.
265+
|
266+
*/
267+
268+
'queue' => [
269+
'connections' => [
270+
'redis-sentinel' => [
271+
'driver' => 'redis-sentinel',
272+
'connection' => env(
273+
'QUEUE_REDIS_SENTINEL_CONNECTION',
274+
env('QUEUE_REDIS_CONNECTION', 'queue')
275+
),
276+
'queue' => 'default',
277+
'retry_after' => 90,
278+
'expire' => 90, // Legacy, Laravel < 5.4.30
279+
],
280+
],
281+
],
282+
283+
];

0 commit comments

Comments
 (0)