连接Consul小demo

引入依赖
compile group: 'com.orbitz.consul', name: 'consul-client', version: '1.2.0'
HostAndPort consulUrl = HostAndPort.fromParts("127.0.0.1",8500);
Consul consul = Consul.builder().withHostAndPort(consulUrl).build();
KeyValueClient keyValueClient = consul.keyValueClient();
JSONObject o = JSON.parseObject(keyValueClient.getValue(key).get().getValueAsString().get());
o.getIntValue("abc")  ......
/**
 * Build a HostAndPort instance from separate host and port values.
 *
 * <p>Note: Non-bracketed IPv6 literals are allowed. Use {@link #requireBracketsForIPv6()} to
 * prohibit these.
 *
 * @param host the host string to parse. Must not contain a port number.
 * @param port a port number from [0..65535]
 * @return if parsing was successful, a populated HostAndPort object.
 * @throws IllegalArgumentException if {@code host} contains a port number, or {@code port} is out
 *     of range.
 */
public static HostAndPort fromParts(String host, int port) {
  checkArgument(isValidPort(port), "Port out of range: %s", port);
  HostAndPort parsedHost = fromString(host);
  checkArgument(!parsedHost.hasPort(), "Host has a port: %s", host);
  return new HostAndPort(parsedHost.host, port, parsedHost.hasBracketlessColons);
}
/**
 * Sets the URL from a {@link HostAndPort} object.
 *
 * @param hostAndPort The Consul agent host and port.
 * @return The builder.
 */
public Builder withHostAndPort(HostAndPort hostAndPort) {
    try {
        this.url = new URL("http", hostAndPort.getHost(), hostAndPort.getPort(), "");
    } catch (MalformedURLException e) {
        throw new RuntimeException(e);
    }

    return this;
}
/**
 * Get the Key/Value HTTP client.
 * <p>
 * /v1/kv
 *
 * @return The Key/Value HTTP client.
 */
public KeyValueClient keyValueClient() {
    return keyValueClient;
}

转载于:https://my.oschina.net/u/3915790/blog/3085706

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值