AlbumenJ commented on code in PR #138: URL: https://github.com/apache/dubbo-spi-extensions/pull/138#discussion_r971581912
########## dubbo-registry-extensions/dubbo-registry-nameservice/src/main/java/org/apache/dubbo/registry/nameservice/NameServiceRegistry.java: ########## @@ -0,0 +1,233 @@ +/* + * 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.nameservice; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; +import java.util.Objects; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import java.util.concurrent.ThreadFactory; +import java.util.concurrent.TimeUnit; + +import org.apache.dubbo.common.URL; +import org.apache.dubbo.common.constants.CommonConstants; +import org.apache.dubbo.common.constants.RegistryConstants; +import org.apache.dubbo.common.logger.Logger; +import org.apache.dubbo.common.logger.LoggerFactory; +import org.apache.dubbo.registry.NotifyListener; +import org.apache.dubbo.registry.support.FailbackRegistry; + +import org.apache.rocketmq.client.exception.MQBrokerException; +import org.apache.rocketmq.client.exception.MQClientException; +import org.apache.rocketmq.common.TopicConfig; +import org.apache.rocketmq.common.constant.PermName; +import org.apache.rocketmq.common.protocol.body.ClusterInfo; +import org.apache.rocketmq.common.protocol.body.GroupList; +import org.apache.rocketmq.common.protocol.body.TopicList; +import org.apache.rocketmq.common.protocol.route.BrokerData; +import org.apache.rocketmq.common.protocol.route.QueueData; +import org.apache.rocketmq.common.protocol.route.TopicRouteData; +import org.apache.rocketmq.remoting.exception.RemotingException; +import org.apache.rocketmq.tools.admin.DefaultMQAdminExt; +import org.apache.rocketmq.tools.admin.DefaultMQAdminExtImpl; +import org.apache.rocketmq.tools.admin.MQAdminExt; + +public class NameServiceRegistry extends FailbackRegistry { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private ScheduledExecutorService scheduledExecutorService; + + private Map<URL, RegistryInfoWrapper> consumerRegistryInfoWrapperMap = new ConcurrentHashMap<>(); + + private MQAdminExt mqAdminExt; + + private boolean isNotRoute = true; + + private ClusterInfo clusterInfo; + + private TopicList topicList; + + private long timeoutMillis; + + private String instanceName; + + public NameServiceRegistry(URL url) { + super(url); + this.isNotRoute = !url.getParameter("route", false); + if (this.isNotRoute) { + return; + } + this.timeoutMillis = url.getParameter("timeoutMillis", 3000); + this.instanceName = url.getParameter("instanceName", "nameservic-registry"); + DefaultMQAdminExt clientConfig = new DefaultMQAdminExt(); + clientConfig.setNamesrvAddr(url.getAddress()); + clientConfig.setInstanceName(instanceName); + mqAdminExt = new DefaultMQAdminExtImpl(clientConfig, this.timeoutMillis); + try { + mqAdminExt.start(); + this.initBeasInfo(); + } catch (Exception e) { + throw new RuntimeException(e); + } + this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() { + @Override + public Thread newThread(Runnable r) { + return new Thread(r, "dubbo-registry-nameservice"); + } + }); + scheduledExecutorService.scheduleAtFixedRate(new Runnable() { + @Override + public void run() { + try { + NameServiceRegistry.this.initBeasInfo(); + + if (consumerRegistryInfoWrapperMap.isEmpty()) { + return; + } + for (Entry<URL, RegistryInfoWrapper> e : consumerRegistryInfoWrapperMap.entrySet()) { + List<URL> urls = new ArrayList<URL>(); + NameServiceRegistry.this.pullRoute(e.getValue().serviceName, e.getKey(), urls); + e.getValue().listener.notify(urls); + } + } catch (Exception e) { + logger.error("ScheduledTask pullRoute exception", e); + } + } + }, 1000 * 10, 3000 * 10, TimeUnit.MILLISECONDS); + } + + private void initBeasInfo() throws Exception { + this.clusterInfo = this.mqAdminExt.examineBrokerClusterInfo(); + this.topicList = this.mqAdminExt.fetchAllTopicList(); + } + + private URL createProviderURL(ServiceName serviceName, URL url, int queue) { + Map<String, String> parameters = url.getParameters(); Review Comment: how about `url.addParameters(Map<String, String>)` -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@dubbo.apache.org For additional commands, e-mail: notifications-h...@dubbo.apache.org