- How do I integrate sensiolabs/consul-php-sdk into a Laravel 9+ project?
- Install via Composer: `composer require sensiolabs/consul-php-sdk`. Bind the client in `config/app.php` under `bindings` or use a service provider to register it as a singleton. Configure the Consul agent URI and ACL token in `config/consul.php`. For Laravel 10, ensure PHP 8.1+ compatibility is met.
- Can I use this SDK to replace Laravel’s .env file with Consul KV for configuration?
- Yes. Fetch configs dynamically using `consul()->kv->get('path/to/config')`. Override Laravel’s `config()` method or use a facade to merge Consul KV with cached `.env` values. Cache responses with `Cache::remember()` to reduce latency. Example: `config('app.debug', consul()->kv->get('app/debug') ?? false).`
- What Laravel versions and PHP versions does sensiolabs/consul-php-sdk support?
- The SDK supports PHP 8.1+ and works with Laravel 9+. For Laravel 10, no breaking changes are expected, but test thoroughly. PHP 8.0 may work with minor polyfills, but official support ends at 8.1. Always check the latest release notes for version-specific quirks.
- How do I handle service discovery for Laravel queues (e.g., Horizon) using Consul?
- Register your queue workers as services in Consul via the agent’s `check register` command. Use the SDK to dynamically resolve worker URLs: `consul()->catalog->service('queue-worker')->getNodes()`. In Horizon, override the `connection()` method to fetch the Consul-backed queue URL at runtime.
- What’s the best way to test Laravel apps using this SDK in CI/CD?
- Mock the Consul client in PHPUnit using `Mockery` or Laravel’s `MockFacade`. Stub responses for `getService()`, `kv->get()`, and health checks. Use a local Consul dev cluster (via Docker) for integration tests, but avoid hitting production Consul in CI. Example: `Consul::shouldReceive('getService')->andReturn(['ServiceAddress' => 'localhost']);`
- How do I ensure high availability if Consul goes down in production?
- Implement a fallback mechanism: cache service lists locally with `Cache::remember()` and set a TTL (e.g., 30 seconds). Use Laravel’s `config('consul.fallback')` to default to `.env` or static configs. Monitor Consul health via `consul()->agent->self()` and trigger alerts if unavailable. For critical systems, run a local Consul agent in passive mode.
- Are there Laravel-specific packages that wrap this SDK for easier use?
- No official Laravel wrapper exists, but you can create one. Build a facade (e.g., `ConsulService::getService()`) or a Laravel package with service providers, config publishers, and artisan commands. Example: `php artisan consul:register` to auto-register services. Check Packagist for community alternatives like `spatie/laravel-consul`.
- How do I secure Consul communication from my Laravel app?
- Enable mTLS for Consul communication by configuring the SDK with `Consul::create(['verify_peer' => true, 'ca_file' => '/path/to/ca.crt'])` in Laravel’s `AppServiceProvider`. Restrict your Laravel app’s Consul ACLs to read-only or minimal write permissions. Use environment variables for tokens: `CONSUL_HTTP_TOKEN`. Rotate tokens regularly.
- Can I use this SDK for dynamic Laravel route resolution (e.g., API gateways)?
- Yes. Fetch service endpoints dynamically in Laravel’s `routes/api.php` using `consul()->catalog->service('api-gateway')->getNodes()`. Override the `RouteServiceProvider` to resolve routes at runtime. Cache resolved routes to avoid repeated Consul calls. Example: `Route::get('/dynamic', function() { return consul()->getService('dynamic-service'); });`
- What are the performance implications of frequent Consul API calls in Laravel?
- Frequent calls add latency (typically 50–200ms per request). Mitigate this by caching responses (e.g., `Cache::remember('services', 10, fn() => consul()->catalog->services())`). Use Consul’s watch API for real-time updates instead of polling. For high-throughput apps, consider a local Consul proxy or sidecar container to reduce network hops.