Skip to content

Commit

Permalink
Refactor consul registry and fix the subscribe bugs. (sofastack#629)
Browse files Browse the repository at this point in the history
* refactor(registry-consul): refactor consul registry, redesigning the data structure and fixing the subscribe bugs

* clean imports

* add default port

* fix tests

* assert until 10s

* fix tests

* fix notify tests

* minor fixes

* remove my jenv
  • Loading branch information
ScienJus authored and leizhiyuan committed Jun 17, 2019
1 parent ad8d7de commit a20537e
Show file tree
Hide file tree
Showing 27 changed files with 842 additions and 2,466 deletions.
2 changes: 1 addition & 1 deletion bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -348,7 +348,7 @@
<dependency>
<groupId>com.ecwid.consul</groupId>
<artifactId>consul-api</artifactId>
<version>1.3.0</version>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.alipay.sofa.rpc.common.utils.NetUtils;
import com.alipay.sofa.rpc.common.utils.StringUtils;
import com.alipay.sofa.rpc.config.AbstractInterfaceConfig;
import com.alipay.sofa.rpc.config.ConfigUniqueNameGenerator;
import com.alipay.sofa.rpc.config.ConsumerConfig;
import com.alipay.sofa.rpc.config.ProviderConfig;
import com.alipay.sofa.rpc.config.ServerConfig;
Expand Down Expand Up @@ -259,4 +260,44 @@ public static <K, V> void initOrAddList(Map<K, List<V>> orginMap, K key, V needA
listeners.add(needAdd);
}
}

public static String convertInstanceToUrl(String host, int port, Map<String, String> metaData) {
if (metaData == null) {
metaData = new HashMap<String, String>();
}
String uri = "";
String protocol = metaData.get(RpcConstants.CONFIG_KEY_PROTOCOL);
if (StringUtils.isNotEmpty(protocol)) {
uri = protocol + "://";
}
uri += host + ":" + port;

StringBuilder sb = new StringBuilder();
for (Map.Entry<String, String> entry : metaData.entrySet()) {
sb.append("&").append(entry.getKey()).append("=").append(entry.getValue());
}
if (sb.length() > 0) {
uri += sb.replace(0, 1, "?").toString();
}
return uri;
}

public static String getServerHost(ServerConfig server) {
String host = server.getVirtualHost();
if (host == null) {
host = server.getHost();
if (NetUtils.isLocalHost(host) || NetUtils.isAnyHost(host)) {
host = SystemInfo.getLocalHost();
}
}
return host;
}

public static String buildUniqueName(AbstractInterfaceConfig config, String protocol) {
if (RpcConstants.PROTOCOL_TYPE_BOLT.equals(protocol) || RpcConstants.PROTOCOL_TYPE_TR.equals(protocol)) {
return ConfigUniqueNameGenerator.getUniqueName(config) + "@DEFAULT";
} else {
return ConfigUniqueNameGenerator.getUniqueName(config) + "@" + protocol;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
* 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 com.alipay.sofa.rpc.registry.consul;

/**
* All constants of the consul registry
*
* @author <a href=mailto:[email protected]>ScienJus</a>
*/
public class ConsulConstants {

public static final String CONSUL_SERVICE_NAME_KEY = "consulServiceName";

public static final String HEARTBEAT_INTERVAL_KEY = "heartbeat.interval";

public static final String HEARTBEAT_CORE_SIZE_KEY = "heartbeat.coreSize";

public static final String LOOKUP_INTERVAL_KEY = "lookup.interval";

public static final String WATCH_TIMEOUT_KEY = "watch.timeout";

public static final String HEALTH_CHECK_TYPE_KEY = "healthCheck.type";

public static final String HEALTH_CHECK_TTL_KEY = "healthCheck.ttl";

public static final String HEALTH_CHECK_HOST_KEY = "healthCheck.host";

public static final String HEALTH_CHECK_PORT_KEY = "healthCheck.port";

public static final String HEALTH_CHECK_TIMEOUT_KEY = "healthCheck.timeout";

public static final String HEALTH_CHECK_INTERVAL_KEY = "healthCheck.interval";

public static final String HEALTH_CHECK_PROTOCOL_KEY = "healthCheck.protocol";

public static final String HEALTH_CHECK_PATH_KEY = "healthCheck.path";

public static final String HEALTH_CHECK_METHOD_KEY = "healthCheck.method";

public static final int DEFAULT_CONSUL_PORT = 8500;

public static final int DEFAULT_HEARTBEAT_INTERVAL = 3000;

public static final int DEFAULT_HEARTBEAT_CORE_SIZE = 1;

public static final int DEFAULT_LOOKUP_INTERVAL = 1000;

public static final int DEFAULT_WATCH_TIMEOUT = 5;

public static final ConsulRegistryProperties.HealthCheckType DEFAULT_HEALTH_CHECK_TYPE = ConsulRegistryProperties.HealthCheckType.TTL;

public static final String DEFAULT_HEALTH_CHECK_TTL = "10s";

public static final String DEFAULT_HEALTH_CHECK_TIMEOUT = "1s";

public static final String DEFAULT_HEALTH_CHECK_INTERVAL = "5s";

public static final String DEFAULT_HEALTH_CHECK_PROTOCOL = "http";

public static final String DEFAULT_HEALTH_CHECK_PATH = "/health";

public static final String DEFAULT_HEALTH_CHECK_METHOD = "GET";
}
Loading

0 comments on commit a20537e

Please sign in to comment.