consul使用小结

项目中用到consul作为服务注册/发现、配置下发、负载均衡,保证了服务的高可用,最近简单学习了下它的原理。
关于consul的安装就不细说了,启几个server组成集群,再启俩个agent。

这里就有俩中方式注册服务: 
 1. 在启动agent的时候可以在consul的配置文件里直接注册服务
 2. 也可以先不注册,之后服务通过http API向agent注册。
我们选的是第二种方式,这样agent不需要关注具体业务。
服务注册

要用到开源项目consul-clinet,文档上有使用案例 github地址

<dependencies>
    <dependency>
        <groupId>com.orbitz.consul</groupId>
        <artifactId>consul-client</artifactId>
        <version>0.16.3</version>
    </dependency>
</dependencies>
import com.google.common.net.HostAndPort;
import com.orbitz.consul.AgentClient;
import com.orbitz.consul.Consul;
import com.orbitz.consul.HealthClient;
import com.orbitz.consul.model.health.ServiceHealth;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import java.net.MalformedURLException;
import java.net.URI;
import java.util.List;


@RestController
public class ConsulController {
    private static final String serviceName = "demo";
    private static final String serviceTag = "dev";
    private static final String host = "192.168.21.253";
    private static final int port = 8804;
    private static final String serviceId = host + port;
    private Consul consul;

    //注册服务名、tag和健康检查地址
    @PostConstruct
    public void consulInit() {
        consul = Consul.builder().withHostAndPort(HostAndPort.fromParts("localhost", 8500)).build();
        AgentClient agentClient = consul.agentClient();

        try {
            agentClient.register(port, URI.create("http://" + host + ":" + port + "/health").toURL(), 5L, serviceName, serviceId, serviceTag);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        }

        System.out.println("consul init");
    }

    //获取健康的服务
    @RequestMapping("/servicelist")
    public String serviceList() {
        HealthClient healthClient = consul.healthClient();
        List<ServiceHealth> list = healthClient.getHealthyServiceInstances(serviceName).getResponse();
        StringBuffer buffer = new StringBuffer();
        for (ServiceHealth serviceHealth : list) {
            String address = serviceHealth.getService().getAddress();
            if (StringUtils.isEmpty(address)) {
                address = serviceHealth.getNode().getAddress();
            }

            int port = serviceHealth.getService().getPort();
            buffer.append(String.format("service (%s) address is (%s) port is (%d) \n", serviceName, address, port));
        }
        return buffer.toString();
    }
}

这样就完成了服务的注册与发现,具体的发现规则,可以是随机、轮询、一致性哈希、哈希、权重。这个可以自己定义。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

李昂的数字之旅

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值