Merge pull request #2964 from alibaba/develop

update version to 1.3.0
This commit is contained in:
yanlinly 2020-06-05 16:31:09 +08:00 committed by GitHub
commit c648249822
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
277 changed files with 7605 additions and 3920 deletions

View File

@ -16,7 +16,7 @@
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.3.0-BETA</version>
<version>1.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -24,7 +24,7 @@
<packaging>jar</packaging>
<name>nacos-address ${project.version}</name>
<url>http://maven.apache.org</url>
<url>http://nacos.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -56,13 +56,11 @@
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
</dependencies>
@ -80,7 +78,6 @@
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>findbugs-maven-plugin</artifactId>
<version>3.0.4</version>
</plugin>
</plugins>
</reporting>

View File

@ -21,7 +21,7 @@ import com.alibaba.nacos.address.constant.AddressServerConstants;
import com.alibaba.nacos.address.misc.Loggers;
import com.alibaba.nacos.address.util.AddressServerParamCheckUtil;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.alibaba.nacos.naming.core.Cluster;
import com.alibaba.nacos.naming.core.Instance;
import com.alibaba.nacos.naming.core.Service;

View File

@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.3.0-BETA</version>
<version>1.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -25,7 +25,7 @@
<packaging>jar</packaging>
<name>nacos-api ${project.version}</name>
<url>http://maven.apache.org</url>
<url>http://nacos.io</url>
<build>
<plugins>
<plugin>
@ -45,8 +45,12 @@
<dependencies>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>

View File

@ -0,0 +1,57 @@
/*
* 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.api.exception.runtime;
/**
* Nacos deserialization exception.
*
* @author yangyi
*/
public class NacosDeserializationException extends NacosRuntimeException {
public static final int ERROR_CODE = 101;
private static final long serialVersionUID = -2742350751684273728L;
private static final String DEFAULT_MSG = "Nacos deserialize failed. ";
private static final String MSG_FOR_SPECIFIED_CLASS = "Nacos deserialize for class [%s] failed. ";
private Class<?> targetClass;
public NacosDeserializationException() {
super(ERROR_CODE);
}
public NacosDeserializationException(Class<?> targetClass) {
super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()));
this.targetClass = targetClass;
}
public NacosDeserializationException(Throwable throwable) {
super(ERROR_CODE, DEFAULT_MSG, throwable);
}
public NacosDeserializationException(Class<?> targetClass, Throwable throwable) {
super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, targetClass.getName()), throwable);
this.targetClass = targetClass;
}
public Class<?> getTargetClass() {
return targetClass;
}
}

View File

@ -0,0 +1,59 @@
/*
* 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.api.exception.runtime;
/**
* Nacos runtime exception.
*
* @author yangyi
*/
public class NacosRuntimeException extends RuntimeException {
private static final long serialVersionUID = 3513491993982293262L;
public static final String ERROR_MESSAGE_FORMAT = "errCode: %d, errMsg: %s ";
private int errCode;
public NacosRuntimeException(int errCode) {
super();
this.errCode = errCode;
}
public NacosRuntimeException(int errCode, String errMsg) {
super(String.format(ERROR_MESSAGE_FORMAT, errCode, errMsg));
this.errCode = errCode;
}
public NacosRuntimeException(int errCode, Throwable throwable) {
super(throwable);
this.errCode = errCode;
}
public NacosRuntimeException(int errCode, String errMsg, Throwable throwable) {
super(String.format(ERROR_MESSAGE_FORMAT, errCode, errMsg), throwable);
this.errCode = errCode;
}
public int getErrCode() {
return errCode;
}
public void setErrCode(int errCode) {
this.errCode = errCode;
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.api.exception.runtime;
/**
* Nacos serialization exception.
*
* @author yangyi
*/
public class NacosSerializationException extends NacosRuntimeException {
public static final int ERROR_CODE = 100;
private static final long serialVersionUID = -4308536346316915612L;
private static final String DEFAULT_MSG = "Nacos serialize failed. ";
private static final String MSG_FOR_SPECIFIED_CLASS = "Nacos serialize for class [%s] failed. ";
private Class<?> serializedClass;
public NacosSerializationException() {
super(ERROR_CODE);
}
public NacosSerializationException(Class<?> serializedClass) {
super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()));
this.serializedClass = serializedClass;
}
public NacosSerializationException(Throwable throwable) {
super(ERROR_CODE, DEFAULT_MSG, throwable);
}
public NacosSerializationException(Class<?> serializedClass, Throwable throwable) {
super(ERROR_CODE, String.format(MSG_FOR_SPECIFIED_CLASS, serializedClass.getName()), throwable);
this.serializedClass = serializedClass;
}
public Class<?> getSerializedClass() {
return serializedClass;
}
}

View File

@ -1,291 +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.api.naming.pojo;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.fastjson.serializer.SerializeWriter;
import com.alibaba.nacos.api.common.Constants;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import com.google.common.base.Objects;
/**
* @author nkorange
*/
public abstract class AbstractHealthChecker implements Cloneable {
protected String type = "unknown";
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
/**
* Clone all fields of this instance to another one
*
* @return Another instance with exactly the same fields.
* @throws CloneNotSupportedException
*/
@Override
public abstract AbstractHealthChecker clone() throws CloneNotSupportedException;
/**
* used to JsonAdapter
*/
public void jsonAdapterCallback(SerializeWriter writer) {
// do nothing
}
public static class None extends AbstractHealthChecker {
public static final String TYPE = "NONE";
public None() {
this.setType(TYPE);
}
@Override
public AbstractHealthChecker clone() throws CloneNotSupportedException {
return new None();
}
}
public static class Http extends AbstractHealthChecker {
public static final String TYPE = "HTTP";
private String path = "";
private String headers = "";
private int expectedResponseCode = 200;
public Http() {
this.type = TYPE;
}
public int getExpectedResponseCode() {
return expectedResponseCode;
}
public void setExpectedResponseCode(int expectedResponseCode) {
this.expectedResponseCode = expectedResponseCode;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getHeaders() {
return headers;
}
public void setHeaders(String headers) {
this.headers = headers;
}
@JSONField(serialize = false)
public Map<String, String> getCustomHeaders() {
if (StringUtils.isBlank(headers)) {
return Collections.emptyMap();
}
Map<String, String> headerMap = new HashMap<String, String>(16);
for (String s : headers.split(Constants.NAMING_HTTP_HEADER_SPILIER)) {
String[] splits = s.split(":");
if (splits.length != 2) {
continue;
}
headerMap.put(StringUtils.trim(splits[0]), StringUtils.trim(splits[1]));
}
return headerMap;
}
/**
* used to JsonAdapter
*
* @param writer
*/
@Override
public void jsonAdapterCallback(SerializeWriter writer) {
writer.writeFieldValue(',', "path", getPath());
writer.writeFieldValue(',', "headers", getHeaders());
}
@Override
public int hashCode() {
return Objects.hashCode(path, headers, expectedResponseCode);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Http)) {
return false;
}
Http other = (Http) obj;
if (!strEquals(type, other.getType())) {
return false;
}
if (!strEquals(path, other.getPath())) {
return false;
}
if (!strEquals(headers, other.getHeaders())) {
return false;
}
return expectedResponseCode == other.getExpectedResponseCode();
}
@Override
public Http clone() throws CloneNotSupportedException {
Http config = new Http();
config.setPath(this.getPath());
config.setHeaders(this.getHeaders());
config.setType(this.getType());
config.setExpectedResponseCode(this.getExpectedResponseCode());
return config;
}
}
public static class Tcp extends AbstractHealthChecker {
public static final String TYPE = "TCP";
public Tcp() {
this.type = TYPE;
}
@Override
public int hashCode() {
return Objects.hashCode(TYPE);
}
@Override
public boolean equals(Object obj) {
return obj instanceof Tcp;
}
@Override
public Tcp clone() throws CloneNotSupportedException {
Tcp config = new Tcp();
config.setType(this.type);
return config;
}
}
public static class Mysql extends AbstractHealthChecker {
public static final String TYPE = "MYSQL";
private String user;
private String pwd;
private String cmd;
public Mysql() {
this.type = TYPE;
}
public String getCmd() {
return cmd;
}
public String getPwd() {
return pwd;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
/**
* used to JsonAdapter
*
* @param writer
*/
@Override
public void jsonAdapterCallback(SerializeWriter writer) {
writer.writeFieldValue(',', "user", getUser());
writer.writeFieldValue(',', "pwd", getPwd());
writer.writeFieldValue(',', "cmd", getCmd());
}
@Override
public int hashCode() {
return Objects.hashCode(user, pwd, cmd);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Mysql)) {
return false;
}
Mysql other = (Mysql) obj;
if (!strEquals(user, other.getUser())) {
return false;
}
if (!strEquals(pwd, other.getPwd())) {
return false;
}
return strEquals(cmd, other.getCmd());
}
@Override
public Mysql clone() throws CloneNotSupportedException {
Mysql config = new Mysql();
config.setUser(this.getUser());
config.setPwd(this.getPwd());
config.setCmd(this.getCmd());
config.setType(this.getType());
return config;
}
}
private static boolean strEquals(String str1, String str2) {
return str1 == null ? str2 == null : str1.equals(str2);
}
}

View File

@ -18,6 +18,9 @@ package com.alibaba.nacos.api.naming.pojo;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Tcp;
/**
* Cluster
*
@ -38,7 +41,7 @@ public class Cluster {
/**
* Health check config of this cluster
*/
private AbstractHealthChecker healthChecker = new AbstractHealthChecker.Tcp();
private AbstractHealthChecker healthChecker = new Tcp();
/**
* Default registered port for instances in this cluster.

View File

@ -15,9 +15,11 @@
*/
package com.alibaba.nacos.api.naming.pojo;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.PreservedMetadataKeys;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import org.apache.commons.lang3.StringUtils;
import java.util.HashMap;
@ -30,6 +32,7 @@ import static com.alibaba.nacos.api.common.Constants.NUMBER_PATTERN;
*
* @author nkorange
*/
@JsonInclude(Include.NON_NULL)
public class Instance {
/**
@ -173,7 +176,18 @@ public class Instance {
@Override
public String toString() {
return JSON.toJSONString(this);
return "Instance{" +
"instanceId='" + instanceId + '\'' +
", ip='" + ip + '\'' +
", port=" + port +
", weight=" + weight +
", healthy=" + healthy +
", enabled=" + enabled +
", ephemeral=" + ephemeral +
", clusterName='" + clusterName + '\'' +
", serviceName='" + serviceName + '\'' +
", metadata=" + metadata +
'}';
}
public String toInetAddr() {

View File

@ -15,8 +15,6 @@
*/
package com.alibaba.nacos.api.naming.pojo;
import com.alibaba.fastjson.JSON;
import java.util.List;
/**
@ -47,6 +45,9 @@ public class ListView<T> {
@Override
public String toString() {
return JSON.toJSONString(this);
return "ListView{" +
"data=" + data +
", count=" + count +
'}';
}
}

View File

@ -15,8 +15,10 @@
*/
package com.alibaba.nacos.api.naming.pojo;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.api.common.Constants;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
@ -29,10 +31,12 @@ import java.util.List;
*
* @author nkorange
*/
@JsonInclude(Include.NON_NULL)
public class ServiceInfo {
@JSONField(serialize = false)
@JsonIgnore
private String jsonFromServer = EMPTY;
public static final String SPLITER = "@@";
private String name;
@ -43,7 +47,6 @@ public class ServiceInfo {
private long cacheMillis = 1000L;
@JSONField(name = "hosts")
private List<Instance> hosts = new ArrayList<Instance>();
private long lastRefTime = 0L;
@ -162,7 +165,7 @@ public class ServiceInfo {
return true;
}
@JSONField(serialize = false)
@JsonIgnore
public String getJsonFromServer() {
return jsonFromServer;
}
@ -171,12 +174,12 @@ public class ServiceInfo {
this.jsonFromServer = jsonFromServer;
}
@JSONField(serialize = false)
@JsonIgnore
public String getKey() {
return getKey(name, clusters);
}
@JSONField(serialize = false)
@JsonIgnore
public String getKeyEncoded() {
try {
return getKey(URLEncoder.encode(name, "UTF-8"), clusters);
@ -185,7 +188,6 @@ public class ServiceInfo {
}
}
@JSONField(serialize = false)
public static ServiceInfo fromKey(String key) {
ServiceInfo serviceInfo = new ServiceInfo();
int maxSegCount = 3;
@ -201,7 +203,7 @@ public class ServiceInfo {
return serviceInfo;
}
@JSONField(serialize = false)
@JsonIgnore
public static String getKey(String name, String clusters) {
if (!isEmpty(clusters)) {

View File

@ -0,0 +1,75 @@
/*
* 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.api.naming.pojo.healthcheck;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker.None;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Tcp;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
/**
* @author nkorange
*/
@JsonTypeInfo(use = Id.NAME, property = "type", defaultImpl = None.class)
@JsonSubTypes({
@JsonSubTypes.Type(name = Http.TYPE, value = Http.class),
@JsonSubTypes.Type(name = Mysql.TYPE, value = Mysql.class),
@JsonSubTypes.Type(name = Tcp.TYPE, value = Tcp.class)
})
public abstract class AbstractHealthChecker implements Cloneable {
@JsonIgnore
protected final String type;
protected AbstractHealthChecker(String type) {
this.type = type;
}
public String getType() {
return type;
}
/**
* Clone all fields of this instance to another one.
*
* @return Another instance with exactly the same fields
* @throws CloneNotSupportedException clone not supported exception
*/
@Override
public abstract AbstractHealthChecker clone() throws CloneNotSupportedException;
/**
* Default implementation of Health checker.
*/
public static class None extends AbstractHealthChecker {
public static final String TYPE = "NONE";
public None() {
super(TYPE);
}
@Override
public AbstractHealthChecker clone() throws CloneNotSupportedException {
return new None();
}
}
}

View File

@ -13,9 +13,11 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.nacos.naming.healthcheck;
package com.alibaba.nacos.api.naming.pojo.healthcheck;
import com.alibaba.nacos.api.naming.pojo.AbstractHealthChecker;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Http;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Mysql;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Tcp;
import java.util.ArrayList;
import java.util.List;
@ -29,37 +31,36 @@ public enum HealthCheckType {
/**
* TCP type
*/
TCP("tcp", AbstractHealthChecker.Tcp.class),
TCP(Tcp.class),
/**
* HTTP type
*/
HTTP("http", AbstractHealthChecker.Http.class),
HTTP(Http.class),
/**
* MySQL type
*/
MYSQL("mysql", AbstractHealthChecker.Mysql.class),
MYSQL(Mysql.class),
/**
* No check
*/
NONE("none", AbstractHealthChecker.None.class);
NONE(AbstractHealthChecker.None.class);
private String name;
private final Class<? extends AbstractHealthChecker> healthCheckerClass;
private Class healthCheckerClass;
private static final Map<String, Class<? extends AbstractHealthChecker>> EXTEND = new ConcurrentHashMap<String, Class<? extends AbstractHealthChecker>>();
private static Map<String, Class> EXTEND =
new ConcurrentHashMap<>();
HealthCheckType(String name, Class healthCheckerClass) {
this.name = name;
HealthCheckType(Class<? extends AbstractHealthChecker> healthCheckerClass) {
this.healthCheckerClass = healthCheckerClass;
}
public static void registerHealthChecker(String type, Class healthCheckerClass){
EXTEND.putIfAbsent(type, healthCheckerClass);
public static void registerHealthChecker(String type, Class<? extends AbstractHealthChecker> healthCheckerClass){
if (!EXTEND.containsKey(type)) {
EXTEND.put(type, healthCheckerClass);
HealthCheckerFactory.registerSubType(healthCheckerClass, type);
}
}
public static Class ofHealthCheckerClass(String type){
public static Class<? extends AbstractHealthChecker> ofHealthCheckerClass(String type){
HealthCheckType enumType;
try {
enumType = valueOf(type);
@ -69,12 +70,12 @@ public enum HealthCheckType {
return enumType.healthCheckerClass;
}
public static List<Class> getLoadedHealthCheckerClasses(){
List<Class> all = new ArrayList<>();
public static List<Class<? extends AbstractHealthChecker>> getLoadedHealthCheckerClasses(){
List<Class<? extends AbstractHealthChecker>> all = new ArrayList<Class<? extends AbstractHealthChecker>>();
for(HealthCheckType type : values()){
all.add(type.healthCheckerClass);
}
for(Map.Entry<String, Class> entry : EXTEND.entrySet()){
for(Map.Entry<String, Class<? extends AbstractHealthChecker>> entry : EXTEND.entrySet()){
all.add(entry.getValue());
}
return all;

View File

@ -0,0 +1,97 @@
/*
* 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.api.naming.pojo.healthcheck;
import java.io.IOException;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker.None;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
/**
* health checker factory.
*
* @author yangyi
*/
public class HealthCheckerFactory {
private static final ObjectMapper MAPPER = new ObjectMapper();
static {
MAPPER.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
/**
* Register new sub type of health checker to factory for serialize and deserialize.
*
* @param extendHealthChecker extend health checker
*/
public static void registerSubType(AbstractHealthChecker extendHealthChecker) {
registerSubType(extendHealthChecker.getClass(), extendHealthChecker.getType());
}
/**
* Register new sub type of health checker to factory for serialize and deserialize.
*
* @param extendHealthCheckerClass extend health checker
* @param typeName typeName of health checker
*/
public static void registerSubType(Class<? extends AbstractHealthChecker> extendHealthCheckerClass, String typeName) {
MAPPER.registerSubtypes(new NamedType(extendHealthCheckerClass, typeName));
}
/**
* Create default {@link None} health checker.
*
* @return new none health checker
*/
public static None createNoneHealthChecker() {
return new None();
}
/**
* Deserialize and create a instance of health checker.
*
* @param jsonString json string of health checker
* @return new instance
*/
public static AbstractHealthChecker deserialize(String jsonString) {
try {
return MAPPER.readValue(jsonString, AbstractHealthChecker.class);
} catch (IOException e) {
// TODO replace with NacosDeserializeException.
throw new RuntimeException("Deserialize health checker from json failed", e);
}
}
/**
* Serialize a instance of health checker to json
*
* @param healthChecker health checker instance
* @return son string after serializing
*/
public static String serialize(AbstractHealthChecker healthChecker) {
try {
return MAPPER.writeValueAsString(healthChecker);
} catch (JsonProcessingException e) {
// TODO replace with NacosSerializeException.
throw new RuntimeException("Serialize health checker to json failed", e);
}
}
}

View File

@ -0,0 +1,121 @@
/*
* 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.api.naming.pojo.healthcheck.impl;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.base.Objects;
import org.apache.commons.lang3.StringUtils;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* Implementation of health checker for HTTP.
*
* @author yangyi
*/
public class Http extends AbstractHealthChecker {
public static final String TYPE = "HTTP";
private String path = "";
private String headers = "";
private int expectedResponseCode = 200;
public Http() {
super(TYPE);
}
public int getExpectedResponseCode() {
return expectedResponseCode;
}
public void setExpectedResponseCode(int expectedResponseCode) {
this.expectedResponseCode = expectedResponseCode;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
public String getHeaders() {
return headers;
}
public void setHeaders(String headers) {
this.headers = headers;
}
@JsonIgnore
public Map<String, String> getCustomHeaders() {
if (StringUtils.isBlank(headers)) {
return Collections.emptyMap();
}
Map<String, String> headerMap = new HashMap<String, String>(16);
for (String s : headers.split(Constants.NAMING_HTTP_HEADER_SPILIER)) {
String[] splits = s.split(":");
if (splits.length != 2) {
continue;
}
headerMap.put(StringUtils.trim(splits[0]), StringUtils.trim(splits[1]));
}
return headerMap;
}
@Override
public int hashCode() {
return Objects.hashCode(path, headers, expectedResponseCode);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Http)) {
return false;
}
Http other = (Http) obj;
if (!StringUtils.equals(type, other.getType())) {
return false;
}
if (!StringUtils.equals(path, other.getPath())) {
return false;
}
if (!StringUtils.equals(headers, other.getHeaders())) {
return false;
}
return expectedResponseCode == other.getExpectedResponseCode();
}
@Override
public Http clone() throws CloneNotSupportedException {
Http config = new Http();
config.setPath(this.getPath());
config.setHeaders(this.getHeaders());
config.setExpectedResponseCode(this.getExpectedResponseCode());
return config;
}
}

View File

@ -0,0 +1,98 @@
/*
* 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.api.naming.pojo.healthcheck.impl;
import org.apache.commons.lang3.StringUtils;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.google.common.base.Objects;
/**
* Implementation of health checker for MYSQL.
*
* @author yangyi
*/
public class Mysql extends AbstractHealthChecker {
public static final String TYPE = "MYSQL";
private String user;
private String pwd;
private String cmd;
public Mysql() {
super(TYPE);
}
public String getCmd() {
return cmd;
}
public String getPwd() {
return pwd;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public void setCmd(String cmd) {
this.cmd = cmd;
}
public void setPwd(String pwd) {
this.pwd = pwd;
}
@Override
public int hashCode() {
return Objects.hashCode(user, pwd, cmd);
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof Mysql)) {
return false;
}
Mysql other = (Mysql) obj;
if (!StringUtils.equals(user, other.getUser())) {
return false;
}
if (!StringUtils.equals(pwd, other.getPwd())) {
return false;
}
return StringUtils.equals(cmd, other.getCmd());
}
@Override
public Mysql clone() throws CloneNotSupportedException {
Mysql config = new Mysql();
config.setUser(this.getUser());
config.setPwd(this.getPwd());
config.setCmd(this.getCmd());
return config;
}
}

View File

@ -0,0 +1,49 @@
/*
* 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.api.naming.pojo.healthcheck.impl;
import com.alibaba.nacos.api.naming.pojo.healthcheck.AbstractHealthChecker;
import com.google.common.base.Objects;
/**
* Implementation of health checker for TCP.
*
* @author yangyi
*/
public class Tcp extends AbstractHealthChecker {
public static final String TYPE = "TCP";
public Tcp() {
super(TYPE);
}
@Override
public int hashCode() {
return Objects.hashCode(TYPE);
}
@Override
public boolean equals(Object obj) {
return obj instanceof Tcp;
}
@Override
public Tcp clone() throws CloneNotSupportedException {
return new Tcp();
}
}

View File

@ -15,24 +15,30 @@
*/
package com.alibaba.nacos.api.selector;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
/**
* Abstract selector that only contains a type
*
* @author nkorange
* @since 0.7.0
*/
@JsonTypeInfo(use = Id.NAME, property = "type", defaultImpl = NoneSelector.class)
public abstract class AbstractSelector {
/**
* The type of this selector, each child class should announce its own unique type.
*/
private String type;
@JsonIgnore
private final String type;
protected AbstractSelector(String type) {
this.type = type;
}
public String getType() {
return type;
}
protected void setType(String type) {
this.type = type;
}
}

View File

@ -29,7 +29,7 @@ public class ExpressionSelector extends AbstractSelector {
private String expression;
public ExpressionSelector() {
this.setType(SelectorType.label.name());
super(SelectorType.label.name());
}
public String getExpression() {

View File

@ -23,6 +23,6 @@ package com.alibaba.nacos.api.selector;
public class NoneSelector extends AbstractSelector {
public NoneSelector() {
this.setType(SelectorType.none.name());
super(SelectorType.none.name());
}
}

View File

@ -15,7 +15,6 @@
*/
package com.alibaba.nacos.api.annotation;
import com.alibaba.fastjson.JSON;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.mock.env.MockEnvironment;

View File

@ -0,0 +1,71 @@
/*
* 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.api.naming.pojo;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static org.junit.Assert.*;
public class ServiceInfoTest {
private ObjectMapper mapper;
private ServiceInfo serviceInfo;
@Before
public void setUp() throws Exception {
mapper = new ObjectMapper();
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
serviceInfo = new ServiceInfo("testName", "testClusters");
}
@Test
public void testSerialize() throws JsonProcessingException {
String actual = mapper.writeValueAsString(serviceInfo);
assertTrue(actual.contains("\"name\":\"testName\""));
assertTrue(actual.contains("\"clusters\":\"testClusters\""));
assertTrue(actual.contains("\"cacheMillis\":1000"));
assertTrue(actual.contains("\"hosts\":[]"));
assertTrue(actual.contains("\"lastRefTime\":0"));
assertTrue(actual.contains("\"checksum\":\"\""));
assertTrue(actual.contains("\"valid\":true"));
assertTrue(actual.contains("\"allIPs\":false"));
assertFalse(actual.contains("jsonFromServer"));
assertFalse(actual.contains("key"));
assertFalse(actual.contains("keyEncoded"));
}
@Test
public void testDeserialize() throws IOException {
String example = "{\"name\":\"testName\",\"clusters\":\"testClusters\",\"cacheMillis\":1000,\"hosts\":[],\"lastRefTime\":0,\"checksum\":\"\",\"allIPs\":false,\"valid\":true,\"groupName\":\"\"}";
ServiceInfo actual = mapper.readValue(example, ServiceInfo.class);
assertEquals("testName", actual.getName());
assertEquals("testClusters", actual.getClusters());
assertEquals("", actual.getChecksum());
assertEquals("", actual.getGroupName());
assertEquals(1000, actual.getCacheMillis());
assertEquals(0, actual.getLastRefTime());
assertTrue(actual.getHosts().isEmpty());
assertTrue(actual.isValid());
assertFalse(actual.isAllIPs());
}
}

View File

@ -0,0 +1,57 @@
/*
* 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.api.naming.pojo.healthcheck;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
public class AbstractHealthCheckerTest {
private ObjectMapper objectMapper = new ObjectMapper();
@Before
public void setUp() {
objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
objectMapper.registerSubtypes(new NamedType(TestChecker.class, TestChecker.TYPE));
}
@Test
public void testSerialize() throws JsonProcessingException {
TestChecker testChecker = new TestChecker();
testChecker.setTestValue("");
String actual = objectMapper.writeValueAsString(testChecker);
assertTrue(actual.contains("\"testValue\":\"\""));
assertTrue(actual.contains("\"type\":\"TEST\""));
}
@Test
public void testDeserialize() throws IOException {
String testChecker = "{\"type\":\"TEST\",\"testValue\":\"\"}";
TestChecker actual = objectMapper.readValue(testChecker, TestChecker.class);
assertEquals("", actual.getTestValue());
assertEquals(TestChecker.TYPE, actual.getType());
}
}

View File

@ -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.api.naming.pojo.healthcheck;
import static org.junit.Assert.*;
import org.junit.Test;
import com.alibaba.nacos.api.naming.pojo.healthcheck.impl.Tcp;
public class HealthCheckerFactoryTest {
@Test
public void testSerialize() {
Tcp tcp = new Tcp();
String actual = HealthCheckerFactory.serialize(tcp);
assertTrue(actual.contains("\"type\":\"TCP\""));
}
@Test
public void testSerializeExtend() {
HealthCheckerFactory.registerSubType(TestChecker.class, TestChecker.TYPE);
TestChecker testChecker = new TestChecker();
String actual = HealthCheckerFactory.serialize(testChecker);
assertTrue(actual.contains("\"type\":\"TEST\""));
}
@Test
public void testDeserialize() {
String tcpString = "{\"type\":\"TCP\"}";
AbstractHealthChecker actual = HealthCheckerFactory.deserialize(tcpString);
assertEquals(Tcp.class, actual.getClass());
}
@Test
public void testDeserializeExtend() {
String tcpString = "{\"type\":\"TEST\",\"testValue\":null}";
AbstractHealthChecker actual = HealthCheckerFactory.deserialize(tcpString);
assertEquals(TestChecker.class, actual.getClass());
}
}

View File

@ -0,0 +1,45 @@
/*
* 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.api.naming.pojo.healthcheck;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
public class TestChecker extends AbstractHealthChecker {
@JsonTypeInfo(use = Id.NAME, property = "type")
public static final String TYPE = "TEST";
private String testValue;
public String getTestValue() {
return testValue;
}
public void setTestValue(String testValue) {
this.testValue = testValue;
}
public TestChecker() {
super(TYPE);
}
@Override
public AbstractHealthChecker clone() throws CloneNotSupportedException {
return null;
}
}

View File

@ -0,0 +1,80 @@
/*
* 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.api.naming.pojo.healthcheck.impl;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.Map;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class HttpTest {
private ObjectMapper objectMapper;
private Http http;
@Before
public void setUp() {
objectMapper = new ObjectMapper();
http = new Http();
}
@Test
public void testGetExpectedResponseCodeWithEmpty() {
http.setHeaders("");
assertTrue(http.getCustomHeaders().isEmpty());
}
@Test
public void testGetExpectedResponseCodeWithoutEmpty() {
http.setHeaders("x:a|y:");
Map<String, String> actual = http.getCustomHeaders();
assertFalse(actual.isEmpty());
assertEquals(1, actual.size());
assertEquals("a", actual.get("x"));
}
@Test
public void testSerialize() throws JsonProcessingException {
http.setHeaders("x:a|y:");
http.setPath("/x");
String actual = objectMapper.writeValueAsString(http);
assertTrue(actual.contains("\"path\":\"/x\""));
assertTrue(actual.contains("\"type\":\"HTTP\""));
assertTrue(actual.contains("\"headers\":\"x:a|y:\""));
assertTrue(actual.contains("\"expectedResponseCode\":200"));
}
@Test
public void testDeserialize() throws IOException {
String testChecker = "{\"type\":\"HTTP\",\"path\":\"/x\",\"headers\":\"x:a|y:\",\"expectedResponseCode\":200}";
Http actual = objectMapper.readValue(testChecker, Http.class);
assertEquals("x:a|y:", actual.getHeaders());
assertEquals("/x", actual.getPath());
assertEquals(200, actual.getExpectedResponseCode());
assertEquals("x:a|y:", actual.getHeaders());
assertEquals(Http.TYPE, actual.getType());
}
}

View File

@ -0,0 +1,62 @@
/*
* 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.api.naming.pojo.healthcheck.impl;
import static org.junit.Assert.*;
import java.io.IOException;
import org.junit.Before;
import org.junit.Test;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class MysqlTest {
private ObjectMapper objectMapper;
private Mysql mysql;
@Before
public void setUp() throws Exception {
mysql = new Mysql();
mysql.setUser("user");
mysql.setPwd("pwd");
mysql.setCmd("cmd");
objectMapper = new ObjectMapper();
}
@Test
public void testSerialize() throws JsonProcessingException {
String actual = objectMapper.writeValueAsString(mysql);
assertTrue(actual.contains("\"user\":\"user\""));
assertTrue(actual.contains("\"type\":\"MYSQL\""));
assertTrue(actual.contains("\"pwd\":\"pwd\""));
assertTrue(actual.contains("\"cmd\":\"cmd\""));
}
@Test
public void testDeserialize() throws IOException {
String testChecker = "{\"type\":\"MYSQL\",\"user\":\"user\",\"pwd\":\"pwd\",\"cmd\":\"cmd\"}";
Mysql actual = objectMapper.readValue(testChecker, Mysql.class);
assertEquals("cmd", actual.getCmd());
assertEquals("pwd", actual.getPwd());
assertEquals("user", actual.getUser());
assertEquals(Mysql.TYPE, actual.getType());
}
}

View File

@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.3.0-BETA</version>
<version>1.3.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
@ -26,7 +26,7 @@
<packaging>jar</packaging>
<name>nacos-client ${project.version}</name>
<url>https://github.com/alibaba/nacos</url>
<url>http://nacos.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -109,7 +109,6 @@
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient</artifactId>
<version>0.5.0</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>

View File

@ -24,11 +24,11 @@ import com.alibaba.nacos.client.config.impl.ServerListManager;
import com.alibaba.nacos.client.config.impl.SpasAdapter;
import com.alibaba.nacos.client.identify.STSConfig;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.JSONUtils;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import org.apache.commons.lang3.StringUtils;
@ -366,8 +366,7 @@ public class ServerHttpAgent implements HttpAgent {
}
}
String stsResponse = getSTSResponse();
STSCredential stsCredentialTmp = JSONUtils.deserializeObject(stsResponse,
new TypeReference<STSCredential>() {
STSCredential stsCredentialTmp = JacksonUtils.toObj(stsResponse, new TypeReference<STSCredential>() {
});
sTSCredential = stsCredentialTmp;
LOGGER.info("[getSTSCredential] code:{}, accessKeyId:{}, lastUpdated:{}, expiration:{}", sTSCredential.getCode(),

View File

@ -61,6 +61,7 @@ public class NacosNamingMaintainService implements NamingMaintainService {
private void init(Properties properties) {
ValidatorUtils.checkInitParam(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
initServerAddr(properties);
InitUtils.initWebRootContext();
serverProxy = new NamingProxy(namespace, endpoint, serverList, properties);

View File

@ -34,7 +34,6 @@ import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.InitUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.client.security.SecurityProxy;
import com.alibaba.nacos.client.utils.ValidatorUtils;
import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.StringUtils;
@ -44,7 +43,6 @@ import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.TimeUnit;
/**
* Nacos Naming Service
@ -88,6 +86,7 @@ public class NacosNamingService implements NamingService {
private void init(Properties properties) {
ValidatorUtils.checkInitParam(properties);
namespace = InitUtils.initNamespaceForNaming(properties);
InitUtils.initSerialization();
initServerAddr(properties);
InitUtils.initWebRootContext();
initCacheDir();

View File

@ -15,13 +15,14 @@
*/
package com.alibaba.nacos.client.naming.backups;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.cache.ConcurrentDiskUtil;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.core.HostReactor;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.utils.JacksonUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
@ -180,7 +181,7 @@ public class FailoverReactor {
String json;
if ((json = reader.readLine()) != null) {
try {
dom = JSON.parseObject(json, ServiceInfo.class);
dom = JacksonUtils.toObj(json, ServiceInfo.class);
} catch (Exception e) {
NAMING_LOGGER.error("[NA] error while parsing cached dom : " + json, e);
}

View File

@ -15,8 +15,6 @@
*/
package com.alibaba.nacos.client.naming.beat;
import com.alibaba.fastjson.JSON;
import java.util.Map;
/**
@ -36,7 +34,17 @@ public class BeatInfo {
@Override
public String toString() {
return JSON.toJSONString(this);
return "BeatInfo{" +
"port=" + port +
", ip='" + ip + '\'' +
", weight=" + weight +
", serviceName='" + serviceName + '\'' +
", cluster='" + cluster + '\'' +
", metadata=" + metadata +
", scheduled=" + scheduled +
", period=" + period +
", stopped=" + stopped +
'}';
}
public String getServiceName() {

View File

@ -15,8 +15,6 @@
*/
package com.alibaba.nacos.client.naming.beat;
import com.alibaba.fastjson.JSON;
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.CommonParams;
@ -26,6 +24,8 @@ import com.alibaba.nacos.api.naming.utils.NamingUtils;
import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import java.util.Map;
import java.util.concurrent.*;
@ -43,7 +43,7 @@ public class BeatReactor {
private boolean lightBeatEnabled = false;
public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<String, BeatInfo>();
public final Map<String, BeatInfo> dom2Beat = new ConcurrentHashMap<>();
public BeatReactor(NamingProxy serverProxy) {
this(serverProxy, UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
@ -105,19 +105,19 @@ public class BeatReactor {
}
long nextTime = beatInfo.getPeriod();
try {
JSONObject result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
long interval = result.getIntValue("clientBeatInterval");
JsonNode result = serverProxy.sendBeat(beatInfo, BeatReactor.this.lightBeatEnabled);
long interval = result.get("clientBeatInterval").asInt();
boolean lightBeatEnabled = false;
if (result.containsKey(CommonParams.LIGHT_BEAT_ENABLED)) {
lightBeatEnabled = result.getBooleanValue(CommonParams.LIGHT_BEAT_ENABLED);
if (result.has(CommonParams.LIGHT_BEAT_ENABLED)) {
lightBeatEnabled = result.get(CommonParams.LIGHT_BEAT_ENABLED).asBoolean();
}
BeatReactor.this.lightBeatEnabled = lightBeatEnabled;
if (interval > 0) {
nextTime = interval;
}
int code = NamingResponseCode.OK;
if (result.containsKey(CommonParams.CODE)) {
code = result.getIntValue(CommonParams.CODE);
if (result.has(CommonParams.CODE)) {
code = result.get(CommonParams.CODE).asInt();
}
if (code == NamingResponseCode.RESOURCE_NOT_FOUND) {
Instance instance = new Instance();
@ -137,7 +137,7 @@ public class BeatReactor {
}
} catch (NacosException ne) {
NAMING_LOGGER.error("[CLIENT-BEAT] failed to send beat: {}, code: {}, msg: {}",
JSON.toJSONString(beatInfo), ne.getErrCode(), ne.getErrMsg());
JacksonUtils.toJson(beatInfo), ne.getErrCode(), ne.getErrMsg());
}
executorService.schedule(new BeatTask(beatInfo), nextTime, TimeUnit.MILLISECONDS);

View File

@ -15,11 +15,12 @@
*/
package com.alibaba.nacos.client.naming.cache;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.utils.CollectionUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import org.apache.commons.lang3.StringUtils;
import java.io.BufferedReader;
@ -58,7 +59,7 @@ public class DiskCache {
String json = dom.getJsonFromServer();
if (StringUtils.isEmpty(json)) {
json = JSON.toJSONString(dom);
json = JacksonUtils.toJson(dom);
}
keyContentBuffer.append(json);
@ -112,10 +113,10 @@ public class DiskCache {
continue;
}
newFormat = JSON.parseObject(json, ServiceInfo.class);
newFormat = JacksonUtils.toObj(json, ServiceInfo.class);
if (StringUtils.isEmpty(newFormat.getName())) {
ips.add(JSON.parseObject(json, Instance.class));
ips.add(JacksonUtils.toObj(json, Instance.class));
}
} catch (Throwable e) {
NAMING_LOGGER.error("[NA] error while parsing cache file: " + json, e);

View File

@ -15,7 +15,6 @@
*/
package com.alibaba.nacos.client.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
@ -24,6 +23,8 @@ import com.alibaba.nacos.client.naming.backups.FailoverReactor;
import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import com.alibaba.nacos.common.utils.JacksonUtils;
import org.apache.commons.lang3.StringUtils;
import java.util.*;
@ -98,7 +99,7 @@ public class HostReactor {
}
public ServiceInfo processServiceJSON(String json) {
ServiceInfo serviceInfo = JSON.parseObject(json, ServiceInfo.class);
ServiceInfo serviceInfo = JacksonUtils.toObj(json, ServiceInfo.class);
ServiceInfo oldService = serviceInfoMap.get(serviceInfo.getKey());
if (serviceInfo.getHosts() == null || !serviceInfo.validate()) {
//empty or error push, just ignore
@ -162,19 +163,19 @@ public class HostReactor {
if (newHosts.size() > 0) {
changed = true;
NAMING_LOGGER.info("new ips(" + newHosts.size() + ") service: "
+ serviceInfo.getKey() + " -> " + JSON.toJSONString(newHosts));
+ serviceInfo.getKey() + " -> " + JacksonUtils.toJson(newHosts));
}
if (remvHosts.size() > 0) {
changed = true;
NAMING_LOGGER.info("removed ips(" + remvHosts.size() + ") service: "
+ serviceInfo.getKey() + " -> " + JSON.toJSONString(remvHosts));
+ serviceInfo.getKey() + " -> " + JacksonUtils.toJson(remvHosts));
}
if (modHosts.size() > 0) {
changed = true;
NAMING_LOGGER.info("modified ips(" + modHosts.size() + ") service: "
+ serviceInfo.getKey() + " -> " + JSON.toJSONString(modHosts));
+ serviceInfo.getKey() + " -> " + JacksonUtils.toJson(modHosts));
}
serviceInfo.setJsonFromServer(json);
@ -186,8 +187,8 @@ public class HostReactor {
} else {
changed = true;
NAMING_LOGGER.info("init new ips(" + serviceInfo.ipCount() + ") service: " + serviceInfo.getKey() + " -> " + JSON
.toJSONString(serviceInfo.getHosts()));
NAMING_LOGGER.info("init new ips(" + serviceInfo.ipCount() + ") service: " + serviceInfo.getKey() + " -> " +
JacksonUtils.toJson(serviceInfo.getHosts()));
serviceInfoMap.put(serviceInfo.getKey(), serviceInfo);
eventDispatcher.serviceChanged(serviceInfo);
serviceInfo.setJsonFromServer(json);
@ -198,7 +199,7 @@ public class HostReactor {
if (changed) {
NAMING_LOGGER.info("current ips:(" + serviceInfo.ipCount() + ") service: " + serviceInfo.getKey() +
" -> " + JSON.toJSONString(serviceInfo.getHosts()));
" -> " + JacksonUtils.toJson(serviceInfo.getHosts()));
}
return serviceInfo;
@ -214,7 +215,7 @@ public class HostReactor {
public ServiceInfo getServiceInfoDirectlyFromServer(final String serviceName, final String clusters) throws NacosException {
String result = serverProxy.queryList(serviceName, clusters, 0, false);
if (StringUtils.isNotEmpty(result)) {
return JSON.parseObject(result, ServiceInfo.class);
return JacksonUtils.toObj(result, ServiceInfo.class);
}
return null;
}

View File

@ -15,7 +15,7 @@
*/
package com.alibaba.nacos.client.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.common.utils.IoUtils;
@ -75,7 +75,7 @@ public class PushReceiver implements Runnable {
String json = new String(IoUtils.tryDecompress(packet.getData()), "UTF-8").trim();
NAMING_LOGGER.info("received push data: " + json + " from " + packet.getAddress().toString());
PushPacket pushPacket = JSON.parseObject(json, PushPacket.class);
PushPacket pushPacket = JacksonUtils.toObj(json, PushPacket.class);
String ack;
if ("dom".equals(pushPacket.type) || "service".equals(pushPacket.type)) {
hostReactor.processServiceJSON(pushPacket.data);
@ -89,7 +89,7 @@ public class PushReceiver implements Runnable {
ack = "{\"type\": \"dump-ack\""
+ ", \"lastRefTime\": \"" + pushPacket.lastRefTime
+ "\", \"data\":" + "\""
+ StringUtils.escapeJavaScript(JSON.toJSONString(hostReactor.getServiceInfoMap()))
+ StringUtils.escapeJavaScript(JacksonUtils.toJson(hostReactor.getServiceInfoMap()))
+ "\"}";
} else {
// do nothing send ack only

View File

@ -129,8 +129,14 @@ public class HttpClient {
if (encodingGzip.equals(respHeaders.get(HttpHeaders.CONTENT_ENCODING))) {
inputStream = new GZIPInputStream(inputStream);
}
return new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)), respHeaders);
HttpResult httpResult = new HttpResult(respCode, IoUtils.toString(inputStream, getCharset(conn)), respHeaders);
//InputStream from HttpURLConnection can be closed automatically,but new GZIPInputStream can't be closed automatically
//so needs to close it manually
if (inputStream instanceof GZIPInputStream) {
inputStream.close();
}
return httpResult;
}
private static String getCharset(HttpURLConnection conn) {

View File

@ -15,9 +15,6 @@
*/
package com.alibaba.nacos.client.naming.net;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
@ -42,8 +39,12 @@ import com.alibaba.nacos.client.utils.TemplateUtils;
import com.alibaba.nacos.common.constant.HttpHeaderConsts;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.common.utils.IoUtils;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.UuidUtils;
import com.alibaba.nacos.common.utils.VersionUtils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.lang3.StringUtils;
import java.io.IOException;
@ -207,7 +208,7 @@ public class NamingProxy {
params.put("enable", String.valueOf(instance.isEnabled()));
params.put("healthy", String.valueOf(instance.isHealthy()));
params.put("ephemeral", String.valueOf(instance.isEphemeral()));
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
params.put("metadata", JacksonUtils.toJson(instance.getMetadata()));
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.POST);
@ -243,7 +244,7 @@ public class NamingProxy {
params.put("weight", String.valueOf(instance.getWeight()));
params.put("enabled", String.valueOf(instance.isEnabled()));
params.put("ephemeral", String.valueOf(instance.isEphemeral()));
params.put("metadata", JSON.toJSONString(instance.getMetadata()));
params.put("metadata", JacksonUtils.toJson(instance.getMetadata()));
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.PUT);
}
@ -258,8 +259,7 @@ public class NamingProxy {
params.put(CommonParams.GROUP_NAME, groupName);
String result = reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.GET);
JSONObject jsonObject = JSON.parseObject(result);
return jsonObject.toJavaObject(Service.class);
return JacksonUtils.toObj(result, Service.class);
}
public void createService(Service service, AbstractSelector selector) throws NacosException {
@ -272,8 +272,8 @@ public class NamingProxy {
params.put(CommonParams.SERVICE_NAME, service.getName());
params.put(CommonParams.GROUP_NAME, service.getGroupName());
params.put("protectThreshold", String.valueOf(service.getProtectThreshold()));
params.put("metadata", JSON.toJSONString(service.getMetadata()));
params.put("selector", JSON.toJSONString(selector));
params.put("metadata", JacksonUtils.toJson(service.getMetadata()));
params.put("selector", JacksonUtils.toJson(selector));
reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.POST);
@ -301,8 +301,8 @@ public class NamingProxy {
params.put(CommonParams.SERVICE_NAME, service.getName());
params.put(CommonParams.GROUP_NAME, service.getGroupName());
params.put("protectThreshold", String.valueOf(service.getProtectThreshold()));
params.put("metadata", JSON.toJSONString(service.getMetadata()));
params.put("selector", JSON.toJSONString(selector));
params.put("metadata", JacksonUtils.toJson(service.getMetadata()));
params.put("selector", JacksonUtils.toJson(selector));
reqAPI(UtilAndComs.NACOS_URL_SERVICE, params, HttpMethod.PUT);
}
@ -321,7 +321,7 @@ public class NamingProxy {
return reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/list", params, HttpMethod.GET);
}
public JSONObject sendBeat(BeatInfo beatInfo, boolean lightBeatEnabled) throws NacosException {
public JsonNode sendBeat(BeatInfo beatInfo, boolean lightBeatEnabled) throws NacosException {
if (NAMING_LOGGER.isDebugEnabled()) {
NAMING_LOGGER.debug("[BEAT] {} sending beat to server: {}", namespaceId, beatInfo.toString());
@ -330,7 +330,7 @@ public class NamingProxy {
String body = StringUtils.EMPTY;
if (!lightBeatEnabled) {
try {
body = "beat=" + URLEncoder.encode(JSON.toJSONString(beatInfo), "UTF-8");
body = "beat=" + URLEncoder.encode(JacksonUtils.toJson(beatInfo), "UTF-8");
} catch (UnsupportedEncodingException e) {
throw new NacosException(NacosException.SERVER_ERROR, "encode beatInfo error", e);
}
@ -341,7 +341,7 @@ public class NamingProxy {
params.put("ip", beatInfo.getIp());
params.put("port", String.valueOf(beatInfo.getPort()));
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/instance/beat", params, body, HttpMethod.PUT);
return JSON.parseObject(result);
return JacksonUtils.toObj(result);
}
public boolean serverHealthy() {
@ -349,8 +349,8 @@ public class NamingProxy {
try {
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/operator/metrics",
new HashMap<String, String>(2), HttpMethod.GET);
JSONObject json = JSON.parseObject(result);
String serverStatus = json.getString("status");
JsonNode json = JacksonUtils.toObj(result);
String serverStatus = json.get("status").asText();
return "UP".equals(serverStatus);
} catch (Exception e) {
return false;
@ -375,7 +375,7 @@ public class NamingProxy {
break;
case label:
ExpressionSelector expressionSelector = (ExpressionSelector) selector;
params.put("selector", JSON.toJSONString(expressionSelector));
params.put("selector", JacksonUtils.toJson(expressionSelector));
break;
default:
break;
@ -384,11 +384,10 @@ public class NamingProxy {
String result = reqAPI(UtilAndComs.NACOS_URL_BASE + "/service/list", params, HttpMethod.GET);
JSONObject json = JSON.parseObject(result);
ListView<String> listView = new ListView<String>();
listView.setCount(json.getInteger("count"));
listView.setData(JSON.parseObject(json.getString("doms"), new TypeReference<List<String>>() {
}));
JsonNode json = JacksonUtils.toObj(result);
ListView<String> listView = new ListView<>();
listView.setCount(json.get("count").asInt());
listView.setData(JacksonUtils.toObj(json.get("doms").toString(), new TypeReference<List<String>>() {}));
return listView;
}

View File

@ -19,7 +19,11 @@ package com.alibaba.nacos.client.naming.utils;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.SystemPropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.selector.ExpressionSelector;
import com.alibaba.nacos.api.selector.NoneSelector;
import com.alibaba.nacos.api.selector.SelectorType;
import com.alibaba.nacos.client.utils.*;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import java.util.Properties;
@ -151,4 +155,21 @@ public class InitUtils {
return endpointUrl + ":" + endpointPort;
}
/**
* Register subType for serialization
*
* Now these subType implementation class has registered in static code.
* But there are some problem for classloader. The implementation class
* will be loaded when they are used, which will make deserialize
* before register.
*
* 子类实现类中的静态代码串中已经向Jackson进行了注册但是由于classloader的原因只有当
* 该子类被使用的时候才会加载该类这可能会导致Jackson先进性反序列化再注册子类从而导致
* 反序列化失败
*/
public static void initSerialization() {
// TODO register in implementation class or remove subType
JacksonUtils.registerSubtype(NoneSelector.class, SelectorType.none.name());
JacksonUtils.registerSubtype(ExpressionSelector.class, SelectorType.label.name());
}
}

View File

@ -15,13 +15,13 @@
*/
package com.alibaba.nacos.client.security;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.client.naming.net.HttpClient;
import com.alibaba.nacos.common.utils.HttpMethod;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.JsonNode;
import org.apache.commons.codec.Charsets;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
@ -122,14 +122,14 @@ public class SecurityProxy {
params, body, Charsets.UTF_8.name(), HttpMethod.POST);
if (result.code != HttpURLConnection.HTTP_OK) {
SECURITY_LOGGER.error("login failed: {}", JSON.toJSONString(result));
SECURITY_LOGGER.error("login failed: {}", JacksonUtils.toJson(result));
return false;
}
JSONObject obj = JSON.parseObject(result.content);
if (obj.containsKey(Constants.ACCESS_TOKEN)) {
accessToken = obj.getString(Constants.ACCESS_TOKEN);
tokenTtl = obj.getIntValue(Constants.TOKEN_TTL);
JsonNode obj = JacksonUtils.toObj(result.content);
if (obj.has(Constants.ACCESS_TOKEN)) {
accessToken = obj.get(Constants.ACCESS_TOKEN).asText();
tokenTtl = obj.get(Constants.TOKEN_TTL).asInt();
tokenRefreshWindow = tokenTtl / 10;
}
}

View File

@ -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.client.utils;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;
/**
* Json tool
*
* @author Nacos
*/
@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
public class JSONUtils {
private static ObjectMapper mapper = new ObjectMapper();
static {
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
public static String serializeObject(Object o) throws IOException {
return mapper.writeValueAsString(o);
}
public static Object deserializeObject(String s, Class<?> clazz) throws IOException {
return mapper.readValue(s, clazz);
}
public static <T> T deserializeObject(String s, TypeReference<T> typeReference)
throws IOException {
return mapper.readValue(s, typeReference);
}
}

View File

@ -1,18 +1,18 @@
/*
*
* * 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.
* 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.
*
*/
@ -21,13 +21,17 @@ package com.alibaba.nacos.client;
import com.alibaba.nacos.api.NacosFactory;
import com.alibaba.nacos.api.PropertyKeyConst;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.config.listener.AbstractListener;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Objects;
import java.util.Properties;
import java.util.Scanner;
import java.util.concurrent.CountDownLatch;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
@ -35,20 +39,20 @@ import java.util.Properties;
@Ignore
public class ConfigTest {
private ConfigService configService;
private static ConfigService configService;
@Before
public void before() throws Exception {
public static void main(String[] args) throws Exception {
before();
test();
}
public static void before() throws Exception {
Properties properties = new Properties();
properties.setProperty(PropertyKeyConst.NAMESPACE, "bebf0150-e1ea-47e2-81fe-6814caf2b952");
properties.setProperty(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
properties.setProperty(PropertyKeyConst.USERNAME, "chuntaojun");
properties.setProperty(PropertyKeyConst.PASSWORD, "1017");
configService = NacosFactory.createConfigService(properties);
}
@Test
public void test() throws Exception {
public static void test() throws Exception {
final String dataId = "lessspring";
final String group = "lessspring";
final String content = "lessspring-" + System.currentTimeMillis();
@ -56,8 +60,25 @@ public class ConfigTest {
Assert.assertTrue(result);
ThreadUtils.sleep(10_000);
String response = configService.getConfig(dataId, group, 5000);
System.out.println(response);
String response = configService.getConfigAndSignListener(dataId, group, 5000, new AbstractListener() {
@Override
public void receiveConfigInfo(String configInfo) {
System.err.println(configInfo);
}
});
Assert.assertEquals(content, response);
Scanner scanner = new Scanner(System.in);
System.out.println("input content");
while (scanner.hasNextLine()){
String s = scanner.next();
if (Objects.equals("exit", s)) {
scanner.close();
return;
}
configService.publishConfig(dataId, group, s);
}
}
}

View File

@ -0,0 +1,95 @@
/*
* 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.client.naming.cache;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
import java.io.File;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
public class DiskCacheTest {
private static final String CACHE_DIR = DiskCacheTest.class.getResource("/").getPath() + "cache/";
private ServiceInfo serviceInfo;
private Instance instance;
@Before
public void setUp() throws Exception {
System.out.println(CACHE_DIR);
serviceInfo = new ServiceInfo("testName", "testClusters");
instance = new Instance();
instance.setClusterName("testClusters");
instance.setIp("1.1.1.1");
instance.setPort(1234);
instance.setServiceName("testName");
serviceInfo.setHosts(Collections.singletonList(instance));
}
@After
public void tearDown() {
File file = new File(CACHE_DIR);
if (file.exists() && file.list().length > 0) {
for (File each : file.listFiles()) {
each.delete();
}
}
}
@Test
public void testCache() {
DiskCache.write(serviceInfo, CACHE_DIR);
Map<String, ServiceInfo> actual = DiskCache.read(CACHE_DIR);
assertEquals(1, actual.size());
assertTrue(actual.containsKey(serviceInfo.getKeyEncoded()));
assertServiceInfo(actual.get(serviceInfo.getKeyEncoded()), serviceInfo);
}
private void assertServiceInfo(ServiceInfo actual, ServiceInfo expected) {
assertEquals(actual.getName(), expected.getName());
assertEquals(actual.getGroupName(), expected.getGroupName());
assertEquals(actual.getClusters(), expected.getClusters());
assertEquals(actual.getCacheMillis(), expected.getCacheMillis());
assertEquals(actual.getLastRefTime(), expected.getLastRefTime());
assertEquals(actual.getKey(), expected.getKey());
assertHosts(actual.getHosts(), expected.getHosts());
}
private void assertHosts(List<Instance> actual, List<Instance> expected) {
assertEquals(actual.size(), expected.size());
for (int i = 0; i < expected.size(); i++) {
assertInstance(actual.get(i), expected.get(i));
}
}
private void assertInstance(Instance actual, Instance expected) {
assertEquals(actual.getServiceName(), actual.getServiceName());
assertEquals(actual.getClusterName(), actual.getClusterName());
assertEquals(actual.getIp(), actual.getIp());
assertEquals(actual.getPort(), actual.getPort());
}
}

View File

@ -0,0 +1,108 @@
/*
* 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.client.naming.core;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ServiceInfo;
import com.alibaba.nacos.client.naming.net.NamingProxy;
@RunWith(MockitoJUnitRunner.class)
public class HostReactorTest {
private static final String CACHE_DIR = HostReactorTest.class.getResource("/").getPath() + "cache/";
@Mock
private NamingProxy namingProxy;
@Mock
private EventDispatcher eventDispatcher;
private HostReactor hostReactor;
@Before
public void setUp() throws Exception {
hostReactor = new HostReactor(eventDispatcher, namingProxy, CACHE_DIR);
}
@Test
public void testProcessServiceJSON() {
ServiceInfo actual = hostReactor.processServiceJSON(EXAMPLE);
assertServiceInfo(actual);
}
@Test
public void testGetServiceInfoDirectlyFromServer() throws NacosException {
when(namingProxy.queryList("testName", "testClusters", 0, false)).thenReturn(EXAMPLE);
ServiceInfo actual = hostReactor.getServiceInfoDirectlyFromServer("testName", "testClusters");
assertServiceInfo(actual);
}
private void assertServiceInfo(ServiceInfo actual) {
assertEquals("testName", actual.getName());
assertEquals("testClusters", actual.getClusters());
assertEquals("", actual.getChecksum());
assertEquals(1000, actual.getCacheMillis());
assertEquals(0, actual.getLastRefTime());
assertNull(actual.getGroupName());
assertTrue(actual.isValid());
assertFalse(actual.isAllIPs());
assertEquals(1, actual.getHosts().size());
assertInstance(actual.getHosts().get(0));
}
private void assertInstance(Instance actual) {
assertEquals("1.1.1.1", actual.getIp());
assertEquals("testClusters", actual.getClusterName());
assertEquals("testName", actual.getServiceName());
assertEquals(1234, actual.getPort());
}
private static final String EXAMPLE = "{\n"
+ "\t\"name\": \"testName\",\n"
+ "\t\"clusters\": \"testClusters\",\n"
+ "\t\"cacheMillis\": 1000,\n"
+ "\t\"hosts\": [{\n"
+ "\t\t\"ip\": \"1.1.1.1\",\n"
+ "\t\t\"port\": 1234,\n"
+ "\t\t\"weight\": 1.0,\n"
+ "\t\t\"healthy\": true,\n"
+ "\t\t\"enabled\": true,\n"
+ "\t\t\"ephemeral\": true,\n"
+ "\t\t\"clusterName\": \"testClusters\",\n"
+ "\t\t\"serviceName\": \"testName\",\n"
+ "\t\t\"metadata\": {},\n"
+ "\t\t\"instanceHeartBeatInterval\": 5000,\n"
+ "\t\t\"instanceHeartBeatTimeOut\": 15000,\n"
+ "\t\t\"ipDeleteTimeout\": 30000,\n"
+ "\t\t\"instanceIdGenerator\": \"simple\"\n"
+ "\t}],\n"
+ "\t\"lastRefTime\": 0,\n"
+ "\t\"checksum\": \"\",\n"
+ "\t\"allIPs\": false,\n"
+ "\t\"valid\": true\n"
+ "}";
}

View File

@ -0,0 +1,44 @@
/*
* 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.client.security;
import org.junit.Test;
import static org.junit.Assert.*;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.common.utils.JacksonUtils;
import com.fasterxml.jackson.databind.JsonNode;
public class SecurityProxyTest {
/**
* Just test for replace fastjson with jackson.
*
*/
@Test
public void testLogin() {
String example = "{\"accessToken\":\"ttttttttttttttttt\",\"tokenTtl\":1000}";
JsonNode obj = JacksonUtils.toObj(example);
if (obj.has(Constants.ACCESS_TOKEN)) {
if (obj.has(Constants.ACCESS_TOKEN)) {
assertEquals("ttttttttttttttttt", obj.get(Constants.ACCESS_TOKEN).asText());
assertEquals(1000, obj.get(Constants.TOKEN_TTL).asInt());
}
}
}
}

View File

@ -18,7 +18,7 @@
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.3.0-BETA</version>
<version>1.3.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -27,8 +27,7 @@
<packaging>jar</packaging>
<name>nacos-cmdb ${project.version}</name>
<!-- FIXME change it to the project's website -->
<url>http://www.example.com</url>
<url>http://nacos.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

View File

@ -15,7 +15,6 @@
*/
package com.alibaba.nacos.cmdb.memory;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.cmdb.spi.CmdbService;
import com.alibaba.nacos.api.cmdb.pojo.Entity;
import com.alibaba.nacos.api.cmdb.pojo.EntityEvent;
@ -26,6 +25,8 @@ import com.alibaba.nacos.cmdb.service.CmdbReader;
import com.alibaba.nacos.cmdb.service.CmdbWriter;
import com.alibaba.nacos.cmdb.utils.Loggers;
import com.alibaba.nacos.cmdb.utils.UtilsAndCommons;
import com.alibaba.nacos.common.utils.JacksonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -166,7 +167,7 @@ public class CmdbProvider implements CmdbReader, CmdbWriter {
}
if (Loggers.MAIN.isDebugEnabled()) {
Loggers.MAIN.debug("LABEL-TASK {}", "got label map:" + JSON.toJSONString(tmpLabelMap));
Loggers.MAIN.debug("LABEL-TASK {}", "got label map:" + JacksonUtils.toJson(tmpLabelMap));
}
labelMap = tmpLabelMap;
@ -219,7 +220,7 @@ public class CmdbProvider implements CmdbReader, CmdbWriter {
eventTimestamp = current;
if (Loggers.MAIN.isDebugEnabled()) {
Loggers.MAIN.debug("EVENT-TASK {}", "got events size:" + ", events:" + JSON.toJSONString(events));
Loggers.MAIN.debug("EVENT-TASK {}", "got events size:" + ", events:" + JacksonUtils.toJson(events));
}
if (events != null && !events.isEmpty()) {

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.3.0-BETA</version>
<version>1.3.0</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -27,7 +27,7 @@
<packaging>jar</packaging>
<name>nacos-common ${project.version}</name>
<url>http://maven.apache.org</url>
<url>http://nacos.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -82,8 +82,8 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>7</source>
<target>7</target>
<source>6</source>
<target>6</target>
</configuration>
</plugin>
</plugins>

View File

@ -16,10 +16,8 @@
package com.alibaba.nacos.common.executor;
import java.util.concurrent.Executor;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ForkJoinPool;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
@ -39,19 +37,6 @@ public final class ExecutorFactory {
public static final String DEFAULT_NAMESPACE = "nacos";
public static ForkJoinPool newForkJoinPool(final String group) {
ForkJoinPool forkJoinPool = new ForkJoinPool();
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, forkJoinPool);
return forkJoinPool;
}
public static ForkJoinPool newForkJoinPool(final String group,
final int nThreads) {
ForkJoinPool forkJoinPool = new ForkJoinPool(nThreads);
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, forkJoinPool);
return forkJoinPool;
}
public static ExecutorService newSingleExecutorService(final String group) {
ExecutorService executorService = Executors.newFixedThreadPool(1);
THREAD_POOL_MANAGER.register(DEFAULT_NAMESPACE, group, executorService);

View File

@ -17,6 +17,7 @@ package com.alibaba.nacos.common.executor;
import com.alibaba.nacos.common.utils.ShutdownUtils;
import com.alibaba.nacos.common.utils.ThreadUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -69,7 +70,7 @@ public final class ThreadPoolManager {
private ThreadPoolManager() {}
private void init() {
resourcesManager = new ConcurrentHashMap<>(8);
resourcesManager = new ConcurrentHashMap<String, Map<String, Set<ExecutorService>>>(8);
}
/**
@ -89,7 +90,7 @@ public final class ThreadPoolManager {
synchronized (monitor) {
Map<String, Set<ExecutorService>> map = resourcesManager.get(namespace);
if (map == null) {
map = new HashMap<>(8);
map = new HashMap<String, Set<ExecutorService>>(8);
map.put(group, new HashSet<ExecutorService>());
map.get(group).add(executor);
resourcesManager.put(namespace, map);
@ -136,7 +137,12 @@ public final class ThreadPoolManager {
}
}
public void destroy(String namespace) {
/**
* Destroys all thread pool resources under this namespace
*
* @param namespace namespace
*/
public void destroy(final String namespace) {
final Object monitor = lockers.get(namespace);
if (monitor == null) {
return;
@ -148,7 +154,7 @@ public final class ThreadPoolManager {
}
for (Map.Entry<String, Set<ExecutorService>> entry : subResource.entrySet()) {
for (ExecutorService executor : entry.getValue()) {
shutdownThreadPool(executor);
ThreadUtils.shutdownThreadPool(executor);
}
}
resourcesManager.get(namespace).clear();
@ -156,23 +162,28 @@ public final class ThreadPoolManager {
}
}
private void shutdownThreadPool(ExecutorService executor) {
executor.shutdown();
int retry = 3;
while (retry > 0) {
retry --;
try {
if (executor.awaitTermination(10, TimeUnit.SECONDS)) {
return;
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.interrupted();
} catch (Throwable ex) {
LOGGER.error("ThreadPoolManager shutdown executor has error : {}", ex);
}
/**
* This namespace destroys all thread pool resources under the grouping
*
* @param namespace namespace
* @param group group
*/
public void destroy(final String namespace, final String group) {
final Object monitor = lockers.get(namespace);
if (monitor == null) {
return;
}
synchronized (monitor) {
Map<String, Set<ExecutorService>> subResource = resourcesManager.get(namespace);
if (subResource == null) {
return;
}
Set<ExecutorService> waitDestroy = subResource.get(group);
for (ExecutorService executor : waitDestroy) {
ThreadUtils.shutdownThreadPool(executor);
}
resourcesManager.get(namespace).remove(group);
}
executor.shutdownNow();
}
public static void shutdown() {

View File

@ -65,6 +65,7 @@ public abstract class BaseHttpClient {
try {
final String body = EntityUtils.toString(response.getEntity());
RestResult<T> data = ResponseHandler.convert(body, type);
data.setCode(response.getStatusLine().getStatusCode());
callback.onReceive(data);
}
catch (Throwable e) {

View File

@ -21,7 +21,6 @@ import com.alibaba.nacos.common.utils.StringUtils;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;

View File

@ -17,12 +17,14 @@
package com.alibaba.nacos.common.utils;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import com.alibaba.nacos.api.common.Constants;
import org.apache.commons.lang3.StringUtils;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@SuppressWarnings("all")
public final class ByteUtils {
public static final byte[] EMPTY = new byte[0];
@ -31,7 +33,7 @@ public final class ByteUtils {
if (s == null) {
return EMPTY;
}
return s.getBytes(Charset.forName(StandardCharsets.UTF_8.name()));
return s.getBytes(Charset.forName(Constants.ENCODE));
}
public static byte[] toBytes(Object s) {
@ -45,7 +47,7 @@ public final class ByteUtils {
if (bytes == null) {
return StringUtils.EMPTY;
}
return new String(bytes, Charset.forName(StandardCharsets.UTF_8.name()));
return new String(bytes, Charset.forName(Constants.ENCODE));
}
public static boolean isEmpty(byte[] data) {

View File

@ -0,0 +1,208 @@
/*
* 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.common.utils;
import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
/**
* copy from <link>org.apache.commons.collections</link>
*
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public final class CollectionUtils {
/**
* Returns the <code>index</code>-th value in <code>object</code>, throwing
* <code>IndexOutOfBoundsException</code> if there is no such element or
* <code>IllegalArgumentException</code> if <code>object</code> is not an
* instance of one of the supported types.
* <p>
* The supported types, and associated semantics are:
* <ul>
* <li> Map -- the value returned is the <code>Map.Entry</code> in position
* <code>index</code> in the map's <code>entrySet</code> iterator,
* if there is such an entry.</li>
* <li> List -- this method is equivalent to the list's get method.</li>
* <li> Array -- the <code>index</code>-th array entry is returned,
* if there is such an entry; otherwise an <code>IndexOutOfBoundsException</code>
* is thrown.</li>
* <li> Collection -- the value returned is the <code>index</code>-th object
* returned by the collection's default iterator, if there is such an element.</li>
* <li> Iterator or Enumeration -- the value returned is the
* <code>index</code>-th object in the Iterator/Enumeration, if there
* is such an element. The Iterator/Enumeration is advanced to
* <code>index</code> (or to the end, if <code>index</code> exceeds the
* number of entries) as a side effect of this method.</li>
* </ul>
*
* @param object the object to get a value from
* @param index the index to get
* @return the object at the specified index
* @throws IndexOutOfBoundsException if the index is invalid
* @throws IllegalArgumentException if the object type is invalid
*/
public static Object get(Object object, int index) {
if (index < 0) {
throw new IndexOutOfBoundsException("Index cannot be negative: " + index);
}
if (object instanceof Map) {
Map map = (Map) object;
Iterator iterator = map.entrySet().iterator();
return get(iterator, index);
} else if (object instanceof List) {
return ((List) object).get(index);
} else if (object instanceof Object[]) {
return ((Object[]) object)[index];
} else if (object instanceof Iterator) {
Iterator it = (Iterator) object;
while (it.hasNext()) {
index--;
if (index == -1) {
return it.next();
} else {
it.next();
}
}
throw new IndexOutOfBoundsException("Entry does not exist: " + index);
} else if (object instanceof Collection) {
Iterator iterator = ((Collection) object).iterator();
return get(iterator, index);
} else if (object instanceof Enumeration) {
Enumeration it = (Enumeration) object;
while (it.hasMoreElements()) {
index--;
if (index == -1) {
return it.nextElement();
} else {
it.nextElement();
}
}
throw new IndexOutOfBoundsException("Entry does not exist: " + index);
} else if (object == null) {
throw new IllegalArgumentException("Unsupported object type: null");
} else {
try {
return Array.get(object, index);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
}
}
/**
* Gets the size of the collection/iterator specified.
* <p>
* This method can handles objects as follows
* <ul>
* <li>Collection - the collection size
* <li>Map - the map size
* <li>Array - the array size
* <li>Iterator - the number of elements remaining in the iterator
* <li>Enumeration - the number of elements remaining in the enumeration
* </ul>
*
* @param object the object to get the size of
* @return the size of the specified collection
* @throws IllegalArgumentException thrown if object is not recognised or null
* @since Commons Collections 3.1
*/
public static int size(Object object) {
int total = 0;
if (object instanceof Map) {
total = ((Map) object).size();
} else if (object instanceof Collection) {
total = ((Collection) object).size();
} else if (object instanceof Object[]) {
total = ((Object[]) object).length;
} else if (object instanceof Iterator) {
Iterator it = (Iterator) object;
while (it.hasNext()) {
total++;
it.next();
}
} else if (object instanceof Enumeration) {
Enumeration it = (Enumeration) object;
while (it.hasMoreElements()) {
total++;
it.nextElement();
}
} else if (object == null) {
throw new IllegalArgumentException("Unsupported object type: null");
} else {
try {
total = Array.getLength(object);
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
}
return total;
}
public static boolean sizeIsEmpty(Object object) {
if (object instanceof Collection) {
return ((Collection) object).isEmpty();
} else if (object instanceof Map) {
return ((Map) object).isEmpty();
} else if (object instanceof Object[]) {
return ((Object[]) object).length == 0;
} else if (object instanceof Iterator) {
return ((Iterator) object).hasNext() == false;
} else if (object instanceof Enumeration) {
return ((Enumeration) object).hasMoreElements() == false;
} else if (object == null) {
throw new IllegalArgumentException("Unsupported object type: null");
} else {
try {
return Array.getLength(object) == 0;
} catch (IllegalArgumentException ex) {
throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
}
}
}
/**
* Null-safe check if the specified collection is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
* @since Commons Collections 3.2
*/
public static boolean isEmpty(Collection coll) {
return (coll == null || coll.isEmpty());
}
/**
* Null-safe check if the specified collection is not empty.
* <p>
* Null returns false.
*
* @param coll the collection to check, may be null
* @return true if non-null and non-empty
* @since Commons Collections 3.2
*/
public static boolean isNotEmpty(Collection coll) {
return !CollectionUtils.isEmpty(coll);
}
}

View File

@ -42,7 +42,7 @@ public final class ConvertUtils {
return Long.parseLong(val);
}
public static boolean toBoolean(String val, boolean defaultValue) {
public static boolean toBool(String val, boolean defaultValue) {
if (StringUtils.isBlank(val)) {
return defaultValue;
}

View File

@ -41,18 +41,23 @@ public class ExceptionUtil {
return strBuilder.toString();
}
public static Throwable getCause(final Throwable t) {
final Throwable cause = t.getCause();
if (Objects.isNull(cause)) {
return t;
}
return cause;
}
public static String getStackTrace(final Throwable t) {
if (t == null) {
return "";
}
try (final ByteArrayOutputStream out = new ByteArrayOutputStream(); final PrintStream ps = new PrintStream(out)) {
t.printStackTrace(ps);
ps.flush();
return new String(out.toByteArray());
} catch (final IOException ignored) {
// ignored
}
return "";
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream ps = new PrintStream(out);
t.printStackTrace(ps);
ps.flush();
return new String(out.toByteArray());
}
}

View File

@ -16,9 +16,19 @@
package com.alibaba.nacos.common.utils;
import com.alibaba.nacos.api.exception.runtime.NacosDeserializationException;
import com.alibaba.nacos.api.exception.runtime.NacosSerializationException;
import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.jsontype.NamedType;
import com.fasterxml.jackson.databind.node.ArrayNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.IOException;
import java.lang.reflect.Type;
/**
@ -30,29 +40,103 @@ public final class JacksonUtils {
static {
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.setSerializationInclusion(Include.NON_NULL);
}
public static String toJson(Object obj) throws Exception {
return mapper.writeValueAsString(obj);
public static String toJson(Object obj) {
try {
return mapper.writeValueAsString(obj);
}
catch (JsonProcessingException e) {
throw new NacosSerializationException(obj.getClass(), e);
}
}
public static byte[] toJsonBytes(Object obj) throws Exception {
return ByteUtils.toBytes(mapper.writeValueAsString(obj));
public static byte[] toJsonBytes(Object obj) {
try {
return ByteUtils.toBytes(mapper.writeValueAsString(obj));
}
catch (JsonProcessingException e) {
throw new NacosSerializationException(obj.getClass(), e);
}
}
public static <T> T toObj(byte[] json, Class<T> cls) throws Exception {
return toObj(StringUtils.newString4UTF8(json), cls);
public static <T> T toObj(byte[] json, Class<T> cls) {
try {
return toObj(StringUtils.newString4UTF8(json), cls);
}
catch (Exception e) {
throw new NacosDeserializationException(cls, e);
}
}
public static <T> T toObj(byte[] json, Type cls) throws Exception {
return toObj(StringUtils.newString4UTF8(json), cls);
public static <T> T toObj(byte[] json, Type cls) {
try {
return toObj(StringUtils.newString4UTF8(json), cls);
}
catch (Exception e) {
throw new NacosDeserializationException(e);
}
}
public static <T> T toObj(String json, Class<T> cls) throws Exception {
return mapper.readValue(json, cls);
public static <T> T toObj(byte[] json, TypeReference<T> typeReference) {
try {
return toObj(StringUtils.newString4UTF8(json), typeReference);
}
catch (Exception e) {
throw new NacosDeserializationException(e);
}
}
public static <T> T toObj(String json, Type type) throws Exception {
return mapper.readValue(json, mapper.constructType(type));
public static <T> T toObj(String json, Class<T> cls) {
try {
return mapper.readValue(json, cls);
}
catch (IOException e) {
throw new NacosDeserializationException(cls, e);
}
}
public static <T> T toObj(String json, Type type) {
try {
return mapper.readValue(json, mapper.constructType(type));
}
catch (IOException e) {
throw new NacosDeserializationException(e);
}
}
public static <T> T toObj(String json, TypeReference<T> typeReference) {
try {
return mapper.readValue(json, typeReference);
}
catch (IOException e) {
throw new NacosDeserializationException(typeReference.getClass(), e);
}
}
public static JsonNode toObj(String json) {
try {
return mapper.readTree(json);
}
catch (IOException e) {
throw new NacosDeserializationException(e);
}
}
public static void registerSubtype(Class<?> clz, String type) {
mapper.registerSubtypes(new NamedType(clz, type));
}
public static ObjectNode createEmptyJsonNode() {
return new ObjectNode(mapper.getNodeFactory());
}
public static ArrayNode createEmptyArrayNode() {
return new ArrayNode(mapper.getNodeFactory());
}
public static JsonNode transferToJsonNode(Object obj) {
return mapper.valueToTree(obj);
}
}

View File

@ -23,6 +23,16 @@ import org.slf4j.Logger;
*/
public final class LoggerUtils {
public static final String TRACE = "TRACE";
public static final String INFO = "INFO";
public static final String DEBUG = "DEBUG";
public static final String WARN = "WARN";
public static final String ERROR = "ERROR";
public static void printIfDebugEnabled(Logger logger, String s, Object... args) {
if (logger.isDebugEnabled()) {
logger.debug(s, args);
@ -35,4 +45,22 @@ public final class LoggerUtils {
}
}
public static void printIfTraceEnabled(Logger logger, String s, Object... args) {
if (logger.isTraceEnabled()) {
logger.trace(s, args);
}
}
public static void printIfWarnEnabled(Logger logger, String s, Object... args) {
if (logger.isWarnEnabled()) {
logger.warn(s, args);
}
}
public static void printIfErrorEnabled(Logger logger, String s, Object... args) {
if (logger.isErrorEnabled()) {
logger.error(s, args);
}
}
}

View File

@ -0,0 +1,114 @@
/*
*
* 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.common.utils;
import java.util.Collection;
import java.util.Dictionary;
import java.util.Map;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@SuppressWarnings("all")
public class MapUtils {
/**
* Null-safe check if the specified Dictionary is empty.
* <p>
* Null returns true.
*
* @param map the collection to check, may be null
* @return true if empty or null
*/
public static boolean isEmpty(Map map) {
return (map == null || map.isEmpty());
}
/**
* Null-safe check if the specified Dictionary is not empty.
* <p>
* Null returns false.
*
* @param map the collection to check, may be null
* @return true if non-null and non-empty
*/
public static boolean isNotEmpty(Map map) {
return !isEmpty(map);
}
/**
* Null-safe check if the specified Dictionary is empty.
* <p>
* Null returns true.
*
* @param coll the collection to check, may be null
* @return true if empty or null
*/
public static boolean isEmpty(Dictionary coll) {
return (coll == null || coll.isEmpty());
}
/**
* Null-safe check if the specified Dictionary is not empty.
* <p>
* Null returns false.
*
* @param coll the collection to check, may be null
* @return true if non-null and non-empty
*/
public static boolean isNotEmpty(Dictionary coll) {
return !isEmpty(coll);
}
public static void putIfValNoNull(Map target, Object key, Object value) {
Objects.requireNonNull(key, "key");
if (value != null) {
target.put(key, value);
}
}
public static void putIfValNoEmpty(Map target, Object key, Object value) {
Objects.requireNonNull(key, "key");
if (value instanceof String) {
if (StringUtils.isNotEmpty((String) value)) {
target.put(key, value);
}
return;
}
if (value instanceof Collection) {
if (CollectionUtils.isNotEmpty((Collection) value)) {
target.put(key, value);
}
return;
}
if (value instanceof Map) {
if (isNotEmpty((Map) value)) {
target.put(key, value);
}
return;
}
if (value instanceof Dictionary) {
if (isNotEmpty((Dictionary) value)) {
target.put(key, value);
}
return;
}
}
}

View File

@ -0,0 +1,240 @@
/*
* 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.common.utils;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
@SuppressWarnings("all")
public class Objects {
/**
* Returns {@code true} if the arguments are equal to each other
* and {@code false} otherwise.
* Consequently, if both arguments are {@code null}, {@code true}
* is returned and if exactly one argument is {@code null}, {@code
* false} is returned. Otherwise, equality is determined by using
* the {@link Object#equals equals} method of the first
* argument.
*
* @param a an object
* @param b an object to be compared with {@code a} for equality
* @return {@code true} if the arguments are equal to each other
* and {@code false} otherwise
* @see Object#equals(Object)
*/
public static boolean equals(Object a, Object b) {
return (a == b) || (a != null && a.equals(b));
}
/**
* Returns the hash code of a non-{@code null} argument and 0 for
* a {@code null} argument.
*
* @param o an object
* @return the hash code of a non-{@code null} argument and 0 for
* a {@code null} argument
* @see Object#hashCode
*/
public static int hashCode(Object o) {
return o != null ? o.hashCode() : 0;
}
/**
* Generates a hash code for a sequence of input values. The hash
* code is generated as if all the input values were placed into an
* array, and that array were hashed by calling {@link
* Arrays#hashCode(Object[])}.
*
* <p>This method is useful for implementing {@link
* Object#hashCode()} on objects containing multiple fields. For
* example, if an object that has three fields, {@code x}, {@code
* y}, and {@code z}, one could write:
*
* <blockquote><pre>
* &#064;Override public int hashCode() {
* return Objects.hash(x, y, z);
* }
* </pre></blockquote>
*
* <b>Warning: When a single object reference is supplied, the returned
* value does not equal the hash code of that object reference.</b> This
* value can be computed by calling {@link #hashCode(Object)}.
*
* @param values the values to be hashed
* @return a hash value of the sequence of input values
* @see Arrays#hashCode(Object[])
* @see List#hashCode
*/
public static int hash(Object... values) {
return Arrays.hashCode(values);
}
/**
* Returns the result of calling {@code toString} for a non-{@code
* null} argument and {@code "null"} for a {@code null} argument.
*
* @param o an object
* @return the result of calling {@code toString} for a non-{@code
* null} argument and {@code "null"} for a {@code null} argument
* @see Object#toString
* @see String#valueOf(Object)
*/
public static String toString(Object o) {
return String.valueOf(o);
}
/**
* Returns the result of calling {@code toString} on the first
* argument if the first argument is not {@code null} and returns
* the second argument otherwise.
*
* @param o an object
* @param nullDefault string to return if the first argument is
* {@code null}
* @return the result of calling {@code toString} on the first
* argument if it is not {@code null} and the second argument
* otherwise.
*/
public static String toString(Object o, String nullDefault) {
return (o != null) ? o.toString() : nullDefault;
}
/**
* Returns 0 if the arguments are identical and {@code
* c.compare(a, b)} otherwise.
* Consequently, if both arguments are {@code null} 0
* is returned.
*
* <p>Note that if one of the arguments is {@code null}, a {@code
* NullPointerException} may or may not be thrown depending on
* what ordering policy, if any, the {@link Comparator Comparator}
* chooses to have for {@code null} values.
*
* @param <T> the type of the objects being compared
* @param a an object
* @param b an object to be compared with {@code a}
* @param c the {@code Comparator} to compare the first two arguments
* @return 0 if the arguments are identical and {@code
* c.compare(a, b)} otherwise.
* @see Comparable
* @see Comparator
*/
public static <T> int compare(T a, T b, Comparator<? super T> c) {
return (a == b) ? 0 : c.compare(a, b);
}
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.requireNonNull(bar, "bar must not be null");
* this.baz = Objects.requireNonNull(baz, "baz must not be null");
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
/**
* Returns {@code true} if the provided reference is {@code null} otherwise
* returns {@code false}.
*
* @apiNote This method exists to be used as a
*
* @param obj a reference to be checked against {@code null}
* @return {@code true} if the provided reference is {@code null} otherwise
* {@code false}
*
* @since 1.8
*/
public static boolean isNull(Object obj) {
return obj == null;
}
/**
* Returns {@code true} if the provided reference is non-{@code null}
* otherwise returns {@code false}.
*
* @apiNote This method exists to be used as a
*
* @param obj a reference to be checked against {@code null}
* @return {@code true} if the provided reference is non-{@code null}
* otherwise {@code false}
*
* @since 1.8
*/
public static boolean nonNull(Object obj) {
return obj != null;
}
/**
* Returns the first argument if it is non-{@code null} and
* otherwise returns the non-{@code null} second argument.
*
* @param obj an object
* @param defaultObj a non-{@code null} object to return if the first argument
* is {@code null}
* @param <T> the type of the reference
* @return the first argument if it is non-{@code null} and
* otherwise the second argument if it is non-{@code null}
* @throws NullPointerException if both {@code obj} is null and
* {@code defaultObj} is {@code null}
* @since 9
*/
public static <T> T requireNonNullElse(T obj, T defaultObj) {
return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
}
}

View File

@ -16,7 +16,6 @@
package com.alibaba.nacos.common.utils;
import java.util.Objects;
import java.util.Set;
/**
@ -25,7 +24,7 @@ import java.util.Set;
public class Observable {
private transient boolean changed = false;
private transient Set<Observer> obs = new ConcurrentHashSet<>();
private transient Set<Observer> obs = new ConcurrentHashSet<Observer>();
private volatile int observerCnt = 0;
private boolean alreadyAddObserver = false;

View File

@ -28,7 +28,7 @@ public class Pair<A, B> {
}
public static <A, B> Pair<A, B> with(A first, B second) {
return new Pair<>(first, second);
return new Pair<A, B>(first, second);
}
public A getFirst() {

View File

@ -15,11 +15,12 @@
*/
package com.alibaba.nacos.common.utils;
import com.alibaba.nacos.api.common.Constants;
import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collection;
import java.util.Locale;
@ -39,7 +40,7 @@ public class StringUtils {
public static final String EMPTY = "";
public static String newString4UTF8(byte[] bytes) {
return new String(bytes, Charset.forName(StandardCharsets.UTF_8.name()));
return new String(bytes, Charset.forName(Constants.ENCODE));
}
public static boolean isBlank(String str) {
@ -214,4 +215,89 @@ public class StringUtils {
}
}
}
// The following utility functions are extracted from <link>org.apache.commons.lang3</link>
// start
/**
* <p>Checks if CharSequence contains a search CharSequence irrespective of case,
* handling {@code null}. Case-insensitivity is defined as by
* {@link String#equalsIgnoreCase(String)}.
*
* <p>A {@code null} CharSequence will return {@code false}.</p>
*
* <pre>
* StringUtils.contains(null, *) = false
* StringUtils.contains(*, null) = false
* StringUtils.contains("", "") = true
* StringUtils.contains("abc", "") = true
* StringUtils.contains("abc", "a") = true
* StringUtils.contains("abc", "z") = false
* StringUtils.contains("abc", "A") = true
* StringUtils.contains("abc", "Z") = false
* </pre>
*
* @param str the CharSequence to check, may be null
* @param searchStr the CharSequence to find, may be null
* @return true if the CharSequence contains the search CharSequence irrespective of
* case or false if not or {@code null} string input
* @since 3.0 Changed signature from containsIgnoreCase(String, String) to containsIgnoreCase(CharSequence, CharSequence)
*/
public static boolean containsIgnoreCase(final CharSequence str, final CharSequence searchStr) {
if (str == null || searchStr == null) {
return false;
}
final int len = searchStr.length();
final int max = str.length() - len;
for (int i = 0; i <= max; i++) {
if (regionMatches(str, true, i, searchStr, 0, len)) {
return true;
}
}
return false;
}
/**
* Green implementation of regionMatches.
*
* @param cs the {@code CharSequence} to be processed
* @param ignoreCase whether or not to be case insensitive
* @param thisStart the index to start on the {@code cs} CharSequence
* @param substring the {@code CharSequence} to be looked for
* @param start the index to start on the {@code substring} CharSequence
* @param length character length of the region
* @return whether the region matched
*/
static boolean regionMatches(final CharSequence cs, final boolean ignoreCase, final int thisStart,
final CharSequence substring, final int start, final int length) {
if (cs instanceof String && substring instanceof String) {
return ((String) cs).regionMatches(ignoreCase, thisStart, (String) substring, start, length);
}
int index1 = thisStart;
int index2 = start;
int tmpLen = length;
while (tmpLen-- > 0) {
final char c1 = cs.charAt(index1++);
final char c2 = substring.charAt(index2++);
if (c1 == c2) {
continue;
}
if (!ignoreCase) {
return false;
}
// The same check as in String.regionMatches():
if (Character.toUpperCase(c1) != Character.toUpperCase(c2)
&& Character.toLowerCase(c1) != Character.toLowerCase(c2)) {
return false;
}
}
return true;
}
// end
}

View File

@ -16,7 +16,10 @@
package com.alibaba.nacos.common.utils;
import org.slf4j.Logger;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
/**
@ -40,6 +43,11 @@ public final class ThreadUtils {
}
}
public static void countDown(CountDownLatch latch) {
Objects.requireNonNull(latch, "latch");
latch.countDown();
}
public static void latchAwait(CountDownLatch latch) {
try {
latch.await();
@ -71,6 +79,31 @@ public final class ThreadUtils {
return workerCount;
}
public static void shutdownThreadPool(ExecutorService executor) {
shutdownThreadPool(executor, null);
}
public static void shutdownThreadPool(ExecutorService executor, Logger logger) {
executor.shutdown();
int retry = 3;
while (retry > 0) {
retry --;
try {
if (executor.awaitTermination(10, TimeUnit.SECONDS)) {
return;
}
} catch (InterruptedException e) {
executor.shutdownNow();
Thread.interrupted();
} catch (Throwable ex) {
if (logger != null) {
logger.error("ThreadPoolManager shutdown executor has error : {}", ex);
}
}
}
executor.shutdownNow();
}
private final static int THREAD_MULTIPLER = 2;
}

View File

@ -28,7 +28,7 @@ public class VersionUtils {
/**
* 获取当前version
*/
public static final String VERSION_DEFAULT = "${project.version}";
public static final String VERSION_PLACEHOLDER = "${project.version}";
static {
@ -39,7 +39,7 @@ public class VersionUtils {
Properties props = new Properties();
props.load(in);
String val = props.getProperty("version");
if (val != null && !VERSION_DEFAULT.equals(val)) {
if (val != null && !VERSION_PLACEHOLDER.equals(val)) {
VERSION = val;
}
} catch (Exception e) {

View File

@ -0,0 +1,50 @@
package com.alibaba.nacos.common.utils;
import org.junit.Assert;
import org.junit.Test;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class MapUtilsTest {
@Test
public void test_map() {
Map<Object, Object> map = new HashMap<Object, Object>();
MapUtils.putIfValNoNull(map, "key-1", null);
Assert.assertFalse(map.containsKey("key-1"));
MapUtils.putIfValNoEmpty(map, "key-str", null);
Assert.assertFalse(map.containsKey("key-str"));
MapUtils.putIfValNoEmpty(map, "key-str", "");
Assert.assertFalse(map.containsKey("key-str"));
MapUtils.putIfValNoEmpty(map, "key-str", "1");
Assert.assertTrue(map.containsKey("key-str"));
MapUtils.putIfValNoEmpty(map, "key-list", null);
Assert.assertFalse(map.containsKey("key-list"));
MapUtils.putIfValNoEmpty(map, "key-list", Collections.emptyList());
Assert.assertFalse(map.containsKey("key-list"));
MapUtils.putIfValNoEmpty(map, "key-list", Collections.singletonList(1));
Assert.assertTrue(map.containsKey("key-list"));
MapUtils.putIfValNoEmpty(map, "key-map", null);
Assert.assertFalse(map.containsKey("key-map"));
MapUtils.putIfValNoEmpty(map, "key-map", Collections.emptyMap());
Assert.assertFalse(map.containsKey("key-map"));
Map<String, String> map1 = new HashMap<String, String>();
map1.put("1123", "123");
MapUtils.putIfValNoEmpty(map, "key-map", map1);
Assert.assertTrue(map.containsKey("key-map"));
}
}

View File

@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.3.0-BETA</version>
<version>1.3.0</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -26,7 +26,7 @@
<packaging>jar</packaging>
<name>nacos-config ${project.version}</name>
<url>http://maven.apache.org</url>
<url>http://nacos.io</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
@ -54,10 +54,6 @@
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
</dependency>
<!--<dependency>-->
<!--<groupId>taglibs</groupId>-->
<!--<artifactId>standard</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
@ -69,16 +65,11 @@
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
@ -129,7 +120,6 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.4</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
@ -177,18 +167,6 @@
</configuration>
</plugin>
</plugins>
<!-- <resources>
<resource>
<filtering>true</filtering>
<directory>src/main/resources</directory>
<excludes>
<exclude>application.properties</exclude>
<exclude>banner.txt</exclude>
<exclude>schema.sql</exclude>
<exclude>nacos-db.sql</exclude>
</excludes>
</resource>
</resources> -->
</build>
<profiles>
<profile>

View File

@ -18,7 +18,7 @@ package com.alibaba.nacos.config.server.aspect;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.service.ConfigService;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.RequestUtil;
@ -88,7 +88,7 @@ public class RequestLogAspect {
public Object interfaceGetConfig(ProceedingJoinPoint pjp, HttpServletRequest request, HttpServletResponse response,
String dataId, String group, String tenant) throws Throwable {
final String groupKey = GroupKey2.getKey(dataId, group, tenant);
final String md5 = ConfigService.getContentMd5(groupKey);
final String md5 = ConfigCacheService.getContentMd5(groupKey);
MetricsMonitor.getConfigMonitor().incrementAndGet();
return logClientRequest("get", pjp, request, response, dataId, group, tenant, md5);
}

View File

@ -21,7 +21,7 @@ import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.service.repository.EmbeddedStoragePersistServiceImpl;
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.DatabaseOperate;
import com.alibaba.nacos.config.server.service.sql.SqlContextUtils;
import com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
@ -29,7 +29,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import static com.alibaba.nacos.config.server.service.RowMapperManager.PERMISSION_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.PERMISSION_ROW_MAPPER;
/**
* There is no self-augmented primary key
@ -73,14 +73,14 @@ public class EmbeddedPermissionPersistServiceImpl implements PermissionPersistSe
public void addPermission(String role, String resource, String action) {
String sql = "INSERT into permissions (role, resource, action) VALUES (?, ?, ?)";
SqlContextUtils.addSqlContext(sql, role, resource, action);
databaseOperate.smartUpdate();
EmbeddedStorageContextUtils.addSqlContext(sql, role, resource, action);
databaseOperate.blockUpdate();
}
public void deletePermission(String role, String resource, String action) {
String sql = "DELETE from permissions WHERE role=? and resource=? and action=?";
SqlContextUtils.addSqlContext(sql, role, resource, action);
databaseOperate.smartUpdate();
EmbeddedStorageContextUtils.addSqlContext(sql, role, resource, action);
databaseOperate.blockUpdate();
}
}

View File

@ -21,7 +21,7 @@ import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.service.repository.EmbeddedStoragePersistServiceImpl;
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.DatabaseOperate;
import com.alibaba.nacos.config.server.service.sql.SqlContextUtils;
import com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
@ -29,7 +29,7 @@ import org.springframework.stereotype.Component;
import java.util.ArrayList;
import static com.alibaba.nacos.config.server.service.RowMapperManager.ROLE_INFO_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.ROLE_INFO_ROW_MAPPER;
/**
* There is no self-augmented primary key
@ -93,30 +93,30 @@ public class EmbeddedRolePersistServiceImpl implements RolePersistService {
String sql = "INSERT into roles (role, username) VALUES (?, ?)";
try {
SqlContextUtils.addSqlContext(sql, role, userName);
databaseOperate.update(SqlContextUtils.getCurrentSqlContext());
EmbeddedStorageContextUtils.addSqlContext(sql, role, userName);
databaseOperate.update(EmbeddedStorageContextUtils.getCurrentSqlContext());
} finally {
SqlContextUtils.cleanCurrentSqlContext();
EmbeddedStorageContextUtils.cleanAllContext();
}
}
public void deleteRole(String role) {
String sql = "DELETE from roles WHERE role=?";
try {
SqlContextUtils.addSqlContext(sql, role);
databaseOperate.update(SqlContextUtils.getCurrentSqlContext());
EmbeddedStorageContextUtils.addSqlContext(sql, role);
databaseOperate.update(EmbeddedStorageContextUtils.getCurrentSqlContext());
} finally {
SqlContextUtils.cleanCurrentSqlContext();
EmbeddedStorageContextUtils.cleanAllContext();
}
}
public void deleteRole(String role, String username) {
String sql = "DELETE from roles WHERE role=? and username=?";
try {
SqlContextUtils.addSqlContext(sql, role, username);
databaseOperate.update(SqlContextUtils.getCurrentSqlContext());
EmbeddedStorageContextUtils.addSqlContext(sql, role, username);
databaseOperate.update(EmbeddedStorageContextUtils.getCurrentSqlContext());
} finally {
SqlContextUtils.cleanCurrentSqlContext();
EmbeddedStorageContextUtils.cleanAllContext();
}
}

View File

@ -22,14 +22,14 @@ import com.alibaba.nacos.config.server.model.User;
import com.alibaba.nacos.config.server.service.repository.EmbeddedStoragePersistServiceImpl;
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.service.repository.DatabaseOperate;
import com.alibaba.nacos.config.server.service.sql.SqlContextUtils;
import com.alibaba.nacos.config.server.service.sql.EmbeddedStorageContextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Conditional;
import org.springframework.stereotype.Component;
import java.util.ArrayList;
import static com.alibaba.nacos.config.server.service.RowMapperManager.USER_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.USER_ROW_MAPPER;
/**
* There is no self-augmented primary key
@ -50,31 +50,31 @@ public class EmbeddedUserPersistServiceImpl implements UserPersistService {
String sql = "INSERT into users (username, password, enabled) VALUES (?, ?, ?)";
try {
SqlContextUtils.addSqlContext(sql, username, password, true);
databaseOperate.smartUpdate();
EmbeddedStorageContextUtils.addSqlContext(sql, username, password, true);
databaseOperate.blockUpdate();
} finally {
SqlContextUtils.cleanCurrentSqlContext();
EmbeddedStorageContextUtils.cleanAllContext();
}
}
public void deleteUser(String username) {
String sql = "DELETE from users WHERE username=?";
try {
SqlContextUtils.addSqlContext(sql, username);
databaseOperate.smartUpdate();
EmbeddedStorageContextUtils.addSqlContext(sql, username);
databaseOperate.blockUpdate();
} finally {
SqlContextUtils.cleanCurrentSqlContext();
EmbeddedStorageContextUtils.cleanAllContext();
}
}
public void updateUserPassword(String username, String password) {
try {
SqlContextUtils.addSqlContext(
EmbeddedStorageContextUtils.addSqlContext(
"UPDATE users SET password = ? WHERE username=?",
password, username);
databaseOperate.smartUpdate();
databaseOperate.blockUpdate();
} finally {
SqlContextUtils.cleanCurrentSqlContext();
EmbeddedStorageContextUtils.cleanAllContext();
}
}

View File

@ -16,7 +16,6 @@
package com.alibaba.nacos.config.server.auth;
import com.alibaba.nacos.config.server.configuration.ConditionOnEmbeddedStorage;
import com.alibaba.nacos.config.server.configuration.ConditionOnExternalStorage;
import com.alibaba.nacos.config.server.model.Page;
import com.alibaba.nacos.config.server.service.repository.ExternalStoragePersistServiceImpl;
@ -24,7 +23,6 @@ import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
@ -33,7 +31,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import static com.alibaba.nacos.config.server.service.RowMapperManager.PERMISSION_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.PERMISSION_ROW_MAPPER;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>

View File

@ -23,7 +23,6 @@ import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.utils.LogUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
@ -35,7 +34,7 @@ import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import static com.alibaba.nacos.config.server.service.RowMapperManager.ROLE_INFO_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.ROLE_INFO_ROW_MAPPER;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>

View File

@ -23,7 +23,6 @@ import com.alibaba.nacos.config.server.service.repository.ExternalStoragePersist
import com.alibaba.nacos.config.server.service.repository.PaginationHelper;
import com.alibaba.nacos.config.server.utils.LogUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Conditional;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
@ -33,7 +32,7 @@ import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.ArrayList;
import static com.alibaba.nacos.config.server.service.RowMapperManager.USER_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.USER_ROW_MAPPER;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>

View File

@ -15,6 +15,8 @@
*/
package com.alibaba.nacos.config.server.constant;
import com.alibaba.nacos.config.server.model.event.ConfigDumpEvent;
/**
* Server Constants
*
@ -175,4 +177,19 @@ public class Constants {
public static final int ATOMIC_MAX_SIZE = 1000;
public static final String CONFIG_MODEL_RAFT_GROUP = "nacos_config";
public static int DATA_IN_BODY_VERSION = 204;
/**
* Configure the dump event name
*/
public static final String EXTEND_INFO_CONFIG_DUMP_EVENT = ConfigDumpEvent.class.getName();
/**
* Configure the dump event-list name
*/
public static final String EXTEND_INFOS_CONFIG_DUMP_EVENT = ConfigDumpEvent.class.getName() + "@@many";
/**
* Specifies that reads wait without timeout
*/
public static final String EXTEND_NEED_READ_UNTIL_HAVE_DATA = "00--0-read-join-0--00";
}

View File

@ -17,6 +17,7 @@ package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.utils.MapUtils;
import com.alibaba.nacos.config.server.auth.ConfigResourceParser;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.controller.parameters.SameNamespaceCloneConfigBean;
@ -32,7 +33,7 @@ import com.alibaba.nacos.config.server.model.SampleResult;
import com.alibaba.nacos.config.server.result.ResultBuilder;
import com.alibaba.nacos.config.server.result.code.ResultCodeEnum;
import com.alibaba.nacos.config.server.service.AggrWhitelist;
import com.alibaba.nacos.config.server.service.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.model.event.ConfigDataChangeEvent;
import com.alibaba.nacos.config.server.service.ConfigSubService;
import com.alibaba.nacos.config.server.service.repository.PersistService;
import com.alibaba.nacos.config.server.service.trace.ConfigTraceService;
@ -133,32 +134,20 @@ public class ConfigController {
@RequestParam(value = "type", required = false) String type,
@RequestParam(value = "schema", required = false) String schema)
throws NacosException {
final String srcIp = RequestUtil.getRemoteIp(request);
String requestIpApp = RequestUtil.getAppName(request);
// check tenant
ParamUtils.checkTenant(tenant);
ParamUtils.checkParam(dataId, group, "datumId", content);
ParamUtils.checkParam(tag);
Map<String, Object> configAdvanceInfo = new HashMap<String, Object>(10);
if (configTags != null) {
configAdvanceInfo.put("config_tags", configTags);
}
if (desc != null) {
configAdvanceInfo.put("desc", desc);
}
if (use != null) {
configAdvanceInfo.put("use", use);
}
if (effect != null) {
configAdvanceInfo.put("effect", effect);
}
if (type != null) {
configAdvanceInfo.put("type", type);
}
if (schema != null) {
configAdvanceInfo.put("schema", schema);
}
MapUtils.putIfValNoNull(configAdvanceInfo, "config_tags", configTags);
MapUtils.putIfValNoNull(configAdvanceInfo, "desc", desc);
MapUtils.putIfValNoNull(configAdvanceInfo, "use", use);
MapUtils.putIfValNoNull(configAdvanceInfo, "effect", effect);
MapUtils.putIfValNoNull(configAdvanceInfo, "type", type);
MapUtils.putIfValNoNull(configAdvanceInfo, "schema", schema);
ParamUtils.checkParam(configAdvanceInfo);
if (AggrWhitelist.isAggrDataId(dataId)) {
@ -171,33 +160,24 @@ public class ConfigController {
final Timestamp time = TimeUtils.getCurrentTime();
String betaIps = request.getHeader("betaIps");
ConfigInfo configInfo = new ConfigInfo(dataId, group, tenant, appName, content);
configInfo.setType(type);
if (StringUtils.isBlank(betaIps)) {
if (StringUtils.isBlank(tag)) {
persistService.insertOrUpdate(srcIp, srcUser, configInfo, time,
configAdvanceInfo, false);
EventDispatcher.fireEvent(
new ConfigDataChangeEvent(false, dataId, group, tenant,
time.getTime()));
configAdvanceInfo, true);
}
else {
persistService
.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, false);
EventDispatcher.fireEvent(
new ConfigDataChangeEvent(false, dataId, group, tenant, tag,
time.getTime()));
.insertOrUpdateTag(configInfo, tag, srcIp, srcUser, time, true);
}
}
else { // beta publish
else {
// beta publish
persistService
.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, false);
EventDispatcher.fireEvent(
new ConfigDataChangeEvent(true, dataId, group, tenant,
time.getTime()));
.insertOrUpdateBeta(configInfo, betaIps, srcIp, srcUser, time, true);
}
ConfigTraceService
.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(),
InetUtils.getSelfIp(), ConfigTraceService.PERSISTENCE_EVENT_PUB, content);
ConfigTraceService.logPersistenceEvent(dataId, group, tenant, requestIpApp, time.getTime(),
InetUtils.getSelfIp(), ConfigTraceService.PERSISTENCE_EVENT_PUB, content);
return true;
}
@ -275,9 +255,6 @@ public class ConfigController {
ConfigTraceService
.logPersistenceEvent(dataId, group, tenant, null, time.getTime(),
clientIp, ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
EventDispatcher.fireEvent(
new ConfigDataChangeEvent(false, dataId, group, tenant, tag,
time.getTime()));
return true;
}
@ -303,10 +280,6 @@ public class ConfigController {
configInfo.getGroup(), configInfo.getTenant(), null,
time.getTime(), clientIp,
ConfigTraceService.PERSISTENCE_EVENT_REMOVE, null);
EventDispatcher.fireEvent(
new ConfigDataChangeEvent(false, configInfo.getDataId(),
configInfo.getGroup(), configInfo.getTenant(),
time.getTime()));
}
}
return ResultBuilder.buildSuccessResult(true);
@ -453,8 +426,6 @@ public class ConfigController {
rr.setMessage("remove beta data error");
return rr;
}
EventDispatcher.fireEvent(new ConfigDataChangeEvent(true, dataId, group, tenant,
System.currentTimeMillis()));
rr.setCode(200);
rr.setData(true);
rr.setMessage("stop beta ok");

View File

@ -18,8 +18,8 @@ package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.common.model.RestResult;
import com.alibaba.nacos.common.model.RestResultUtils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.service.LocalDataSourceServiceImpl;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.service.datasource.LocalDataSourceServiceImpl;
import com.alibaba.nacos.config.server.service.repository.PersistService;
import com.alibaba.nacos.config.server.service.dump.DumpService;
import com.alibaba.nacos.config.server.utils.LogUtil;

View File

@ -18,8 +18,8 @@ package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.model.ConfigInfoBase;
import com.alibaba.nacos.config.server.service.ConfigService;
import com.alibaba.nacos.config.server.service.DiskUtil;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.utils.DiskUtil;
import com.alibaba.nacos.config.server.service.LongPollingService;
import com.alibaba.nacos.config.server.service.repository.PersistService;
import com.alibaba.nacos.config.server.service.trace.ConfigTraceService;
@ -60,7 +60,7 @@ public class ConfigServletInner {
private static final int TRY_GET_LOCK_TIMES = 9;
private static final int START_LONGPOLLING_VERSION_NUM = 204;
private static final int START_LONG_POLLING_VERSION_NUM = 204;
/**
* 轮询接口
@ -91,7 +91,7 @@ public class ConfigServletInner {
/**
* 2.0.4版本以前, 返回值放入header中
*/
if (versionNum < START_LONGPOLLING_VERSION_NUM) {
if (versionNum < START_LONG_POLLING_VERSION_NUM) {
response.addHeader(Constants.PROBE_MODIFY_RESPONSE, oldResult);
response.addHeader(Constants.PROBE_MODIFY_RESPONSE_NEW, newResult);
} else {
@ -125,7 +125,7 @@ public class ConfigServletInner {
try {
String md5 = Constants.NULL;
long lastModified = 0L;
CacheItem cacheItem = ConfigService.getContentCache(groupKey);
CacheItem cacheItem = ConfigCacheService.getContentCache(groupKey);
if (cacheItem != null) {
if (cacheItem.isBeta()) {
if (cacheItem.getIps4Beta().contains(clientIp)) {
@ -288,7 +288,7 @@ public class ConfigServletInner {
}
private static void releaseConfigReadLock(String groupKey) {
ConfigService.releaseReadLock(groupKey);
ConfigCacheService.releaseReadLock(groupKey);
}
private static int tryConfigReadLock(String groupKey) {
@ -300,7 +300,7 @@ public class ConfigServletInner {
* 尝试加锁最多10次
*/
for (int i = TRY_GET_LOCK_TIMES; i >= 0; --i) {
lockResult = ConfigService.tryReadLock(groupKey);
lockResult = ConfigCacheService.tryReadLock(groupKey);
/**
* 数据不存在
*/

View File

@ -16,8 +16,8 @@
package com.alibaba.nacos.config.server.controller;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.service.DataSourceService;
import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.service.datasource.DataSourceService;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
import com.alibaba.nacos.core.utils.InetUtils;
import org.springframework.beans.factory.annotation.Autowired;

View File

@ -22,7 +22,9 @@ import com.alibaba.nacos.common.utils.Observer;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.event.RaftDBErrorEvent;
import com.alibaba.nacos.config.server.model.event.RaftDBErrorRecoverEvent;
import com.alibaba.nacos.config.server.service.repository.DerbyLoadEvent;
import com.alibaba.nacos.consistency.cp.CPProtocol;
import com.alibaba.nacos.consistency.cp.MetadataKey;
import com.alibaba.nacos.core.cluster.Member;
import com.alibaba.nacos.core.cluster.MemberMetaDataConstants;
import com.alibaba.nacos.core.cluster.ServerMemberManager;
@ -115,7 +117,7 @@ public class CurcuitFilter implements Filter {
private void listenerSelfInCluster() {
protocol.protocolMetaData().subscribe(Constants.CONFIG_MODEL_RAFT_GROUP,
com.alibaba.nacos.consistency.cp.Constants.RAFT_GROUP_MEMBER,
MetadataKey.RAFT_GROUP_MEMBER,
new Observer() {
@Override
public void update(Observable o, Object arg) {

View File

@ -15,6 +15,9 @@
*/
package com.alibaba.nacos.config.server.model;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.Serializable;
import java.sql.Timestamp;
@ -30,7 +33,7 @@ public class ConfigHistoryInfo implements Serializable {
* id, nid, data_id, group_id, content, md5, gmt_create, gmt_modified, 配置创建时间配置变更时间 src_user, src_ip, (变更操作者)
* op_type变更操作类型
*/
@JsonSerialize(using = ToStringSerializer.class)
private long id;
/**
* 上次改动历史的id

View File

@ -15,6 +15,9 @@
*/
package com.alibaba.nacos.config.server.model;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.Serializable;
/**
@ -25,7 +28,7 @@ import java.io.Serializable;
public class ConfigInfoAggr implements Serializable {
private static final long serialVersionUID = -3845825581059306364L;
@JsonSerialize(using = ToStringSerializer.class)
private long id;
private String dataId;

View File

@ -17,6 +17,8 @@ package com.alibaba.nacos.config.server.model;
import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.PrintWriter;
import java.io.Serializable;
@ -32,6 +34,7 @@ public class ConfigInfoBase implements Serializable, Comparable<ConfigInfoBase>
/**
* 不能增加字段
*/
@JsonSerialize(using = ToStringSerializer.class)
private long id;
private String dataId;
private String group;

View File

@ -15,6 +15,9 @@
*/
package com.alibaba.nacos.config.server.model;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.Serializable;
/**
@ -24,6 +27,8 @@ import java.io.Serializable;
*/
public class GroupInfo implements Serializable {
static final long serialVersionUID = -1L;
@JsonSerialize(using = ToStringSerializer.class)
private long id;
private String address;
private String group;

View File

@ -15,6 +15,9 @@
*/
package com.alibaba.nacos.config.server.model.capacity;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import java.io.Serializable;
import java.sql.Timestamp;
@ -27,6 +30,8 @@ import java.sql.Timestamp;
public class Capacity implements Serializable {
private static final long serialVersionUID = 77343194329627468L;
@JsonSerialize(using = ToStringSerializer.class)
private Long id;
private Integer quota;
private Integer usage;

View File

@ -1,4 +1,5 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,8 +13,9 @@
* 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.config.server.service;
package com.alibaba.nacos.config.server.model.event;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher.Event;
import org.apache.commons.lang3.StringUtils;

View File

@ -0,0 +1,221 @@
/*
*
* 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.config.server.model.event;
import com.alibaba.nacos.core.notify.Event;
/**
* @author <a href="mailto:liaochuntao@live.com">liaochuntao</a>
*/
public class ConfigDumpEvent implements Event {
private static final long serialVersionUID = -8776888606458370294L;
private boolean remove;
private String namespaceId;
private String dataId;
private String group;
private boolean isBeta;
private String tag;
private String content;
private String betaIps;
private String handleIp;
private String type;
private long lastModifiedTs;
public boolean isRemove() {
return remove;
}
public void setRemove(boolean remove) {
this.remove = remove;
}
public String getNamespaceId() {
return namespaceId;
}
public void setNamespaceId(String namespaceId) {
this.namespaceId = namespaceId;
}
public String getDataId() {
return dataId;
}
public void setDataId(String dataId) {
this.dataId = dataId;
}
public String getGroup() {
return group;
}
public void setGroup(String group) {
this.group = group;
}
public boolean isBeta() {
return isBeta;
}
public void setBeta(boolean beta) {
isBeta = beta;
}
public String getTag() {
return tag;
}
public void setTag(String tag) {
this.tag = tag;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getBetaIps() {
return betaIps;
}
public void setBetaIps(String betaIps) {
this.betaIps = betaIps;
}
public String getHandleIp() {
return handleIp;
}
public void setHandleIp(String handleIp) {
this.handleIp = handleIp;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public long getLastModifiedTs() {
return lastModifiedTs;
}
public void setLastModifiedTs(long lastModifiedTs) {
this.lastModifiedTs = lastModifiedTs;
}
public static ConfigDumpEventBuilder builder() {
return new ConfigDumpEventBuilder();
}
public static final class ConfigDumpEventBuilder {
private boolean remove;
private String namespaceId;
private String dataId;
private String group;
private boolean isBeta;
private String tag;
private String content;
private String betaIps;
private String handleIp;
private String type;
private long lastModifiedTs;
private ConfigDumpEventBuilder() {
}
public ConfigDumpEventBuilder remove(boolean remove) {
this.remove = remove;
return this;
}
public ConfigDumpEventBuilder namespaceId(String namespaceId) {
this.namespaceId = namespaceId;
return this;
}
public ConfigDumpEventBuilder dataId(String dataId) {
this.dataId = dataId;
return this;
}
public ConfigDumpEventBuilder group(String group) {
this.group = group;
return this;
}
public ConfigDumpEventBuilder isBeta(boolean isBeta) {
this.isBeta = isBeta;
return this;
}
public ConfigDumpEventBuilder tag(String tag) {
this.tag = tag;
return this;
}
public ConfigDumpEventBuilder content(String content) {
this.content = content;
return this;
}
public ConfigDumpEventBuilder betaIps(String betaIps) {
this.betaIps = betaIps;
return this;
}
public ConfigDumpEventBuilder handleIp(String handleIp) {
this.handleIp = handleIp;
return this;
}
public ConfigDumpEventBuilder type(String type) {
this.type = type;
return this;
}
public ConfigDumpEventBuilder lastModifiedTs(long lastModifiedTs) {
this.lastModifiedTs = lastModifiedTs;
return this;
}
public ConfigDumpEvent build() {
ConfigDumpEvent configDumpEvent = new ConfigDumpEvent();
configDumpEvent.setRemove(remove);
configDumpEvent.setNamespaceId(namespaceId);
configDumpEvent.setDataId(dataId);
configDumpEvent.setGroup(group);
configDumpEvent.setTag(tag);
configDumpEvent.setContent(content);
configDumpEvent.setBetaIps(betaIps);
configDumpEvent.setHandleIp(handleIp);
configDumpEvent.setType(type);
configDumpEvent.setLastModifiedTs(lastModifiedTs);
configDumpEvent.isBeta = this.isBeta;
return configDumpEvent;
}
}
}

View File

@ -1,4 +1,5 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,8 +13,9 @@
* 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.config.server.service;
package com.alibaba.nacos.config.server.model.event;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher.Event;

View File

@ -16,9 +16,9 @@
package com.alibaba.nacos.config.server.monitor;
import com.alibaba.nacos.config.server.service.ClientTrackService;
import com.alibaba.nacos.config.server.service.ConfigService;
import com.alibaba.nacos.config.server.service.TimerTaskService;
import com.alibaba.nacos.config.server.service.ConfigCacheService;
import com.alibaba.nacos.config.server.service.notify.AsyncNotifyService;
import com.alibaba.nacos.config.server.utils.ConfigExecutor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
@ -39,13 +39,13 @@ public class MemoryMonitor {
@Autowired
public MemoryMonitor(AsyncNotifyService notifySingleService) {
TimerTaskService.scheduleWithFixedDelay(new PrintMemoryTask(), DELAY_SECONDS,
ConfigExecutor.scheduleWithFixedDelay(new PrintMemoryTask(), DELAY_SECONDS,
DELAY_SECONDS, TimeUnit.SECONDS);
TimerTaskService.scheduleWithFixedDelay(new PrintGetConfigResponeTask(), DELAY_SECONDS,
ConfigExecutor.scheduleWithFixedDelay(new PrintGetConfigResponeTask(), DELAY_SECONDS,
DELAY_SECONDS, TimeUnit.SECONDS);
TimerTaskService.scheduleWithFixedDelay(new NotifyTaskQueueMonitorTask(notifySingleService), DELAY_SECONDS,
ConfigExecutor.scheduleWithFixedDelay(new NotifyTaskQueueMonitorTask(notifySingleService), DELAY_SECONDS,
DELAY_SECONDS, TimeUnit.SECONDS);
}
@ -70,7 +70,7 @@ class PrintMemoryTask implements Runnable {
@Override
public void run() {
int groupCount = ConfigService.groupCount();
int groupCount = ConfigCacheService.groupCount();
int subClientCount = ClientTrackService.subscribeClientCount();
long subCount = ClientTrackService.subscriberCount();
memoryLog.info("groupCount={}, subscriberClientCount={}, subscriberCount={}", groupCount, subClientCount,

View File

@ -85,7 +85,7 @@ public class ClientTrackService {
String groupKey = entry.getKey();
String clientMd5 = entry.getValue();
long lastPollingTs = record.groupKey2pollingTsMap.get(groupKey);
boolean isUpdate = ConfigService.isUptodate(groupKey, clientMd5);
boolean isUpdate = ConfigCacheService.isUptodate(groupKey, clientMd5);
status.put(groupKey, new SubscriberStatus(groupKey, isUpdate, clientMd5, lastPollingTs));
}
@ -104,7 +104,7 @@ public class ClientTrackService {
Long lastPollingTs = clientRec.groupKey2pollingTsMap.get(groupKey);
if (null != clientMd5 && lastPollingTs != null) {
Boolean isUpdate = ConfigService.isUptodate(groupKey, clientMd5);
Boolean isUpdate = ConfigCacheService.isUptodate(groupKey, clientMd5);
subs.put(clientRec.ip, new SubscriberStatus(groupKey, isUpdate, clientMd5, lastPollingTs));
}
@ -120,7 +120,7 @@ public class ClientTrackService {
for (Map.Entry<String, String> entry : getClientRecord(ip).groupKey2md5Map.entrySet()) {
String groupKey = entry.getKey();
String clientMd5 = entry.getValue();
Boolean isuptodate = ConfigService.isUptodate(groupKey, clientMd5);
Boolean isuptodate = ConfigCacheService.isUptodate(groupKey, clientMd5);
result.put(groupKey, isuptodate);
}
return result;
@ -135,7 +135,7 @@ public class ClientTrackService {
for (ClientRecord clientRec : clientRecords.values()) {
String clientMd5 = clientRec.groupKey2md5Map.get(groupKey);
if (null != clientMd5) {
Boolean isuptodate = ConfigService.isUptodate(groupKey, clientMd5);
Boolean isuptodate = ConfigCacheService.isUptodate(groupKey, clientMd5);
subs.put(clientRec.ip, isuptodate);
}
}

View File

@ -19,7 +19,9 @@ import com.alibaba.nacos.common.utils.MD5Utils;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.model.CacheItem;
import com.alibaba.nacos.config.server.model.ConfigInfoBase;
import com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent;
import com.alibaba.nacos.config.server.service.repository.PersistService;
import com.alibaba.nacos.config.server.utils.DiskUtil;
import com.alibaba.nacos.config.server.utils.GroupKey;
import com.alibaba.nacos.config.server.utils.GroupKey2;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
@ -41,7 +43,7 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.*;
*
* @author Nacos
*/
public class ConfigService {
public class ConfigCacheService {
@Autowired
private static PersistService persistService;
@ -72,11 +74,11 @@ public class ConfigService {
try {
final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentMd5(groupKey))) {
if (md5.equals(ConfigCacheService.getContentMd5(groupKey))) {
dumpLog.warn(
"[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
groupKey, md5, ConfigCacheService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!PropertyUtil.isDirectRead()) {
DiskUtil.saveToDisk(dataId, group, tenant, content);
}
@ -117,11 +119,11 @@ public class ConfigService {
try {
final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentBetaMd5(groupKey))) {
if (md5.equals(ConfigCacheService.getContentBetaMd5(groupKey))) {
dumpLog.warn(
"[dump-beta-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
groupKey, md5, ConfigCacheService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!PropertyUtil.isDirectRead()) {
DiskUtil.saveBetaToDisk(dataId, group, tenant, content);
}
@ -156,11 +158,11 @@ public class ConfigService {
try {
final String md5 = MD5Utils.md5Hex(content, Constants.ENCODE);
if (md5.equals(ConfigService.getContentTagMd5(groupKey, tag))) {
if (md5.equals(ConfigCacheService.getContentTagMd5(groupKey, tag))) {
dumpLog.warn(
"[dump-tag-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
groupKey, md5, ConfigCacheService.getLastModifiedTs(groupKey), lastModifiedTs);
} else if (!PropertyUtil.isDirectRead()) {
DiskUtil.saveTagToDisk(dataId, group, tenant, tag, content);
}
@ -199,7 +201,7 @@ public class ConfigService {
dumpLog.warn(
"[dump-ignore] ignore to save cache file. groupKey={}, md5={}, lastModifiedOld={}, "
+ "lastModifiedNew={}",
groupKey, md5, ConfigService.getLastModifiedTs(groupKey), lastModifiedTs);
groupKey, md5, ConfigCacheService.getLastModifiedTs(groupKey), lastModifiedTs);
} else {
DiskUtil.saveToDisk(dataId, group, tenant, content);
}
@ -517,12 +519,12 @@ public class ConfigService {
}
static public boolean isUptodate(String groupKey, String md5) {
String serverMd5 = ConfigService.getContentMd5(groupKey);
String serverMd5 = ConfigCacheService.getContentMd5(groupKey);
return StringUtils.equals(md5, serverMd5);
}
static public boolean isUptodate(String groupKey, String md5, String ip, String tag) {
String serverMd5 = ConfigService.getContentMd5(groupKey, ip, tag);
String serverMd5 = ConfigCacheService.getContentMd5(groupKey, ip, tag);
return StringUtils.equals(md5, serverMd5);
}
@ -584,7 +586,7 @@ public class ConfigService {
private final static String NO_SPACE_EN = "No space left on device";
private final static String DISK_QUATA_CN = "超出磁盘限额";
private final static String DISK_QUATA_EN = "Disk quota exceeded";
static final Logger log = LoggerFactory.getLogger(ConfigService.class);
static final Logger log = LoggerFactory.getLogger(ConfigCacheService.class);
/**
* groupKey -> cacheItem
*/

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.config.server.service;
import com.alibaba.nacos.config.server.model.SampleResult;
import com.alibaba.nacos.config.server.model.event.LocalDataChangeEvent;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.utils.GroupKey;
import com.alibaba.nacos.config.server.utils.LogUtil;
@ -302,7 +303,7 @@ public class LongPollingService extends AbstractEventListener {
@Override
public void run() {
try {
ConfigService.getContentBetaMd5(groupKey);
ConfigCacheService.getContentBetaMd5(groupKey);
for (Iterator<ClientLongPolling> iter = allSubs.iterator(); iter.hasNext(); ) {
ClientLongPolling clientSub = iter.next();
if (clientSub.clientMd5Map.containsKey(groupKey)) {

View File

@ -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.config.server.service;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
/**
* 定时任务服务
*
* @author Nacos
*/
public class TimerTaskService {
@SuppressWarnings("PMD.ThreadPoolCreationRule")
private static ScheduledExecutorService scheduledExecutorService = Executors
.newScheduledThreadPool(10, new ThreadFactory() {
AtomicInteger count = new AtomicInteger(0);
@Override
public Thread newThread(Runnable r) {
Thread t = new Thread(r);
t.setDaemon(true);
t.setName("com.alibaba.nacos.server.Timer-" + count.getAndIncrement());
return t;
}
});
static public void scheduleWithFixedDelay(Runnable command, long initialDelay, long delay,
TimeUnit unit) {
scheduledExecutorService.scheduleWithFixedDelay(command, initialDelay, delay, unit);
}
}

View File

@ -17,8 +17,8 @@ package com.alibaba.nacos.config.server.service.capacity;
import com.alibaba.nacos.config.server.model.capacity.Capacity;
import com.alibaba.nacos.config.server.model.capacity.GroupCapacity;
import com.alibaba.nacos.config.server.service.DataSourceService;
import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.service.datasource.DataSourceService;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.google.common.collect.Lists;

View File

@ -16,8 +16,8 @@
package com.alibaba.nacos.config.server.service.capacity;
import com.alibaba.nacos.config.server.model.capacity.TenantCapacity;
import com.alibaba.nacos.config.server.service.DataSourceService;
import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.service.datasource.DataSourceService;
import com.alibaba.nacos.config.server.service.datasource.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.google.common.collect.Lists;

View File

@ -1,4 +1,5 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,8 +13,9 @@
* 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.config.server.service;
package com.alibaba.nacos.config.server.service.datasource;
import java.io.IOException;
import org.springframework.jdbc.core.JdbcTemplate;

View File

@ -1,4 +1,5 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,8 +13,9 @@
* 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.config.server.service;
package com.alibaba.nacos.config.server.service.datasource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
@ -48,7 +50,7 @@ public class DynamicDataSource {
}
else {
if (basicDataSourceService == null) {
basicDataSourceService = new BasicDataSourceServiceImpl();
basicDataSourceService = new ExternalDataSourceServiceImpl();
basicDataSourceService.init();
}
return basicDataSourceService;

View File

@ -1,4 +1,5 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,15 +13,17 @@
* 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.config.server.service;
package com.alibaba.nacos.config.server.service.datasource;
import com.alibaba.nacos.common.utils.ConvertUtils;
import com.alibaba.nacos.common.utils.StringUtils;
import com.alibaba.nacos.config.server.monitor.MetricsMonitor;
import com.alibaba.nacos.config.server.utils.ConfigExecutor;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import com.zaxxer.hikari.HikariDataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
@ -38,7 +41,7 @@ import java.util.concurrent.TimeUnit;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.alibaba.nacos.config.server.service.RowMapperManager.CONFIG_INFO4BETA_ROW_MAPPER;
import static com.alibaba.nacos.config.server.service.repository.RowMapperManager.CONFIG_INFO4BETA_ROW_MAPPER;
import static com.alibaba.nacos.config.server.utils.LogUtil.defaultLog;
import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
@ -47,9 +50,10 @@ import static com.alibaba.nacos.config.server.utils.LogUtil.fatalLog;
*
* @author Nacos
*/
public class BasicDataSourceServiceImpl implements DataSourceService {
public class ExternalDataSourceServiceImpl implements DataSourceService {
private static final Logger log = LoggerFactory.getLogger(BasicDataSourceServiceImpl.class);
private static final Logger log = LoggerFactory.getLogger(
ExternalDataSourceServiceImpl.class);
private static final String DEFAULT_MYSQL_DRIVER = "com.mysql.jdbc.Driver";
private static final String MYSQL_HIGH_LEVEL_DRIVER = "com.mysql.cj.jdbc.Driver";
private static String JDBC_DRIVER_NAME;
@ -63,7 +67,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
private static final String DB_LOAD_ERROR_MSG = "[db-load-error]load jdbc.properties error";
private List<BasicDataSource> dataSourceList = new ArrayList<BasicDataSource>();
private List<HikariDataSource> dataSourceList = new ArrayList<>();
private JdbcTemplate jt;
private DataSourceTransactionManager tm;
private TransactionTemplate tjt;
@ -89,7 +93,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
@PostConstruct
public void init() {
queryTimeout = NumberUtils.toInt(System.getProperty("QUERYTIMEOUT"), 3);
queryTimeout = ConvertUtils.toInt(System.getProperty("QUERYTIMEOUT"), 3);
jt = new JdbcTemplate();
/**
* 设置最大记录数防止内存膨胀
@ -125,16 +129,16 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
throw new RuntimeException(DB_LOAD_ERROR_MSG);
}
TimerTaskService.scheduleWithFixedDelay(new SelectMasterTask(), 10, 10,
ConfigExecutor.scheduleWithFixedDelay(new SelectMasterTask(), 10, 10,
TimeUnit.SECONDS);
TimerTaskService.scheduleWithFixedDelay(new CheckDBHealthTask(), 10, 10,
ConfigExecutor.scheduleWithFixedDelay(new CheckDBHealthTask(), 10, 10,
TimeUnit.SECONDS);
}
}
@Override
public synchronized void reload() throws IOException {
List<BasicDataSource> dblist = new ArrayList<BasicDataSource>();
List<HikariDataSource> dblist = new ArrayList<>();
try {
String val = null;
val = ApplicationUtils.getProperty("db.num");
@ -144,7 +148,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
int dbNum = Integer.parseInt(val.trim());
for (int i = 0; i < dbNum; i++) {
BasicDataSource ds = new BasicDataSource();
HikariDataSource ds = new HikariDataSource();
ds.setDriverClassName(JDBC_DRIVER_NAME);
val = ApplicationUtils.getProperty("db.url." + i);
@ -152,7 +156,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
fatalLog.error("db.url." + i + " is null");
throw new IllegalArgumentException("db.url." + i + " is null");
}
ds.setUrl(val.trim());
ds.setJdbcUrl(val.trim());
val = ApplicationUtils.getProperty("db.user." + i, ApplicationUtils.getProperty("db.user"));
if (null == val) {
@ -168,23 +172,17 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
}
ds.setPassword(val.trim());
val = ApplicationUtils.getProperty("db.initialSize." + i, ApplicationUtils.getProperty("db.initialSize"));
ds.setInitialSize(Integer.parseInt(defaultIfNull(val, "10")));
val = ApplicationUtils.getProperty("db.maxPoolSize." + i, ApplicationUtils.getProperty("db.maxPoolSize"));
ds.setMaximumPoolSize(Integer.parseInt(defaultIfNull(val, "20")));
val = ApplicationUtils.getProperty("db.maxActive." + i, ApplicationUtils.getProperty("db.maxActive"));
ds.setMaxActive(Integer.parseInt(defaultIfNull(val, "20")));
val = ApplicationUtils.getProperty("db.minIdle." + i, ApplicationUtils.getProperty("db.minIdle"));
ds.setMinimumIdle(Integer.parseInt(defaultIfNull(val, "50")));
val = ApplicationUtils.getProperty("db.maxIdle." + i, ApplicationUtils.getProperty("db.maxIdle"));
ds.setMaxIdle(Integer.parseInt(defaultIfNull(val, "50")));
ds.setMaxWait(3000L);
ds.setPoolPreparedStatements(true);
ds.setConnectionTimeout(3000L);
// 每10分钟检查一遍连接池
ds.setTimeBetweenEvictionRunsMillis(TimeUnit.MINUTES
.toMillis(10L));
ds.setTestWhileIdle(true);
ds.setValidationQuery("SELECT 1 FROM dual");
ds.setValidationTimeout(TimeUnit.MINUTES.toMillis(10L));
ds.setConnectionTestQuery("SELECT 1 FROM dual");
dblist.add(ds);
@ -250,8 +248,8 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
if (ds == null) {
return StringUtils.EMPTY;
}
BasicDataSource bds = (BasicDataSource) ds;
return bds.getUrl();
HikariDataSource bds = (HikariDataSource) ds;
return bds.getJdbcUrl();
}
@Override
@ -262,12 +260,12 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
/**
* 主库不健康
*/
return "DOWN:" + getIpFromUrl(dataSourceList.get(i).getUrl());
return "DOWN:" + getIpFromUrl(dataSourceList.get(i).getJdbcUrl());
} else {
/**
* 从库不健康
*/
return "WARN:" + getIpFromUrl(dataSourceList.get(i).getUrl());
return "WARN:" + getIpFromUrl(dataSourceList.get(i).getJdbcUrl());
}
}
}
@ -299,7 +297,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
boolean isFound = false;
int index = -1;
for (BasicDataSource ds : dataSourceList) {
for (HikariDataSource ds : dataSourceList) {
index++;
testMasterJT.setDataSource(ds);
testMasterJT.setQueryTimeout(queryTimeout);
@ -308,7 +306,7 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
.update(
"DELETE FROM config_info WHERE data_id='com.alibaba.nacos.testMasterDB'");
if (jt.getDataSource() != ds) {
fatalLog.warn("[master-db] {}", ds.getUrl());
fatalLog.warn("[master-db] {}", ds.getJdbcUrl());
}
jt.setDataSource(ds);
tm.setDataSource(ds);
@ -345,10 +343,10 @@ public class BasicDataSourceServiceImpl implements DataSourceService {
} catch (DataAccessException e) {
if (i == masterIndex) {
fatalLog.error("[db-error] master db {} down.",
getIpFromUrl(dataSourceList.get(i).getUrl()));
getIpFromUrl(dataSourceList.get(i).getJdbcUrl()));
} else {
fatalLog.error("[db-error] slave db {} down.",
getIpFromUrl(dataSourceList.get(i).getUrl()));
getIpFromUrl(dataSourceList.get(i).getJdbcUrl()));
}
isHealthList.set(i, Boolean.FALSE);

View File

@ -1,4 +1,5 @@
/*
*
* Copyright 1999-2018 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -12,13 +13,16 @@
* 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.config.server.service;
package com.alibaba.nacos.config.server.service.datasource;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.api.exception.runtime.NacosRuntimeException;
import com.alibaba.nacos.config.server.constant.Constants;
import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.common.utils.DiskUtils;
import com.alibaba.nacos.core.utils.DiskUtils;
import com.alibaba.nacos.core.utils.ApplicationUtils;
import com.zaxxer.hikari.HikariDataSource;
import java.io.File;
@ -87,7 +91,7 @@ public class LocalDataSourceServiceImpl implements DataSourceService {
if (LogUtil.defaultLog.isErrorEnabled()) {
LogUtil.defaultLog.error(e.getMessage(), e);
}
throw new RuntimeException("load schema.sql error.", e);
throw new NacosRuntimeException(NacosException.SERVER_ERROR, "load schema.sql error.", e);
}
}

Some files were not shown because too many files have changed in this diff Show More