What is Service Discovery?
Service discovery is the process of automatically detecting devices and services on a network. It enables dynamic scaling, fault‑tolerance, and reduces the need for manual configuration.
Common scenarios:
- Microservices locating each other in a cloud environment.
- IoT devices advertising capabilities on a local network.
- Clients discovering APIs at runtime.
Discovery Patterns
Client‑Side Discovery
The client queries a registry (e.g., Consul, etcd) and selects an instance.
var client = new HttpClient();
var address = registry.GetService("orders");
var response = await client.GetAsync($"{address}/api/orders");
Server‑Side Discovery (Proxy)
A sidecar or API gateway resolves the service name and forwards the request.
apiVersion: v1
kind: Service
metadata:
name: orders
spec:
selector:
app: orders
ports:
- protocol: TCP
port: 80
targetPort: 8080
Zero‑Configuration (mDNS, SSDP)
Devices broadcast their presence without a central registry.
Implementation Guides
Using Consul with .NET
- Install
Consul
agent. - Add
Consul.AspNetCore
NuGet package. - Register service in
Program.cs
:
builder.Host.UseConsul(options =>
{
options.Address = new Uri("http://localhost:8500");
options.Datacenter = "dc1";
options.ServiceName = "orders";
options.ServiceId = "orders-1";
});
mDNS with Node.js
const mdns = require('mdns-js');
const browser = mdns.createBrowser();
browser.on('ready', () => browser.discover());
browser.on('update', data => {
console.log('Discovered service:', data.fullname);
});
Tools & Libraries
Tool | Language | Use‑Case |
---|---|---|
Consul | Any | Distributed KV store & service registry |
Eureka | Java | Netflix OSS service discovery |
etcd | Go/All | Key‑value store with watch capability |
Bonjour (mDNS) | Swift/Obj‑C | Zero‑conf on local networks |
ZooKeeper | Java/Scala | Configuration & discovery for Hadoop ecosystem |
Frequently Asked Questions
- Do I need a central registry?
- Not always. For small LANs, mDNS works well. Large cloud deployments benefit from a central store like Consul or etcd.
- How does load balancing work?
- Clients can pick instances using round‑robin, least‑connections, or custom health‑based algorithms after querying the registry.
- Is discovery secure?
- Yes, use TLS between clients and the registry, and enable ACLs or token‑based authentication on the registry.