Skip to content

Commit

Permalink
fix empty protocol in consul registry (#4354)
Browse files Browse the repository at this point in the history
fixes #4294
  • Loading branch information
cvictory authored and chickenlj committed Jun 26, 2019
1 parent e3c35f2 commit 6b7d118
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -237,12 +237,12 @@ private static void deserializeInternal(Object result, JavaBeanDescriptor beanDe
for (Map.Entry<Object, Object> entry : beanDescriptor) {
Object key = entry.getKey();
Object value = entry.getValue();
if (key != null && key instanceof JavaBeanDescriptor) {
if (key instanceof JavaBeanDescriptor) {
JavaBeanDescriptor keyDescriptor = (JavaBeanDescriptor) entry.getKey();
key = instantiateForDeserialize(keyDescriptor, loader, cache);
deserializeInternal(key, keyDescriptor, loader, cache);
}
if (value != null && value instanceof JavaBeanDescriptor) {
if (value instanceof JavaBeanDescriptor) {
JavaBeanDescriptor valueDescriptor = (JavaBeanDescriptor) entry.getValue();
value = instantiateForDeserialize(valueDescriptor, loader, cache);
deserializeInternal(value, valueDescriptor, loader, cache);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object get(int index) {
*/
public boolean getBoolean(int index, boolean def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Boolean ? ((Boolean) tmp).booleanValue() : def;
return tmp instanceof Boolean ? ((Boolean) tmp).booleanValue() : def;
}

/**
Expand All @@ -59,7 +59,7 @@ public boolean getBoolean(int index, boolean def) {
*/
public int getInt(int index, int def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).intValue() : def;
return tmp instanceof Number ? ((Number) tmp).intValue() : def;
}

/**
Expand All @@ -71,7 +71,7 @@ public int getInt(int index, int def) {
*/
public long getLong(int index, long def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).longValue() : def;
return tmp instanceof Number ? ((Number) tmp).longValue() : def;
}

/**
Expand All @@ -83,7 +83,7 @@ public long getLong(int index, long def) {
*/
public float getFloat(int index, float def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).floatValue() : def;
return tmp instanceof Number ? ((Number) tmp).floatValue() : def;
}

/**
Expand All @@ -95,7 +95,7 @@ public float getFloat(int index, float def) {
*/
public double getDouble(int index, double def) {
Object tmp = mArray.get(index);
return tmp != null && tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
return tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public Object get(String key) {
*/
public boolean getBoolean(String key, boolean def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Boolean ? (Boolean) tmp : def;
return tmp instanceof Boolean ? (Boolean) tmp : def;
}

/**
Expand All @@ -59,7 +59,7 @@ public boolean getBoolean(String key, boolean def) {
*/
public int getInt(String key, int def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).intValue() : def;
return tmp instanceof Number ? ((Number) tmp).intValue() : def;
}

/**
Expand All @@ -71,7 +71,7 @@ public int getInt(String key, int def) {
*/
public long getLong(String key, long def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).longValue() : def;
return tmp instanceof Number ? ((Number) tmp).longValue() : def;
}

/**
Expand All @@ -83,7 +83,7 @@ public long getLong(String key, long def) {
*/
public float getFloat(String key, float def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).floatValue() : def;
return tmp instanceof Number ? ((Number) tmp).floatValue() : def;
}

/**
Expand All @@ -95,7 +95,7 @@ public float getFloat(String key, float def) {
*/
public double getDouble(String key, double def) {
Object tmp = mMap.get(key);
return tmp != null && tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
return tmp instanceof Number ? ((Number) tmp).doubleValue() : def;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-consumer/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,14 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-config-spring</artifactId>
Expand Down
8 changes: 8 additions & 0 deletions dubbo-demo/dubbo-demo-xml/dubbo-demo-xml-provider/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-multicast</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-registry-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-configcenter-zookeeper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-rpc-dubbo</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ public void setReg(boolean reg) {

@Override
public boolean equals(Object o) {
if (o == null || !(o instanceof ProviderInvokerWrapper)) {
if (!(o instanceof ProviderInvokerWrapper)) {
return false;
}
ProviderInvokerWrapper other = (ProviderInvokerWrapper) o;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
package org.apache.dubbo.registry.consul;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.URLBuilder;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.utils.CollectionUtils;
import org.apache.dubbo.common.utils.NamedThreadFactory;
import org.apache.dubbo.registry.NotifyListener;
import org.apache.dubbo.registry.support.FailbackRegistry;
Expand Down Expand Up @@ -49,6 +51,8 @@

import static java.util.concurrent.Executors.newCachedThreadPool;
import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.RegistryConstants.CATEGORY_KEY;
import static org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL;
import static org.apache.dubbo.registry.Constants.CONSUMER_PROTOCOL;
import static org.apache.dubbo.registry.Constants.PROVIDER_PROTOCOL;

Expand Down Expand Up @@ -136,12 +140,12 @@ public void doSubscribe(URL url, NotifyListener listener) {
Response<Map<String, List<String>>> response = getAllServices(-1, buildWatchTimeout(url));
index = response.getConsulIndex();
List<HealthService> services = getHealthServices(response.getValue());
urls = convert(services);
urls = convert(services, url);
} else {
String service = url.getServiceKey();
Response<List<HealthService>> response = getHealthServices(service, -1, buildWatchTimeout(url));
index = response.getConsulIndex();
urls = convert(response.getValue());
urls = convert(response.getValue(), url);
}

notify(url, listener, urls);
Expand Down Expand Up @@ -175,7 +179,7 @@ public List<URL> lookup(URL url) {
if (result == null || result.getValue() == null || result.getValue().isEmpty()) {
return new ArrayList<>();
} else {
return convert(result.getValue());
return convert(result.getValue(), url);
}
} catch (Throwable e) {
throw new RpcException("Failed to lookup " + url + " from consul " + getUrl() + ", cause: " + e.getMessage(), e);
Expand Down Expand Up @@ -241,7 +245,10 @@ private boolean isProviderSide(URL url) {
return url.getProtocol().equals(PROVIDER_PROTOCOL);
}

private List<URL> convert(List<HealthService> services) {
private List<URL> convert(List<HealthService> services, URL consumerURL) {
if (CollectionUtils.isEmpty(services)) {
return emptyURL(consumerURL);
}
return services.stream()
.map(HealthService::getService)
.filter(Objects::nonNull)
Expand All @@ -252,6 +259,17 @@ private List<URL> convert(List<HealthService> services) {
.collect(Collectors.toList());
}

private List<URL> emptyURL(URL consumerURL) {
// No Category Parameter
URL empty = URLBuilder.from(consumerURL)
.setProtocol(EMPTY_PROTOCOL)
.removeParameter(CATEGORY_KEY)
.build();
List<URL> result = new ArrayList<URL>();
result.add(empty);
return result;
}

private NewService buildService(URL url) {
NewService service = new NewService();
service.setAddress(url.getHost());
Expand Down Expand Up @@ -318,7 +336,7 @@ private void processService() {
if (currentIndex != null && currentIndex > consulIndex) {
consulIndex = currentIndex;
List<HealthService> services = response.getValue();
List<URL> urls = convert(services);
List<URL> urls = convert(services, url);
for (NotifyListener listener : getSubscribed().get(url)) {
doNotify(url, listener, urls);
}
Expand All @@ -331,7 +349,7 @@ private void processServices() {
if (currentIndex != null && currentIndex > consulIndex) {
consulIndex = currentIndex;
List<HealthService> services = getHealthServices(response.getValue());
List<URL> urls = convert(services);
List<URL> urls = convert(services, url);
for (NotifyListener listener : getSubscribed().get(url)) {
doNotify(url, listener, urls);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ final class NettyHelper {

public static void setNettyLoggerFactory() {
InternalLoggerFactory factory = InternalLoggerFactory.getDefaultFactory();
if (factory == null || !(factory instanceof DubboLoggerFactory)) {
if (!(factory instanceof DubboLoggerFactory)) {
InternalLoggerFactory.setDefaultFactory(new DubboLoggerFactory());
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ private ByteBuf createDubboByteBuf() throws IOException {
}

private static boolean checkTelnetDecoded(Object msg) {
if (msg != null && msg instanceof String && !msg.toString().contains("Unsupported command:")) {
if (msg instanceof String && !msg.toString().contains("Unsupported command:")) {
return true;
}
return false;
Expand Down

0 comments on commit 6b7d118

Please sign in to comment.