Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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 org.apache.dubbo.registry.consul;
import org.apache.dubbo.common.Constants;
import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.support.FailbackRegistry;
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.catalog.CatalogServicesRequest;
import com.ecwid.consul.v1.health.HealthServicesRequest;
import com.ecwid.consul.v1.health.model.HealthService;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService;
import java.util.stream.Collectors;
import static java.util.concurrent.Executors.newCachedThreadPool;
import static org.apache.dubbo.common.Constants.ANY_VALUE;
/**
* registry center implementation for consul
*/
public class ConsulRegistry extends FailbackRegistry {
private static final Logger logger = LoggerFactory.getLogger(ConsulRegistry.class);
private static final String SERVICE_TAG = "dubbo";
private static final String URL_META_KEY = "url";
private static final String WATCH_TIMEOUT = "consul-watch-timeout";
private static final String CHECK_INTERVAL = "consul-check-interval";
private static final String CHECK_TIMEOUT = "consul-check-timeout";
private static final String DEREGISTER_AFTER = "consul-deregister-critical-service-after";
private static final int DEFAULT_PORT = 8500;
// default watch timeout in millisecond
private static final int DEFAULT_WATCH_TIMEOUT = 60 * 1000;
// default tcp check interval
private static final String DEFAULT_CHECK_INTERVAL = "10s";
// default tcp check timeout
private static final String DEFAULT_CHECK_TIMEOUT = "1s";
// default deregister critical server after
private static final String DEFAULT_DEREGISTER_TIME = "20s";
private ConsulClient client;
private ExecutorService notifierExecutor = newCachedThreadPool(
new NamedThreadFactory("dubbo-consul-notifier", true));
private ConcurrentMap notifiers = new ConcurrentHashMap<>();
public ConsulRegistry(URL url) {
super(url);
String host = url.getHost();
int port = url.getPort() != 0 ? url.getPort() : DEFAULT_PORT;
client = new ConsulClient(host, port);
}
@Override
public void register(URL url) {
if (isConsumerSide(url)) {
return;
}
super.register(url);
}
@Override
public void doRegister(URL url) {
client.agentServiceRegister(buildService(url));
}
@Override
public void unregister(URL url) {
if (isConsumerSide(url)) {
return;
}
super.unregister(url);
}
@Override
public void doUnregister(URL url) {
client.agentServiceDeregister(buildId(url));
}
@Override
public void subscribe(URL url, NotifyListener listener) {
if (isProviderSide(url)) {
return;
}
super.subscribe(url, listener);
}
@Override
public void doSubscribe(URL url, NotifyListener listener) {
Long index;
List urls;
if (ANY_VALUE.equals(url.getServiceInterface())) {
Response