Architecture Intermediate
Service Discovery — How Services Find Each Other¶
Service DiscoveryMicroservicesConsul 3 min read
Service discovery mechanisms in microservice architecture. Client-side vs server-side discovery, Consul and Kubernetes DNS.
Why Service Discovery?¶
In microservices, services are dynamically started and stopped. IP addresses change. Service Discovery solves the question: how does service A find the current address of service B?
Client-Side Discovery¶
The client queries the registry and decides which instance to send the request to.
const consul = require('consul')();
async function getServiceUrl(serviceName) {
const services = await consul.health.service({
service: serviceName, passing: true
});
const idx = Math.floor(Math.random() * services.length);
const svc = services[idx].Service;
return \`http://\${svc.Address}:\${svc.Port}\`;
}
Server-Side Discovery¶
Kubernetes uses server-side discovery natively:
apiVersion: v1
kind: Service
metadata:
name: order-service
spec:
selector:
app: order-service
ports:
- port: 80
targetPort: 8080
# Call: http://order-service.default.svc.cluster.local
Tools¶
- Consul — health checking, KV store, multi-datacenter
- etcd — foundation of Kubernetes
- Eureka — Netflix OSS, Java/Spring
Summary¶
In Kubernetes, service discovery is solved out-of-the-box. Outside K8s, consider Consul or Eureka.
Need Help with Implementation?¶
Our team has experience designing and implementing modern architectures. We’re happy to help.