Java tutorial
/* * Copyright 2016 Phaneesh Nagaraja <phaneesh.n@gmail.com>. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package feign.ranger.client; import com.fasterxml.jackson.core.type.TypeReference; import com.fasterxml.jackson.databind.ObjectMapper; import com.flipkart.ranger.ServiceFinderBuilders; import com.flipkart.ranger.finder.sharded.SimpleShardedServiceFinder; import com.flipkart.ranger.model.ServiceNode; import feign.ranger.common.ShardInfo; import lombok.Builder; import lombok.extern.slf4j.Slf4j; import org.apache.curator.framework.CuratorFramework; import java.util.List; import java.util.Optional; /** * @author phaneesh */ @Slf4j public class ServiceDiscoveryClient { private final ShardInfo criteria; private SimpleShardedServiceFinder<ShardInfo> serviceFinder; @Builder ServiceDiscoveryClient(String namespace, String serviceName, String environment, ObjectMapper objectMapper, CuratorFramework curator) throws Exception { this.criteria = ShardInfo.builder().environment(environment).build(); this.serviceFinder = ServiceFinderBuilders.<ShardInfo>shardedFinderBuilder().withCuratorFramework(curator) .withNamespace(namespace).withServiceName(serviceName).withDeserializer(data -> { try { return objectMapper.readValue(data, new TypeReference<ServiceNode<ShardInfo>>() { }); } catch (Exception e) { log.warn("Could not parse node data", e); } return null; }).build(); } public void start() throws Exception { serviceFinder.start(); } public void stop() throws Exception { serviceFinder.stop(); } public Optional<ServiceNode<ShardInfo>> getNode() { return Optional.ofNullable(serviceFinder.get(criteria)); } public List<ServiceNode<ShardInfo>> getAllNodes() { return serviceFinder.getAll(criteria); } }