Skip to content

A no-frills lib to keep your consul config up to date

License

Notifications You must be signed in to change notification settings

serialseb/ConsulStructure

Repository files navigation

Coverage Status Coverity Scan Build Status GitHub release NuGet Badge GitHub pull requests

ConsulStructure – Simple no-dependency configuration

Consul is, amongst other things, a distributed key/value store, so it can store any bit of data to a specified key, which is just a string.

Little bits of string are pretty useful for configuration data. There are manny ways to get that data in your configuration. But sometimes you just want very simple configuration that doesn't suck, without bringing in a whole set of packages for it, so you can hit go as quickly as possible.

This is ConsulStructure.

Quickstart

ConsulStructure ships as a source-code package, so you don't add more pressure to your dependency graph.

To get started, you can use nuget to install ConsulStructure.

Using the package manager console:

Install-Package ConsulStructure -pre

Using the command line client:

nuget install ConsulStructure -pre

To start receiving updates from Consul into your settings, it's one line, no dependencies, no frills.

var configuration = my ProjectConfiguration {
  MyValue = "a very good value", // key: /myvalue
  Subconfiguration = {
    IsStructureAwesome = true // key: /subconfiguration/isstructureawesome
  }
};
var updater = Structure.Start(configuration);

Your configuration class can have any level of nesting, it's up to you what it looks like. Any change to consul is reflected immediately in your class.

Supported types

Currently, Structure supports nesting, and a couple of base data types for keys, bool, string, int. As consul only sees values as byte arrays, Converters are customisable to match your needs.

Using without configuration objects

You can also use ConsulStructure to receive key changes without building objects.

public class ConsulListening
{
    public void ListenToKeyValues(IEnumerable<KeyValuePair<string,byte[]>> keyValues)
    {
        foreach(var kv in keyValues) Console.WriteLine($"Received key={kv.Key} with value {Encoding.UTF8.GetString(kv.Value)}");
    }

    public Task Main()
    {
        Structure.Start(ListenToKeyValues);
    }
}

How does it work

On starting up, Structure pre-compiles all the keys in your system, so there is no run-time reflection. It then does a continuous polling on the agent's HTTP API, to receive values as soon as they become available.

Whenever a key is updated, the value is assigned immediately, and fast. The data is parsed as json using the excellent SimpleJson library.

Things to do

  • Test HTTP part of the library
  • Create AppVeyor / Travis build
  • Create nuget package
  • Publish to nuget
  • Add all base datatype converters
  • Add converters for go's way of writing data (dates and timespans come to mind)
  • Exponential back off strategy

Credits

This project is inspired by the excellent functionality provided by the Go-based consulstructure, and uses SimpleJson for embeded json serialisation.