#consul #proxy #service-discovery #tcp

bin+lib cotoxy

使用 Consul 进行服务发现的 TCP 代理

1 个不稳定版本

使用旧 Rust 2015

0.1.0 2018 年 2 月 7 日

#12 in #consul

MIT 许可证

34KB
765

cotoxy

Documentation License: MIT

使用 Consul 进行服务发现的 TCP 代理。

这使用 列出服务节点 API 来收集候选服务器。

安装

预编译的二进制文件

Linux 环境的预编译二进制文件可在 [发行版] 页面找到。

$ curl -L https://github.com/sile/cotoxy/releases/download/0.1.0/cotoxy-0.1.0.linux -o cotoxy
$ chmod +x cotoxy
$ ./cotoxy -h
cotoxy 0.1.0
A TCP proxy using Consul for service discovery

USAGE:
    cotoxy [OPTIONS] <SERVICE> [--] [THREADS]

FLAGS:
    -h, --help       Prints help information
    -V, --version    Prints version information

OPTIONS:
        --bind-addr <BIND_ADDR>                TCP address to which the proxy bind [default: 0.0.0.0:17382]
        --connect-timeout <CONNECT_TIMEOUT>    TCP connect timeout in milliseconds [default: 1000]
        --consul-addr <CONSUL_ADDR>            TCP address of the consul agent which the proxy queries [default:
                                               127.0.0.1:8500]
        --dc <DC>                              Datacenter to query [default: <Datacenter of the consul agent being
                                               queried>]
        --log-level <LOG_LEVEL>                 [default: info]  [values: debug, info, warning, error]
        --near <NEAR>                          Node name to sort the service node list in ascending order based on the
                                               estimated round trip time from that node. If `_agent` is specified, the
                                               node of the consul agent being queried will be used for the sort.
        --node-meta <NODE_META>...             Node metadata key/value pair of the form `key:value`. Service nodes will
                                               be filtered with the specified key/value pairs.
        --service-port <SERVICE_PORT>          Port number of the service [default: <Port number registered in Consul>]
        --tag <TAG>                            Tag to filter service nodes on

ARGS:
    <SERVICE>    Name of the service to which clients connect
    <THREADS>    Number of worker threads [default: 1]

使用 Cargo

如果您已经安装了 [Cargo][cargo],您可以在以下命令中轻松安装 cotoxy

$ cargo install cotoxy

示例

/// Run the consul agent in the background.
$ docker run -d --rm -p 8500:8500 consul

/// Start `cotoxy` which proxies "consul" service.
$ cotoxy consul --service-port 8500
Feb 07 13:23:13.028 INFO Proxy server started, service: consul, proxy: 0.0.0.0:17382

/// Call consul API via the proxy.
$ curl -s https://127.0.0.1:17382/v1/catalog/service/consul
[
    {
        "ID": "2db3224e-e9cb-fab4-edd7-6e98a2842f16",
        "Node": "e10993d941f1",
        "Address": "127.0.0.1",
        "Datacenter": "dc1",
        "TaggedAddresses": {
            "lan": "127.0.0.1",
            "wan": "127.0.0.1"
        },
        "NodeMeta": {
            "consul-network-segment": ""
        },
        "ServiceID": "consul",
        "ServiceName": "consul",
        "ServiceTags": [],
        "ServiceAddress": "",
        "ServicePort": 8300,
        "ServiceEnableTagOverride": false,
        "CreateIndex": 5,
        "ModifyIndex": 5
    }
]

依赖项

~10–20MB
~294K SLoC