node-consul-service
Version:
A robust Node.js service that integrates with HashiCorp Consul for service discovery and configuration management. This service provides a comprehensive solution for managing distributed systems and microservices architecture, making it easier to handle s
17 lines (16 loc) • 635 B
JavaScript
import client from './client';
export async function listServices() {
const services = await client.agent.service.list();
return Object.values(services);
}
export async function getServiceInstances(serviceName) {
const services = await client.catalog.service.nodes(serviceName);
return services;
}
export async function getRandomServiceInstance(serviceName) {
const instances = await getServiceInstances(serviceName);
if (!instances.length)
throw new Error(`Service "${serviceName}" not found`);
const randomIndex = Math.floor(Math.random() * instances.length);
return instances[randomIndex];
}