Skip to content

Instantly share code, notes, and snippets.

@pgressa
Last active July 27, 2020 20:24
Show Gist options
  • Save pgressa/5c920510e048916875752d72e7bd5016 to your computer and use it in GitHub Desktop.
Save pgressa/5c920510e048916875752d72e7bd5016 to your computer and use it in GitHub Desktop.
Consul watches support for micronaut

Consul watches support for Micronaut Service Discovery

Currently supported watches by Consul [1]:

  • key - Watch a specific KV pair
  • keyprefix - Watch a prefix in the KV store
  • services - Watch the list of available services
  • nodes - Watch the list of nodes
  • service- Watch the instances of a service
  • checks - Watch the value of health checks
  • event - Watch for custom user events

Watches are driven by Consul itself. This means that consul either executes command or sends request to API endpoint. For our cause the API endpoint is the choice. Http handler options are:

{
    "path": "https://localhost:8000/watch",
    "method": "POST",
    "header": { "x-foo": ["bar", "baz"] },
    "timeout": "10s",
    "tls_skip_verify": false
}

As an MVP let's implement just keyprefix as from property source it makes most sense atm.

Design

Flow

1] How to receive watches

  • Use watches over HTTP handlers
  • Create management WatchesEndpoint endpoint in io.micronaut.discovery.consul.endpoint package
  • Basically for every watch type there would be specific method that handles watch specific payload

2] How to register watches

  • Have for every watch type Bean annotated with @ConfigurationProperties("endpoints.consul.watches.keyprefix")
  • Have for every watch type Bean that based on (@Requires) configuration and (@Requires) ConsulClient registers the watches

3] How to handle payloads

Depends on type of watches.

3.1] keyprefix

Motivation:

  • user can manually specify prefixes of configuration to watch
  • by providing some keywords like: all we can autoregister either all existing AppContext configuration or do some filering in microservice
  • this can basically allow to implement feature toggles (altough I registered some Toggleable annotation but didn't find documentation for it)

Two possible approaches:

  • Create or replace PropertySource and refresh environment -> AFAIK will cause recreation of all beans??
  • Do it like with /refresh management endpoint where refresh is called but after that RefresEvent is emitted

Configuration

General configuration of the watches in micronaut

endpoints:
  consul:
    enabled: true/false
    sensitive: true/false
    watches:
      <watch-type>:
        sensitive: true/false
	<watch-type-specific-config>

keyprefix configuration

keyprefix:
  prefixes:
  - <prefix_1>
  - <prefix_2>
  - <prefix_N>

[1] https://www.consul.io/docs/agent/watches.html#watch-types

@pgressa
Copy link
Author

pgressa commented Jul 27, 2020

io.micronaut.discovery.consul.endpoint -> io.micronaut.discovery.consul.management

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment