Skip to content

This crate provides access to a set of strongly typed apis to interact with consul (https://www.consul.io/)

Notifications You must be signed in to change notification settings

Roblox/rs-consul

Repository files navigation

rs-consul

Crates.io: rs-consul Documentation Main

This crate provides access to a set of strongly typed apis to interact with consul (https://www.consul.io/)

Installation

Simply include the rs-consul in your Cargo dependencies.

[dependencies]
rs-consul = "0.9.0"

Usage

Check /examples for more detailed usage

Initialize the client

Environment Configuration (Recommended)

The client can be configured automatically using environment variables:

use rs_consul::{types::*, Config, Consul};

let consul_config = Config::from_env();
let consul = Consul::new(consul_config);

Manual Configuration

Alternatively, you can configure the client manually:

let consul_config = Config {
    address: "http://localhost:8500".to_string(), 
    token: None, // No token required in development mode
    ..Default::default() // Uses default values for other settings
};

let consul = Consul::new(consul_config);

Register a Service

    let node_id = "root-node"; //node name
    let service_name = "new-service-1"; //service name

    let payload = RegisterEntityPayload {
        ID: None,
        Node: node_id.to_string(),
        Address: "127.0.0.1".to_string(), //server address
        Datacenter: None,
        TaggedAddresses: Default::default(),
        NodeMeta: Default::default(),
        Service: Some(RegisterEntityService {
            ID: None,
            Service: service_name.to_string(),
            Tags: vec![],
            TaggedAddresses: Default::default(),
            Meta: Default::default(),
            Port: Some(42424), 
            Namespace: None,
        }),
        Check: None,
        SkipNodeUpdate: None,
    };

    consul.register_entity(&payload).await.unwrap();

Deregister a service

    let node_id = "root-node";
    let service_name = "new-service-1";

    let payload = DeregisterEntityPayload {
        Node: Some(node_id.to_string()),
        Datacenter: None,
        CheckID: None,
        ServiceID: Some(service_name.to_string()),
        Namespace: None,
    };
    consul.deregister_entity(&payload).await.unwrap();

Development

cargo build

Tests

Local Consul

Start consul locally with a docker image.

docker-compose up -d

CI Consul

In CI, we start a service container for the test.

Running Tests

cargo test

Contributions

For contributions, please:

  1. Make a pull request
  2. Make sure the tests pass
  3. Add a bullet to the Changelog

License

rs-consul is available under the MIT license. See LICENSE for details.

About

This crate provides access to a set of strongly typed apis to interact with consul (https://www.consul.io/)

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published