Service Discovery
Overview
The dt.service
module provides functionality for service discovery and management within the Datatailr platform. It integrates with Consul for service registration and discovery, allowing applications to locate and connect to other services in the distributed system.
Module Functions
service_host_and_port(service)
service_host_and_port(service)
Retrieves the host and port information for a specified service from Consul.
Parameters:
service
(str): The name of the service to look up
Returns:
tuple
: A tuple containing(host, port)
where:host
(str or None): The hostname or IP address of the serviceport
(int or None): The port number the service is running on- Returns
(None, None)
if the service is not found
Example:
import dt.service
# Get host and port for a database service
host, port = dt.service.service_host_and_port('database')
if host and port:
print(f"Service found at {host}:{port}")
else:
print("Service not found")
Service Discovery Pattern
The module follows a standard service discovery pattern:
- Service Registration: Services register themselves when deployed.
- Service Lookup: Applications / BatchJobs / Services use
service_host_and_port()
to find service endpoints. - Environment Isolation: Services are scoped by environment (dev, pre, prod), i.e. calling this method from
dev
environment will only return services fromdev
.
Error Handling
The module handles various error conditions gracefully:
- Service Not Found: Returns
(None, None)
instead of raising exceptions - Invalid Port: Attempts to convert port to integer, handles conversion errors
- Connection Issues: Relies on underlying KV storage client error handling
Notes
- The module is designed for internal Datatailr platform use
- Service names should follow the platform's naming conventions
- Environment information is automatically retrieved from the host configuration
Updated about 16 hours ago