Skip to content

Service Discovery ​

An Instance is one running copy of a service: a service name, an endpoint, a weight and metadata. A service is simply the group of Instances that share a name.

Registering ​

With the starter, an application registers once its web server is ready, and removes itself on shutdown.

yaml
spring:
  application:
    name: order-service
cosky:
  registry:
    enabled: true
    weight: 1
    metadata:
      zone: az1
  • The service is spring.application.name, which must be a DNS-style name: lowercase letters, digits, - and ., up to 63 characters. OrderService is refused.
  • A listener of Spring Cloud's InstancePreRegisteredEvent may add to the registration's metadata, as with Spring Cloud's other registries; what it adds is registered, and is registered again after a lost lease, while cosky.registry.metadata stays as configured.
  • A separate management port is not registered.
  • spring.cloud.service-registry.auto-registration.enabled: false turns registering off.

Leases ​

A process holds one lease per namespace, however many instances it registers, and renews it every third of its TTL (30 seconds by default). When renewals stop, its instances go.

EventWhat happens
Clean shutdownThe lease is revoked within 3 seconds, and the instances go at once.
CrashThe lease expires, and the instances go a third of the TTL later: 40 seconds by default.
Lease lost, such as after a Redis failoverThe client notices, waits up to 5 seconds so that not everyone returns at once, and registers everything again.
Client reconnectsIt renews at once, without waiting for the next period.

Allowed TTLs are 5 to 300 seconds.

Finding services ​

DiscoveryClient and ReactiveDiscoveryClient read the local view. The first read of a service waits up to cosky.discovery.load-timeout (5 seconds), so that "not known yet" is not mistaken for "empty". If the server refuses, for lack of permission or because the namespace does not exist, the read throws a CoSkyException at once. Afterwards, a read costs no network round trip.

Add spring-cloud-starter-loadbalancer, and @LoadBalanced clients get their instances from CoSky, with Spring Cloud's own algorithms choosing among them.

Weights ​

A weight of 0 keeps an instance registered but takes it out of rotation, which is how to drain one: load balancers leave it out, while DiscoveryClient still lists it. The Kotlin client's load balancer picks at random in proportion to weight. Spring Cloud LoadBalancer's default round robin treats every other weight alike.

An operator can set it on an instance an application registered. It lasts until the application registers again, after restarting or losing its lease, and so do metadata an operator changed: the application then registers what it was given. To keep an instance drained through those, set its own weight, cosky.registry.weight: 0 with the starter.

Fixed instances ​

An Instance without a lease stays until someone deletes it. Use this for something that cannot register itself, such as a database:

bash
curl -X PUT localhost:8080/v1/namespaces/prod/instances/orders-db \
  -H 'Content-Type: application/json' \
  -d '{"instance":{"service":"orders-db","endpoint":{"scheme":"tcp","host":"10.0.0.5","port":3306}}}'

The Dashboard marks each instance Leased or Fixed. Nothing checks whether a fixed instance is alive.

Moving an instance to another service ​

An instance stays in the service it was created in. A write with another service is refused with INVALID_ARGUMENT (HTTP 400): delete the instance, then create it again. The default instance name holds the service, so this only comes up with a name of your own. An application registering under such a name for a new service is refused until the old registration is gone, which takes at most its lease's TTL if the old process did not stop cleanly.

When things fail ​

CoSky favours keeping a stale instance over dropping a live one, and callers are expected to retry and use circuit breakers.

FailureWhat applications see
Server unreachableReads keep serving the last view, and the health indicator stays UP with connection: DEGRADED, because a restart would not help.
Restart while the server is awayThe view comes back from the snapshot on disk.
Server or network was down for longer than the TTLThe reaper waits 30 seconds after recovering, so that live clients can renew rather than be removed.
More than half of a namespace's leases expire at onceReaping pauses, as this looks like a partition. It resumes when the share falls, or after 5 minutes regardless.
Redis fails over and loses recent writesEvery replica notices the revision going backwards, and clients start over from a fresh snapshot. Writes that were lost stay lost.

These all have settings under cosky.lease. A process that really crashed during such a pause stays listed for up to 5 minutes longer.

Who calls whom ​

A client that watches a service counts as consuming it. The topology on the Dashboard and at /views/topology shows these edges, under the name in spring.application.name, or the signed-in subject without one. They expire 90 seconds after the last replica saw the watch, so a stopped consumer disappears by itself.

Released under the Apache License 2.0.