4.2 KiB
4.2 KiB
Story 3.5: Service Registry and Discovery
Metadata
- Story ID: 3.5
- Title: Service Registry and Discovery
- Epic: 3 - Module Framework
- Status: Pending
- Priority: High
- Estimated Time: 5-6 hours
- Dependencies: 1.7, 3.1
Goal
Implement a service registry that enables service discovery for microservices, allowing services to locate and communicate with each other.
Description
This story creates a service registry system supporting Consul and Kubernetes service discovery. The registry enables service discovery, health checking, and automatic service registration.
Deliverables
1. Service Registry Interface (pkg/services/registry.go)
ServiceRegistryinterface with:Register(service ServiceInfo) error- Register a serviceDeregister(serviceID string) error- Deregister a serviceDiscover(serviceName string) ([]ServiceInfo, error)- Discover servicesGetService(serviceName string) (ServiceInfo, error)- Get specific serviceListServices() ([]ServiceInfo, error)- List all servicesHealthCheck(serviceID string) error- Check service health
2. Service Info Structure
ServiceInfostruct with:- ID, Name, Version
- Address (host:port)
- Protocol (local, grpc, http)
- Health status
- Metadata
3. Consul Registry (internal/services/registry/consul.go)
- Consul integration (primary for production)
- Service registration and discovery
- Health checking
- Automatic service registration
4. Kubernetes Service Discovery (internal/services/registry/kubernetes.go)
- Kubernetes service discovery
- Service health checking
- Automatic service registration via K8s services
5. Service Registration
- Auto-register services on startup
- Health check endpoints
- Graceful deregistration on shutdown
6. Configuration
- Registry configuration in
config/default.yaml:service_registry: type: consul # consul, kubernetes, etcd consul: address: localhost:8500 kubernetes: namespace: default etcd: endpoints: - localhost:2379
7. Integration
- Integrate with service factory
- Auto-register core services
- Support module service registration
Implementation Steps
-
Create Service Registry Interface
- Create
pkg/services/registry.go - Define ServiceRegistry interface
- Define ServiceInfo struct
- Create
-
Implement Consul Registry
- Create
internal/services/registry/consul.go - Implement Consul integration
- Add health checking
- Create
-
Implement Kubernetes Registry
- Create
internal/services/registry/kubernetes.go - Implement K8s service discovery
- Add health checking
- Create
-
Add Service Registration
- Auto-register services on startup
- Add health check endpoints
- Handle graceful shutdown
-
Add Configuration
- Add registry configuration
- Support multiple registry types
-
Integrate with Service Factory
- Use registry for service discovery
- Resolve services via registry
Acceptance Criteria
- Service registry interface is defined
- Consul registry works correctly
- Kubernetes registry works correctly
- Services are auto-registered on startup
- Service discovery works
- Health checking works
- Registry is configurable
- Graceful deregistration works
Related ADRs
Implementation Notes
- Consul is the primary registry for production
- Kubernetes service discovery for K8s deployments
- Health checks should be lightweight
- Support service versioning
Testing
# Test service registry
go test ./internal/services/registry/...
# Test service discovery
go test ./internal/services/registry/... -run TestDiscovery
Files to Create/Modify
pkg/services/registry.go- Service registry interfaceinternal/services/registry/consul.go- Consul registryinternal/services/registry/kubernetes.go- Kubernetes registryinternal/services/factory.go- Integrate with registryinternal/di/providers.go- Add registry providerconfig/default.yaml- Add registry configuration