Skip to content

Commit

Permalink
Polish API
Browse files Browse the repository at this point in the history
  • Loading branch information
AlbumenJ committed Nov 3, 2024
1 parent bd2cf30 commit 731ecbb
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,4 +166,8 @@ public interface Constants {
String TARGET_PORT = "targetPort";

String TARGET_PROTOCOL = "targetProtocol";

String DNS_REGISTRY = "dns";

String DNS_DEFAULT_NAMESERVER = "DEFAULT_DNS_HOST";
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@
import java.util.Set;
import java.util.TreeSet;
import java.util.concurrent.locks.ReentrantLock;
import java.util.stream.Collectors;

import static org.apache.dubbo.common.constants.CommonConstants.ANY_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.CLUSTER_DOMAIN;
Expand All @@ -76,6 +77,7 @@
import static org.apache.dubbo.common.constants.CommonConstants.DEFAULT_MESH_PORT;
import static org.apache.dubbo.common.constants.CommonConstants.DubboProperty.DUBBO_IP_TO_REGISTRY;
import static org.apache.dubbo.common.constants.CommonConstants.INTERFACE_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.LAZY_CONNECT_KEY;
import static org.apache.dubbo.common.constants.CommonConstants.LOCALHOST_VALUE;
import static org.apache.dubbo.common.constants.CommonConstants.MESH_ENABLE;
import static org.apache.dubbo.common.constants.CommonConstants.METHODS_KEY;
Expand All @@ -101,6 +103,7 @@
import static org.apache.dubbo.registry.Constants.REGISTER_IP_KEY;
import static org.apache.dubbo.rpc.Constants.GENERIC_KEY;
import static org.apache.dubbo.rpc.Constants.LOCAL_PROTOCOL;
import static org.apache.dubbo.rpc.cluster.Constants.CLUSTER_STICKY_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.PEER_KEY;
import static org.apache.dubbo.rpc.cluster.Constants.REFER_KEY;

Expand Down Expand Up @@ -492,22 +495,7 @@ private T createProxy(Map<String, String> referenceParameters) {

meshModeHandleUrl(referenceParameters);

if (StringUtils.isNotEmpty(url)) {
// user specified URL, could be peer-to-peer address, or register center's address.
// parseUrl(referenceParameters);
URL u = URL.valueOf(url);
referenceParameters.put(Constants.TARGET_PROTOCOL, u.getProtocol());
referenceParameters.put(Constants.DNS_NAME, u.getHost());
referenceParameters.put(Constants.TARGET_PORT, String.valueOf(u.getPort()));
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol("dns");
registryConfig.setAddress("DEFAULT_DNS_HOST");
this.setRegistry(registryConfig);
aggregateUrlFromRegistry(referenceParameters);
} else {
// if protocols not in jvm checkRegistry
aggregateUrlFromRegistry(referenceParameters);
}
aggregateUrlFromRegistry(referenceParameters);
createInvoker();

if (logger.isInfoEnabled()) {
Expand Down Expand Up @@ -641,6 +629,33 @@ private void parseUrl(Map<String, String> referenceParameters) {
*/
private void aggregateUrlFromRegistry(Map<String, String> referenceParameters) {
checkRegistry();
if (StringUtils.isNoneEmpty(url)) {
// user specified URL, could be peer-to-peer address, or register center's address.
// parseUrl(referenceParameters);
URL u = URL.valueOf(url);
referenceParameters.put(Constants.TARGET_PROTOCOL, u.getProtocol());
referenceParameters.put(Constants.DNS_NAME, u.getHost());
referenceParameters.put(Constants.TARGET_PORT, String.valueOf(u.getPort()));
u.getParameters().forEach(referenceParameters::putIfAbsent);
referenceParameters.putIfAbsent(CLUSTER_STICKY_KEY, Boolean.TRUE.toString());
// TODO Triple should support lazy connect
referenceParameters.putIfAbsent(LAZY_CONNECT_KEY, Boolean.TRUE.toString());

if (!getRegistries().isEmpty()) {
// If the URL has been set, only support DNS registry
setRegistries(getRegistries().stream()
.filter(registryConfig -> Constants.DNS_REGISTRY.equals(registryConfig.getProtocol()))
.collect(Collectors.toList()));
}
if (getRegistries().isEmpty()) {
// If none of the registries are configured, use the default DNS registry
RegistryConfig registryConfig = new RegistryConfig();
registryConfig.setProtocol(Constants.DNS_REGISTRY);
registryConfig.setAddress(Constants.DNS_DEFAULT_NAMESERVER);
registryConfig.refresh();
setRegistry(registryConfig);
}
}
List<URL> us = ConfigValidationUtils.loadRegistries(this, false);
if (CollectionUtils.isNotEmpty(us)) {
for (URL u : us) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
package org.apache.dubbo.registry.dns;

import org.apache.dubbo.common.URL;
import org.apache.dubbo.common.constants.CommonConstants;
import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.apache.dubbo.common.url.component.DubboServiceAddressURL;
import org.apache.dubbo.common.url.component.ServiceConfigURL;
import org.apache.dubbo.common.utils.UrlUtils;
Expand All @@ -33,14 +36,15 @@
import java.util.concurrent.ConcurrentHashMap;

public class DnsRegistry extends CacheableFailbackRegistry {
private final Logger logger = LoggerFactory.getLogger(DnsRegistry.class);

private final DNSResolver dnsResolver;

private final Map<SubscribeKey, DnsResultListener> subscribeListeners = new ConcurrentHashMap<>();

public DnsRegistry(URL url) {
super(url);
if ("DEFAULT_DNS_HOST".equals(url.getHost())) {
if (Constants.DNS_DEFAULT_NAMESERVER.equals(url.getHost())) {
this.dnsResolver = new DNSResolver();
} else {
this.dnsResolver = new DNSResolver(url.getHost(), url.getPort());
Expand Down Expand Up @@ -69,18 +73,23 @@ public void doSubscribe(URL url, NotifyListener listener) {
throw new IllegalArgumentException("The value of 'dnsName' in the URL cannot be null.");
}
int targetPost = url.getParameter(Constants.TARGET_PORT, 50051);
String targetProtocol = url.getParameter(Constants.TARGET_PROTOCOL, "tri");
String targetProtocol = url.getParameter(Constants.TARGET_PROTOCOL, CommonConstants.TRIPLE);

DnsResultListener dnsResultListener =
subscribeListeners.computeIfAbsent(new SubscribeKey(url, listener), key -> inetAddresses -> {
logger.info("Resolved DNS name: " + dnsName + " to " + inetAddresses
+ ". Start to notify ServiceKey: " + url.getServiceKey());
List<URL> targetUrls = new ArrayList<>();
for (InetAddress inetAddress : inetAddresses) {
targetUrls.add(buildURL(url, targetProtocol, inetAddress.getHostAddress(), targetPost));
}
listener.notify(targetUrls);
logger.info("Notified ServiceKey: " + url.getServiceKey() + " with " + targetUrls.size()
+ " target URLs.");
});

dnsResolver.subscribe(dnsName, dnsResultListener);
logger.info("Subscribed DNS name: " + dnsName + " with ServiceKey: " + url.getServiceKey());
}

private URL buildURL(URL consumerURL, String protocol, String host, int port) {
Expand All @@ -90,15 +99,18 @@ private URL buildURL(URL consumerURL, String protocol, String host, int port) {

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
String dnsName = url.getParameter(Constants.DNS_NAME);
DnsResultListener dnsResultListener = subscribeListeners.remove(new SubscribeKey(url, listener));
if (dnsResultListener != null) {
dnsResolver.unsubscribe(url.getParameter(Constants.DNS_NAME), dnsResultListener);
dnsResolver.unsubscribe(dnsName, dnsResultListener);
logger.info("Unsubscribed DNS name: " + dnsName + " with ServiceKey: " + url.getServiceKey());
}
}

@Override
public void destroy() {
dnsResolver.stop();
logger.info("DNS resolver stopped.");
}

@Override
Expand Down

0 comments on commit 731ecbb

Please sign in to comment.