cmobi/microservice-framework-bundle
Installation Add the bundle via Composer:
composer require cmobi/microservice-framework-bundle
Register the bundle in config/bundles.php:
return [
// ...
ContaMobi\MicroserviceFrameworkBundle\ContaMobiMicroserviceFrameworkBundle::class => ['all' => true],
];
Configuration Publish the default config:
php bin/console cmobi:microservice:init
Edit config/packages/cmobi_microservice_framework.yaml to define:
First Use Case: Service Registration Annotate a Symfony service as a microservice:
use ContaMobi\MicroserviceFrameworkBundle\Annotation\Microservice;
#[Microservice(name: 'user-service', port: 8000)]
class UserService {}
Register the service with the framework:
php bin/console cmobi:microservice:register
Testing Locally
Use the built-in ProxyClient to test inter-service calls:
$client = $container->get('cmobi.microservice.proxy_client');
$response = $client->call('user-service', 'GET', '/api/users/1');
Dynamic Routing
Configure API gateway rules in yaml to route requests to specific services:
cmobi_microservice_framework:
api_gateway:
routes:
'/users': 'user-service'
'/orders': 'order-service'
Use the GatewayClient to forward requests:
$gateway = $container->get('cmobi.microservice.gateway_client');
$response = $gateway->forward('/users', 'GET');
Inter-Service Calls
Inject the ProxyClient into controllers/services:
public function __construct(private ProxyClient $proxyClient) {}
public function fetchUserData(int $id) {
return $this->proxyClient->call('user-service', 'GET', "/users/{$id}");
}
Event-Driven Workflows Publish/subscribe to events across services:
// In Service A:
$eventDispatcher = $container->get('event_dispatcher');
$eventDispatcher->dispatch(new UserCreatedEvent($user));
// In Service B (listening via Symfony Messenger or custom subscriber):
#[Asynchronous]
public function handleUserCreated(UserCreatedEvent $event) {
// Process event...
}
Circuit Breaker Pattern
Enable resilience with the CircuitBreaker decorator:
$client = new CircuitBreaker($proxyClient, 3, 1000); // 3 failures, 1s timeout
$response = $client->call('failing-service', 'GET', '/health');
Service Health Checks Implement a health check endpoint in each service:
#[Route('/health', name: 'health_check', methods: ['GET'])]
public function healthCheck(): JsonResponse {
return new JsonResponse(['status' => 'healthy']);
}
Register the endpoint in the bundle config:
cmobi_microservice_framework:
health_checks:
- 'user-service:8000/health'
- 'order-service:8001/health'
Configuration Management
Use environment-specific configs (e.g., config/packages/dev/cmobi_microservice_framework.yaml) to switch between:
Service Discovery Lag
php bin/console cmobi:microservice:register --force or implement a retry mechanism in ProxyClient.Circular Dependencies
Configuration Overrides
MICROSERVICE_DISCOVERY_URL).Annotation Caching
@Microservice annotations require cache clearing.php bin/console cache:clear after modifying annotated classes.Enable Verbose Logging
Configure monolog in config/packages/monolog.yaml:
handlers:
cmobi_microservice:
type: stream
path: "%kernel.logs_dir%/%kernel.environment%.cmobi.log"
level: debug
Inspect Service Registry Dump the registered services:
php bin/console debug:container | grep cmobi.microservice
Mock External Services
Use the MockProxyClient for testing:
$mockClient = new MockProxyClient();
$mockClient->shouldReceive('call')
->with('user-service', 'GET', '/users/1')
->andReturn(['id' => 1, 'name' => 'Test User']);
Custom Discovery Providers
Extend DiscoveryProviderInterface to support new backends (e.g., Kubernetes):
class KubernetesDiscoveryProvider implements DiscoveryProviderInterface {
public function getServices(): array {
// Fetch from Kubernetes API
}
}
Register in config:
cmobi_microservice_framework:
discovery_provider: ContaMobi\MicroserviceFrameworkBundle\Discovery\KubernetesDiscoveryProvider
Protocol Plugins
Add support for gRPC or WebSockets by implementing ProtocolHandlerInterface:
class GrpcProtocolHandler implements ProtocolHandlerInterface {
public function handle(string $service, string $method, array $data): mixed {
// gRPC logic
}
}
Custom Metadata
Extend the @Microservice annotation to include additional metadata (e.g., priority, retries):
#[Microservice(
name: 'user-service',
port: 8000,
retries: 2,
priority: 1
)]
ReactPHP) for long-running operations.cmobi_microservice_framework:
bulkheads:
'user-service': 10 # Max 10 concurrent calls
How can I help you explore Laravel packages today?