Installation Add the package via Composer:
composer require bricre/micro-service-common
Publish the package configuration (if applicable):
php artisan vendor:publish --provider="Bricre\MicroServiceCommon\MicroServiceCommonServiceProvider"
First Use Case: API Client If the package includes HTTP client utilities (common in micro-service toolkits), initialize a client in a service:
use Bricre\MicroServiceCommon\Clients\ApiClient;
$client = new ApiClient(config('services.my_microservice.url'));
$response = $client->get('/endpoint');
First Use Case: Logging
If logging utilities are included, configure in config/logging.php:
'channels' => [
'microservice' => [
'driver' => 'single',
'path' => storage_path('logs/microservice.log'),
'level' => 'debug',
],
],
Use in a controller:
use Bricre\MicroServiceCommon\Support\Logger;
Logger::debug('User action', ['user_id' => 123]);
First Use Case: Request Validation If validation helpers exist, extend a request:
use Bricre\MicroServiceCommon\Http\Requests\MicroServiceFormRequest;
class CreateUserRequest extends MicroServiceFormRequest
{
public function rules()
{
return [
'name' => 'required|string|max:255',
'email' => 'required|email',
];
}
}
Service-to-Service Communication
ApiClient for internal service calls.// In a service class
public function fetchUserData($userId)
{
$client = app(ApiClient::class);
$response = $client->get("/users/{$userId}");
return json_decode($response->getBody(), true);
}
Request/Response Transformation
// app/Http/Middleware/TransformResponse.php
public function handle($request, Closure $next)
{
$response = $next($request);
return $response->json([
'success' => true,
'data' => $response->getData(),
'timestamp' => now()->toIso8601String(),
]);
}
Event-Driven Architecture
use Bricre\MicroServiceCommon\Events\UserCreated;
event(new UserCreated($user));
public function handle(UserCreated $event)
{
// Notify other services via API or queue
}
Configuration Management
config/services.php.'microservices' => [
'auth' => [
'url' => env('AUTH_SERVICE_URL'),
'token' => env('AUTH_SERVICE_TOKEN'),
],
'inventory' => [
'url' => env('INVENTORY_SERVICE_URL'),
],
],
Laravel Service Container: Bind interfaces to implementations in AppServiceProvider:
$this->app->bind(
Bricre\MicroServiceCommon\Contracts\LoggerInterface::class,
Bricre\MicroServiceCommon\Support\Logger::class
);
Queue Jobs: Use the package's queue helpers for async operations:
use Bricre\MicroServiceCommon\Jobs\ProcessOrder;
ProcessOrder::dispatch($order)->onQueue('microservices');
Testing: Mock the ApiClient in unit tests:
$mock = Mockery::mock(ApiClient::class);
$mock->shouldReceive('get')->once()->andReturn(new Response(200, [], '{}'));
$this->app->instance(ApiClient::class, $mock);
Configuration Overrides
config(['key' => 'value']) to override specific settings after publishing.HTTP Client Timeouts
config/micro-service-common.php:
'http' => [
'timeout' => 30, // seconds
'connect_timeout' => 5,
],
Circular Dependencies
Logging Overhead
Logger::setLevel(env('APP_ENV') === 'production' ? 'info' : 'debug');
Rate Limiting
use Bricre\MicroServiceCommon\Exceptions\RateLimitExceeded;
try {
$response = $client->get('/endpoint');
} catch (RateLimitExceeded $e) {
sleep($e->getRetryAfter());
retry();
}
Enable Verbose Logging
php artisan config:set micro-service-common.log.level=debug
Inspect HTTP Requests Use Laravel's built-in HTTP client logging:
$client->withOptions([
'debug' => fopen(storage_path('logs/http_client.log'), 'w'),
]);
Check for Deprecated Methods Run:
php artisan vendor:analyze --provider="Bricre\MicroServiceCommon\MicroServiceCommonServiceProvider"
Custom Middleware
Extend the ApiClient with middleware:
$client->withMiddleware([
new AddAuthHeader(),
new LogRequest(),
]);
Event Listeners
Subscribe to package events in EventServiceProvider:
protected $listen = [
'Bricre\MicroServiceCommon\Events\ServiceDown' => [
'App\Listeners\NotifyDevOps',
],
];
Custom Validators
Extend the base MicroServiceFormRequest:
class CustomRequest extends MicroServiceFormRequest
{
public function authorize()
{
return auth()->check() && auth()->user()->isAdmin();
}
}
Service Discovery Dynamically resolve service URLs via DNS or consul:
$serviceUrl = app('service.discovery')->get('auth-service');
Testing Utilities Use the package's test helpers:
use Bricre\MicroServiceCommon\Testing\TestApiClient;
public function testServiceCall()
{
$client = new TestApiClient();
$client->shouldReceive('get')->andReturn(['data' => 'test']);
}
How can I help you explore Laravel packages today?