From b21399b7764e2ba32c35c963df11bba82a885ebb Mon Sep 17 00:00:00 2001 From: zhanglong Date: Sun, 14 Jul 2019 21:52:35 +0800 Subject: [PATCH 1/7] dns module init --- distribution/release-dns.xml | 75 ++++++++++++ dns/pom.xml | 66 +++++++++++ .../java/com/alibaba/nacos/dns/DnsApp.java | 26 ++++ .../nacos/dns/config/DnsProperties.java | 48 ++++++++ .../nacos/dns/constant/DnsConstants.java | 29 +++++ .../nacos/dns/controller/DnsController.java | 56 +++++++++ .../dns/controller/RecordController.java | 34 ++++++ .../dns/controller/SwitchController.java | 33 ++++++ .../nacos/dns/core/DefaultDnsSource.java | 112 ++++++++++++++++++ .../com/alibaba/nacos/dns/core/DnsSource.java | 39 ++++++ .../nacos/dns/dto/UpdateDomainDto.java | 25 ++++ .../exception/DomainNotFoundException.java | 21 ++++ .../nacos/dns/generator/DomainGenerator.java | 24 ++++ .../generator/DomainGeneratorDelegate.java | 38 ++++++ ...faultGroupAndNameSpaceDomainGenerator.java | 38 ++++++ .../impl/DefaultGroupDomainGenerator.java | 37 ++++++ .../impl/DefaultNamespaceDomainGenerator.java | 37 ++++++ .../generator/impl/FullDomainGenerator.java | 35 ++++++ .../dns/listener/ServiceChangeListener.java | 35 ++++++ .../alibaba/nacos/dns/record/BaseRecord.java | 37 ++++++ .../alibaba/nacos/dns/record/CNameRecord.java | 28 +++++ .../nacos/dns/record/InstanceInfo.java | 36 ++++++ .../alibaba/nacos/dns/record/RecordType.java | 17 +++ .../alibaba/nacos/dns/record/SrvRecord.java | 20 ++++ .../alibaba/nacos/dns/record/TxtRecord.java | 28 +++++ .../nacos/dns/service/DomainService.java | 24 ++++ .../nacos/dns/service/DomainServiceImpl.java | 55 +++++++++ .../META-INF/logback/nacos-included.xml | 32 +++++ .../nacos/naming/core/ServiceManager.java | 3 + .../nacos/naming/core/event/ServiceEvent.java | 28 +++++ pom.xml | 1 + 31 files changed, 1117 insertions(+) create mode 100644 distribution/release-dns.xml create mode 100644 dns/pom.xml create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java create mode 100644 dns/src/main/resources/META-INF/logback/nacos-included.xml create mode 100644 naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java diff --git a/distribution/release-dns.xml b/distribution/release-dns.xml new file mode 100644 index 000000000..f737ef899 --- /dev/null +++ b/distribution/release-dns.xml @@ -0,0 +1,75 @@ + + + + dns + false + + dir + tar.gz + zip + + + + ../ + + README.md + + + + + + conf/** + benchmark/* + + + + + + bin/* + + 0755 + + + + + + LICENSE-BIN + LICENSE + + + NOTICE-BIN + NOTICE + + + + + + true + + com.alibaba.nacos:nacos-dns + + + lib/ + false + + + lib/ + + + + + + diff --git a/dns/pom.xml b/dns/pom.xml new file mode 100644 index 000000000..5eafb4b2a --- /dev/null +++ b/dns/pom.xml @@ -0,0 +1,66 @@ + + + + nacos-all + com.alibaba.nacos + 1.1.0 + + 4.0.0 + + nacos-dns + jar + + UTF-8 + 1.8 + 1.8 + + + + com.alibaba.nacos + nacos-naming + + + + org.springframework.boot + spring-boot-configuration-processor + true + + + + + + org.apache.maven.plugins + maven-assembly-plugin + + + + com.alibaba.nacos.naming.DnsApp + + + + jar-with-dependencies + + + + + + + src/main/resources + + + + + + + + + + org.codehaus.mojo + findbugs-maven-plugin + 3.0.4 + + + + diff --git a/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java b/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java new file mode 100644 index 000000000..1e0a1cc41 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java @@ -0,0 +1,26 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class DnsApp { + public static void main(String[] args) { + SpringApplication.run(DnsApp.class, args); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java b/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java new file mode 100644 index 000000000..c246dd3ac --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java @@ -0,0 +1,48 @@ +package com.alibaba.nacos.dns.config; + +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.stereotype.Component; + +import java.io.Serializable; + +@Component +@ConfigurationProperties(prefix = "nacos.dns") +public class DnsProperties implements Serializable { + + private int defaultCacheTime = 10; + private String upstreamServersForDomainSuffixMap; + private String defaultUpstreamServer; + private boolean ednsEnabled = false; + + public int getDefaultCacheTime() { + return defaultCacheTime; + } + + public void setDefaultCacheTime(int defaultCacheTime) { + this.defaultCacheTime = defaultCacheTime; + } + + public String getUpstreamServersForDomainSuffixMap() { + return upstreamServersForDomainSuffixMap; + } + + public void setUpstreamServersForDomainSuffixMap(String upstreamServersForDomainSuffixMap) { + this.upstreamServersForDomainSuffixMap = upstreamServersForDomainSuffixMap; + } + + public String getDefaultUpstreamServer() { + return defaultUpstreamServer; + } + + public void setDefaultUpstreamServer(String defaultUpstreamServer) { + this.defaultUpstreamServer = defaultUpstreamServer; + } + + public boolean isEdnsEnabled() { + return ednsEnabled; + } + + public void setEdnsEnabled(boolean ednsEnabled) { + this.ednsEnabled = ednsEnabled; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java b/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java new file mode 100644 index 000000000..1a1dbebc3 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2019 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 com.alibaba.nacos.dns.constant; + +import com.alibaba.nacos.naming.misc.UtilsAndCommons; + +public class DnsConstants { + + public static final String DEFAULT_CACHE_TIME_KEY = "defaultCacheTime"; + public static final String UPSTREAM_SERVERS_FOR_DOMAIN_SUFFIX_MAP_KEY = "upstreamServersForDomainSuffixMap"; + public static final String DEFAULT_UPSTREAM_SERVER_KEY = "defaultUpstreamServer"; + public static final String EDNS_ENABLED_KEY = "ednsEnabled"; + public static final String CNAME_KEY = "CNAME"; + /** + * the url for dns + */ + public static final String NACOS_DNS_CONTEXT = + UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/dns"; +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java new file mode 100644 index 000000000..59adb5a09 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.controller; + +import com.alibaba.nacos.dns.dto.UpdateDomainDto; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import com.alibaba.nacos.dns.service.DomainService; +import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; + +@RestController +@RequestMapping(value = NACOS_DNS_CONTEXT + "/domain") +public class DnsController { + + final DomainService domainService; + + public DnsController(DomainService domainService) { + this.domainService = domainService; + } + + @GetMapping("/{domainName}") + public ResponseEntity getDomain(@PathVariable("domainName") String domainName) { + + try { + return ResponseEntity.ok().body(domainService.getConfig(domainName)); + } catch (Exception e) { + if (e instanceof DomainNotFoundException) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body("domainName=" + domainName + " not found."); + } else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("get domain failed"); + } + } + + } + + @PutMapping("/{domainName}") + public ResponseEntity updateDomain(@PathVariable("domainName") String domainName, + @RequestBody UpdateDomainDto updateDomainDto) { + + return null; + } + +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java new file mode 100644 index 000000000..2f6d18cb0 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java @@ -0,0 +1,34 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.controller; + +import com.alibaba.nacos.dns.record.RecordType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; + +@RestController +@RequestMapping(value = NACOS_DNS_CONTEXT + "/record") +public class RecordController { + + @GetMapping("/{domainName}/{type}") + public ResponseEntity getRecord(@PathVariable("domainName") String domainName, + @PathVariable(value = "type", required = false) RecordType recordType) { + + return null; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java new file mode 100644 index 000000000..d9ee25e88 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.controller; + +import com.alibaba.nacos.dns.record.RecordType; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.*; + +import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; + +@RestController +@RequestMapping(value = NACOS_DNS_CONTEXT + "/switches") +public class SwitchController { + + @GetMapping("/{domainName}") + public ResponseEntity getSwitch(@PathVariable("domainName") String domainName, + @PathVariable(value = "type", required = false) RecordType recordType) { + + return null; + } + + +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java b/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java new file mode 100644 index 000000000..d9b572edb --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java @@ -0,0 +1,112 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.core; + +import com.alibaba.nacos.dns.config.DnsProperties; +import com.alibaba.nacos.dns.generator.DomainGeneratorDelegate; +import com.alibaba.nacos.naming.core.Service; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.CopyOnWriteArrayList; + +import static com.alibaba.nacos.dns.constant.DnsConstants.*; + +@Component +public class DefaultDnsSource implements DnsSource, InitializingBean { + private final Map serviceMap = new ConcurrentHashMap<>(); + private final Map systemConfig = new ConcurrentHashMap<>(); + private final Map domainCacheTime = new ConcurrentHashMap<>(); + private final Map> domainForMappingName = new ConcurrentHashMap<>(); + private final Map mappingNameForDomain = new ConcurrentHashMap<>(); + + private final DomainGeneratorDelegate domainGeneratorDelegate; + private final DnsProperties dnsProperties; + + public DefaultDnsSource(DomainGeneratorDelegate domainGeneratorDelegate, DnsProperties dnsProperties) { + this.domainGeneratorDelegate = domainGeneratorDelegate; + this.dnsProperties = dnsProperties; + } + + @Override + public void putService(Service service) { + + List domains = domainGeneratorDelegate.create(service); + for (String domain : domains) { + serviceMap.put(domain, service); + } + } + + @Override + public Service getServiceByDomainName(String domain) { + + return serviceMap.get(domain); + } + + @Override + public Map getSystemConfig() { + return Collections.unmodifiableMap(systemConfig); + } + + @Override + public void updateSystemConfig(String key, String value) { + systemConfig.put(key, value); + } + + @Override + public void updateCacheTime(String domain, Integer cacheTime) { + domainCacheTime.put(domain, cacheTime); + } + + @Override + public int getCacheTime(String domain) { + + return domainCacheTime.getOrDefault(domain, Integer.valueOf(systemConfig.get(DEFAULT_CACHE_TIME_KEY))); + } + + @Override + public void putMappingName(String domain, String cName) { + if (mappingNameForDomain.containsKey(cName)) { + + mappingNameForDomain.put(cName, domain); + CopyOnWriteArrayList mappingNames = + domainForMappingName.getOrDefault(domain, new CopyOnWriteArrayList<>()); + domainForMappingName.putIfAbsent(domain, mappingNames); + mappingNames.add(cName); + } + } + + @Override + public List getMappingName(String domain) { + return Collections.unmodifiableList(domainForMappingName.getOrDefault(domain, new CopyOnWriteArrayList<>())); + } + + @Override + public Service getServiceByCname(String cName) { + String domainName = mappingNameForDomain.get(cName); + return serviceMap.get(domainName); + } + + @Override + public void afterPropertiesSet() throws Exception { + systemConfig.put(DEFAULT_CACHE_TIME_KEY, String.valueOf(dnsProperties.getDefaultCacheTime())); + systemConfig.put(UPSTREAM_SERVERS_FOR_DOMAIN_SUFFIX_MAP_KEY, + dnsProperties.getUpstreamServersForDomainSuffixMap()); + systemConfig.put(DEFAULT_UPSTREAM_SERVER_KEY, dnsProperties.getDefaultUpstreamServer()); + systemConfig.put(EDNS_ENABLED_KEY, String.valueOf(dnsProperties.isEdnsEnabled())); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java b/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java new file mode 100644 index 000000000..784156fd2 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java @@ -0,0 +1,39 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.core; + +import com.alibaba.nacos.naming.core.Service; + +import java.util.List; +import java.util.Map; + +public interface DnsSource { + void putService(Service service); + + Service getServiceByDomainName(String domain); + + Map getSystemConfig(); + + void updateSystemConfig(String key, String value); + + void updateCacheTime(String domain, Integer cacheTime); + + int getCacheTime(String domain); + + void putMappingName(String domain, String cName); + + List getMappingName(String domain); + + Service getServiceByCname(String cName); + +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java b/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java new file mode 100644 index 000000000..36ffa4d44 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java @@ -0,0 +1,25 @@ +package com.alibaba.nacos.dns.dto; + +import java.io.Serializable; + +public class UpdateDomainDto implements Serializable { + + private Integer cacheTime; + private String cName; + + public Integer getCacheTime() { + return cacheTime; + } + + public void setCacheTime(Integer cacheTime) { + this.cacheTime = cacheTime; + } + + public String getcName() { + return cName; + } + + public void setcName(String cName) { + this.cName = cName; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java b/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java new file mode 100644 index 000000000..c87094803 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java @@ -0,0 +1,21 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.exception; + + +public class DomainNotFoundException extends RuntimeException { + + public DomainNotFoundException(String message) { + super(message); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java new file mode 100644 index 000000000..0807b328d --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java @@ -0,0 +1,24 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.generator; + +import com.alibaba.nacos.naming.core.Service; + +public interface DomainGenerator { + + String DOMAIN_SUFFIX = ".nacos.local"; + + public boolean isMatch(Service service); + + public String create(Service service); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java new file mode 100644 index 000000000..0ed904c03 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java @@ -0,0 +1,38 @@ +package com.alibaba.nacos.dns.generator; + +import com.alibaba.nacos.naming.core.Service; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.util.ArrayList; +import java.util.List; + +@Component +public class DomainGeneratorDelegate implements ApplicationContextAware, InitializingBean { + + private ApplicationContext applicationContext; + private final List generators = new ArrayList<>(); + + public List create(Service service) { + List domainNames = new ArrayList<>(generators.size()); + for (DomainGenerator domainGenerator : generators) { + if (domainGenerator.isMatch(service)) { + domainNames.add(domainGenerator.create(service)); + } + } + return domainNames; + } + + @Override + public void afterPropertiesSet() throws Exception { + generators.addAll(applicationContext.getBeansOfType(DomainGenerator.class).values()); + } + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java new file mode 100644 index 000000000..7553b03a9 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java @@ -0,0 +1,38 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.generator.impl; + +import com.alibaba.nacos.dns.generator.DomainGenerator; +import com.alibaba.nacos.naming.core.Service; +import org.springframework.stereotype.Component; + +import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP; +import static com.alibaba.nacos.api.common.Constants.DEFAULT_NAMESPACE_ID; + +@Component +public class DefaultGroupAndNameSpaceDomainGenerator implements DomainGenerator { + + private static final String FULL_DOMAIN_MAPPING = "%s.sn" + DOMAIN_SUFFIX; + + @Override + public boolean isMatch(Service service) { + + return DEFAULT_GROUP.equals(service.getGroupName()) && DEFAULT_NAMESPACE_ID.equals(service.getNamespaceId()); + } + + @Override + public String create(Service service) { + + return String.format(FULL_DOMAIN_MAPPING, service.getName()); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java new file mode 100644 index 000000000..d196a48de --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.generator.impl; + +import com.alibaba.nacos.dns.generator.DomainGenerator; +import com.alibaba.nacos.naming.core.Service; +import org.springframework.stereotype.Component; + +import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP; + +@Component +public class DefaultGroupDomainGenerator implements DomainGenerator { + + private static final String FULL_DOMAIN_MAPPING = "%s.sn.%s.ns" + DOMAIN_SUFFIX; + + @Override + public boolean isMatch(Service service) { + + return DEFAULT_GROUP.equals(service.getGroupName()); + } + + @Override + public String create(Service service) { + + return String.format(FULL_DOMAIN_MAPPING, service.getName(), service.getNamespaceId()); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java new file mode 100644 index 000000000..ae7646956 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.generator.impl; + +import com.alibaba.nacos.dns.generator.DomainGenerator; +import com.alibaba.nacos.naming.core.Service; +import org.springframework.stereotype.Component; + +import static com.alibaba.nacos.api.common.Constants.DEFAULT_NAMESPACE_ID; + +@Component +public class DefaultNamespaceDomainGenerator implements DomainGenerator { + + private static final String FULL_DOMAIN_MAPPING = "%s.sn.%s.gn" + DOMAIN_SUFFIX; + + @Override + public boolean isMatch(Service service) { + + return DEFAULT_NAMESPACE_ID.equals(service.getNamespaceId()); + } + + @Override + public String create(Service service) { + + return String.format(FULL_DOMAIN_MAPPING, service.getName(), service.getGroupName()); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java new file mode 100644 index 000000000..1b8e63848 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java @@ -0,0 +1,35 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.generator.impl; + +import com.alibaba.nacos.dns.generator.DomainGenerator; +import com.alibaba.nacos.naming.core.Service; +import org.springframework.stereotype.Component; + +@Component +public class FullDomainGenerator implements DomainGenerator { + + private static final String FULL_DOMAIN_MAPPING = "%s.sn.%s.gn.%s.ns" + DOMAIN_SUFFIX; + + @Override + public boolean isMatch(Service service) { + + return true; + } + + @Override + public String create(Service service) { + + return String.format(FULL_DOMAIN_MAPPING, service.getName(), service.getGroupName(), service.getNamespaceId()); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java b/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java new file mode 100644 index 000000000..5561f613b --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java @@ -0,0 +1,35 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.listener; + +import com.alibaba.nacos.dns.core.DnsSource; +import com.alibaba.nacos.naming.core.Service; +import com.alibaba.nacos.naming.core.event.ServiceEvent; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; + +@Component +public class ServiceChangeListener { + + final DnsSource dnsSource; + + public ServiceChangeListener(DnsSource dnsSource) { + this.dnsSource = dnsSource; + } + + @EventListener + public void serviceChange(ServiceEvent event) { + Service service = (Service)event.getSource(); + dnsSource.putService(service); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java new file mode 100644 index 000000000..e8946a279 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java @@ -0,0 +1,37 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +import java.io.Serializable; + +public abstract class BaseRecord implements Serializable { + + private RecordType record; + private String name; + + public RecordType getRecord() { + return record; + } + + public void setRecord(RecordType record) { + this.record = record; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java new file mode 100644 index 000000000..fff4ccd32 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java @@ -0,0 +1,28 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +import java.util.ArrayList; +import java.util.List; + +public class CNameRecord extends BaseRecord { + private List cNames = new ArrayList<>(); + + public List getcNames() { + return cNames; + } + + public void setcNames(List cNames) { + this.cNames = cNames; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java b/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java new file mode 100644 index 000000000..0e80e078e --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java @@ -0,0 +1,36 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +import java.io.Serializable; + +public class InstanceInfo implements Serializable { + private String address; + private int port; + + public String getAddress() { + return address; + } + + public void setAddress(String address) { + this.address = address; + } + + public int getPort() { + return port; + } + + public void setPort(int port) { + this.port = port; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java b/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java new file mode 100644 index 000000000..3889d1d2d --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java @@ -0,0 +1,17 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +public enum RecordType { + A, SRV, TXT, CNAME +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java new file mode 100644 index 000000000..952ce1fb6 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java @@ -0,0 +1,20 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +import java.util.ArrayList; +import java.util.List; + +public class SrvRecord extends BaseRecord { + private List instances = new ArrayList<>(); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java new file mode 100644 index 000000000..0effb7aba --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java @@ -0,0 +1,28 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +import java.util.HashMap; +import java.util.Map; + +public class TxtRecord extends BaseRecord { + private Map metadata = new HashMap<>(); + + public Map getMetadata() { + return metadata; + } + + public void setMetadata(Map metadata) { + this.metadata = metadata; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java new file mode 100644 index 000000000..ff33f6fb6 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java @@ -0,0 +1,24 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service; + +import com.alibaba.nacos.dns.dto.UpdateDomainDto; + +import java.util.Map; + +public interface DomainService { + + public Map getConfig(String domain); + + public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java new file mode 100644 index 000000000..b98d25e88 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java @@ -0,0 +1,55 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service; + +import com.alibaba.nacos.dns.constant.DnsConstants; +import com.alibaba.nacos.dns.core.DnsSource; +import com.alibaba.nacos.dns.dto.UpdateDomainDto; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import org.apache.commons.lang3.StringUtils; +import org.springframework.stereotype.Service; + +import java.util.HashMap; +import java.util.Map; + +@Service +public class DomainServiceImpl implements DomainService { + + final DnsSource dnsSource; + + public DomainServiceImpl(DnsSource dnsSource) { + this.dnsSource = dnsSource; + } + + @Override + public Map getConfig(String domain) { + com.alibaba.nacos.naming.core.Service service = dnsSource.getServiceByCname(domain); + Map config = new HashMap<>(); + if (service == null) { + service = dnsSource.getServiceByDomainName(domain); + config.put(DnsConstants.CNAME_KEY, StringUtils.join(dnsSource.getMappingName(domain), ",")); + } + if (service == null) { + throw new DomainNotFoundException("domain name " + domain + "not found"); + } + int cacheTime = dnsSource.getCacheTime(domain); + config.put(DnsConstants.DEFAULT_CACHE_TIME_KEY, String.valueOf(cacheTime)); + config.putAll(service.getMetadata()); + return config; + } + + @Override + public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto) { + return false; + } +} diff --git a/dns/src/main/resources/META-INF/logback/nacos-included.xml b/dns/src/main/resources/META-INF/logback/nacos-included.xml new file mode 100644 index 000000000..f80a989ca --- /dev/null +++ b/dns/src/main/resources/META-INF/logback/nacos-included.xml @@ -0,0 +1,32 @@ + + + + + + + + ${LOG_HOME}/naming-server.log + true + + ${LOG_HOME}/dns.log.%d{yyyy-MM-dd}.%i + 2GB + 15 + 7GB + true + + + %date %level %msg%n%n + UTF-8 + + + + + + + + + + + + diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index e45ec2b12..48ded03f9 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -21,6 +21,7 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.servers.Server; import com.alibaba.nacos.naming.consistency.ConsistencyService; @@ -29,6 +30,7 @@ import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; +import com.alibaba.nacos.naming.core.event.ServiceEvent; import com.alibaba.nacos.naming.misc.*; import com.alibaba.nacos.naming.push.PushService; import org.apache.commons.lang3.ArrayUtils; @@ -385,6 +387,7 @@ public class ServiceManager implements RecordListener { public void addOrReplaceService(Service service) throws NacosException { consistencyService.put(KeyBuilder.buildServiceMetaKey(service.getNamespaceId(), service.getName()), service); + SpringContext.getAppContext().publishEvent(new ServiceEvent(service)); } public void createEmptyService(String namespaceId, String serviceName, boolean local) throws NacosException { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java new file mode 100644 index 000000000..52a8bad62 --- /dev/null +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java @@ -0,0 +1,28 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.naming.core.event; + +import com.alibaba.nacos.naming.core.Service; +import org.springframework.context.ApplicationEvent; + +public class ServiceEvent extends ApplicationEvent { + + /** + * Create a new ApplicationEvent. + * + * @param source the object on which the event initially occurred (never {@code null}) + */ + public ServiceEvent(Service source) { + super(source); + } +} diff --git a/pom.xml b/pom.xml index ec713d995..b876fcab3 100644 --- a/pom.xml +++ b/pom.xml @@ -437,6 +437,7 @@ distribution console cmdb + dns From 225452a3ab25ed79c4950dfe0edb39d3a1400127 Mon Sep 17 00:00:00 2001 From: zhanglong Date: Sun, 14 Jul 2019 21:55:00 +0800 Subject: [PATCH 2/7] dns module init --- distribution/pom.xml | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/distribution/pom.xml b/distribution/pom.xml index 57ff500bd..5d400bf64 100644 --- a/distribution/pom.xml +++ b/distribution/pom.xml @@ -99,6 +99,38 @@ ans + + release-dns + + + com.alibaba.nacos + nacos-dns + + + + + + maven-assembly-plugin + + + release-dns + + single + + package + + + release-dns.xml + + false + + + + + + nacos-dns + + release-address From a9b4c040faa10f16ee0261f543061947032aa0d8 Mon Sep 17 00:00:00 2001 From: zhanglong Date: Sun, 28 Jul 2019 19:00:05 +0800 Subject: [PATCH 3/7] Complete dns module coding --- console/pom.xml | 2 +- .../src/main/resources/application.properties | 2 +- dns/pom.xml | 7 +- .../java/com/alibaba/nacos/dns/DnsApp.java | 18 +-- .../nacos/dns/config/DnsProperties.java | 70 +++++++++++- .../nacos/dns/constant/DnsConstants.java | 7 +- .../nacos/dns/controller/DnsController.java | 24 +++- .../dns/controller/RecordController.java | 23 +++- .../dns/controller/SwitchController.java | 44 +++++++- .../nacos/dns/core/DefaultDnsSource.java | 43 ++++--- .../com/alibaba/nacos/dns/core/DnsSource.java | 106 +++++++++++++++++- .../nacos/dns/dto/UpdateDomainDto.java | 3 + .../exception/DomainNotFoundException.java | 4 +- .../SystemEntryNotFoundException.java | 23 ++++ .../nacos/dns/generator/DomainGenerator.java | 25 +++++ .../generator/DomainGeneratorDelegate.java | 3 + ...faultGroupAndNameSpaceDomainGenerator.java | 9 +- .../impl/DefaultGroupDomainGenerator.java | 9 +- .../impl/DefaultNamespaceDomainGenerator.java | 7 +- .../generator/impl/FullDomainGenerator.java | 12 +- .../dns/listener/ServiceChangeListener.java | 14 ++- .../com/alibaba/nacos/dns/misc/Loggers.java | 28 +++++ .../{CNameRecord.java => AddressRecord.java} | 15 ++- .../alibaba/nacos/dns/record/BaseRecord.java | 3 + .../nacos/dns/record/CanonicalNameRecord.java | 31 +++++ .../nacos/dns/record/InstanceInfo.java | 3 + .../alibaba/nacos/dns/record/RecordType.java | 21 +++- .../alibaba/nacos/dns/record/SrvRecord.java | 11 ++ .../alibaba/nacos/dns/record/TxtRecord.java | 13 ++- .../nacos/dns/record/conversion/Record.java | 29 +++++ .../record/conversion/RecordConversion.java | 34 ++++++ .../conversion/RecordConversionFactory.java | 33 ++++++ .../RecordConversionFactoryImpl.java | 56 +++++++++ .../impl/AbstractRecordConversion.java | 43 +++++++ .../impl/AddressRecordConversion.java | 43 +++++++ .../impl/CanonicalNameRecordConversion.java | 33 ++++++ .../conversion/impl/SrvRecordConversion.java | 47 ++++++++ .../conversion/impl/TxtRecordConversion.java | 48 ++++++++ .../nacos/dns/service/DomainService.java | 20 ++++ .../nacos/dns/service/DomainServiceImpl.java | 55 --------- .../nacos/dns/service/RecordService.java | 33 ++++++ .../nacos/dns/service/SwitchService.java | 41 +++++++ .../dns/service/impl/DomainServiceImpl.java | 70 ++++++++++++ .../dns/service/impl/RecordServiceImpl.java | 43 +++++++ .../dns/service/impl/SwitchServiceImpl.java | 51 +++++++++ .../META-INF/logback/nacos-included.xml | 4 +- .../nacos/naming/core/ServiceManager.java | 3 +- .../nacos/naming/core/event/ServiceEvent.java | 5 +- pom.xml | 5 + 49 files changed, 1147 insertions(+), 129 deletions(-) create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java rename dns/src/main/java/com/alibaba/nacos/dns/record/{CNameRecord.java => AddressRecord.java} (71%) create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java create mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java diff --git a/console/pom.xml b/console/pom.xml index 9f80b009d..df1be7835 100644 --- a/console/pom.xml +++ b/console/pom.xml @@ -41,7 +41,7 @@ ${project.groupId} - nacos-naming + nacos-dns diff --git a/console/src/main/resources/application.properties b/console/src/main/resources/application.properties index 64c42676e..b659cf3b4 100644 --- a/console/src/main/resources/application.properties +++ b/console/src/main/resources/application.properties @@ -37,4 +37,4 @@ server.tomcat.basedir= #security.basic.enabled=false #nacos.security.ignore.urls=/** -nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/** +nacos.security.ignore.urls=/,/**/*.css,/**/*.js,/**/*.html,/**/*.map,/**/*.svg,/**/*.png,/**/*.ico,/console-fe/public/**,/v1/auth/login,/v1/console/health/**,/v1/cs/**,/v1/ns/**,/v1/cmdb/**,/actuator/**,/v1/console/server/**,/v1/dns/domain/** diff --git a/dns/pom.xml b/dns/pom.xml index 5eafb4b2a..30b27391a 100644 --- a/dns/pom.xml +++ b/dns/pom.xml @@ -6,6 +6,7 @@ nacos-all com.alibaba.nacos 1.1.0 + ../pom.xml 4.0.0 @@ -21,7 +22,11 @@ com.alibaba.nacos nacos-naming - + + com.ctrip.framework.apollo + apollo-client + 1.1.1 + org.springframework.boot spring-boot-configuration-processor diff --git a/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java b/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java index 1e0a1cc41..35c8b62ab 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java @@ -1,23 +1,23 @@ /* * Copyright 1999-2018 Alibaba Group Holding Ltd. * - * 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 + * 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 + * 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. + * 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.alibaba.nacos.dns; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; +/** + * @author paderlol + */ @SpringBootApplication public class DnsApp { public static void main(String[] args) { diff --git a/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java b/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java index c246dd3ac..79ac788a5 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java @@ -5,43 +5,109 @@ import org.springframework.stereotype.Component; import java.io.Serializable; + +/** + * @author paderlol + */ @Component @ConfigurationProperties(prefix = "nacos.dns") public class DnsProperties implements Serializable { + /** + * @description The Default cache time. + */ private int defaultCacheTime = 10; - private String upstreamServersForDomainSuffixMap; - private String defaultUpstreamServer; + /** + * @description The Upstream servers for domain suffix map. + */ + private String upstreamServersForDomainSuffixMap="null"; + /** + * @description The Default upstream server. + */ + private String defaultUpstreamServer="null"; + /** + * @description The Edns enabled. + */ private boolean ednsEnabled = false; + /** + * @return the default cache time + * @description default cache time TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public int getDefaultCacheTime() { return defaultCacheTime; } + /** + * @param defaultCacheTime the default cache time + * @description Sets default cache time TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public void setDefaultCacheTime(int defaultCacheTime) { this.defaultCacheTime = defaultCacheTime; } + /** + * @return the upstream servers for domain suffix map + * @description upstream servers for domain suffix map TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public String getUpstreamServersForDomainSuffixMap() { return upstreamServersForDomainSuffixMap; } + /** + * @param upstreamServersForDomainSuffixMap the upstream servers for domain suffix map + * @description Sets upstream servers for domain suffix map TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public void setUpstreamServersForDomainSuffixMap(String upstreamServersForDomainSuffixMap) { this.upstreamServersForDomainSuffixMap = upstreamServersForDomainSuffixMap; } + /** + * @return the default upstream server + * @description default upstream server TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public String getDefaultUpstreamServer() { return defaultUpstreamServer; } + /** + * @param defaultUpstreamServer the default upstream server + * @description Sets default upstream server TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public void setDefaultUpstreamServer(String defaultUpstreamServer) { this.defaultUpstreamServer = defaultUpstreamServer; } + /** + * Is edns enabled boolean. + * + * @return the boolean + * @description TODO(do something) + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public boolean isEdnsEnabled() { return ednsEnabled; } + /** + * @param ednsEnabled the edns enabled + * @description Sets edns enabled TODO(do something). + * @author zhanglong + * @date 2019年07月28日, 16:14:53 + */ public void setEdnsEnabled(boolean ednsEnabled) { this.ednsEnabled = ednsEnabled; } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java b/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java index 1a1dbebc3..6ef103cd1 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java @@ -14,6 +14,9 @@ package com.alibaba.nacos.dns.constant; import com.alibaba.nacos.naming.misc.UtilsAndCommons; +/** + * @author paderlol + */ public class DnsConstants { public static final String DEFAULT_CACHE_TIME_KEY = "defaultCacheTime"; @@ -21,9 +24,9 @@ public class DnsConstants { public static final String DEFAULT_UPSTREAM_SERVER_KEY = "defaultUpstreamServer"; public static final String EDNS_ENABLED_KEY = "ednsEnabled"; public static final String CNAME_KEY = "CNAME"; + public static final String GET_DOMAIN_FAILED = "Get domain failed"; /** * the url for dns */ - public static final String NACOS_DNS_CONTEXT = - UtilsAndCommons.NACOS_SERVER_CONTEXT + UtilsAndCommons.NACOS_SERVER_VERSION + "/dns"; + public static final String NACOS_DNS_CONTEXT = UtilsAndCommons.NACOS_SERVER_VERSION + "/dns"; } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java index 59adb5a09..ca7e2cffa 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java @@ -14,18 +14,23 @@ package com.alibaba.nacos.dns.controller; import com.alibaba.nacos.dns.dto.UpdateDomainDto; import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import com.alibaba.nacos.dns.misc.Loggers; import com.alibaba.nacos.dns.service.DomainService; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import static com.alibaba.nacos.dns.constant.DnsConstants.GET_DOMAIN_FAILED; import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; +/** + * @author paderlol + */ @RestController @RequestMapping(value = NACOS_DNS_CONTEXT + "/domain") public class DnsController { - final DomainService domainService; + private final DomainService domainService; public DnsController(DomainService domainService) { this.domainService = domainService; @@ -38,9 +43,10 @@ public class DnsController { return ResponseEntity.ok().body(domainService.getConfig(domainName)); } catch (Exception e) { if (e instanceof DomainNotFoundException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body("domainName=" + domainName + " not found."); + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); } else { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("get domain failed"); + Loggers.dnsLogger.error(GET_DOMAIN_FAILED,e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); } } @@ -50,7 +56,17 @@ public class DnsController { public ResponseEntity updateDomain(@PathVariable("domainName") String domainName, @RequestBody UpdateDomainDto updateDomainDto) { - return null; + try { + domainService.updateConfig(domainName, updateDomainDto); + return ResponseEntity.ok().build(); + } catch (Exception e) { + if (e instanceof DomainNotFoundException) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } else { + Loggers.dnsLogger.error(GET_DOMAIN_FAILED,e); + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); + } + } } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java index 2f6d18cb0..b6149c65c 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java @@ -12,23 +12,42 @@ */ package com.alibaba.nacos.dns.controller; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.service.RecordService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import static com.alibaba.nacos.dns.constant.DnsConstants.GET_DOMAIN_FAILED; import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; +/** + * @author paderlol + */ @RestController @RequestMapping(value = NACOS_DNS_CONTEXT + "/record") public class RecordController { + @Autowired + private RecordService recordService; + @GetMapping("/{domainName}/{type}") public ResponseEntity getRecord(@PathVariable("domainName") String domainName, @PathVariable(value = "type", required = false) RecordType recordType) { - - return null; + try { + return ResponseEntity.ok().body(recordService.getRecord(domainName, recordType)); + } catch (Exception e) { + if (e instanceof DomainNotFoundException) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); + } + } } + } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java index d9ee25e88..8e2122b9b 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java @@ -12,22 +12,56 @@ */ package com.alibaba.nacos.dns.controller; -import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import com.alibaba.nacos.dns.exception.SystemEntryNotFoundException; +import com.alibaba.nacos.dns.service.SwitchService; +import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; +import static com.alibaba.nacos.dns.constant.DnsConstants.GET_DOMAIN_FAILED; import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; +/** + * @author paderlol + */ @RestController @RequestMapping(value = NACOS_DNS_CONTEXT + "/switches") public class SwitchController { - @GetMapping("/{domainName}") - public ResponseEntity getSwitch(@PathVariable("domainName") String domainName, - @PathVariable(value = "type", required = false) RecordType recordType) { + private final SwitchService switchService; - return null; + public SwitchController(SwitchService switchService) { + this.switchService = switchService; } + @GetMapping("/{domainName}") + public ResponseEntity getSystemConfig(@PathVariable("domainName") String domainName) { + + try { + return ResponseEntity.ok().body(switchService.getSystemConfig(domainName)); + } catch (Exception e) { + if (e instanceof DomainNotFoundException) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); + } + } + } + + @PutMapping("/{entry}/{value}") + public ResponseEntity updateSystemConfig(@PathVariable("entry") String entry, @PathVariable("value") String value) { + + try { + switchService.updateSystemConfig(entry, value); + return ResponseEntity.ok().build(); + } catch (Exception e) { + if (e instanceof SystemEntryNotFoundException) { + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); + } else { + return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); + } + } + } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java b/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java index d9b572edb..6662d35a2 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java @@ -15,6 +15,7 @@ package com.alibaba.nacos.dns.core; import com.alibaba.nacos.dns.config.DnsProperties; import com.alibaba.nacos.dns.generator.DomainGeneratorDelegate; import com.alibaba.nacos.naming.core.Service; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.InitializingBean; import org.springframework.stereotype.Component; @@ -26,13 +27,16 @@ import java.util.concurrent.CopyOnWriteArrayList; import static com.alibaba.nacos.dns.constant.DnsConstants.*; +/** + * @author paderlol + */ @Component public class DefaultDnsSource implements DnsSource, InitializingBean { private final Map serviceMap = new ConcurrentHashMap<>(); private final Map systemConfig = new ConcurrentHashMap<>(); private final Map domainCacheTime = new ConcurrentHashMap<>(); - private final Map> domainForMappingName = new ConcurrentHashMap<>(); - private final Map mappingNameForDomain = new ConcurrentHashMap<>(); + private final Map> domainForCanonicalName = new ConcurrentHashMap<>(); + private final Map canonicalNameForDomain = new ConcurrentHashMap<>(); private final DomainGeneratorDelegate domainGeneratorDelegate; private final DnsProperties dnsProperties; @@ -79,30 +83,41 @@ public class DefaultDnsSource implements DnsSource, InitializingBean { } @Override - public void putMappingName(String domain, String cName) { - if (mappingNameForDomain.containsKey(cName)) { + public void putCanonicalName(String domain, String canonicalName) { + if (serviceMap.containsKey(domain)) { - mappingNameForDomain.put(cName, domain); + canonicalNameForDomain.put(canonicalName, domain); CopyOnWriteArrayList mappingNames = - domainForMappingName.getOrDefault(domain, new CopyOnWriteArrayList<>()); - domainForMappingName.putIfAbsent(domain, mappingNames); - mappingNames.add(cName); + domainForCanonicalName.getOrDefault(domain, new CopyOnWriteArrayList<>()); + domainForCanonicalName.putIfAbsent(domain, mappingNames); + mappingNames.add(canonicalName); } } @Override - public List getMappingName(String domain) { - return Collections.unmodifiableList(domainForMappingName.getOrDefault(domain, new CopyOnWriteArrayList<>())); + public List getCanonicalNameByDomain(String domain) { + return Collections.unmodifiableList(domainForCanonicalName.getOrDefault(domain, new CopyOnWriteArrayList<>())); } @Override - public Service getServiceByCname(String cName) { - String domainName = mappingNameForDomain.get(cName); - return serviceMap.get(domainName); + public Service getServiceByCanonicalName(String canonicalName) { + String domainName = canonicalNameForDomain.get(canonicalName); + + return StringUtils.isNotBlank(domainName) ? serviceMap.get(domainName) : null; } @Override - public void afterPropertiesSet() throws Exception { + public boolean isExistDomain(String domain) { + return serviceMap.containsKey(domain); + } + + @Override + public boolean isExistCanonicalName(String canonicalName) { + return canonicalNameForDomain.containsKey(canonicalName); + } + + @Override + public void afterPropertiesSet() { systemConfig.put(DEFAULT_CACHE_TIME_KEY, String.valueOf(dnsProperties.getDefaultCacheTime())); systemConfig.put(UPSTREAM_SERVERS_FOR_DOMAIN_SUFFIX_MAP_KEY, dnsProperties.getUpstreamServersForDomainSuffixMap()); diff --git a/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java b/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java index 784156fd2..b781fbedd 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java @@ -17,23 +17,123 @@ import com.alibaba.nacos.naming.core.Service; import java.util.List; import java.util.Map; +/** + * @author paderlol + * @date 2019年07月28日, 16:31:31 + */ public interface DnsSource { + /** + * Put service. + * + * @param service the service + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:31 + */ void putService(Service service); + /** + * Get service by domain name + * @param domain the domain + * @return the service by domain name + * @description service by domain name . + * @author paderlol + * @date 2019年07月28日, 16:31:31 + */ Service getServiceByDomainName(String domain); + /** + * Get system config + * @return the system config + * @description system config . + * @author paderlol + * @date 2019年07月28日, 16:31:31 + */ Map getSystemConfig(); + /** + * Update system config. + * + * @param key the key + * @param value the value + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:31 + */ void updateSystemConfig(String key, String value); + /** + * Update cache time. + * + * @param domain the domain + * @param cacheTime the cache time + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:31 + */ void updateCacheTime(String domain, Integer cacheTime); + /** + * Get cache time which is ttl + * @param domain the domain + * @return the cache time + * @description cache time . + * @author paderlol + * @date 2019年07月28日, 16:31:32 + */ int getCacheTime(String domain); - void putMappingName(String domain, String cName); + /** + * Put canonical name. + * + * @param domain the domain + * @param canonicalName the canonical name + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:32 + */ + void putCanonicalName(String domain, String canonicalName); - List getMappingName(String domain); + /** + * Get CNAME by domain + * @param domain the domain + * @return the canonical name by domain + * @description canonical name by domain . + * @author paderlol + * @date 2019年07月28日, 16:31:32 + */ + List getCanonicalNameByDomain(String domain); - Service getServiceByCname(String cName); + /** + * Get domain by CNAME + * @param canonicalName the canonical name + * @return the service by canonical name + * @description service by canonical name . + * @author paderlol + * @date 2019年07月28日, 16:31:32 + */ + Service getServiceByCanonicalName(String canonicalName); + + /** + * Is exist domain boolean. + * + * @param domain the domain + * @return the boolean + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:32 + */ + boolean isExistDomain(String domain); + + /** + * Is exist canonical name boolean. + * + * @param canonicalName the canonical name + * @return the boolean + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:32 + */ + boolean isExistCanonicalName(String canonicalName); } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java b/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java index 36ffa4d44..10102ee4e 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java @@ -2,6 +2,9 @@ package com.alibaba.nacos.dns.dto; import java.io.Serializable; +/** + * @author paderlol + */ public class UpdateDomainDto implements Serializable { private Integer cacheTime; diff --git a/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java b/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java index c87094803..837a35bba 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java @@ -12,7 +12,9 @@ */ package com.alibaba.nacos.dns.exception; - +/** + * @author paderlol + */ public class DomainNotFoundException extends RuntimeException { public DomainNotFoundException(String message) { diff --git a/dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java b/dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java new file mode 100644 index 000000000..d2555f02b --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java @@ -0,0 +1,23 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.exception; + +/** + * @author paderlol + */ +public class SystemEntryNotFoundException extends RuntimeException { + + public SystemEntryNotFoundException(String message) { + super(message); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java index 0807b328d..f7311a200 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java @@ -14,11 +14,36 @@ package com.alibaba.nacos.dns.generator; import com.alibaba.nacos.naming.core.Service; +/** + * @author paderlol + * @date 2019年07月28日, 16:31:16 + */ public interface DomainGenerator { + /** + * @description The constant DOMAIN_SUFFIX. + */ String DOMAIN_SUFFIX = ".nacos.local"; + /** + * Is match boolean. + * + * @param service the service + * @return the boolean + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:16 + */ public boolean isMatch(Service service); + /** + * Create string. + * + * @param service the service + * @return the string + * @description + * @author paderlol + * @date 2019年07月28日, 16:31:16 + */ public String create(Service service); } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java index 0ed904c03..db339566b 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java @@ -10,6 +10,9 @@ import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.List; +/** + * @author paderlol + */ @Component public class DomainGeneratorDelegate implements ApplicationContextAware, InitializingBean { diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java index 7553b03a9..9697f63d7 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java @@ -12,6 +12,7 @@ */ package com.alibaba.nacos.dns.generator.impl; +import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.dns.generator.DomainGenerator; import com.alibaba.nacos.naming.core.Service; import org.springframework.stereotype.Component; @@ -19,6 +20,9 @@ import org.springframework.stereotype.Component; import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP; import static com.alibaba.nacos.api.common.Constants.DEFAULT_NAMESPACE_ID; +/** + * @author paderlol + */ @Component public class DefaultGroupAndNameSpaceDomainGenerator implements DomainGenerator { @@ -27,12 +31,13 @@ public class DefaultGroupAndNameSpaceDomainGenerator implements DomainGenerator @Override public boolean isMatch(Service service) { - return DEFAULT_GROUP.equals(service.getGroupName()) && DEFAULT_NAMESPACE_ID.equals(service.getNamespaceId()); + return DEFAULT_GROUP.equals(NamingUtils.getGroupName(service.getName())) + && DEFAULT_NAMESPACE_ID.equals(service.getNamespaceId()); } @Override public String create(Service service) { - return String.format(FULL_DOMAIN_MAPPING, service.getName()); + return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName())); } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java index d196a48de..186f96359 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java @@ -12,12 +12,16 @@ */ package com.alibaba.nacos.dns.generator.impl; +import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.dns.generator.DomainGenerator; import com.alibaba.nacos.naming.core.Service; import org.springframework.stereotype.Component; import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP; +/** + * @author paderlol + */ @Component public class DefaultGroupDomainGenerator implements DomainGenerator { @@ -26,12 +30,13 @@ public class DefaultGroupDomainGenerator implements DomainGenerator { @Override public boolean isMatch(Service service) { - return DEFAULT_GROUP.equals(service.getGroupName()); + return DEFAULT_GROUP.equals(NamingUtils.getGroupName(service.getName())); } @Override public String create(Service service) { - return String.format(FULL_DOMAIN_MAPPING, service.getName(), service.getNamespaceId()); + return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName()), + service.getNamespaceId()); } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java index ae7646956..208a2efd9 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java @@ -12,12 +12,16 @@ */ package com.alibaba.nacos.dns.generator.impl; +import com.alibaba.nacos.api.naming.utils.NamingUtils; import com.alibaba.nacos.dns.generator.DomainGenerator; import com.alibaba.nacos.naming.core.Service; import org.springframework.stereotype.Component; import static com.alibaba.nacos.api.common.Constants.DEFAULT_NAMESPACE_ID; +/** + * @author paderlol + */ @Component public class DefaultNamespaceDomainGenerator implements DomainGenerator { @@ -32,6 +36,7 @@ public class DefaultNamespaceDomainGenerator implements DomainGenerator { @Override public String create(Service service) { - return String.format(FULL_DOMAIN_MAPPING, service.getName(), service.getGroupName()); + return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName()), + NamingUtils.getGroupName(service.getName())); } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java index 1b8e63848..1a35d3b76 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java @@ -12,10 +12,15 @@ */ package com.alibaba.nacos.dns.generator.impl; -import com.alibaba.nacos.dns.generator.DomainGenerator; -import com.alibaba.nacos.naming.core.Service; import org.springframework.stereotype.Component; +import com.alibaba.nacos.api.naming.utils.NamingUtils; +import com.alibaba.nacos.dns.generator.DomainGenerator; +import com.alibaba.nacos.naming.core.Service; + +/** + * @author paderlol + */ @Component public class FullDomainGenerator implements DomainGenerator { @@ -30,6 +35,7 @@ public class FullDomainGenerator implements DomainGenerator { @Override public String create(Service service) { - return String.format(FULL_DOMAIN_MAPPING, service.getName(), service.getGroupName(), service.getNamespaceId()); + return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName()), + NamingUtils.getGroupName(service.getName()), service.getNamespaceId()); } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java b/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java index 5561f613b..8b2c317a1 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java @@ -15,20 +15,24 @@ package com.alibaba.nacos.dns.listener; import com.alibaba.nacos.dns.core.DnsSource; import com.alibaba.nacos.naming.core.Service; import com.alibaba.nacos.naming.core.event.ServiceEvent; -import org.springframework.context.event.EventListener; +import org.springframework.context.ApplicationListener; import org.springframework.stereotype.Component; +/** + * @author paderlol + */ @Component -public class ServiceChangeListener { +public class ServiceChangeListener implements ApplicationListener { - final DnsSource dnsSource; + private final DnsSource dnsSource; public ServiceChangeListener(DnsSource dnsSource) { this.dnsSource = dnsSource; } - @EventListener - public void serviceChange(ServiceEvent event) { + + @Override + public void onApplicationEvent(ServiceEvent event) { Service service = (Service)event.getSource(); dnsSource.putService(service); } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java b/dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java new file mode 100644 index 000000000..f7a58de0f --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2019 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 com.alibaba.nacos.dns.misc; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author paderlol + * @date 2019年07月28日, 18:39:50 + */ +public class Loggers { + + /** + * @description The constant dnsLogger. + */ + public static final Logger dnsLogger = LoggerFactory.getLogger("com.alibaba.nacos.dns.main"); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java similarity index 71% rename from dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java rename to dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java index fff4ccd32..9e3ce8119 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/CNameRecord.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java @@ -15,14 +15,17 @@ package com.alibaba.nacos.dns.record; import java.util.ArrayList; import java.util.List; -public class CNameRecord extends BaseRecord { - private List cNames = new ArrayList<>(); +/** + * @author paderlol + */ +public class AddressRecord extends BaseRecord { + private List address = new ArrayList<>(); - public List getcNames() { - return cNames; + public List getAddress() { + return address; } - public void setcNames(List cNames) { - this.cNames = cNames; + public void setAddress(List address) { + this.address = address; } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java index e8946a279..883c862f4 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java @@ -14,6 +14,9 @@ package com.alibaba.nacos.dns.record; import java.io.Serializable; +/** + * @author paderlol + */ public abstract class BaseRecord implements Serializable { private RecordType record; diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java new file mode 100644 index 000000000..8eeb98ef3 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java @@ -0,0 +1,31 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author paderlol + */ +public class CanonicalNameRecord extends BaseRecord { + private List canonicalNames = new ArrayList<>(); + + public List getCanonicalNames() { + return canonicalNames; + } + + public void setCanonicalNames(List canonicalNames) { + this.canonicalNames = canonicalNames; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java b/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java index 0e80e078e..6c222f69e 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java @@ -14,6 +14,9 @@ package com.alibaba.nacos.dns.record; import java.io.Serializable; +/** + * @author paderlol + */ public class InstanceInfo implements Serializable { private String address; private int port; diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java b/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java index 3889d1d2d..0ed2a047f 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java @@ -12,6 +12,25 @@ */ package com.alibaba.nacos.dns.record; +/** + * @author paderlol + * @date 2019年07月28日, 16:36:20 + */ public enum RecordType { - A, SRV, TXT, CNAME + /** + * @description Address record type. + */ + A, + /** + * @description Srv record type. + */ + SRV, + /** + * @description Txt record type. + */ + TXT, + /** + * @description Canonical name record type. + */ + CNAME } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java index 952ce1fb6..63d65b6f3 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java @@ -15,6 +15,17 @@ package com.alibaba.nacos.dns.record; import java.util.ArrayList; import java.util.List; +/** + * @author paderlol + */ public class SrvRecord extends BaseRecord { private List instances = new ArrayList<>(); + + public List getInstances() { + return instances; + } + + public void setInstances(List instances) { + this.instances = instances; + } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java index 0effb7aba..8e6e903f0 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java @@ -15,14 +15,17 @@ package com.alibaba.nacos.dns.record; import java.util.HashMap; import java.util.Map; +/** + * @author paderlol + */ public class TxtRecord extends BaseRecord { - private Map metadata = new HashMap<>(); + private Map text = new HashMap<>(); - public Map getMetadata() { - return metadata; + public Map getText() { + return text; } - public void setMetadata(Map metadata) { - this.metadata = metadata; + public void setText(Map text) { + this.text = text; } } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java new file mode 100644 index 000000000..34f53f8b5 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java @@ -0,0 +1,29 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion; + +import com.alibaba.nacos.dns.record.RecordType; +import org.springframework.stereotype.Component; + +import java.lang.annotation.*; + +/** + * @author paderlol + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +@Documented +@Component +public @interface Record { + RecordType type(); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java new file mode 100644 index 000000000..7e9a60ee8 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java @@ -0,0 +1,34 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion; + +import com.alibaba.nacos.dns.record.BaseRecord; + +/** + * @author paderlol + * @date 2019年07月28日, 16:29:00 + */ +public interface RecordConversion { + + /** + * Transform base record. + * + * @param domain the domain + * @return the base record + * @description + * @author paderlol + * @date 2019年07月28日, 16:29:00 + */ + BaseRecord transform(String domain); + +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java new file mode 100644 index 000000000..05503feea --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion; + +import com.alibaba.nacos.dns.record.RecordType; + +/** + * @author paderlol + * @date 2019年07月28日, 16:29:20 + */ +public interface RecordConversionFactory { + + /** + * Create record conversion. + * + * @param recordType the record type + * @return the record conversion + * @description + * @author paderlol + * @date 2019年07月28日, 16:29:20 + */ + RecordConversion create(RecordType recordType); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java new file mode 100644 index 000000000..cfdb5073d --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java @@ -0,0 +1,56 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion; + +import com.alibaba.nacos.dns.record.RecordType; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.InitializingBean; +import org.springframework.context.ApplicationContext; +import org.springframework.context.ApplicationContextAware; +import org.springframework.stereotype.Component; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +/** + * @author paderlol + */ +@Component +public class RecordConversionFactoryImpl implements RecordConversionFactory, ApplicationContextAware, InitializingBean { + + private ApplicationContext applicationContext; + + private Map recordConversionMap; + + @Override + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { + this.applicationContext = applicationContext; + } + + @Override + public RecordConversion create(RecordType recordType) { + return recordConversionMap.get(recordType); + } + + @Override + public void afterPropertiesSet() { + Map temp = new HashMap<>(16); + Map beans = this.applicationContext.getBeansOfType(RecordConversion.class); + for (RecordConversion recordConversion : beans.values()) { + Record record = recordConversion.getClass().getAnnotation(Record.class); + temp.put(record.type(), recordConversion); + } + recordConversionMap = Collections.unmodifiableMap(temp); + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java new file mode 100644 index 000000000..33c67cc78 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java @@ -0,0 +1,43 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion.impl; + +import org.springframework.beans.factory.annotation.Autowired; + +import com.alibaba.nacos.dns.core.DnsSource; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import com.alibaba.nacos.dns.record.conversion.RecordConversion; +import com.alibaba.nacos.naming.core.Service; + +/** + * @author paderlol + */ +public abstract class AbstractRecordConversion implements RecordConversion { + + @Autowired + private DnsSource dnsSource; + + public Service getService(String domain) { + if (dnsSource.isExistDomain(domain)) { + return dnsSource.getServiceByDomainName(domain); + } else if (dnsSource.isExistCanonicalName(domain)) { + return dnsSource.getServiceByCanonicalName(domain); + } else { + throw new DomainNotFoundException("domain name " + domain + "not found"); + } + } + + public DnsSource getDnsSource() { + return dnsSource; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java new file mode 100644 index 000000000..fef000fd5 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java @@ -0,0 +1,43 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion.impl; + +import com.alibaba.nacos.dns.record.AddressRecord; +import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.record.conversion.Record; +import com.alibaba.nacos.naming.core.Instance; +import com.alibaba.nacos.naming.core.Service; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * @author paderlol + */ +@Record(type = RecordType.A) +public class AddressRecordConversion extends AbstractRecordConversion { + + @Override + public AddressRecord transform(String domain) { + + Service service = getService(domain); + List instances = service.allIPs(); + AddressRecord addressRecord = new AddressRecord(); + List address = Lists.newArrayListWithCapacity(instances.size()); + for (Instance instance : instances) { + address.add(instance.getIp()); + } + addressRecord.setAddress(address); + return addressRecord; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java new file mode 100644 index 000000000..4f673ff4a --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion.impl; + +import com.alibaba.nacos.dns.record.BaseRecord; +import com.alibaba.nacos.dns.record.CanonicalNameRecord; +import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.record.conversion.Record; + +/** + * @author paderlol + */ +@Record(type = RecordType.CNAME) +public class CanonicalNameRecordConversion extends AbstractRecordConversion { + + @Override + public BaseRecord transform(String domain) { + CanonicalNameRecord canonicalNameRecord = new CanonicalNameRecord(); + canonicalNameRecord.setCanonicalNames(getDnsSource().getCanonicalNameByDomain(domain)); + + return canonicalNameRecord; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java new file mode 100644 index 000000000..e01789bbe --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java @@ -0,0 +1,47 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion.impl; + +import com.alibaba.nacos.dns.record.BaseRecord; +import com.alibaba.nacos.dns.record.InstanceInfo; +import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.record.SrvRecord; +import com.alibaba.nacos.dns.record.conversion.Record; +import com.alibaba.nacos.naming.core.Instance; +import com.alibaba.nacos.naming.core.Service; +import com.google.common.collect.Lists; + +import java.util.List; + +/** + * @author paderlol + */ +@Record(type = RecordType.SRV) +public class SrvRecordConversion extends AbstractRecordConversion { + @Override + public BaseRecord transform(String domain) { + Service service = getService(domain); + List instances = service.allIPs(); + SrvRecord srvRecord = new SrvRecord(); + List instanceInfos = Lists.newArrayListWithCapacity(instances.size()); + for (Instance instance : instances) { + InstanceInfo instanceInfo = new InstanceInfo(); + instanceInfo.setAddress(instance.getIp()); + instanceInfo.setPort(instance.getPort()); + instanceInfos.add(instanceInfo); + } + srvRecord.setInstances(instanceInfos); + + return srvRecord; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java new file mode 100644 index 000000000..a71720173 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java @@ -0,0 +1,48 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.record.conversion.impl; + +import com.alibaba.nacos.dns.constant.DnsConstants; +import com.alibaba.nacos.dns.record.BaseRecord; +import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.record.TxtRecord; +import com.alibaba.nacos.dns.record.conversion.Record; +import org.apache.commons.lang3.StringUtils; + +import java.util.HashMap; +import java.util.Map; +import java.util.Objects; + +/** + * @author paderlol + */ +@Record(type = RecordType.TXT) +public class TxtRecordConversion extends AbstractRecordConversion { + + @Override + public BaseRecord transform(String domain) { + TxtRecord txtRecord = new TxtRecord(); + com.alibaba.nacos.naming.core.Service service = getDnsSource().getServiceByCanonicalName(domain); + Map text = new HashMap<>(16); + if (Objects.isNull(service)) { + service = getDnsSource().getServiceByDomainName(domain); + text.put(DnsConstants.CNAME_KEY, StringUtils.join(getDnsSource().getCanonicalNameByDomain(domain), ",")); + } + int cacheTime = getDnsSource().getCacheTime(domain); + text.putAll(getDnsSource().getSystemConfig()); + text.put(DnsConstants.DEFAULT_CACHE_TIME_KEY, String.valueOf(cacheTime)); + text.putAll(service.getMetadata()); + txtRecord.setText(text); + return txtRecord; + } +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java index ff33f6fb6..27d94ab2a 100644 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java @@ -16,9 +16,29 @@ import com.alibaba.nacos.dns.dto.UpdateDomainDto; import java.util.Map; +/** + * @author paderlol + * @date 2019年07月28日, 16:24:12 + */ public interface DomainService { + /** + * Get config + * @param domain the domain + * @return the config + * @author zhanglong + * @date 2019年07月28日, 16:24:12 + */ public Map getConfig(String domain); + /** + * Update config. + * + * @param domain the domain + * @param updateDomainDto the update domain dto + * @return the boolean + * @author zhanglong + * @date 2019年07月28日, 16:24:12 + */ public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto); } diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java deleted file mode 100644 index b98d25e88..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainServiceImpl.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service; - -import com.alibaba.nacos.dns.constant.DnsConstants; -import com.alibaba.nacos.dns.core.DnsSource; -import com.alibaba.nacos.dns.dto.UpdateDomainDto; -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import org.apache.commons.lang3.StringUtils; -import org.springframework.stereotype.Service; - -import java.util.HashMap; -import java.util.Map; - -@Service -public class DomainServiceImpl implements DomainService { - - final DnsSource dnsSource; - - public DomainServiceImpl(DnsSource dnsSource) { - this.dnsSource = dnsSource; - } - - @Override - public Map getConfig(String domain) { - com.alibaba.nacos.naming.core.Service service = dnsSource.getServiceByCname(domain); - Map config = new HashMap<>(); - if (service == null) { - service = dnsSource.getServiceByDomainName(domain); - config.put(DnsConstants.CNAME_KEY, StringUtils.join(dnsSource.getMappingName(domain), ",")); - } - if (service == null) { - throw new DomainNotFoundException("domain name " + domain + "not found"); - } - int cacheTime = dnsSource.getCacheTime(domain); - config.put(DnsConstants.DEFAULT_CACHE_TIME_KEY, String.valueOf(cacheTime)); - config.putAll(service.getMetadata()); - return config; - } - - @Override - public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto) { - return false; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java new file mode 100644 index 000000000..92db5e321 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java @@ -0,0 +1,33 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service; + +import com.alibaba.nacos.dns.record.BaseRecord; +import com.alibaba.nacos.dns.record.RecordType; + +/** + * @author paderlol + * @date 2019年07月28日, 16:23:50 + */ +public interface RecordService { + + /** + * Get record from domain and recordType + * @param domain the domain + * @param recordType the record type + * @return the record + * @author zhanglong + * @date 2019年07月28日, 16:23:50 + */ + BaseRecord getRecord(String domain, RecordType recordType); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java new file mode 100644 index 000000000..c3dce4663 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java @@ -0,0 +1,41 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service; + +import java.util.Map; + +/** + * @author paderlol + * @date 2019年07月28日, 16:23:30 + */ +public interface SwitchService { + + /** + * Get dns system config + * @param domain the domain + * @return the system config + * @author zhanglong + * @date 2019年07月28日, 16:23:30 + */ + Map getSystemConfig(String domain); + + /** + * Update system config. + * + * @param entry the entry + * @param value the value + * @author zhanglong + * @date 2019年07月28日, 16:23:30 + */ + void updateSystemConfig(String entry, String value); +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java new file mode 100644 index 000000000..5ef96c622 --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java @@ -0,0 +1,70 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service.impl; + +import com.alibaba.nacos.dns.core.DnsSource; +import com.alibaba.nacos.dns.dto.UpdateDomainDto; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.record.TxtRecord; +import com.alibaba.nacos.dns.record.conversion.RecordConversion; +import com.alibaba.nacos.dns.record.conversion.RecordConversionFactoryImpl; +import com.alibaba.nacos.dns.service.DomainService; +import org.springframework.stereotype.Service; + +import java.util.Map; +import java.util.Objects; + +/** + * @author paderlol + */ +@Service +public class DomainServiceImpl implements DomainService { + + private final DnsSource dnsSource; + + private final RecordConversionFactoryImpl recordConversionFactory; + + public DomainServiceImpl(DnsSource dnsSource, RecordConversionFactoryImpl recordConversionFactory) { + this.dnsSource = dnsSource; + this.recordConversionFactory = recordConversionFactory; + } + + @Override + public Map getConfig(String domain) { + checkDomainIsExist(domain); + RecordConversion recordConversion = recordConversionFactory.create(RecordType.TXT); + TxtRecord txtRecord = (TxtRecord)recordConversion.transform(domain); + return txtRecord.getText(); + } + + @Override + public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto) { + checkDomainIsExist(domain); + if (Objects.nonNull(updateDomainDto.getCacheTime())) { + dnsSource.updateCacheTime(domain, updateDomainDto.getCacheTime()); + } + if (Objects.nonNull(updateDomainDto.getcName()) && dnsSource.isExistDomain(domain)) { + dnsSource.putCanonicalName(domain, updateDomainDto.getcName()); + } + + return false; + } + + private void checkDomainIsExist(String domain) { + if (!dnsSource.isExistCanonicalName(domain) && !dnsSource.isExistDomain(domain)) { + throw new DomainNotFoundException("domain name " + domain + " was not found"); + } + } + +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java new file mode 100644 index 000000000..aaba2f81b --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java @@ -0,0 +1,43 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service.impl; + +import com.alibaba.nacos.dns.record.BaseRecord; +import com.alibaba.nacos.dns.record.RecordType; +import com.alibaba.nacos.dns.record.conversion.RecordConversion; +import com.alibaba.nacos.dns.record.conversion.RecordConversionFactoryImpl; +import com.alibaba.nacos.dns.service.RecordService; + +/** + * @author paderlol + */ +@org.springframework.stereotype.Service +public class RecordServiceImpl implements RecordService { + + private final RecordConversionFactoryImpl recordConversionFactory; + + public RecordServiceImpl(RecordConversionFactoryImpl recordConversionFactory) { + this.recordConversionFactory = recordConversionFactory; + } + + @Override + public BaseRecord getRecord(String domain, RecordType recordType) { + + RecordConversion recordConversion = recordConversionFactory.create(recordType); + BaseRecord baseRecord = recordConversion.transform(domain); + baseRecord.setName(domain); + baseRecord.setRecord(recordType); + return baseRecord; + } + +} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java new file mode 100644 index 000000000..35d64eafb --- /dev/null +++ b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java @@ -0,0 +1,51 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * 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 com.alibaba.nacos.dns.service.impl; + +import com.alibaba.nacos.dns.core.DnsSource; +import com.alibaba.nacos.dns.exception.DomainNotFoundException; +import com.alibaba.nacos.dns.exception.SystemEntryNotFoundException; +import com.alibaba.nacos.dns.service.SwitchService; +import org.springframework.stereotype.Service; + +import java.util.Map; + +/** + * @author paderlol + */ +@Service +public class SwitchServiceImpl implements SwitchService { + private final DnsSource dnsSource; + + public SwitchServiceImpl(DnsSource dnsSource) { + this.dnsSource = dnsSource; + } + + @Override + public Map getSystemConfig(String domain) { + if (!dnsSource.isExistCanonicalName(domain) && !dnsSource.isExistDomain(domain)) { + throw new DomainNotFoundException("Domain name " + domain + " was not found"); + } + return dnsSource.getSystemConfig(); + } + + @Override + public void updateSystemConfig(String entry, String value) { + if (dnsSource.getSystemConfig().containsKey(entry)) { + + dnsSource.updateSystemConfig(entry, value); + } else { + throw new SystemEntryNotFoundException(" Entry " + entry + " was not in the system property"); + } + } +} diff --git a/dns/src/main/resources/META-INF/logback/nacos-included.xml b/dns/src/main/resources/META-INF/logback/nacos-included.xml index f80a989ca..682da1eb9 100644 --- a/dns/src/main/resources/META-INF/logback/nacos-included.xml +++ b/dns/src/main/resources/META-INF/logback/nacos-included.xml @@ -6,10 +6,10 @@ - ${LOG_HOME}/naming-server.log + ${LOG_HOME}/nacos-dns.log true - ${LOG_HOME}/dns.log.%d{yyyy-MM-dd}.%i + ${LOG_HOME}/nacos-dns.%d{yyyy-MM-dd}.%i 2GB 15 7GB diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index 48ded03f9..0803d8f15 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -152,6 +152,7 @@ public class ServiceManager implements RecordListener { consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service); Loggers.SRV_LOG.info("[NEW-SERVICE] {}", service.toJSON()); } + SpringContext.getAppContext().publishEvent(new ServiceEvent(service)); } catch (Throwable e) { Loggers.SRV_LOG.error("[NACOS-SERVICE] error while processing service update", e); } @@ -387,7 +388,7 @@ public class ServiceManager implements RecordListener { public void addOrReplaceService(Service service) throws NacosException { consistencyService.put(KeyBuilder.buildServiceMetaKey(service.getNamespaceId(), service.getName()), service); - SpringContext.getAppContext().publishEvent(new ServiceEvent(service)); + } public void createEmptyService(String namespaceId, String serviceName, boolean local) throws NacosException { diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java index 52a8bad62..658789782 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java @@ -14,7 +14,10 @@ package com.alibaba.nacos.naming.core.event; import com.alibaba.nacos.naming.core.Service; import org.springframework.context.ApplicationEvent; - +/** + * @author paderlol + * + */ public class ServiceEvent extends ApplicationEvent { /** diff --git a/pom.xml b/pom.xml index b876fcab3..391b22e47 100644 --- a/pom.xml +++ b/pom.xml @@ -479,6 +479,11 @@ nacos-naming ${project.version} + + ${project.groupId} + nacos-dns + ${project.version} + ${project.groupId} nacos-api From 0074aa294a1d64719f0e7e2aa41a89059bf5406f Mon Sep 17 00:00:00 2001 From: zhanglong Date: Fri, 17 Jan 2020 00:52:47 +0800 Subject: [PATCH 4/7] Deleted dns module --- console/pom.xml | 4 - distribution/pom.xml | 32 ---- distribution/release-dns.xml | 75 ---------- dns/pom.xml | 71 --------- .../java/com/alibaba/nacos/dns/DnsApp.java | 26 ---- .../nacos/dns/config/DnsProperties.java | 114 -------------- .../nacos/dns/constant/DnsConstants.java | 32 ---- .../nacos/dns/controller/DnsController.java | 72 --------- .../dns/controller/RecordController.java | 53 ------- .../dns/controller/SwitchController.java | 67 --------- .../nacos/dns/core/DefaultDnsSource.java | 127 ---------------- .../com/alibaba/nacos/dns/core/DnsSource.java | 139 ------------------ .../nacos/dns/dto/UpdateDomainDto.java | 28 ---- .../exception/DomainNotFoundException.java | 23 --- .../SystemEntryNotFoundException.java | 23 --- .../nacos/dns/generator/DomainGenerator.java | 49 ------ .../generator/DomainGeneratorDelegate.java | 41 ------ ...faultGroupAndNameSpaceDomainGenerator.java | 43 ------ .../impl/DefaultGroupDomainGenerator.java | 42 ------ .../impl/DefaultNamespaceDomainGenerator.java | 42 ------ .../generator/impl/FullDomainGenerator.java | 41 ------ .../dns/listener/ServiceChangeListener.java | 39 ----- .../com/alibaba/nacos/dns/misc/Loggers.java | 28 ---- .../nacos/dns/record/AddressRecord.java | 31 ---- .../alibaba/nacos/dns/record/BaseRecord.java | 40 ----- .../nacos/dns/record/CanonicalNameRecord.java | 31 ---- .../nacos/dns/record/InstanceInfo.java | 39 ----- .../alibaba/nacos/dns/record/RecordType.java | 36 ----- .../alibaba/nacos/dns/record/SrvRecord.java | 31 ---- .../alibaba/nacos/dns/record/TxtRecord.java | 31 ---- .../nacos/dns/record/conversion/Record.java | 29 ---- .../record/conversion/RecordConversion.java | 34 ----- .../conversion/RecordConversionFactory.java | 33 ----- .../RecordConversionFactoryImpl.java | 56 ------- .../impl/AbstractRecordConversion.java | 43 ------ .../impl/AddressRecordConversion.java | 43 ------ .../impl/CanonicalNameRecordConversion.java | 33 ----- .../conversion/impl/SrvRecordConversion.java | 47 ------ .../conversion/impl/TxtRecordConversion.java | 48 ------ .../nacos/dns/service/DomainService.java | 44 ------ .../nacos/dns/service/RecordService.java | 33 ----- .../nacos/dns/service/SwitchService.java | 41 ------ .../dns/service/impl/DomainServiceImpl.java | 70 --------- .../dns/service/impl/RecordServiceImpl.java | 43 ------ .../dns/service/impl/SwitchServiceImpl.java | 51 ------- .../META-INF/logback/nacos-included.xml | 32 ---- 46 files changed, 2130 deletions(-) delete mode 100644 distribution/release-dns.xml delete mode 100644 dns/pom.xml delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java delete mode 100644 dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java delete mode 100644 dns/src/main/resources/META-INF/logback/nacos-included.xml diff --git a/console/pom.xml b/console/pom.xml index 78da24a62..5a77f934e 100644 --- a/console/pom.xml +++ b/console/pom.xml @@ -39,10 +39,6 @@ tomcat-embed-jasper 7.0.59 - - ${project.groupId} - nacos-dns - ${project.groupId} diff --git a/distribution/pom.xml b/distribution/pom.xml index 35369ed46..6fc92dc59 100644 --- a/distribution/pom.xml +++ b/distribution/pom.xml @@ -99,38 +99,6 @@ ans - - release-dns - - - com.alibaba.nacos - nacos-dns - - - - - - maven-assembly-plugin - - - release-dns - - single - - package - - - release-dns.xml - - false - - - - - - nacos-dns - - release-address diff --git a/distribution/release-dns.xml b/distribution/release-dns.xml deleted file mode 100644 index f737ef899..000000000 --- a/distribution/release-dns.xml +++ /dev/null @@ -1,75 +0,0 @@ - - - - dns - false - - dir - tar.gz - zip - - - - ../ - - README.md - - - - - - conf/** - benchmark/* - - - - - - bin/* - - 0755 - - - - - - LICENSE-BIN - LICENSE - - - NOTICE-BIN - NOTICE - - - - - - true - - com.alibaba.nacos:nacos-dns - - - lib/ - false - - - lib/ - - - - - - diff --git a/dns/pom.xml b/dns/pom.xml deleted file mode 100644 index 30b27391a..000000000 --- a/dns/pom.xml +++ /dev/null @@ -1,71 +0,0 @@ - - - - nacos-all - com.alibaba.nacos - 1.1.0 - ../pom.xml - - 4.0.0 - - nacos-dns - jar - - UTF-8 - 1.8 - 1.8 - - - - com.alibaba.nacos - nacos-naming - - - com.ctrip.framework.apollo - apollo-client - 1.1.1 - - - org.springframework.boot - spring-boot-configuration-processor - true - - - - - - org.apache.maven.plugins - maven-assembly-plugin - - - - com.alibaba.nacos.naming.DnsApp - - - - jar-with-dependencies - - - - - - - src/main/resources - - - - - - - - - - org.codehaus.mojo - findbugs-maven-plugin - 3.0.4 - - - - diff --git a/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java b/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java deleted file mode 100644 index 35c8b62ab..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/DnsApp.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; - -/** - * @author paderlol - */ -@SpringBootApplication -public class DnsApp { - public static void main(String[] args) { - SpringApplication.run(DnsApp.class, args); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java b/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java deleted file mode 100644 index 79ac788a5..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/config/DnsProperties.java +++ /dev/null @@ -1,114 +0,0 @@ -package com.alibaba.nacos.dns.config; - -import org.springframework.boot.context.properties.ConfigurationProperties; -import org.springframework.stereotype.Component; - -import java.io.Serializable; - - -/** - * @author paderlol - */ -@Component -@ConfigurationProperties(prefix = "nacos.dns") -public class DnsProperties implements Serializable { - - /** - * @description The Default cache time. - */ - private int defaultCacheTime = 10; - /** - * @description The Upstream servers for domain suffix map. - */ - private String upstreamServersForDomainSuffixMap="null"; - /** - * @description The Default upstream server. - */ - private String defaultUpstreamServer="null"; - /** - * @description The Edns enabled. - */ - private boolean ednsEnabled = false; - - /** - * @return the default cache time - * @description default cache time TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public int getDefaultCacheTime() { - return defaultCacheTime; - } - - /** - * @param defaultCacheTime the default cache time - * @description Sets default cache time TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public void setDefaultCacheTime(int defaultCacheTime) { - this.defaultCacheTime = defaultCacheTime; - } - - /** - * @return the upstream servers for domain suffix map - * @description upstream servers for domain suffix map TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public String getUpstreamServersForDomainSuffixMap() { - return upstreamServersForDomainSuffixMap; - } - - /** - * @param upstreamServersForDomainSuffixMap the upstream servers for domain suffix map - * @description Sets upstream servers for domain suffix map TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public void setUpstreamServersForDomainSuffixMap(String upstreamServersForDomainSuffixMap) { - this.upstreamServersForDomainSuffixMap = upstreamServersForDomainSuffixMap; - } - - /** - * @return the default upstream server - * @description default upstream server TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public String getDefaultUpstreamServer() { - return defaultUpstreamServer; - } - - /** - * @param defaultUpstreamServer the default upstream server - * @description Sets default upstream server TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public void setDefaultUpstreamServer(String defaultUpstreamServer) { - this.defaultUpstreamServer = defaultUpstreamServer; - } - - /** - * Is edns enabled boolean. - * - * @return the boolean - * @description TODO(do something) - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public boolean isEdnsEnabled() { - return ednsEnabled; - } - - /** - * @param ednsEnabled the edns enabled - * @description Sets edns enabled TODO(do something). - * @author zhanglong - * @date 2019年07月28日, 16:14:53 - */ - public void setEdnsEnabled(boolean ednsEnabled) { - this.ednsEnabled = ednsEnabled; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java b/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java deleted file mode 100644 index 6ef103cd1..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/constant/DnsConstants.java +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright (C) 2019 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 com.alibaba.nacos.dns.constant; - -import com.alibaba.nacos.naming.misc.UtilsAndCommons; - -/** - * @author paderlol - */ -public class DnsConstants { - - public static final String DEFAULT_CACHE_TIME_KEY = "defaultCacheTime"; - public static final String UPSTREAM_SERVERS_FOR_DOMAIN_SUFFIX_MAP_KEY = "upstreamServersForDomainSuffixMap"; - public static final String DEFAULT_UPSTREAM_SERVER_KEY = "defaultUpstreamServer"; - public static final String EDNS_ENABLED_KEY = "ednsEnabled"; - public static final String CNAME_KEY = "CNAME"; - public static final String GET_DOMAIN_FAILED = "Get domain failed"; - /** - * the url for dns - */ - public static final String NACOS_DNS_CONTEXT = UtilsAndCommons.NACOS_SERVER_VERSION + "/dns"; -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java deleted file mode 100644 index ca7e2cffa..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/controller/DnsController.java +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.controller; - -import com.alibaba.nacos.dns.dto.UpdateDomainDto; -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import com.alibaba.nacos.dns.misc.Loggers; -import com.alibaba.nacos.dns.service.DomainService; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import static com.alibaba.nacos.dns.constant.DnsConstants.GET_DOMAIN_FAILED; -import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; - -/** - * @author paderlol - */ -@RestController -@RequestMapping(value = NACOS_DNS_CONTEXT + "/domain") -public class DnsController { - - private final DomainService domainService; - - public DnsController(DomainService domainService) { - this.domainService = domainService; - } - - @GetMapping("/{domainName}") - public ResponseEntity getDomain(@PathVariable("domainName") String domainName) { - - try { - return ResponseEntity.ok().body(domainService.getConfig(domainName)); - } catch (Exception e) { - if (e instanceof DomainNotFoundException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } else { - Loggers.dnsLogger.error(GET_DOMAIN_FAILED,e); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); - } - } - - } - - @PutMapping("/{domainName}") - public ResponseEntity updateDomain(@PathVariable("domainName") String domainName, - @RequestBody UpdateDomainDto updateDomainDto) { - - try { - domainService.updateConfig(domainName, updateDomainDto); - return ResponseEntity.ok().build(); - } catch (Exception e) { - if (e instanceof DomainNotFoundException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } else { - Loggers.dnsLogger.error(GET_DOMAIN_FAILED,e); - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); - } - } - } - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java deleted file mode 100644 index b6149c65c..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/controller/RecordController.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.controller; - -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.service.RecordService; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.PathVariable; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; - -import static com.alibaba.nacos.dns.constant.DnsConstants.GET_DOMAIN_FAILED; -import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; - -/** - * @author paderlol - */ -@RestController -@RequestMapping(value = NACOS_DNS_CONTEXT + "/record") -public class RecordController { - - @Autowired - private RecordService recordService; - - @GetMapping("/{domainName}/{type}") - public ResponseEntity getRecord(@PathVariable("domainName") String domainName, - @PathVariable(value = "type", required = false) RecordType recordType) { - try { - return ResponseEntity.ok().body(recordService.getRecord(domainName, recordType)); - } catch (Exception e) { - if (e instanceof DomainNotFoundException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } else { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); - } - } - } - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java b/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java deleted file mode 100644 index 8e2122b9b..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/controller/SwitchController.java +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.controller; - -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import com.alibaba.nacos.dns.exception.SystemEntryNotFoundException; -import com.alibaba.nacos.dns.service.SwitchService; -import org.springframework.http.HttpStatus; -import org.springframework.http.ResponseEntity; -import org.springframework.web.bind.annotation.*; - -import static com.alibaba.nacos.dns.constant.DnsConstants.GET_DOMAIN_FAILED; -import static com.alibaba.nacos.dns.constant.DnsConstants.NACOS_DNS_CONTEXT; - -/** - * @author paderlol - */ -@RestController -@RequestMapping(value = NACOS_DNS_CONTEXT + "/switches") -public class SwitchController { - - private final SwitchService switchService; - - public SwitchController(SwitchService switchService) { - this.switchService = switchService; - } - - @GetMapping("/{domainName}") - public ResponseEntity getSystemConfig(@PathVariable("domainName") String domainName) { - - try { - return ResponseEntity.ok().body(switchService.getSystemConfig(domainName)); - } catch (Exception e) { - if (e instanceof DomainNotFoundException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } else { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); - } - } - } - - @PutMapping("/{entry}/{value}") - public ResponseEntity updateSystemConfig(@PathVariable("entry") String entry, @PathVariable("value") String value) { - - try { - switchService.updateSystemConfig(entry, value); - return ResponseEntity.ok().build(); - } catch (Exception e) { - if (e instanceof SystemEntryNotFoundException) { - return ResponseEntity.status(HttpStatus.NOT_FOUND).body(e.getMessage()); - } else { - return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(GET_DOMAIN_FAILED); - } - } - } - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java b/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java deleted file mode 100644 index 6662d35a2..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/core/DefaultDnsSource.java +++ /dev/null @@ -1,127 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.core; - -import com.alibaba.nacos.dns.config.DnsProperties; -import com.alibaba.nacos.dns.generator.DomainGeneratorDelegate; -import com.alibaba.nacos.naming.core.Service; -import org.apache.commons.lang3.StringUtils; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.stereotype.Component; - -import java.util.Collections; -import java.util.List; -import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; -import java.util.concurrent.CopyOnWriteArrayList; - -import static com.alibaba.nacos.dns.constant.DnsConstants.*; - -/** - * @author paderlol - */ -@Component -public class DefaultDnsSource implements DnsSource, InitializingBean { - private final Map serviceMap = new ConcurrentHashMap<>(); - private final Map systemConfig = new ConcurrentHashMap<>(); - private final Map domainCacheTime = new ConcurrentHashMap<>(); - private final Map> domainForCanonicalName = new ConcurrentHashMap<>(); - private final Map canonicalNameForDomain = new ConcurrentHashMap<>(); - - private final DomainGeneratorDelegate domainGeneratorDelegate; - private final DnsProperties dnsProperties; - - public DefaultDnsSource(DomainGeneratorDelegate domainGeneratorDelegate, DnsProperties dnsProperties) { - this.domainGeneratorDelegate = domainGeneratorDelegate; - this.dnsProperties = dnsProperties; - } - - @Override - public void putService(Service service) { - - List domains = domainGeneratorDelegate.create(service); - for (String domain : domains) { - serviceMap.put(domain, service); - } - } - - @Override - public Service getServiceByDomainName(String domain) { - - return serviceMap.get(domain); - } - - @Override - public Map getSystemConfig() { - return Collections.unmodifiableMap(systemConfig); - } - - @Override - public void updateSystemConfig(String key, String value) { - systemConfig.put(key, value); - } - - @Override - public void updateCacheTime(String domain, Integer cacheTime) { - domainCacheTime.put(domain, cacheTime); - } - - @Override - public int getCacheTime(String domain) { - - return domainCacheTime.getOrDefault(domain, Integer.valueOf(systemConfig.get(DEFAULT_CACHE_TIME_KEY))); - } - - @Override - public void putCanonicalName(String domain, String canonicalName) { - if (serviceMap.containsKey(domain)) { - - canonicalNameForDomain.put(canonicalName, domain); - CopyOnWriteArrayList mappingNames = - domainForCanonicalName.getOrDefault(domain, new CopyOnWriteArrayList<>()); - domainForCanonicalName.putIfAbsent(domain, mappingNames); - mappingNames.add(canonicalName); - } - } - - @Override - public List getCanonicalNameByDomain(String domain) { - return Collections.unmodifiableList(domainForCanonicalName.getOrDefault(domain, new CopyOnWriteArrayList<>())); - } - - @Override - public Service getServiceByCanonicalName(String canonicalName) { - String domainName = canonicalNameForDomain.get(canonicalName); - - return StringUtils.isNotBlank(domainName) ? serviceMap.get(domainName) : null; - } - - @Override - public boolean isExistDomain(String domain) { - return serviceMap.containsKey(domain); - } - - @Override - public boolean isExistCanonicalName(String canonicalName) { - return canonicalNameForDomain.containsKey(canonicalName); - } - - @Override - public void afterPropertiesSet() { - systemConfig.put(DEFAULT_CACHE_TIME_KEY, String.valueOf(dnsProperties.getDefaultCacheTime())); - systemConfig.put(UPSTREAM_SERVERS_FOR_DOMAIN_SUFFIX_MAP_KEY, - dnsProperties.getUpstreamServersForDomainSuffixMap()); - systemConfig.put(DEFAULT_UPSTREAM_SERVER_KEY, dnsProperties.getDefaultUpstreamServer()); - systemConfig.put(EDNS_ENABLED_KEY, String.valueOf(dnsProperties.isEdnsEnabled())); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java b/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java deleted file mode 100644 index b781fbedd..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/core/DnsSource.java +++ /dev/null @@ -1,139 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.core; - -import com.alibaba.nacos.naming.core.Service; - -import java.util.List; -import java.util.Map; - -/** - * @author paderlol - * @date 2019年07月28日, 16:31:31 - */ -public interface DnsSource { - /** - * Put service. - * - * @param service the service - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:31 - */ - void putService(Service service); - - /** - * Get service by domain name - * @param domain the domain - * @return the service by domain name - * @description service by domain name . - * @author paderlol - * @date 2019年07月28日, 16:31:31 - */ - Service getServiceByDomainName(String domain); - - /** - * Get system config - * @return the system config - * @description system config . - * @author paderlol - * @date 2019年07月28日, 16:31:31 - */ - Map getSystemConfig(); - - /** - * Update system config. - * - * @param key the key - * @param value the value - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:31 - */ - void updateSystemConfig(String key, String value); - - /** - * Update cache time. - * - * @param domain the domain - * @param cacheTime the cache time - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:31 - */ - void updateCacheTime(String domain, Integer cacheTime); - - /** - * Get cache time which is ttl - * @param domain the domain - * @return the cache time - * @description cache time . - * @author paderlol - * @date 2019年07月28日, 16:31:32 - */ - int getCacheTime(String domain); - - /** - * Put canonical name. - * - * @param domain the domain - * @param canonicalName the canonical name - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:32 - */ - void putCanonicalName(String domain, String canonicalName); - - /** - * Get CNAME by domain - * @param domain the domain - * @return the canonical name by domain - * @description canonical name by domain . - * @author paderlol - * @date 2019年07月28日, 16:31:32 - */ - List getCanonicalNameByDomain(String domain); - - /** - * Get domain by CNAME - * @param canonicalName the canonical name - * @return the service by canonical name - * @description service by canonical name . - * @author paderlol - * @date 2019年07月28日, 16:31:32 - */ - Service getServiceByCanonicalName(String canonicalName); - - /** - * Is exist domain boolean. - * - * @param domain the domain - * @return the boolean - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:32 - */ - boolean isExistDomain(String domain); - - /** - * Is exist canonical name boolean. - * - * @param canonicalName the canonical name - * @return the boolean - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:32 - */ - boolean isExistCanonicalName(String canonicalName); - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java b/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java deleted file mode 100644 index 10102ee4e..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/dto/UpdateDomainDto.java +++ /dev/null @@ -1,28 +0,0 @@ -package com.alibaba.nacos.dns.dto; - -import java.io.Serializable; - -/** - * @author paderlol - */ -public class UpdateDomainDto implements Serializable { - - private Integer cacheTime; - private String cName; - - public Integer getCacheTime() { - return cacheTime; - } - - public void setCacheTime(Integer cacheTime) { - this.cacheTime = cacheTime; - } - - public String getcName() { - return cName; - } - - public void setcName(String cName) { - this.cName = cName; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java b/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java deleted file mode 100644 index 837a35bba..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/exception/DomainNotFoundException.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.exception; - -/** - * @author paderlol - */ -public class DomainNotFoundException extends RuntimeException { - - public DomainNotFoundException(String message) { - super(message); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java b/dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java deleted file mode 100644 index d2555f02b..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/exception/SystemEntryNotFoundException.java +++ /dev/null @@ -1,23 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.exception; - -/** - * @author paderlol - */ -public class SystemEntryNotFoundException extends RuntimeException { - - public SystemEntryNotFoundException(String message) { - super(message); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java deleted file mode 100644 index f7311a200..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGenerator.java +++ /dev/null @@ -1,49 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.generator; - -import com.alibaba.nacos.naming.core.Service; - -/** - * @author paderlol - * @date 2019年07月28日, 16:31:16 - */ -public interface DomainGenerator { - - /** - * @description The constant DOMAIN_SUFFIX. - */ - String DOMAIN_SUFFIX = ".nacos.local"; - - /** - * Is match boolean. - * - * @param service the service - * @return the boolean - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:16 - */ - public boolean isMatch(Service service); - - /** - * Create string. - * - * @param service the service - * @return the string - * @description - * @author paderlol - * @date 2019年07月28日, 16:31:16 - */ - public String create(Service service); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java deleted file mode 100644 index db339566b..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/DomainGeneratorDelegate.java +++ /dev/null @@ -1,41 +0,0 @@ -package com.alibaba.nacos.dns.generator; - -import com.alibaba.nacos.naming.core.Service; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.stereotype.Component; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author paderlol - */ -@Component -public class DomainGeneratorDelegate implements ApplicationContextAware, InitializingBean { - - private ApplicationContext applicationContext; - private final List generators = new ArrayList<>(); - - public List create(Service service) { - List domainNames = new ArrayList<>(generators.size()); - for (DomainGenerator domainGenerator : generators) { - if (domainGenerator.isMatch(service)) { - domainNames.add(domainGenerator.create(service)); - } - } - return domainNames; - } - - @Override - public void afterPropertiesSet() throws Exception { - generators.addAll(applicationContext.getBeansOfType(DomainGenerator.class).values()); - } - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java deleted file mode 100644 index 9697f63d7..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupAndNameSpaceDomainGenerator.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.generator.impl; - -import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.dns.generator.DomainGenerator; -import com.alibaba.nacos.naming.core.Service; -import org.springframework.stereotype.Component; - -import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP; -import static com.alibaba.nacos.api.common.Constants.DEFAULT_NAMESPACE_ID; - -/** - * @author paderlol - */ -@Component -public class DefaultGroupAndNameSpaceDomainGenerator implements DomainGenerator { - - private static final String FULL_DOMAIN_MAPPING = "%s.sn" + DOMAIN_SUFFIX; - - @Override - public boolean isMatch(Service service) { - - return DEFAULT_GROUP.equals(NamingUtils.getGroupName(service.getName())) - && DEFAULT_NAMESPACE_ID.equals(service.getNamespaceId()); - } - - @Override - public String create(Service service) { - - return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName())); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java deleted file mode 100644 index 186f96359..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultGroupDomainGenerator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.generator.impl; - -import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.dns.generator.DomainGenerator; -import com.alibaba.nacos.naming.core.Service; -import org.springframework.stereotype.Component; - -import static com.alibaba.nacos.api.common.Constants.DEFAULT_GROUP; - -/** - * @author paderlol - */ -@Component -public class DefaultGroupDomainGenerator implements DomainGenerator { - - private static final String FULL_DOMAIN_MAPPING = "%s.sn.%s.ns" + DOMAIN_SUFFIX; - - @Override - public boolean isMatch(Service service) { - - return DEFAULT_GROUP.equals(NamingUtils.getGroupName(service.getName())); - } - - @Override - public String create(Service service) { - - return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName()), - service.getNamespaceId()); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java deleted file mode 100644 index 208a2efd9..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/DefaultNamespaceDomainGenerator.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.generator.impl; - -import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.dns.generator.DomainGenerator; -import com.alibaba.nacos.naming.core.Service; -import org.springframework.stereotype.Component; - -import static com.alibaba.nacos.api.common.Constants.DEFAULT_NAMESPACE_ID; - -/** - * @author paderlol - */ -@Component -public class DefaultNamespaceDomainGenerator implements DomainGenerator { - - private static final String FULL_DOMAIN_MAPPING = "%s.sn.%s.gn" + DOMAIN_SUFFIX; - - @Override - public boolean isMatch(Service service) { - - return DEFAULT_NAMESPACE_ID.equals(service.getNamespaceId()); - } - - @Override - public String create(Service service) { - - return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName()), - NamingUtils.getGroupName(service.getName())); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java b/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java deleted file mode 100644 index 1a35d3b76..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/generator/impl/FullDomainGenerator.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.generator.impl; - -import org.springframework.stereotype.Component; - -import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.dns.generator.DomainGenerator; -import com.alibaba.nacos.naming.core.Service; - -/** - * @author paderlol - */ -@Component -public class FullDomainGenerator implements DomainGenerator { - - private static final String FULL_DOMAIN_MAPPING = "%s.sn.%s.gn.%s.ns" + DOMAIN_SUFFIX; - - @Override - public boolean isMatch(Service service) { - - return true; - } - - @Override - public String create(Service service) { - - return String.format(FULL_DOMAIN_MAPPING, NamingUtils.getServiceName(service.getName()), - NamingUtils.getGroupName(service.getName()), service.getNamespaceId()); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java b/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java deleted file mode 100644 index 8b2c317a1..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/listener/ServiceChangeListener.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.listener; - -import com.alibaba.nacos.dns.core.DnsSource; -import com.alibaba.nacos.naming.core.Service; -import com.alibaba.nacos.naming.core.event.ServiceEvent; -import org.springframework.context.ApplicationListener; -import org.springframework.stereotype.Component; - -/** - * @author paderlol - */ -@Component -public class ServiceChangeListener implements ApplicationListener { - - private final DnsSource dnsSource; - - public ServiceChangeListener(DnsSource dnsSource) { - this.dnsSource = dnsSource; - } - - - @Override - public void onApplicationEvent(ServiceEvent event) { - Service service = (Service)event.getSource(); - dnsSource.putService(service); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java b/dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java deleted file mode 100644 index f7a58de0f..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/misc/Loggers.java +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (C) 2019 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 com.alibaba.nacos.dns.misc; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * @author paderlol - * @date 2019年07月28日, 18:39:50 - */ -public class Loggers { - - /** - * @description The constant dnsLogger. - */ - public static final Logger dnsLogger = LoggerFactory.getLogger("com.alibaba.nacos.dns.main"); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java deleted file mode 100644 index 9e3ce8119..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/AddressRecord.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author paderlol - */ -public class AddressRecord extends BaseRecord { - private List address = new ArrayList<>(); - - public List getAddress() { - return address; - } - - public void setAddress(List address) { - this.address = address; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java deleted file mode 100644 index 883c862f4..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/BaseRecord.java +++ /dev/null @@ -1,40 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -import java.io.Serializable; - -/** - * @author paderlol - */ -public abstract class BaseRecord implements Serializable { - - private RecordType record; - private String name; - - public RecordType getRecord() { - return record; - } - - public void setRecord(RecordType record) { - this.record = record; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java deleted file mode 100644 index 8eeb98ef3..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/CanonicalNameRecord.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author paderlol - */ -public class CanonicalNameRecord extends BaseRecord { - private List canonicalNames = new ArrayList<>(); - - public List getCanonicalNames() { - return canonicalNames; - } - - public void setCanonicalNames(List canonicalNames) { - this.canonicalNames = canonicalNames; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java b/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java deleted file mode 100644 index 6c222f69e..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/InstanceInfo.java +++ /dev/null @@ -1,39 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -import java.io.Serializable; - -/** - * @author paderlol - */ -public class InstanceInfo implements Serializable { - private String address; - private int port; - - public String getAddress() { - return address; - } - - public void setAddress(String address) { - this.address = address; - } - - public int getPort() { - return port; - } - - public void setPort(int port) { - this.port = port; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java b/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java deleted file mode 100644 index 0ed2a047f..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/RecordType.java +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -/** - * @author paderlol - * @date 2019年07月28日, 16:36:20 - */ -public enum RecordType { - /** - * @description Address record type. - */ - A, - /** - * @description Srv record type. - */ - SRV, - /** - * @description Txt record type. - */ - TXT, - /** - * @description Canonical name record type. - */ - CNAME -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java deleted file mode 100644 index 63d65b6f3..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/SrvRecord.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -import java.util.ArrayList; -import java.util.List; - -/** - * @author paderlol - */ -public class SrvRecord extends BaseRecord { - private List instances = new ArrayList<>(); - - public List getInstances() { - return instances; - } - - public void setInstances(List instances) { - this.instances = instances; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java b/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java deleted file mode 100644 index 8e6e903f0..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/TxtRecord.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record; - -import java.util.HashMap; -import java.util.Map; - -/** - * @author paderlol - */ -public class TxtRecord extends BaseRecord { - private Map text = new HashMap<>(); - - public Map getText() { - return text; - } - - public void setText(Map text) { - this.text = text; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java deleted file mode 100644 index 34f53f8b5..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/Record.java +++ /dev/null @@ -1,29 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion; - -import com.alibaba.nacos.dns.record.RecordType; -import org.springframework.stereotype.Component; - -import java.lang.annotation.*; - -/** - * @author paderlol - */ -@Target(ElementType.TYPE) -@Retention(RetentionPolicy.RUNTIME) -@Documented -@Component -public @interface Record { - RecordType type(); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java deleted file mode 100644 index 7e9a60ee8..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversion.java +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion; - -import com.alibaba.nacos.dns.record.BaseRecord; - -/** - * @author paderlol - * @date 2019年07月28日, 16:29:00 - */ -public interface RecordConversion { - - /** - * Transform base record. - * - * @param domain the domain - * @return the base record - * @description - * @author paderlol - * @date 2019年07月28日, 16:29:00 - */ - BaseRecord transform(String domain); - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java deleted file mode 100644 index 05503feea..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactory.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion; - -import com.alibaba.nacos.dns.record.RecordType; - -/** - * @author paderlol - * @date 2019年07月28日, 16:29:20 - */ -public interface RecordConversionFactory { - - /** - * Create record conversion. - * - * @param recordType the record type - * @return the record conversion - * @description - * @author paderlol - * @date 2019年07月28日, 16:29:20 - */ - RecordConversion create(RecordType recordType); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java deleted file mode 100644 index cfdb5073d..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/RecordConversionFactoryImpl.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion; - -import com.alibaba.nacos.dns.record.RecordType; -import org.springframework.beans.BeansException; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.ApplicationContext; -import org.springframework.context.ApplicationContextAware; -import org.springframework.stereotype.Component; - -import java.util.Collections; -import java.util.HashMap; -import java.util.Map; - -/** - * @author paderlol - */ -@Component -public class RecordConversionFactoryImpl implements RecordConversionFactory, ApplicationContextAware, InitializingBean { - - private ApplicationContext applicationContext; - - private Map recordConversionMap; - - @Override - public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { - this.applicationContext = applicationContext; - } - - @Override - public RecordConversion create(RecordType recordType) { - return recordConversionMap.get(recordType); - } - - @Override - public void afterPropertiesSet() { - Map temp = new HashMap<>(16); - Map beans = this.applicationContext.getBeansOfType(RecordConversion.class); - for (RecordConversion recordConversion : beans.values()) { - Record record = recordConversion.getClass().getAnnotation(Record.class); - temp.put(record.type(), recordConversion); - } - recordConversionMap = Collections.unmodifiableMap(temp); - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java deleted file mode 100644 index 33c67cc78..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AbstractRecordConversion.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion.impl; - -import org.springframework.beans.factory.annotation.Autowired; - -import com.alibaba.nacos.dns.core.DnsSource; -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import com.alibaba.nacos.dns.record.conversion.RecordConversion; -import com.alibaba.nacos.naming.core.Service; - -/** - * @author paderlol - */ -public abstract class AbstractRecordConversion implements RecordConversion { - - @Autowired - private DnsSource dnsSource; - - public Service getService(String domain) { - if (dnsSource.isExistDomain(domain)) { - return dnsSource.getServiceByDomainName(domain); - } else if (dnsSource.isExistCanonicalName(domain)) { - return dnsSource.getServiceByCanonicalName(domain); - } else { - throw new DomainNotFoundException("domain name " + domain + "not found"); - } - } - - public DnsSource getDnsSource() { - return dnsSource; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java deleted file mode 100644 index fef000fd5..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/AddressRecordConversion.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion.impl; - -import com.alibaba.nacos.dns.record.AddressRecord; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.record.conversion.Record; -import com.alibaba.nacos.naming.core.Instance; -import com.alibaba.nacos.naming.core.Service; -import com.google.common.collect.Lists; - -import java.util.List; - -/** - * @author paderlol - */ -@Record(type = RecordType.A) -public class AddressRecordConversion extends AbstractRecordConversion { - - @Override - public AddressRecord transform(String domain) { - - Service service = getService(domain); - List instances = service.allIPs(); - AddressRecord addressRecord = new AddressRecord(); - List address = Lists.newArrayListWithCapacity(instances.size()); - for (Instance instance : instances) { - address.add(instance.getIp()); - } - addressRecord.setAddress(address); - return addressRecord; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java deleted file mode 100644 index 4f673ff4a..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/CanonicalNameRecordConversion.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion.impl; - -import com.alibaba.nacos.dns.record.BaseRecord; -import com.alibaba.nacos.dns.record.CanonicalNameRecord; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.record.conversion.Record; - -/** - * @author paderlol - */ -@Record(type = RecordType.CNAME) -public class CanonicalNameRecordConversion extends AbstractRecordConversion { - - @Override - public BaseRecord transform(String domain) { - CanonicalNameRecord canonicalNameRecord = new CanonicalNameRecord(); - canonicalNameRecord.setCanonicalNames(getDnsSource().getCanonicalNameByDomain(domain)); - - return canonicalNameRecord; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java deleted file mode 100644 index e01789bbe..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/SrvRecordConversion.java +++ /dev/null @@ -1,47 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion.impl; - -import com.alibaba.nacos.dns.record.BaseRecord; -import com.alibaba.nacos.dns.record.InstanceInfo; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.record.SrvRecord; -import com.alibaba.nacos.dns.record.conversion.Record; -import com.alibaba.nacos.naming.core.Instance; -import com.alibaba.nacos.naming.core.Service; -import com.google.common.collect.Lists; - -import java.util.List; - -/** - * @author paderlol - */ -@Record(type = RecordType.SRV) -public class SrvRecordConversion extends AbstractRecordConversion { - @Override - public BaseRecord transform(String domain) { - Service service = getService(domain); - List instances = service.allIPs(); - SrvRecord srvRecord = new SrvRecord(); - List instanceInfos = Lists.newArrayListWithCapacity(instances.size()); - for (Instance instance : instances) { - InstanceInfo instanceInfo = new InstanceInfo(); - instanceInfo.setAddress(instance.getIp()); - instanceInfo.setPort(instance.getPort()); - instanceInfos.add(instanceInfo); - } - srvRecord.setInstances(instanceInfos); - - return srvRecord; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java b/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java deleted file mode 100644 index a71720173..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/record/conversion/impl/TxtRecordConversion.java +++ /dev/null @@ -1,48 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.record.conversion.impl; - -import com.alibaba.nacos.dns.constant.DnsConstants; -import com.alibaba.nacos.dns.record.BaseRecord; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.record.TxtRecord; -import com.alibaba.nacos.dns.record.conversion.Record; -import org.apache.commons.lang3.StringUtils; - -import java.util.HashMap; -import java.util.Map; -import java.util.Objects; - -/** - * @author paderlol - */ -@Record(type = RecordType.TXT) -public class TxtRecordConversion extends AbstractRecordConversion { - - @Override - public BaseRecord transform(String domain) { - TxtRecord txtRecord = new TxtRecord(); - com.alibaba.nacos.naming.core.Service service = getDnsSource().getServiceByCanonicalName(domain); - Map text = new HashMap<>(16); - if (Objects.isNull(service)) { - service = getDnsSource().getServiceByDomainName(domain); - text.put(DnsConstants.CNAME_KEY, StringUtils.join(getDnsSource().getCanonicalNameByDomain(domain), ",")); - } - int cacheTime = getDnsSource().getCacheTime(domain); - text.putAll(getDnsSource().getSystemConfig()); - text.put(DnsConstants.DEFAULT_CACHE_TIME_KEY, String.valueOf(cacheTime)); - text.putAll(service.getMetadata()); - txtRecord.setText(text); - return txtRecord; - } -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java deleted file mode 100644 index 27d94ab2a..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/DomainService.java +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service; - -import com.alibaba.nacos.dns.dto.UpdateDomainDto; - -import java.util.Map; - -/** - * @author paderlol - * @date 2019年07月28日, 16:24:12 - */ -public interface DomainService { - - /** - * Get config - * @param domain the domain - * @return the config - * @author zhanglong - * @date 2019年07月28日, 16:24:12 - */ - public Map getConfig(String domain); - - /** - * Update config. - * - * @param domain the domain - * @param updateDomainDto the update domain dto - * @return the boolean - * @author zhanglong - * @date 2019年07月28日, 16:24:12 - */ - public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java deleted file mode 100644 index 92db5e321..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/RecordService.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service; - -import com.alibaba.nacos.dns.record.BaseRecord; -import com.alibaba.nacos.dns.record.RecordType; - -/** - * @author paderlol - * @date 2019年07月28日, 16:23:50 - */ -public interface RecordService { - - /** - * Get record from domain and recordType - * @param domain the domain - * @param recordType the record type - * @return the record - * @author zhanglong - * @date 2019年07月28日, 16:23:50 - */ - BaseRecord getRecord(String domain, RecordType recordType); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java b/dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java deleted file mode 100644 index c3dce4663..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/SwitchService.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service; - -import java.util.Map; - -/** - * @author paderlol - * @date 2019年07月28日, 16:23:30 - */ -public interface SwitchService { - - /** - * Get dns system config - * @param domain the domain - * @return the system config - * @author zhanglong - * @date 2019年07月28日, 16:23:30 - */ - Map getSystemConfig(String domain); - - /** - * Update system config. - * - * @param entry the entry - * @param value the value - * @author zhanglong - * @date 2019年07月28日, 16:23:30 - */ - void updateSystemConfig(String entry, String value); -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java deleted file mode 100644 index 5ef96c622..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/DomainServiceImpl.java +++ /dev/null @@ -1,70 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service.impl; - -import com.alibaba.nacos.dns.core.DnsSource; -import com.alibaba.nacos.dns.dto.UpdateDomainDto; -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.record.TxtRecord; -import com.alibaba.nacos.dns.record.conversion.RecordConversion; -import com.alibaba.nacos.dns.record.conversion.RecordConversionFactoryImpl; -import com.alibaba.nacos.dns.service.DomainService; -import org.springframework.stereotype.Service; - -import java.util.Map; -import java.util.Objects; - -/** - * @author paderlol - */ -@Service -public class DomainServiceImpl implements DomainService { - - private final DnsSource dnsSource; - - private final RecordConversionFactoryImpl recordConversionFactory; - - public DomainServiceImpl(DnsSource dnsSource, RecordConversionFactoryImpl recordConversionFactory) { - this.dnsSource = dnsSource; - this.recordConversionFactory = recordConversionFactory; - } - - @Override - public Map getConfig(String domain) { - checkDomainIsExist(domain); - RecordConversion recordConversion = recordConversionFactory.create(RecordType.TXT); - TxtRecord txtRecord = (TxtRecord)recordConversion.transform(domain); - return txtRecord.getText(); - } - - @Override - public boolean updateConfig(String domain, UpdateDomainDto updateDomainDto) { - checkDomainIsExist(domain); - if (Objects.nonNull(updateDomainDto.getCacheTime())) { - dnsSource.updateCacheTime(domain, updateDomainDto.getCacheTime()); - } - if (Objects.nonNull(updateDomainDto.getcName()) && dnsSource.isExistDomain(domain)) { - dnsSource.putCanonicalName(domain, updateDomainDto.getcName()); - } - - return false; - } - - private void checkDomainIsExist(String domain) { - if (!dnsSource.isExistCanonicalName(domain) && !dnsSource.isExistDomain(domain)) { - throw new DomainNotFoundException("domain name " + domain + " was not found"); - } - } - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java deleted file mode 100644 index aaba2f81b..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/RecordServiceImpl.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service.impl; - -import com.alibaba.nacos.dns.record.BaseRecord; -import com.alibaba.nacos.dns.record.RecordType; -import com.alibaba.nacos.dns.record.conversion.RecordConversion; -import com.alibaba.nacos.dns.record.conversion.RecordConversionFactoryImpl; -import com.alibaba.nacos.dns.service.RecordService; - -/** - * @author paderlol - */ -@org.springframework.stereotype.Service -public class RecordServiceImpl implements RecordService { - - private final RecordConversionFactoryImpl recordConversionFactory; - - public RecordServiceImpl(RecordConversionFactoryImpl recordConversionFactory) { - this.recordConversionFactory = recordConversionFactory; - } - - @Override - public BaseRecord getRecord(String domain, RecordType recordType) { - - RecordConversion recordConversion = recordConversionFactory.create(recordType); - BaseRecord baseRecord = recordConversion.transform(domain); - baseRecord.setName(domain); - baseRecord.setRecord(recordType); - return baseRecord; - } - -} diff --git a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java b/dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java deleted file mode 100644 index 35d64eafb..000000000 --- a/dns/src/main/java/com/alibaba/nacos/dns/service/impl/SwitchServiceImpl.java +++ /dev/null @@ -1,51 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.dns.service.impl; - -import com.alibaba.nacos.dns.core.DnsSource; -import com.alibaba.nacos.dns.exception.DomainNotFoundException; -import com.alibaba.nacos.dns.exception.SystemEntryNotFoundException; -import com.alibaba.nacos.dns.service.SwitchService; -import org.springframework.stereotype.Service; - -import java.util.Map; - -/** - * @author paderlol - */ -@Service -public class SwitchServiceImpl implements SwitchService { - private final DnsSource dnsSource; - - public SwitchServiceImpl(DnsSource dnsSource) { - this.dnsSource = dnsSource; - } - - @Override - public Map getSystemConfig(String domain) { - if (!dnsSource.isExistCanonicalName(domain) && !dnsSource.isExistDomain(domain)) { - throw new DomainNotFoundException("Domain name " + domain + " was not found"); - } - return dnsSource.getSystemConfig(); - } - - @Override - public void updateSystemConfig(String entry, String value) { - if (dnsSource.getSystemConfig().containsKey(entry)) { - - dnsSource.updateSystemConfig(entry, value); - } else { - throw new SystemEntryNotFoundException(" Entry " + entry + " was not in the system property"); - } - } -} diff --git a/dns/src/main/resources/META-INF/logback/nacos-included.xml b/dns/src/main/resources/META-INF/logback/nacos-included.xml deleted file mode 100644 index 682da1eb9..000000000 --- a/dns/src/main/resources/META-INF/logback/nacos-included.xml +++ /dev/null @@ -1,32 +0,0 @@ - - - - - - - - ${LOG_HOME}/nacos-dns.log - true - - ${LOG_HOME}/nacos-dns.%d{yyyy-MM-dd}.%i - 2GB - 15 - 7GB - true - - - %date %level %msg%n%n - UTF-8 - - - - - - - - - - - - From 74b9d3a197bceb8ea31b0e8256ddb67900ed6f9b Mon Sep 17 00:00:00 2001 From: zhanglong Date: Fri, 17 Jan 2020 01:45:57 +0800 Subject: [PATCH 5/7] Fixed heath check error with Nacos by standalone #2295 --- .../server/service/ServerListService.java | 190 ++++++------------ 1 file changed, 57 insertions(+), 133 deletions(-) diff --git a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java index 40b16e360..f3635ce43 100644 --- a/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java +++ b/config/src/main/java/com/alibaba/nacos/config/server/service/ServerListService.java @@ -20,7 +20,6 @@ import com.alibaba.nacos.config.server.constant.Constants; import com.alibaba.nacos.config.server.monitor.MetricsMonitor; import com.alibaba.nacos.config.server.service.notify.NotifyService; import com.alibaba.nacos.config.server.service.notify.NotifyService.HttpResult; -import com.alibaba.nacos.config.server.utils.LogUtil; import com.alibaba.nacos.config.server.utils.PropertyUtil; import com.alibaba.nacos.config.server.utils.RunningConfigUtils; import com.alibaba.nacos.config.server.utils.event.EventDispatcher; @@ -32,10 +31,9 @@ import org.apache.http.client.utils.HttpClientUtils; import org.apache.http.concurrent.FutureCallback; import org.apache.http.impl.nio.client.CloseableHttpAsyncClient; import org.apache.http.impl.nio.client.HttpAsyncClients; -import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.web.context.WebServerInitializedEvent; import org.springframework.context.ApplicationListener; -import org.springframework.core.env.Environment; import org.springframework.stereotype.Service; import org.springframework.util.CollectionUtils; @@ -44,9 +42,14 @@ import javax.servlet.ServletContext; import javax.servlet.http.HttpServletResponse; import java.io.IOException; import java.io.StringReader; -import java.util.*; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadFactory; import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog; import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog; @@ -60,17 +63,21 @@ import static com.alibaba.nacos.core.utils.SystemUtils.*; @Service public class ServerListService implements ApplicationListener { - @Autowired - private Environment env; - @Autowired - private ServletContext servletContext; + private final ServletContext servletContext; + @Value("${server.port:8848}") private int port; + @Value("${useAddressServer}") + private Boolean isUseAddressServer = true; + + public ServerListService(ServletContext servletContext) { + this.servletContext = servletContext; + } + @PostConstruct public void init() { - serverPort = System.getProperty("nacos.server.port", "8848"); String envDomainName = System.getenv("address_server_domain"); if (StringUtils.isBlank(envDomainName)) { domainName = System.getProperty("address.server.domain", "jmenv.tbsite.net"); @@ -88,55 +95,23 @@ public class ServerListService implements ApplicationListener getServerList() { return new ArrayList(serverList); @@ -161,21 +136,15 @@ public class ServerListService implements ApplicationListener newList) { - if (newList.isEmpty()) { + if (CollectionUtils.isEmpty(newList)||newList.equals(serverList)) { return; } - boolean isContainSelfIp = false; - for (String ipPortTmp : newList) { - if (ipPortTmp.contains(LOCAL_IP)) { - isContainSelfIp = true; - break; - } - } + boolean isContainSelfIp = newList.stream().anyMatch(ipPortTmp -> ipPortTmp.contains(LOCAL_IP)); if (isContainSelfIp) { isInIpList = true; @@ -186,30 +155,23 @@ public class ServerListService implements ApplicationListener(newList); - List unhealthRemoved = new ArrayList(); - for (String unhealthIp : serverListUnhealth) { - if (!newList.contains(unhealthIp)) { - unhealthRemoved.add(unhealthIp); + if(!serverListUnhealth.isEmpty()){ + + List unhealthyRemoved = serverListUnhealth.stream() + .filter(unhealthyIp -> !newList.contains(unhealthyIp)).collect(Collectors.toList()); + + serverListUnhealth.removeAll(unhealthyRemoved); + + List unhealthyCountRemoved = serverIp2unhealthCount.keySet().stream() + .filter(key -> !newList.contains(key)).collect(Collectors.toList()); + + for (String unhealthyCountTmp : unhealthyCountRemoved) { + serverIp2unhealthCount.remove(unhealthyCountTmp); } - } - serverListUnhealth.removeAll(unhealthRemoved); - List unhealthCountRemoved = new ArrayList(); - for (Map.Entry ip2UnhealthCountTmp : serverIp2unhealthCount.entrySet()) { - if (!newList.contains(ip2UnhealthCountTmp.getKey())) { - unhealthCountRemoved.add(ip2UnhealthCountTmp.getKey()); - } - } - - for (String unhealthCountTmp : unhealthCountRemoved) { - serverIp2unhealthCount.remove(unhealthCountTmp); } defaultLog.warn("[serverlist] updated to {}", serverList); @@ -217,7 +179,7 @@ public class ServerListService implements ApplicationListener lines = IoUtils.readLines(new StringReader(result.content)); List ips = new ArrayList(lines.size()); for (String serverAddr : lines) { @@ -266,16 +228,16 @@ public class ServerListService implements ApplicationListener= maxFailCount) { + addressServerFailCount++; + if (addressServerFailCount >= maxFailCount) { isAddressServerHealth = false; } defaultLog.error("[serverlist] failed to get serverlist, error code {}", result.code); return Collections.emptyList(); } } catch (IOException e) { - addressServerFailCcount++; - if (addressServerFailCcount >= maxFailCount) { + addressServerFailCount++; + if (addressServerFailCount >= maxFailCount) { isAddressServerHealth = false; } defaultLog.error("[serverlist] exception, " + e.toString(), e); @@ -302,23 +264,7 @@ public class ServerListService implements ApplicationListener { + class AsyncCheckServerHealthCallBack implements FutureCallback { private String serverIp; - public AyscCheckServerHealthCallBack(String serverIp) { + public AsyncCheckServerHealthCallBack(String serverIp) { this.serverIp = serverIp; } @@ -363,29 +309,16 @@ public class ServerListService implements ApplicationListener{ - if(oldValue == null){ - return 1; - } - return oldValue+1; - }); - if (failCount > maxFailCount) { - if (!serverListUnhealth.contains(serverIp)) { - serverListUnhealth.add(serverIp); - } - defaultLog.error("unhealthIp:{}, unhealthCount:{}", serverIp, failCount); - MetricsMonitor.getUnhealthException().increment(); - } + computeFailCount(); } @Override public void cancelled() { - int failCount = serverIp2unhealthCount.compute(serverIp,(key,oldValue)->{ - if(oldValue == null){ - return 1; + computeFailCount(); } - return oldValue+1; - }); + + private void computeFailCount() { + int failCount = serverIp2unhealthCount.compute(serverIp,(key,oldValue)->oldValue == null?1:oldValue+1); if (failCount > maxFailCount) { if (!serverListUnhealth.contains(serverIp)) { serverListUnhealth.add(serverIp); @@ -435,15 +368,15 @@ public class ServerListService implements ApplicationListener serverList = new ArrayList(); - private static volatile List serverListUnhealth = new ArrayList(); + private static volatile List serverListUnhealth = Collections.synchronizedList(new ArrayList());; private static volatile boolean isAddressServerHealth = true; - private static volatile int addressServerFailCcount = 0; + private static volatile int addressServerFailCount = 0; private static volatile boolean isInIpList = true; /** * ip unhealth count */ - private static volatile Map serverIp2unhealthCount = new HashMap(); + private static Map serverIp2unhealthCount = new ConcurrentHashMap<>(); private RequestConfig requestConfig = RequestConfig.custom() .setConnectTimeout(PropertyUtil.getNotifyConnectTimeout()) .setSocketTimeout(PropertyUtil.getNotifySocketTimeout()).build(); @@ -451,29 +384,20 @@ public class ServerListService implements ApplicationListener newList = new ArrayList(); - for (String serverAddrTmp : serverList) { - newList.add(getFormatServerAddr(serverAddrTmp)); - } - setServerList(new ArrayList(newList)); - } + httpclient.start(); + CheckServerHealthTask checkServerHealthTask = new CheckServerHealthTask(); + TimerTaskService.scheduleWithFixedDelay(checkServerHealthTask, 0L, 5L, TimeUnit.SECONDS); + } } From c14ee9ca0423f9ab208245190c08224db10c62cd Mon Sep 17 00:00:00 2001 From: zhanglong Date: Fri, 17 Jan 2020 01:58:13 +0800 Subject: [PATCH 6/7] Removed useless commits --- console/pom.xml | 5 ++- .../nacos/naming/core/ServiceManager.java | 3 -- .../nacos/naming/core/event/ServiceEvent.java | 31 ------------------- 3 files changed, 4 insertions(+), 35 deletions(-) delete mode 100644 naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java diff --git a/console/pom.xml b/console/pom.xml index 5a77f934e..12c0cc6fb 100644 --- a/console/pom.xml +++ b/console/pom.xml @@ -39,7 +39,10 @@ tomcat-embed-jasper 7.0.59 - + + ${project.groupId} + nacos-naming + ${project.groupId} nacos-istio diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index e7e4adfdd..713cd1146 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -21,7 +21,6 @@ import com.alibaba.fastjson.JSONObject; import com.alibaba.nacos.api.common.Constants; import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.naming.utils.NamingUtils; -import com.alibaba.nacos.naming.boot.SpringContext; import com.alibaba.nacos.naming.cluster.ServerListManager; import com.alibaba.nacos.naming.cluster.servers.Server; import com.alibaba.nacos.naming.consistency.ConsistencyService; @@ -30,7 +29,6 @@ import com.alibaba.nacos.naming.consistency.KeyBuilder; import com.alibaba.nacos.naming.consistency.RecordListener; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeer; import com.alibaba.nacos.naming.consistency.persistent.raft.RaftPeerSet; -import com.alibaba.nacos.naming.core.event.ServiceEvent; import com.alibaba.nacos.naming.misc.*; import com.alibaba.nacos.naming.push.PushService; import com.google.common.collect.Maps; @@ -158,7 +156,6 @@ public class ServiceManager implements RecordListener { } else { putServiceAndInit(service); } - SpringContext.getAppContext().publishEvent(new ServiceEvent(service)); } catch (Throwable e) { Loggers.SRV_LOG.error("[NACOS-SERVICE] error while processing service update", e); } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java b/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java deleted file mode 100644 index 658789782..000000000 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/event/ServiceEvent.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * 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 com.alibaba.nacos.naming.core.event; - -import com.alibaba.nacos.naming.core.Service; -import org.springframework.context.ApplicationEvent; -/** - * @author paderlol - * - */ -public class ServiceEvent extends ApplicationEvent { - - /** - * Create a new ApplicationEvent. - * - * @param source the object on which the event initially occurred (never {@code null}) - */ - public ServiceEvent(Service source) { - super(source); - } -} From 26f2cba100b208a93b199cdbd3207107fceb1575 Mon Sep 17 00:00:00 2001 From: zhanglong Date: Fri, 17 Jan 2020 02:01:05 +0800 Subject: [PATCH 7/7] Fixed heath check error with Nacos by standalone --- console/pom.xml | 1 + .../main/java/com/alibaba/nacos/naming/core/ServiceManager.java | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/console/pom.xml b/console/pom.xml index 12c0cc6fb..b62eed5a8 100644 --- a/console/pom.xml +++ b/console/pom.xml @@ -43,6 +43,7 @@ ${project.groupId} nacos-naming + ${project.groupId} nacos-istio diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java index 713cd1146..17ea6ae04 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/ServiceManager.java @@ -423,7 +423,6 @@ public class ServiceManager implements RecordListener { public void addOrReplaceService(Service service) throws NacosException { consistencyService.put(KeyBuilder.buildServiceMetaKey(service.getNamespaceId(), service.getName()), service); - } public void createEmptyService(String namespaceId, String serviceName, boolean local) throws NacosException {