JasperFx 0.5.0


Next

Jasper.LightningDb

Previous

Extensions

Jasper.Consul Edit on GitHub


The Jasper.Consul extension is an integration with the outstanding Consul tool for service discovery and configuration. This extension uses the ConsulDotNet library to read and write to Consul using its HTTP API.

Uri Lookups

You can use the Consul Key/Value store for Uri aliasing in any place where Uri's are part of the service bus configuration. Here's an example:


public class ConsulUsingApp : JasperRegistry
{
    public ConsulUsingApp()
    {
        // These calls are looking up values from the Key/Value
        // store in Consul
        Transports.ListenForMessagesFrom("consul://one");
        Transports.ListenForMessagesFrom("consul://two");
    }
}

In the sample above, the Uri consul://one would refer to a Uri string in the key/value store with the key "one."

See Configuring the Service Bus for more information about Uri lookups.

Subscriptions

For dynamic subscriptions, you can opt into using Consul as Jasper's service discovery mechanism by applying the ConsulBackedSubscriptions extension as shown below:


public class AppUsingConsulBackedSubscriptions : JasperRegistry
{
    public AppUsingConsulBackedSubscriptions()
    {
        Include<ConsulBackedSubscriptions>();
    }
}

The subscriptions are stored in Consul's key/value store using the pattern jasper/subscription/[message-type]/[url encoded destination], and the value is a Json document reflecting the valid message representations for the subscriber.

Customizing Consul Setup

By default, Jasper.Consul will connect to Consul at port 8500, but you can completely configure how the underlying ConsulDotNet ConsulClient will be built out like this:


public class AppUsingConsul : JasperRegistry
{
    public AppUsingConsul()
    {
        // In this case, we're expecting an environment
        // variable called "Consul.Port"
        Configuration.AddEnvironmentVariables();

        Settings.Alter<ConsulSettings>(ConfigureConsul);
    }

    public void ConfigureConsul(IConfigurationRoot config, ConsulSettings settings)
    {
        // Shorthand to use the default Consul setup, but with a different port
        // number retrieved from the application configuration
        settings.Port = config.GetValue<int>("Consul.Port");

        // Configure the underlying ConsulClientConfiguration object
        settings.Configure = _ =>
        {
            // This line is an equivalent to "settings.Port = #" as shown above
            _.Address = $"http://localhost:{config["Consul.Port"]}".ToUri();

            _.Token = config["Consul.Token"];
        };

        // ConsulDotNet allows you to further configure the HttpClient
        // that it uses internallly to talk to Consul
        settings.ClientOverride = httpClient =>
        {
            httpClient.Timeout = 1.Seconds();
        };

        // ConsulDotNet also allows you to configure the HttpClientHandler
        // that it uses internally to talk to Consul
        settings.HandlerOverride = httpClientHandler =>
        {
            httpClientHandler.AutomaticDecompression = DecompressionMethods.None;
        };
    }
}