From b0c17fd2919abb0045cfd67a268f761e24155963 Mon Sep 17 00:00:00 2001 From: flystar32 Date: Wed, 13 Feb 2019 15:10:27 +0800 Subject: [PATCH] support heartbeat event --- .../alibaba/nacos/NacosDiscoveryClient.java | 105 ------------------ ...NacosDiscoveryClientAutoConfiguration.java | 46 -------- 2 files changed, 151 deletions(-) delete mode 100644 spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java delete mode 100644 spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java deleted file mode 100644 index 67e6b441b..000000000 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClient.java +++ /dev/null @@ -1,105 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed 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.springframework.cloud.alibaba.nacos; - -import com.alibaba.nacos.api.naming.pojo.Instance; -import com.alibaba.nacos.api.naming.pojo.ListView; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.springframework.cloud.client.ServiceInstance; -import org.springframework.cloud.client.discovery.DiscoveryClient; - -import java.util.*; - -/** - * @author xiaojing - * @author renhaojun - */ -public class NacosDiscoveryClient implements DiscoveryClient { - - private static final Logger LOGGER = LoggerFactory - .getLogger(NacosDiscoveryClient.class); - public static final String DESCRIPTION = "Spring Cloud Nacos Discovery Client"; - - private NacosDiscoveryProperties discoveryProperties; - - public NacosDiscoveryClient(NacosDiscoveryProperties discoveryProperties) { - this.discoveryProperties = discoveryProperties; - } - - @Override - public String description() { - return DESCRIPTION; - } - - @Override - public List getInstances(String serviceId) { - try { - List instances = discoveryProperties.namingServiceInstance() - .selectInstances(serviceId, true); - return hostToServiceInstanceList(instances, serviceId); - } - catch (Exception e) { - throw new RuntimeException( - "Can not get hosts from nacos server. serviceId: " + serviceId, e); - } - } - - private static ServiceInstance hostToServiceInstance(Instance instance, - String serviceId) { - NacosServiceInstance nacosServiceInstance = new NacosServiceInstance(); - nacosServiceInstance.setHost(instance.getIp()); - nacosServiceInstance.setPort(instance.getPort()); - nacosServiceInstance.setServiceId(serviceId); - Map metadata = new HashMap<>(); - metadata.put("instanceId", instance.getInstanceId()); - metadata.put("weight", instance.getWeight() + ""); - metadata.put("healthy", instance.isHealthy() + ""); - metadata.put("cluster", instance.getClusterName() + ""); - metadata.putAll(instance.getMetadata()); - nacosServiceInstance.setMetadata(metadata); - - if (metadata.containsKey("secure")) { - boolean secure = Boolean.parseBoolean(metadata.get("secure")); - nacosServiceInstance.setSecure(secure); - } - return nacosServiceInstance; - } - - private static List hostToServiceInstanceList( - List instances, String serviceId) { - List result = new ArrayList<>(instances.size()); - for (Instance instance : instances) { - result.add(hostToServiceInstance(instance, serviceId)); - } - return result; - } - - @Override - public List getServices() { - - try { - ListView services = discoveryProperties.namingServiceInstance() - .getServicesOfServer(1, Integer.MAX_VALUE); - return services.getData(); - } - catch (Exception e) { - LOGGER.error("get service name from nacos server fail,", e); - return Collections.emptyList(); - } - } -} diff --git a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java b/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java deleted file mode 100644 index 02f970783..000000000 --- a/spring-cloud-alibaba-nacos-discovery/src/main/java/org/springframework/cloud/alibaba/nacos/NacosDiscoveryClientAutoConfiguration.java +++ /dev/null @@ -1,46 +0,0 @@ -/* - * Copyright (C) 2018 the original author or authors. - * - * Licensed 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.springframework.cloud.alibaba.nacos; - -import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; -import org.springframework.boot.context.properties.EnableConfigurationProperties; -import org.springframework.cloud.client.discovery.DiscoveryClient; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; - -/** - * @author xiaojing - */ -@Configuration -@ConditionalOnMissingBean(DiscoveryClient.class) -@ConditionalOnNacosDiscoveryEnabled -@EnableConfigurationProperties -public class NacosDiscoveryClientAutoConfiguration { - - @Bean - public DiscoveryClient nacosDiscoveryClient( - NacosDiscoveryProperties discoveryProperties) { - return new NacosDiscoveryClient(discoveryProperties); - } - - @Bean - @ConditionalOnMissingBean - public NacosDiscoveryProperties nacosProperties() { - return new NacosDiscoveryProperties(); - } - -}