Complete dns module coding

This commit is contained in:
zhanglong 2019-07-28 19:00:05 +08:00
parent 225452a3ab
commit a9b4c040fa
49 changed files with 1147 additions and 129 deletions

View File

@ -41,7 +41,7 @@
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-naming</artifactId>
<artifactId>nacos-dns</artifactId>
</dependency>
<!-- log -->
<!-- log4j通过slf4j来代理 -->

View File

@ -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/**

View File

@ -6,6 +6,7 @@
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.1.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -21,7 +22,11 @@
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-naming</artifactId>
</dependency>
<dependency>
<groupId>com.ctrip.framework.apollo</groupId>
<artifactId>apollo-client</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>

View File

@ -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) {

View File

@ -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;
}

View File

@ -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";
}

View File

@ -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);
}
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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);
}
}
}
}

View File

@ -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<String, Service> serviceMap = new ConcurrentHashMap<>();
private final Map<String, String> systemConfig = new ConcurrentHashMap<>();
private final Map<String, Integer> domainCacheTime = new ConcurrentHashMap<>();
private final Map<String, CopyOnWriteArrayList<String>> domainForMappingName = new ConcurrentHashMap<>();
private final Map<String, String> mappingNameForDomain = new ConcurrentHashMap<>();
private final Map<String, CopyOnWriteArrayList<String>> domainForCanonicalName = new ConcurrentHashMap<>();
private final Map<String, String> 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<String> 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<String> getMappingName(String domain) {
return Collections.unmodifiableList(domainForMappingName.getOrDefault(domain, new CopyOnWriteArrayList<>()));
public List<String> 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());

View File

@ -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<String, String> 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<String> 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<String> 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);
}

View File

@ -2,6 +2,9 @@ package com.alibaba.nacos.dns.dto;
import java.io.Serializable;
/**
* @author paderlol
*/
public class UpdateDomainDto implements Serializable {
private Integer cacheTime;

View File

@ -12,7 +12,9 @@
*/
package com.alibaba.nacos.dns.exception;
/**
* @author paderlol
*/
public class DomainNotFoundException extends RuntimeException {
public DomainNotFoundException(String message) {

View File

@ -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);
}
}

View File

@ -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);
}

View File

@ -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 {

View File

@ -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()));
}
}

View File

@ -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());
}
}

View File

@ -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()));
}
}

View File

@ -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());
}
}

View File

@ -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<ServiceEvent> {
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);
}

View File

@ -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");
}

View File

@ -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<String> cNames = new ArrayList<>();
/**
* @author paderlol
*/
public class AddressRecord extends BaseRecord {
private List<String> address = new ArrayList<>();
public List<String> getcNames() {
return cNames;
public List<String> getAddress() {
return address;
}
public void setcNames(List<String> cNames) {
this.cNames = cNames;
public void setAddress(List<String> address) {
this.address = address;
}
}

View File

@ -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;

View File

@ -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<String> canonicalNames = new ArrayList<>();
public List<String> getCanonicalNames() {
return canonicalNames;
}
public void setCanonicalNames(List<String> canonicalNames) {
this.canonicalNames = canonicalNames;
}
}

View File

@ -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;

View File

@ -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
}

View File

@ -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<InstanceInfo> instances = new ArrayList<>();
public List<InstanceInfo> getInstances() {
return instances;
}
public void setInstances(List<InstanceInfo> instances) {
this.instances = instances;
}
}

View File

@ -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<String, String> metadata = new HashMap<>();
private Map<String, String> text = new HashMap<>();
public Map<String, String> getMetadata() {
return metadata;
public Map<String, String> getText() {
return text;
}
public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
public void setText(Map<String, String> text) {
this.text = text;
}
}

View File

@ -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();
}

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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<RecordType, RecordConversion> 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<RecordType, RecordConversion> temp = new HashMap<>(16);
Map<String, RecordConversion> 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);
}
}

View File

@ -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;
}
}

View File

@ -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<Instance> instances = service.allIPs();
AddressRecord addressRecord = new AddressRecord();
List<String> address = Lists.newArrayListWithCapacity(instances.size());
for (Instance instance : instances) {
address.add(instance.getIp());
}
addressRecord.setAddress(address);
return addressRecord;
}
}

View File

@ -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;
}
}

View File

@ -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<Instance> instances = service.allIPs();
SrvRecord srvRecord = new SrvRecord();
List<InstanceInfo> 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;
}
}

View File

@ -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<String, String> 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;
}
}

View File

@ -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<String, String> 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);
}

View File

@ -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<String, String> getConfig(String domain) {
com.alibaba.nacos.naming.core.Service service = dnsSource.getServiceByCname(domain);
Map<String, String> 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;
}
}

View File

@ -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);
}

View File

@ -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<String, String> 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);
}

View File

@ -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<String, String> 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");
}
}
}

View File

@ -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;
}
}

View File

@ -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<String, String> 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");
}
}
}

View File

@ -6,10 +6,10 @@
<appender name="dns"
class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_HOME}/naming-server.log</file>
<file>${LOG_HOME}/nacos-dns.log</file>
<append>true</append>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_HOME}/dns.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<fileNamePattern>${LOG_HOME}/nacos-dns.%d{yyyy-MM-dd}.%i</fileNamePattern>
<maxFileSize>2GB</maxFileSize>
<MaxHistory>15</MaxHistory>
<totalSizeCap>7GB</totalSizeCap>

View File

@ -152,6 +152,7 @@ public class ServiceManager implements RecordListener<Service> {
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<Service> {
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 {

View File

@ -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 {
/**

View File

@ -479,6 +479,11 @@
<artifactId>nacos-naming</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-dns</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>nacos-api</artifactId>