import asyncio
from ri import (
RiServiceMesh,
RiServiceMeshConfig,
RiServiceDiscovery,
RiServiceInstance,
RiServiceStatus,
RiServiceMeshStats,
RiServiceEndpoint,
RiServiceHealthStatus,
RiHealthChecker,
RiHealthSummary,
RiHealthCheckType,
RiTrafficManager,
RiTrafficRoute,
RiMatchCriteria,
RiRouteAction,
RiWeightedDestination,
)
async def main():
config = RiServiceMeshConfig()
config.service_name = "ri-example-service"
config.namespace = "production"
config.enable_service_discovery = True
config.enable_health_check = True
config.enable_traffic_management = True
config.discovery_interval_seconds = 30
config.health_check_interval_seconds = 10
service_mesh = RiServiceMesh(config)
print("Creating service discovery...")
discovery = RiServiceDiscovery()
print("\nRegistering service instances...")
user_service_1 = RiServiceInstance()
user_service_1.instance_id = "user-service-1"
user_service_1.service_name = "user-service"
user_service_1.host = "192.168.1.10"
user_service_1.port = 8080
user_service_1.status = RiServiceStatus.HEALTHY
user_service_1.weight = 3
user_service_1.metadata = {"version": "1.0.0", "region": "us-east-1"}
user_service_2 = RiServiceInstance()
user_service_2.instance_id = "user-service-2"
user_service_2.service_name = "user-service"
user_service_2.host = "192.168.1.11"
user_service_2.port = 8080
user_service_2.status = RiServiceStatus.HEALTHY
user_service_2.weight = 2
user_service_2.metadata = {"version": "1.0.0", "region": "us-east-2"}
order_service_1 = RiServiceInstance()
order_service_1.instance_id = "order-service-1"
order_service_1.service_name = "order-service"
order_service_1.host = "192.168.1.20"
order_service_1.port = 8081
order_service_1.status = RiServiceStatus.HEALTHY
order_service_1.weight = 1
order_service_1.metadata = {"version": "1.1.0", "region": "us-east-1"}
discovery.register_service(user_service_1)
discovery.register_service(user_service_2)
discovery.register_service(order_service_1)
print(f"Registered 3 service instances")
print("\nDiscovering services...")
user_services = discovery.discover_services("user-service")
print(f"Found {len(user_services)} instances of user-service")
order_services = discovery.discover_services("order-service")
print(f"Found {len(order_services)} instances of order-service")
print("\nCreating service endpoints...")
user_endpoint = RiServiceEndpoint()
user_endpoint.service_name = "user-service"
user_endpoint.address = "192.168.1.10:8080"
user_endpoint.protocol = "http"
user_endpoint.health_status = RiServiceHealthStatus.HEALTHY
order_endpoint = RiServiceEndpoint()
order_endpoint.service_name = "order-service"
order_endpoint.address = "192.168.1.20:8081"
order_endpoint.protocol = "http"
order_endpoint.health_status = RiServiceHealthStatus.HEALTHY
print(f"Created endpoints for user-service and order-service")
print("\nConfiguring health checking...")
health_checker = RiHealthChecker()
health_checker.check_type = RiHealthCheckType.HTTP
health_checker.interval_seconds = 10
health_checker.timeout_seconds = 5
health_checker.path = "/health"
health_status = health_checker.check(user_endpoint)
print(f"Health check for user-service: {health_status}")
health_summary = RiHealthSummary()
health_summary.total_services = 3
health_summary.healthy_services = 3
health_summary.unhealthy_services = 0
health_summary.unknown_services = 0
print(f"\nHealth summary:")
print(f" Total: {health_summary.total_services}")
print(f" Healthy: {health_summary.healthy_services}")
print(f" Unhealthy: {health_summary.unhealthy_services}")
print("\nConfiguring traffic management...")
traffic_manager = RiTrafficManager()
route1 = RiTrafficRoute()
route1.name = "user-service-route"
route1.priority = 100
match_criteria = RiMatchCriteria()
match_criteria.path = "/api/users/*"
match_criteria.methods = ["GET", "POST", "PUT", "DELETE"]
route1.match = match_criteria
route_action = RiRouteAction()
route_action.route_to = "user-service"
route1.action = route_action
canary_route = RiTrafficRoute()
canary_route.name = "canary-route"
canary_route.priority = 90
canary_match = RiMatchCriteria()
canary_match.headers = {"x-canary": "true"}
canary_route.match = canary_match
stable_dest = RiWeightedDestination()
stable_dest.service = "user-service"
stable_dest.version = "1.0.0"
stable_dest.weight = 90
canary_dest = RiWeightedDestination()
canary_dest.service = "user-service"
canary_dest.version = "1.1.0"
canary_dest.weight = 10
canary_action = RiRouteAction()
canary_action.weighted_destinations = [stable_dest, canary_dest]
canary_route.action = canary_action
traffic_manager.add_route(route1)
traffic_manager.add_route(canary_route)
print(f"Added {len(traffic_manager.routes)} traffic routes")
print("\nService mesh statistics:")
mesh_stats = RiServiceMeshStats()
mesh_stats.total_services = 2
mesh_stats.total_instances = 3
mesh_stats.healthy_instances = 3
mesh_stats.unhealthy_instances = 0
mesh_stats.total_requests = 10000
mesh_stats.average_latency_ms = 25.5
print(f" Total services: {mesh_stats.total_services}")
print(f" Total instances: {mesh_stats.total_instances}")
print(f" Healthy instances: {mesh_stats.healthy_instances}")
print(f" Total requests: {mesh_stats.total_requests}")
print(f" Average latency: {mesh_stats.average_latency_ms}ms")
print("\nDeregistering service...")
discovery.deregister_service("user-service-2")
print("Deregistered user-service-2")
print("\nAll registered services:")
all_services = discovery.list_services()
for service_name in all_services:
instances = discovery.discover_services(service_name)
print(f" {service_name}: {len(instances)} instances")
print("\nService mesh operations completed successfully!")
if __name__ == "__main__":
asyncio.run(main())