Consul客户端(orbitz和ecwid)API介绍

Consul客户端(orbitz 与ecwid)API介绍

本文件简要介绍两个consul客户端的使用。近期因为使用consul,学习了解consul的客户端,主要用来注册服务发现服务:

  • 添加依赖
  • 示例代码
  • 效果展示

添加依赖

pom文件加入如下依赖。

注意ecwid是spring-cloud-consul-core的依赖包

        
            com.ecwid.consul
            consul-api
            1.4.0
        

或者orbitz的consu客户端

        
        
            com.orbitz.consul
            consul-client
            0.10.0
        
        
        
            org.glassfish.jersey.core
            jersey-client
            2.22.2
        

示例代码

package com.yq.service.impl;

import com.ecwid.consul.v1.ConsulClient;
import com.ecwid.consul.v1.QueryParams;
import com.ecwid.consul.v1.Response;
import com.ecwid.consul.v1.agent.model.NewService;
import com.ecwid.consul.v1.health.model.HealthService;
import com.ecwid.consul.v1.kv.model.GetValue;
import com.yq.config.ConsulConfig;
import com.yq.service.IConsulService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * Simple to Introduction

 *  本代码只是为了说明如何使用consul api,两个serviceImpl都实现了接口 IConsulService
 * @version 2018/9/1 10:01
 */

@Service
@Slf4j
@Component
public class EcwidConsulServiceImpl implements IConsulService {
    @Autowired
    private ConsulConfig consulConfig;

    private ConsulClient client = null;

    public EcwidConsulServiceImpl() {
        log.info("consulConfig={}", consulConfig);
        //client = new ConsulClient(consulConfig.getConsulIP(), consulConfig.getConsulPort());
        client = new ConsulClient("127.0.0.1", 8500);
    }

    @Override
    public void registerService(String serviceName, String serviceId) {
        // register new service
        NewService newService = new NewService();
        newService.setId(serviceId);
        newService.setName(serviceName);
        newService.setTags(Arrays.asList("EU-West", "EU-East"));
        newService.setPort(8080);

        NewService.Check serviceCheck = new NewService.Check();
        serviceCheck.setHttp("http://127.0.0.1:8080/health");
        serviceCheck.setInterval("10s");
        newService.setCheck(serviceCheck);
        client.agentServiceRegister(newService);
    }

    @Override
    public List findHealthyService(String serviceName) {
        Response> healthyServices = client.getHealthServices(serviceName, true, QueryParams.DEFAULT);
        return healthyServices.getValue();
    }

    @Override
    public void storeKV(String key, String value) {
        Response booleanResponse = client.setKVValue(key, value);
    }

    @Override
    public String getKV(String key) {
        Response getValueResponse = client.getKVValue(key);
        //return getValueResponse.getValue().getValue();
        return getValueResponse.getValue().getDecodedValue();
    }

    @Override
    public List findRaftPeers() {
        Response> listResponse = client.getStatusPeers();
        return listResponse.getValue();
    }

    @Override
    public String findRaftLeader() {
        Response stringResponse = client.getStatusLeader();
        return stringResponse.getValue();
    }
}


orbitz的代码类似,主要区别在Ecwid的服务是HealthService, 而orbitz的服务是ServiceHealth. 以及获取key/value的差异。详情可见代码

##效果展示
第一个例子,先使用Ecwid客户端注册服务,然后通过Ecwid和orbitz分别获取服务
注册的服务
Consul客户端(orbitz和ecwid)API介绍_第1张图片

分别使用两种客户端获取服务
Consul客户端(orbitz和ecwid)API介绍_第2张图片

第二个例子,我们使用orbitz在consul上创建key/value,然后分别通过orbitz和ecwid获取该key的值。
Consul客户端(orbitz和ecwid)API介绍_第3张图片


你可能感兴趣的:(consul,微服务,ecwid,Java,springcloud)