ezdeliveryco/laravel-database-redundancy

This package is abandoned and no longer maintained. No replacement package was suggested.

A lightweight middleware to enforce database redundancy in Laravel and Lumen.

dev-master / 1.0.x-dev 2017-05-24 22:58 UTC

This package is not auto-updated.

Last update: 2019-04-29 13:28:29 UTC


README

A lightweight middleware to enforce database redundancy in Laravel and Lumen.

Ideology

This package is part of the result of our work to increase our application disponibility.

How it works?

We read from a new settings area of database.php two things: host and port. WIth them, we do a random list with those settings. Next, we try to connect each by each.

The result is the support for more than one database settings and fallback in case of failure.

How to install?

Download dependency

Just run composer require ezdeliveryco/DatabaseRedundancyMiddleware

Setup new settings

Inside your database.php settings, you need add the target connection settings.

    /*
    |--------------------------------------------------------------------------
    | Connection rendundancy plan
    |--------------------------------------------------------------------------
    |
    | This settings make possible the smart usage of more than one connection
    |
    */

    'redundancies' => [

        'connection_1' => [
            'host' => env('DB_HOST_1'),
            'port' => env('DB_PORT_1'),
        ],

        'connection_2' => [
            'host' => env('DB_HOST_2'),
            'port' => env('DB_PORT_2'),
        ],

    ],

TIP: You need to repeat the main connection bacause the package replace them anyway.

TIP: We only use host and port but you use anything, since we use a merge.

Enable de middleware

Laravel 5.1+

Http Middleware

To enable the middleware you need to change your app/Http/Kernel.php file. Basically you need to do this:

use DatabaseRedundancyMiddleware\Laravel\HttpMiddleware;

class Kernel extends HttpKernel
{
    protected $middleware = [
        HttpMiddleware::class,
    ];
}
Artisan Implementation

To enable the middleware you need to hack your app/Console/Kernel.php file. Basically you need to do this:

use DatabaseRedundancyMiddleware\Laravel\ArtisanBoostrap;

class Kernel extends ConsoleKernel
{
    /**
     * Hack to make possibe the redundancy in Artisan
     *
     * @return void
     */
    public function bootstrap()
    {
        // We still want to boostrap the default stuff
        parent::bootstrap();

        // Boostrap database redundancy
        $redundancyEngine = new ArtisanBoostrap();
        $redundancyEngine->handle();
    }
}

Lumen 5.2+

Http Middleware

To enable the middleware you need to change your bootstrap/app.php file. Basically you need to do this:

$app->middleware([
    DatabaseRedundancyMiddleware\Lumen\HttpMiddleware::class,
]);
Artisan Implementation

To enable the middleware you need to hack your app/Console/Kernel.php file. Basically you need to do this:

use DatabaseRedundancyMiddleware\Laravel\ArtisanBoostrap;
use Illuminate\Console\Scheduling\Schedule;

class Kernel extends ConsoleKernel
{
    /**
     * Hack to make possibe the redundancy in Artisan
     *
     * @return void
     */
    public function __construct(Application $app)
    {
        // We still want to boostrap the default stuff
        parent::__construct($app);

        // Boostrap database redundancy
        $redundancyEngine = new ArtisanBoostrap();
        $redundancyEngine->handle();
    }
}