Merge pull request #999 from alibaba/1.0.0-RC3

1.0.0 rc3
This commit is contained in:
Fury Zhu 2019-04-03 12:13:39 +08:00 committed by GitHub
commit 539280c44b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
68 changed files with 654 additions and 412 deletions

View File

@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -32,6 +32,8 @@ public class PropertyKeyConst {
public final static String SECRET_KEY = "secretKey";
public final static String RAM_ROLE_NAME = "ramRoleName";
public final static String SERVER_ADDR = "serverAddr";
public final static String CONTEXT_PATH = "contextPath";

View File

@ -138,6 +138,16 @@ public interface NamingService {
*/
void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException;
/**
* deregister instance with full instance information
*
* @param serviceName name of service
* @param groupName group of service
* @param instance instance information
* @throws NacosException
*/
void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException;
/**
* get all instances of a service
*

View File

@ -0,0 +1,30 @@
/*
* 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;
/**
* Some keys of metadata that are recognized by Nacos
*
* @author nkorange
* @since 1.0.0
*/
public class PreservedMetadataKeys {
/**
* The key to indicate the registry source of service instance, such as Dubbo, SpringCloud, etc.
*/
public static final String REGISTER_SOURCE = "preserved.register.source";
}

View File

@ -16,7 +16,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>
@ -85,18 +85,18 @@
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
</dependency>
<dependency>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<optional>true</optional>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<optional>true</optional>
</dependency>

View File

@ -29,10 +29,9 @@ 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.StringUtils;
import org.codehaus.jackson.annotate.JsonProperty;
import org.codehaus.jackson.type.TypeReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.type.TypeReference;
import org.slf4j.Logger;
import java.io.IOException;
import java.net.ConnectException;
import java.net.HttpURLConnection;
@ -222,6 +221,11 @@ public class ServerHttpAgent implements HttpAgent {
}
private void initAkSk(Properties properties) {
String ramRoleName = properties.getProperty(PropertyKeyConst.RAM_ROLE_NAME);
if (!StringUtils.isBlank(ramRoleName)) {
STSConfig.getInstance().setRamRoleName(ramRoleName);
}
String ak = properties.getProperty(PropertyKeyConst.ACCESS_KEY);
if (StringUtils.isBlank(ak)) {
accessKey = SpasAdapter.getAk();

View File

@ -29,7 +29,6 @@ import com.alibaba.nacos.client.monitor.MetricsMonitor;
import com.alibaba.nacos.client.utils.LogUtils;
import com.alibaba.nacos.client.utils.ParamUtil;
import com.alibaba.nacos.client.utils.StringUtils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import java.io.File;
@ -102,7 +101,6 @@ public class ClientWorker {
}
}
@SuppressFBWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER")
void removeCache(String dataId, String group) {
String groupKey = GroupKey.getKey(dataId, group);
synchronized (cacheMap) {
@ -115,7 +113,6 @@ public class ClientWorker {
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
}
@SuppressFBWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER")
void removeCache(String dataId, String group, String tenant) {
String groupKey = GroupKey.getKeyTenant(dataId, group, tenant);
synchronized (cacheMap) {
@ -128,7 +125,6 @@ public class ClientWorker {
MetricsMonitor.getListenConfigCountMonitor().set(cacheMap.get().size());
}
@SuppressFBWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER")
public CacheData addCacheDataIfAbsent(String dataId, String group) {
CacheData cache = getCache(dataId, group);
if (null != cache) {
@ -163,7 +159,6 @@ public class ClientWorker {
return cache;
}
@SuppressFBWarnings("JLM_JSR166_UTILCONCURRENT_MONITORENTER")
public CacheData addCacheDataIfAbsent(String dataId, String group, String tenant) {
CacheData cache = getCache(dataId, group, tenant);
if (null != cache) {

View File

@ -21,7 +21,6 @@ import com.alibaba.nacos.client.config.impl.EventDispatcher.ServerlistChangeEven
import com.alibaba.nacos.client.config.impl.HttpSimpleClient.HttpResult;
import com.alibaba.nacos.client.config.utils.IOUtils;
import com.alibaba.nacos.client.utils.*;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import java.io.IOException;
@ -396,7 +395,6 @@ class ServerAddressIterator implements Iterator<String> {
}
@Override
@SuppressFBWarnings("EQ_COMPARETO_USE_OBJECT_EQUALS")
public int compareTo(RandomizedServerAddress other) {
if (this.priority != other.priority) {
return other.priority - this.priority;

View File

@ -25,7 +25,7 @@ import com.alibaba.nacos.client.utils.StringUtils;
@SuppressWarnings("PMD.ClassNamingShouldBeCamelRule")
public class STSConfig {
private static final String RAM_SECURITY_CREDENTIALS_URL
= "";
= "http://100.100.100.200/latest/meta-data/ram/security-credentials/";
private String ramRoleName;
/**
* STS 临时凭证有效期剩余多少时开始刷新允许本地时间比 STS 服务时间最多慢多久

View File

@ -104,14 +104,11 @@ public class NacosNamingService implements NamingService {
private int initClientBeatThreadCount(Properties properties) {
if (properties == null) {
return UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT;
}
int clientBeatThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
return NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_CLIENT_BEAT_THREAD_COUNT),
UtilAndComs.DEFAULT_CLIENT_BEAT_THREAD_COUNT);
return clientBeatThreadCount;
}
private int initPollingThreadCount(Properties properties) {
@ -120,10 +117,8 @@ public class NacosNamingService implements NamingService {
return UtilAndComs.DEFAULT_POLLING_THREAD_COUNT;
}
int pollingThreadCount = NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
return NumberUtils.toInt(properties.getProperty(PropertyKeyConst.NAMING_POLLING_THREAD_COUNT),
UtilAndComs.DEFAULT_POLLING_THREAD_COUNT);
return pollingThreadCount;
}
private boolean isLoadCacheAtStart(Properties properties) {
@ -283,16 +278,18 @@ public class NacosNamingService implements NamingService {
@Override
public void registerInstance(String serviceName, String groupName, Instance instance) throws NacosException {
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName());
beatInfo.setWeight(instance.getWeight());
beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false);
if (instance.isEphemeral()) {
BeatInfo beatInfo = new BeatInfo();
beatInfo.setServiceName(NamingUtils.getGroupedName(serviceName, groupName));
beatInfo.setIp(instance.getIp());
beatInfo.setPort(instance.getPort());
beatInfo.setCluster(instance.getClusterName());
beatInfo.setWeight(instance.getWeight());
beatInfo.setMetadata(instance.getMetadata());
beatInfo.setScheduled(false);
beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
beatReactor.addBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), beatInfo);
}
serverProxy.registerService(NamingUtils.getGroupedName(serviceName, groupName), groupName, instance);
}
@ -314,8 +311,18 @@ public class NacosNamingService implements NamingService {
@Override
public void deregisterInstance(String serviceName, String groupName, String ip, int port, String clusterName) throws NacosException {
beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), ip, port);
serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), ip, port, clusterName);
Instance instance = new Instance();
instance.setIp(ip);
instance.setPort(port);
instance.setClusterName(clusterName);
deregisterInstance(serviceName, groupName, instance);
}
@Override
public void deregisterInstance(String serviceName, String groupName, Instance instance) throws NacosException {
beatReactor.removeBeatInfo(NamingUtils.getGroupedName(serviceName, groupName), instance.getIp(), instance.getPort());
serverProxy.deregisterService(NamingUtils.getGroupedName(serviceName, groupName), instance);
}
@Override

View File

@ -25,7 +25,6 @@ import com.alibaba.nacos.client.naming.cache.DiskCache;
import com.alibaba.nacos.client.naming.net.NamingProxy;
import com.alibaba.nacos.client.naming.utils.StringUtils;
import com.alibaba.nacos.client.naming.utils.UtilAndComs;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.util.*;
import java.util.concurrent.*;
@ -267,7 +266,6 @@ public class HostReactor {
}
}
@SuppressFBWarnings("NN_NAKED_NOTIFY")
public void updateServiceNow(String serviceName, String clusters) {
ServiceInfo oldService = getSerivceInfo0(serviceName, clusters);
try {

View File

@ -187,17 +187,18 @@ public class NamingProxy {
}
public void deregisterService(String serviceName, String ip, int port, String clusterName) throws NacosException {
public void deregisterService(String serviceName, Instance instance) throws NacosException {
NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}:{}@{}",
namespaceId, serviceName, ip, port, clusterName);
NAMING_LOGGER.info("[DEREGISTER-SERVICE] {} deregistering service {} with instance: {}",
namespaceId, serviceName, instance);
final Map<String, String> params = new HashMap<String, String>(8);
params.put(CommonParams.NAMESPACE_ID, namespaceId);
params.put("ip", ip);
params.put("port", String.valueOf(port));
params.put(CommonParams.SERVICE_NAME, serviceName);
params.put(CommonParams.CLUSTER_NAME, clusterName);
params.put(CommonParams.CLUSTER_NAME, instance.getClusterName());
params.put("ip", instance.getIp());
params.put("port", String.valueOf(instance.getPort()));
params.put("ephemeral", String.valueOf(instance.isEphemeral()));
reqAPI(UtilAndComs.NACOS_URL_INSTANCE, params, HttpMethod.DELETE);
}

View File

@ -15,7 +15,6 @@
*/
package com.alibaba.nacos.client.naming.utils;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import java.io.*;
import java.nio.channels.FileChannel;
@ -163,7 +162,6 @@ public class IoUtils {
}
}
@SuppressFBWarnings("BIT_IOR_OF_SIGNED_BYTE")
public static boolean isGzipStream(byte[] bytes) {
int minByteArraySize = 2;

View File

@ -16,11 +16,9 @@
package com.alibaba.nacos.client.utils;
import java.io.IOException;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* Json tool
@ -33,7 +31,7 @@ public class JSONUtils {
static ObjectMapper mapper = new ObjectMapper();
static {
mapper.disable(Feature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
public static String serializeObject(Object o) throws IOException {
@ -49,12 +47,4 @@ public class JSONUtils {
return mapper.readValue(s, typeReference);
}
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
public static Object deserializeCollection(String s, JavaType type) throws IOException {
return mapper.readValue(s, type);
}
}

View File

@ -18,7 +18,7 @@
<parent>
<artifactId>nacos-all</artifactId>
<groupId>com.alibaba.nacos</groupId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -30,8 +30,9 @@ public class VersionUtils {
static{
InputStream in = null;
try{
InputStream in = VersionUtils.class.getClassLoader()
in = VersionUtils.class.getClassLoader()
.getResourceAsStream("nacos-version.txt");
Properties props = new Properties();
props.load(in);
@ -41,6 +42,14 @@ public class VersionUtils {
}
} catch(Exception e) {
e.printStackTrace();
} finally {
try {
if (in != null) {
in.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
}

View File

@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
@ -103,19 +103,20 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</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>net.jcip</groupId>
<artifactId>jcip-annotations</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>

View File

@ -23,10 +23,9 @@ import com.alibaba.nacos.config.server.utils.LogUtil;
import com.alibaba.nacos.config.server.utils.RunningConfigUtils;
import com.alibaba.nacos.config.server.utils.ThreadUtil;
import org.apache.commons.lang3.StringUtils;
import org.codehaus.jackson.type.TypeReference;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.net.HttpURLConnection;
import java.net.URLEncoder;
import java.util.ArrayList;

View File

@ -157,16 +157,17 @@ public class DiskUtil {
static public String getConfig(String dataId, String group, String tenant)
throws IOException {
FileInputStream fis = null;
File file = targetFile(dataId, group, tenant);
if (file.exists()) {
FileInputStream fis = null;
try {
fis = new FileInputStream(file);
return IOUtils.toString(fis, Constants.ENCODE);
} catch (FileNotFoundException e) {
return StringUtils.EMPTY;
} finally {
IOUtils.closeQuietly(fis);
}
String content = IOUtils.toString(fis, Constants.ENCODE);
return content;
} else {
return StringUtils.EMPTY;
}

View File

@ -60,8 +60,6 @@ import com.alibaba.nacos.config.server.utils.PaginationHelper;
import com.alibaba.nacos.config.server.utils.event.EventDispatcher;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
/**
* 数据库服务提供ConfigInfo在数据库的存取<br> 3.0开始增加数据版本号, 并将物理删除改为逻辑删除<br> 3.0增加数据库切换功能
*
@ -2639,9 +2637,6 @@ public class PersistService {
try {
jt.update(new PreparedStatementCreator() {
@SuppressFBWarnings(value = {"OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
"SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"},
justification = "findbugs does not trust jdbctemplate, sql is constant in practice")
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ps.setString(1, configInfo.getDataId());

View File

@ -22,7 +22,6 @@ import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
@ -128,9 +127,6 @@ public class GroupCapacityPersistService {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
@Override
@SuppressFBWarnings(value = {"OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
"SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"},
justification = "findbugs does not trust jdbctemplate, sql is constant in practice")
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
String group = capacity.getGroup();
@ -306,9 +302,6 @@ public class GroupCapacityPersistService {
try {
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
@Override
@SuppressFBWarnings(value = {"OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
"SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"},
justification = "findbugs does not trust jdbctemplate, sql is constant in practice")
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(
"DELETE FROM group_capacity WHERE group_id = ?;");

View File

@ -21,7 +21,6 @@ import com.alibaba.nacos.config.server.service.DynamicDataSource;
import com.alibaba.nacos.config.server.utils.PropertyUtil;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.google.common.collect.Lists;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.CannotGetJdbcConnectionException;
import org.springframework.jdbc.core.JdbcTemplate;
@ -96,9 +95,6 @@ public class TenantCapacityPersistService {
GeneratedKeyHolder generatedKeyHolder = new GeneratedKeyHolder();
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
@Override
@SuppressFBWarnings(value = {"OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
"SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"},
justification = "findbugs does not trust jdbctemplate, sql is constant in practice")
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
String tenant = tenantCapacity.getTenant();
@ -257,9 +253,6 @@ public class TenantCapacityPersistService {
try {
PreparedStatementCreator preparedStatementCreator = new PreparedStatementCreator() {
@Override
@SuppressFBWarnings(value = {"OBL_UNSATISFIED_OBLIGATION_EXCEPTION_EDGE",
"SQL_PREPARED_STATEMENT_GENERATED_FROM_NONCONSTANT_STRING"},
justification = "findbugs does not trust jdbctemplate, sql is constant in practice")
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement(
"DELETE FROM tenant_capacity WHERE tenant_id = ?;");

View File

@ -15,12 +15,11 @@
*/
package com.alibaba.nacos.config.server.utils;
import org.codehaus.jackson.map.DeserializationConfig.Feature;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;
import org.codehaus.jackson.type.TypeReference;
import java.io.IOException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
/**
* json util
@ -33,7 +32,7 @@ public class JSONUtils {
static ObjectMapper mapper = new ObjectMapper();
static {
mapper.disable(Feature.FAIL_ON_UNKNOWN_PROPERTIES);
mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
}
public static String serializeObject(Object o) throws IOException {
@ -49,12 +48,4 @@ public class JSONUtils {
return mapper.readValue(s, typeReference);
}
public static JavaType getCollectionType(Class<?> collectionClass, Class<?>... elementClasses) {
return mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
}
public static Object deserializeCollection(String s, JavaType type) throws IOException {
return mapper.readValue(s, type);
}
}

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
</parent>
<artifactId>nacos-console</artifactId>
<!--<packaging>war</packaging>-->

View File

@ -161,7 +161,7 @@ module.exports = {
'com.alibaba.nacos.page.newconfig.the_more_advanced': 'Group不能为空',
'nacos.page.namespace.ACM_special_SecretKey_will_be_abandoned,_not_recommended_for_use4':
'ACM专用SecretKey即将废弃不建议使用:',
'com.alibaba.nacos.page.namespace.public': 'public(保留控件)',
'com.alibaba.nacos.page.namespace.public': 'public(保留空间)',
'com.alibaba.nacos.page.configsync.error': '错误',
'nacos.page.namespace.AccessKey_recommended1': 'AccessKey开发环境推荐:',
'com.alibaba.nacos.page.pushTrajectory.configuration': '配置',

View File

@ -64,7 +64,7 @@ const I18N_CONF = {
namespaceDelete: '删除',
details: '详情',
edit: '编辑',
namespacePublic: 'public(保留控件)',
namespacePublic: 'public(保留空间)',
pubNoData: '没有数据',
namespaceAdd: '新建命名空间',
namespaceNames: '命名空间名称',

View File

@ -19,16 +19,19 @@
<meta name="viewport" content="width=device-width,initial-scale=1">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Nacos</title>
<link rel="shortcut icon" href="//www.aliyun.com/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/console1412.css">
<link rel="shortcut icon" href="//www.aliyun.com/favicon.ico" type="image/x-icon">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/bootstrap.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/console1412.css">
<!-- 第三方css开始 -->
<link rel="stylesheet" type="text/css" href="console-fe/public/css/codemirror.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/merge.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/icon.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/font-awesome.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/codemirror.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/merge.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/icon.css">
<link rel="stylesheet" type="text/css" href="console-fe/public/css/font-awesome.css">
<!-- 第三方css结束 -->
<link href="css/main.css" rel="stylesheet"></head>
<link href="css/main.css" rel="stylesheet">
</head>
<body>
<div id="root" style="overflow:hidden"></div>
@ -36,18 +39,19 @@
<div id="other"></div>
<!-- 第三方js开始 -->
<script src="console-fe/public/js/jquery.js"></script>
<script src="console-fe/public/js/codemirror.js"></script>
<script src="console-fe/public/js/javascript.js"></script>
<script src="console-fe/public/js/xml.js"></script>
<script src="console-fe/public/js/codemirror.addone.fullscreen.js"></script>
<script src="console-fe/public/js/codemirror.addone.lint.js"></script>
<script src="console-fe/public/js/codemirror.lib.json-lint.js"></script>
<script src="console-fe/public/js/codemirror.addone.json-lint.js"></script>
<script src="console-fe/public/js/codemirror.lib.clike-lint.js"></script>
<script src="console-fe/public/js/diff_match_patch.js"></script>
<script src="console-fe/public/js/merge.js"></script>
<script src="console-fe/public/js/loader.js"></script>
<script src="console-fe/public/js/jquery.js"></script>
<script src="console-fe/public/js/codemirror.js"></script>
<script src="console-fe/public/js/javascript.js"></script>
<script src="console-fe/public/js/xml.js"></script>
<script src="console-fe/public/js/codemirror.addone.fullscreen.js"></script>
<script src="console-fe/public/js/codemirror.addone.lint.js"></script>
<script src="console-fe/public/js/codemirror.lib.json-lint.js"></script>
<script src="console-fe/public/js/codemirror.addone.json-lint.js"></script>
<script src="console-fe/public/js/codemirror.lib.clike-lint.js"></script>
<script src="console-fe/public/js/diff_match_patch.js"></script>
<script src="console-fe/public/js/merge.js"></script>
<script src="console-fe/public/js/loader.js"></script>
<!-- 第三方js结束 -->
<script type="text/javascript" src="js/main.js"></script></body>

File diff suppressed because one or more lines are too long

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -16,6 +16,7 @@
package com.alibaba.nacos.core.utils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -34,8 +35,8 @@ public class PropertyUtil {
private static final Logger log = LoggerFactory.getLogger(PropertyUtil.class);
static {
InputStream inputStream = null;
try {
InputStream inputStream = null;
String baseDir = System.getProperty("nacos.home");
if (!StringUtils.isBlank(baseDir)) {
inputStream = new FileInputStream(baseDir + "/conf/application.properties");
@ -46,6 +47,8 @@ public class PropertyUtil {
properties.load(inputStream);
} catch (Exception e) {
log.error("read property file error:" + e);
} finally {
IOUtils.closeQuietly(inputStream);
}
}

View File

@ -22,10 +22,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.lang.management.ManagementFactory;
import java.util.ArrayList;
import java.util.Arrays;
@ -59,8 +56,7 @@ public class SystemUtils {
public static final String FUNCTION_MODE_NAMING = "naming";
private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean)ManagementFactory
private static OperatingSystemMXBean operatingSystemMXBean = (OperatingSystemMXBean) ManagementFactory
.getOperatingSystemMXBean();
/**
@ -98,15 +94,15 @@ public class SystemUtils {
}
public static float getLoad() {
return (float)operatingSystemMXBean.getSystemLoadAverage();
return (float) operatingSystemMXBean.getSystemLoadAverage();
}
public static float getCPU() {
return (float)operatingSystemMXBean.getSystemCpuLoad();
return (float) operatingSystemMXBean.getSystemCpuLoad();
}
public static float getMem() {
return (float)(1 - (double)operatingSystemMXBean.getFreePhysicalMemorySize() / (double)operatingSystemMXBean
return (float) (1 - (double) operatingSystemMXBean.getFreePhysicalMemorySize() / (double) operatingSystemMXBean
.getTotalPhysicalMemorySize());
}
@ -128,23 +124,31 @@ public class SystemUtils {
public static List<String> readClusterConf() throws IOException {
List<String> instanceList = new ArrayList<String>();
List<String> lines = IoUtils.readLines(
new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), UTF_8));
String comment = "#";
for (String line : lines) {
String instance = line.trim();
if (instance.startsWith(comment)) {
// # it is ip
continue;
Reader reader = null;
try {
reader = new InputStreamReader(new FileInputStream(new File(CLUSTER_CONF_FILE_PATH)), UTF_8);
List<String> lines = IoUtils.readLines(reader);
String comment = "#";
for (String line : lines) {
String instance = line.trim();
if (instance.startsWith(comment)) {
// # it is ip
continue;
}
if (instance.contains(comment)) {
// 192.168.71.52:8848 # Instance A
instance = instance.substring(0, instance.indexOf(comment));
instance = instance.trim();
}
instanceList.add(instance);
}
if (instance.contains(comment)) {
// 192.168.71.52:8848 # Instance A
instance = instance.substring(0, instance.indexOf(comment));
instance = instance.trim();
return instanceList;
} finally {
if (reader != null) {
reader.close();
}
instanceList.add(instance);
}
return instanceList;
}
public static void writeClusterConf(String content) throws IOException {

View File

@ -26,7 +26,7 @@ management.metrics.export.influx.enabled=false
#management.metrics.export.influx.compressed=true
server.tomcat.accesslog.enabled=true
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D
server.tomcat.accesslog.pattern=%h %l %u %t "%r" %s %b %D %{User-Agent}i
# default current work dir
server.tomcat.basedir=

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>

View File

@ -18,7 +18,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>
@ -150,10 +150,6 @@
<artifactId>jul-to-slf4j</artifactId>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-cmdb</artifactId>

View File

@ -17,7 +17,6 @@ package com.alibaba.nacos.naming.boot;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.context.WebServerInitializedEvent;
@ -39,7 +38,6 @@ public class RunningConfig implements ApplicationListener<WebServerInitializedEv
@Autowired
private ServletContext servletContext;
@SuppressFBWarnings("ST_WRITE_TO_STATIC_FROM_INSTANCE_METHOD")
@Override
public void onApplicationEvent(WebServerInitializedEvent event) {

View File

@ -100,7 +100,9 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
public volatile Notifier notifier = new Notifier();
private volatile Map<String, CopyOnWriteArrayList<RecordListener>> listeners = new ConcurrentHashMap<>();
private Map<String, CopyOnWriteArrayList<RecordListener>> listeners = new ConcurrentHashMap<>();
private Map<String, String> syncChecksumTasks = new ConcurrentHashMap<>(16);
@PostConstruct
public void init() throws Exception {
@ -190,48 +192,62 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
public void onReceiveChecksums(Map<String, String> checksumMap, String server) {
List<String> toUpdateKeys = new ArrayList<>();
List<String> toRemoveKeys = new ArrayList<>();
for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) {
// this key should not be sent from remote server:
Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server);
// abort the procedure:
return;
}
if (!dataStore.contains(entry.getKey()) ||
dataStore.get(entry.getKey()).value == null ||
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
toUpdateKeys.add(entry.getKey());
}
}
for (String key : dataStore.keys()) {
if (!server.equals(distroMapper.mapSrv(KeyBuilder.getServiceName(key)))) {
continue;
}
if (!checksumMap.containsKey(key)) {
toRemoveKeys.add(key);
}
}
Loggers.EPHEMERAL.info("to remove keys: {}, to update keys: {}, source: {}", toRemoveKeys, toUpdateKeys, server);
for (String key : toRemoveKeys) {
onRemove(key);
}
if (toUpdateKeys.isEmpty()) {
if (syncChecksumTasks.containsKey(server)) {
// Already in process of this server:
Loggers.EPHEMERAL.warn("sync checksum task already in process with {}", server);
return;
}
syncChecksumTasks.put(server, "1");
try {
byte[] result = NamingProxy.getData(toUpdateKeys, server);
processData(result);
} catch (Exception e) {
Loggers.EPHEMERAL.error("get data from " + server + " failed!", e);
List<String> toUpdateKeys = new ArrayList<>();
List<String> toRemoveKeys = new ArrayList<>();
for (Map.Entry<String, String> entry : checksumMap.entrySet()) {
if (distroMapper.responsible(KeyBuilder.getServiceName(entry.getKey()))) {
// this key should not be sent from remote server:
Loggers.EPHEMERAL.error("receive responsible key timestamp of " + entry.getKey() + " from " + server);
// abort the procedure:
return;
}
if (!dataStore.contains(entry.getKey()) ||
dataStore.get(entry.getKey()).value == null ||
!dataStore.get(entry.getKey()).value.getChecksum().equals(entry.getValue())) {
toUpdateKeys.add(entry.getKey());
}
}
for (String key : dataStore.keys()) {
if (!server.equals(distroMapper.mapSrv(KeyBuilder.getServiceName(key)))) {
continue;
}
if (!checksumMap.containsKey(key)) {
toRemoveKeys.add(key);
}
}
Loggers.EPHEMERAL.info("to remove keys: {}, to update keys: {}, source: {}", toRemoveKeys, toUpdateKeys, server);
for (String key : toRemoveKeys) {
onRemove(key);
}
if (toUpdateKeys.isEmpty()) {
return;
}
try {
byte[] result = NamingProxy.getData(toUpdateKeys, server);
processData(result);
} catch (Exception e) {
Loggers.EPHEMERAL.error("get data from " + server + " failed!", e);
}
} finally {
// Remove this 'in process' flag:
syncChecksumTasks.remove(server);
}
}
@ -259,8 +275,9 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
if (!listeners.containsKey(entry.getKey())) {
// pretty sure the service not exist:
if (ServerMode.AP.name().equals(switchDomain.getServerMode())) {
if (switchDomain.isDefaultInstanceEphemeral()) {
// create empty service
Loggers.EPHEMERAL.info("creating service {}", entry.getKey());
Service service = new Service();
String serviceName = KeyBuilder.getServiceName(entry.getKey());
String namespaceId = KeyBuilder.getNamespace(entry.getKey());
@ -277,19 +294,24 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
}
for (Map.Entry<String, Datum<Instances>> entry : datumMap.entrySet()) {
dataStore.put(entry.getKey(), entry.getValue());
if (!listeners.containsKey(entry.getKey())) {
Loggers.EPHEMERAL.warn("listener not found: {}", entry.getKey());
// Should not happen:
Loggers.EPHEMERAL.warn("listener of {} not found.", entry.getKey());
continue;
}
for (RecordListener listener : listeners.get(entry.getKey())) {
try {
try {
for (RecordListener listener : listeners.get(entry.getKey())) {
listener.onChange(entry.getKey(), entry.getValue().value);
} catch (Exception e) {
Loggers.EPHEMERAL.error("notify " + listener + ", key: " + entry.getKey() + " failed.", e);
}
} catch (Exception e) {
Loggers.EPHEMERAL.error("[NACOS-DISTRO] error while execute listener of key: {}", entry.getKey(), e);
continue;
}
// Update data store if listener executed successfully:
dataStore.put(entry.getKey(), entry.getValue());
}
}
}
@ -384,12 +406,13 @@ public class DistroConsistencyServiceImpl implements EphemeralConsistencyService
continue;
}
} catch (Throwable e) {
Loggers.EPHEMERAL.error("[NACOS-DISTRO] error while notifying listener of key: {} {}", datumKey, e);
Loggers.EPHEMERAL.error("[NACOS-DISTRO] error while notifying listener of key: {}", datumKey, e);
}
}
if (Loggers.EPHEMERAL.isDebugEnabled()) {
Loggers.EPHEMERAL.debug("[NACOS-DISTRO] datum change notified, key: {}, listener count: {}", datumKey, count);
Loggers.EPHEMERAL.debug("[NACOS-DISTRO] datum change notified, key: {}, listener count: {}, action: {}",
datumKey, count, action.name());
}
} catch (Throwable e) {
Loggers.EPHEMERAL.error("[NACOS-DISTRO] Error while handling notifying task", e);

View File

@ -17,10 +17,7 @@ package com.alibaba.nacos.naming.consistency.ephemeral.distro;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.naming.cluster.servers.Server;
import com.alibaba.nacos.naming.misc.GlobalConfig;
import com.alibaba.nacos.naming.misc.GlobalExecutor;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.NetUtils;
import com.alibaba.nacos.naming.misc.*;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
@ -58,12 +55,8 @@ public class TaskDispatcher {
}
}
public int mapTask(String key) {
return Math.abs(key.hashCode()) % partitionConfig.getTaskDispatchThreadCount();
}
public void addTask(String key) {
taskSchedulerList.get(mapTask(key)).addTask(key);
taskSchedulerList.get(UtilsAndCommons.shakeUp(key, partitionConfig.getTaskDispatchThreadCount())).addTask(key);
}
public class TaskScheduler implements Runnable {
@ -107,14 +100,16 @@ public class TaskDispatcher {
continue;
}
if (StringUtils.isBlank(key)) {
continue;
}
if (dataSize == 0) {
keys = new ArrayList<>();
}
if (StringUtils.isNotBlank(key)) {
keys.add(key);
dataSize++;
}
keys.add(key);
dataSize++;
if (dataSize == partitionConfig.getBatchSyncKeyCount() ||
(System.currentTimeMillis() - lastDispatchTime) > partitionConfig.getTaskDispatchPeriod()) {

View File

@ -18,6 +18,7 @@ package com.alibaba.nacos.naming.consistency.persistent.raft;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.cluster.ServerStatus;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.persistent.PersistentConsistencyService;
import com.alibaba.nacos.naming.misc.Loggers;
@ -38,6 +39,9 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
@Autowired
private RaftCore raftCore;
@Autowired
private RaftPeerSet peers;
@Autowired
private SwitchDomain switchDomain;
@ -54,6 +58,12 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
@Override
public void remove(String key) throws NacosException {
try {
if (KeyBuilder.matchInstanceListKey(key) && !raftCore.isLeader()) {
Datum datum = new Datum();
datum.key = key;
raftCore.onDelete(datum.key, peers.getLeader());
return;
}
raftCore.signalDelete(key);
} catch (Exception e) {
Loggers.RAFT.error("Raft remove failed.", e);
@ -92,7 +102,7 @@ public class RaftConsistencyServiceImpl implements PersistentConsistencyService
public void onRemove(Datum datum, RaftPeer source) throws NacosException {
try {
raftCore.onDelete(datum, source);
raftCore.onDelete(datum.key, source);
} catch (Exception e) {
Loggers.RAFT.error("Raft onRemove failed.", e);
throw new NacosException(NacosException.SERVER_ERROR, "Raft onRemove failed, datum:" + datum + ", source: " + source);

View File

@ -213,7 +213,7 @@ public class RaftCore {
if (!latch.await(UtilsAndCommons.RAFT_PUBLISH_TIMEOUT, TimeUnit.MILLISECONDS)) {
// only majority servers return success can we consider this update success
Loggers.RAFT.info("data publish failed, caused failed to notify majority, key={}", key);
Loggers.RAFT.error("data publish failed, caused failed to notify majority, key={}", key);
throw new IllegalStateException("data publish failed, caused failed to notify majority, key=" + key);
}
@ -243,7 +243,7 @@ public class RaftCore {
json.put("datum", datum);
json.put("source", peers.local());
onDelete(datum, peers.local());
onDelete(datum.key, peers.local());
for (final String server : peers.allServersWithoutMySelf()) {
String url = buildURL(server, API_ON_DEL);
@ -317,7 +317,7 @@ public class RaftCore {
Loggers.RAFT.info("data added/updated, key={}, term={}", datum.key, local.term);
}
public void onDelete(Datum datum, RaftPeer source) throws Exception {
public void onDelete(String datumKey, RaftPeer source) throws Exception {
RaftPeer local = peers.local();
@ -337,7 +337,7 @@ public class RaftCore {
local.resetLeaderDue();
// do apply
String key = datum.key;
String key = datumKey;
deleteDatum(key);
if (KeyBuilder.matchServiceMetaKey(key)) {
@ -353,7 +353,7 @@ public class RaftCore {
raftStore.updateTerm(local.term.get());
}
Loggers.RAFT.info("data removed, key={}, term={}", datum.key, local.term);
Loggers.RAFT.info("data removed, key={}, term={}", datumKey, local.term);
}
@ -667,41 +667,49 @@ public class RaftCore {
return 1;
}
List<Datum> datumList = JSON.parseObject(response.getResponseBody(), new TypeReference<List<Datum>>() {
List<JSONObject> datumList = JSON.parseObject(response.getResponseBody(), new TypeReference<List<JSONObject>>() {
});
for (Datum datum : datumList) {
for (JSONObject datumJson : datumList) {
OPERATE_LOCK.lock();
Datum newDatum = null;
try {
Datum oldDatum = getDatum(datum.key);
Datum oldDatum = getDatum(datumJson.getString("key"));
if (oldDatum != null && datum.timestamp.get() <= oldDatum.timestamp.get()) {
if (oldDatum != null && datumJson.getLongValue("timestamp") <= oldDatum.timestamp.get()) {
Loggers.RAFT.info("[NACOS-RAFT] timestamp is smaller than that of mine, key: {}, remote: {}, local: {}",
datum.key, datum.timestamp, oldDatum.timestamp);
datumJson.getString("key"), datumJson.getLongValue("timestamp"), oldDatum.timestamp);
continue;
}
raftStore.write(datum);
if (KeyBuilder.matchServiceMetaKey(datum.key)) {
if (KeyBuilder.matchServiceMetaKey(datumJson.getString("key"))) {
Datum<Service> serviceDatum = new Datum<>();
serviceDatum.key = datum.key;
serviceDatum.timestamp.set(datum.timestamp.get());
serviceDatum.value = JSON.parseObject(JSON.toJSONString(datum.value), Service.class);
datum = serviceDatum;
serviceDatum.key = datumJson.getString("key");
serviceDatum.timestamp.set(datumJson.getLongValue("timestamp"));
serviceDatum.value =
JSON.parseObject(JSON.toJSONString(datumJson.getJSONObject("value")), Service.class);
newDatum = serviceDatum;
}
if (KeyBuilder.matchInstanceListKey(datum.key)) {
if (KeyBuilder.matchInstanceListKey(datumJson.getString("key"))) {
Datum<Instances> instancesDatum = new Datum<>();
instancesDatum.key = datum.key;
instancesDatum.timestamp.set(datum.timestamp.get());
instancesDatum.value = JSON.parseObject(JSON.toJSONString(datum.value), Instances.class);
datum = instancesDatum;
instancesDatum.key = datumJson.getString("key");
instancesDatum.timestamp.set(datumJson.getLongValue("timestamp"));
instancesDatum.value =
JSON.parseObject(JSON.toJSONString(datumJson.getJSONObject("value")), Instances.class);
newDatum = instancesDatum;
}
datums.put(datum.key, datum);
notifier.addTask(datum.key, ApplyAction.CHANGE);
if (newDatum == null || newDatum.value == null) {
Loggers.RAFT.error("receive null datum: {}", datumJson);
continue;
}
raftStore.write(newDatum);
datums.put(newDatum.key, newDatum);
notifier.addTask(newDatum.key, ApplyAction.CHANGE);
local.resetLeaderDue();
@ -715,10 +723,10 @@ public class RaftCore {
raftStore.updateTerm(local.term.get());
Loggers.RAFT.info("data updated, key: {}, timestamp: {}, from {}, local term: {}",
datum.key, datum.timestamp, JSON.toJSONString(remote), local.term);
newDatum.key, newDatum.timestamp, JSON.toJSONString(remote), local.term);
} catch (Throwable e) {
Loggers.RAFT.error("[RAFT-BEAT] failed to sync datum from leader, key: {} {}", datum.key, e);
Loggers.RAFT.error("[RAFT-BEAT] failed to sync datum from leader, datum: {}", newDatum, e);
} finally {
OPERATE_LOCK.unlock();
}
@ -863,7 +871,6 @@ public class RaftCore {
}
private void deleteDatum(String key) {
Datum deleted;
try {
deleted = datums.remove(URLDecoder.decode(key, "UTF-8"));
@ -881,6 +888,10 @@ public class RaftCore {
return initialized || !globalConfig.isDataWarmup();
}
public int getNotifyTaskCount() {
return notifier.getTaskSize();
}
public class Notifier implements Runnable {
private ConcurrentHashMap<String, String> services = new ConcurrentHashMap<>(10 * 1024);
@ -895,6 +906,9 @@ public class RaftCore {
if (action == ApplyAction.CHANGE) {
services.put(datumKey, StringUtils.EMPTY);
}
Loggers.RAFT.info("add task {}", datumKey);
tasks.add(Pair.with(datumKey, action));
}
@ -920,6 +934,8 @@ public class RaftCore {
services.remove(datumKey);
Loggers.RAFT.info("remove task {}", datumKey);
int count = 0;
if (listeners.containsKey(KeyBuilder.SERVICE_META_KEY_PREFIX)) {
@ -936,7 +952,7 @@ public class RaftCore {
listener.onDelete(datumKey);
}
} catch (Throwable e) {
Loggers.RAFT.error("[NACOS-RAFT] error while notifying listener of key: {} {}", datumKey, e);
Loggers.RAFT.error("[NACOS-RAFT] error while notifying listener of key: {}", datumKey, e);
}
}
}
@ -961,7 +977,7 @@ public class RaftCore {
continue;
}
} catch (Throwable e) {
Loggers.RAFT.error("[NACOS-RAFT] error while notifying listener of key: {} {}", datumKey, e);
Loggers.RAFT.error("[NACOS-RAFT] error while notifying listener of key: {}", datumKey, e);
}
}

View File

@ -24,6 +24,7 @@ import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.push.ClientInfo;
import com.alibaba.nacos.naming.web.CanDistro;
import com.alibaba.nacos.naming.web.OverrideParameterRequestWrapper;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
@ -138,6 +139,7 @@ public class ApiController extends InstanceController {
agent, clusters, clientIP, udpPort, env, isCheck, app, tenant, healthyOnly);
}
@CanDistro
@RequestMapping("/clientBeat")
public JSONObject clientBeat(HttpServletRequest request) throws Exception {
OverrideParameterRequestWrapper requestWrapper = OverrideParameterRequestWrapper.buildRequest(request);

View File

@ -96,9 +96,8 @@ public class ClusterController {
cluster.setHealthChecker(abstractHealthChecker);
cluster.setMetadata(UtilsAndCommons.parseMetadata(metadata));
cluster.init();
service.getClusterMap().put(clusterName, cluster);
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
service.validate();

View File

@ -15,6 +15,7 @@
*/
package com.alibaba.nacos.naming.controllers;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerMode;
@ -84,8 +85,8 @@ public class DistroController {
String namespaceId = KeyBuilder.getNamespace(entry.getKey());
String serviceName = KeyBuilder.getServiceName(entry.getKey());
if (!serviceManager.containService(namespaceId, serviceName)
&& ServerMode.AP.name().equals(switchDomain.getServerMode())) {
serviceManager.createEmptyService(namespaceId, serviceName);
&& switchDomain.isDefaultInstanceEphemeral()) {
serviceManager.createEmptyService(namespaceId, serviceName, true);
}
consistencyService.onPut(entry.getKey(), entry.getValue().value);
}
@ -106,7 +107,9 @@ public class DistroController {
@RequestMapping(value = "/datum", method = RequestMethod.GET)
public void get(HttpServletRequest request, HttpServletResponse response) throws Exception {
String keys = WebUtils.required(request, "keys");
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
String keys = JSON.parseObject(entity).getString("keys");
String keySplitter = ",";
Map<String, Datum> datumMap = new HashMap<>(64);
for (String key : keys.split(keySplitter)) {

View File

@ -25,20 +25,18 @@ import com.alibaba.nacos.naming.core.Instance;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
import com.alibaba.nacos.naming.healthcheck.HealthCheckType;
import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.push.PushService;
import com.alibaba.nacos.naming.web.CanDistro;
import com.google.common.collect.Lists;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.net.HttpURLConnection;
import java.util.HashMap;
import java.util.Map;
/**
* Health status related operation controller
@ -68,57 +66,48 @@ public class HealthController {
return result;
}
@CanDistro
@RequestMapping(value = {"", "/instance"}, method = RequestMethod.PUT)
public String update(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
String ip = WebUtils.required(request, "ip");
int port = Integer.parseInt(WebUtils.required(request, "port"));
boolean valid = Boolean.valueOf(WebUtils.required(request, "valid"));
String clusterName = WebUtils.optional(request, CommonParams.CLUSTER_NAME
, UtilsAndCommons.DEFAULT_CLUSTER_NAME);
if (!distroMapper.responsible(serviceName)) {
String server = distroMapper.mapSrv(serviceName);
Loggers.EVT_LOG.info("I'm not responsible for " + serviceName + ", proxy it to " + server);
Map<String, String> proxyParams = new HashMap<>(16);
for (Map.Entry<String, String[]> entry : request.getParameterMap().entrySet()) {
String key = entry.getKey();
String value = entry.getValue()[0];
proxyParams.put(key, value);
}
String ip = WebUtils.required(request, "ip");
int port = Integer.parseInt(WebUtils.required(request, "port"));
if (!server.contains(UtilsAndCommons.IP_PORT_SPLITER)) {
server = server + UtilsAndCommons.IP_PORT_SPLITER + RunningConfig.getServerPort();
}
boolean valid = false;
String url = "http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + "/health";
HttpClient.HttpResult httpResult = HttpClient.httpPost(url, null, proxyParams);
String healthyString = WebUtils.optional(request, "healthy", StringUtils.EMPTY);
if (StringUtils.isBlank(healthyString)) {
healthyString = WebUtils.optional(request, "valid", StringUtils.EMPTY);
}
if (httpResult.code != HttpURLConnection.HTTP_OK) {
throw new IllegalArgumentException("failed to proxy health update to " + server + ", service: " + serviceName);
if (StringUtils.isBlank(healthyString)) {
throw new IllegalArgumentException("Param 'healthy' is required.");
}
Service service = serviceManager.getService(namespaceId, serviceName);
// Only health check "none" need update health status with api
if (HealthCheckType.NONE.name().equals(service.getClusterMap().get(clusterName).getHealthChecker().getType())) {
for (Instance instance : service.allIPs(Lists.newArrayList(clusterName))) {
if (instance.getIp().equals(ip) && instance.getPort() == port) {
instance.setHealthy(valid);
Loggers.EVT_LOG.info((valid ? "[IP-ENABLED]" : "[IP-DISABLED]") + " ips: "
+ instance.getIp() + ":" + instance.getPort() + "@" + instance.getClusterName()
+ ", service: " + serviceName + ", msg: update thought HealthController api");
pushService.serviceChanged(namespaceId, service.getName());
break;
}
}
} else {
Service service = serviceManager.getService(namespaceId, serviceName);
// Only health check "none" need update health status with api
if (HealthCheckType.NONE.name().equals(service.getClusterMap().get(clusterName).getHealthChecker().getType())) {
for (Instance instance : service.allIPs(Lists.newArrayList(clusterName))) {
if (instance.getIp().equals(ip) && instance.getPort() == port) {
instance.setHealthy(valid);
Loggers.EVT_LOG.info((valid ? "[IP-ENABLED]" : "[IP-DISABLED]") + " ips: "
+ instance.getIp() + ":" + instance.getPort() + "@" + instance.getClusterName()
+ ", service: " + serviceName + ", msg: update thought HealthController api");
pushService.serviceChanged(namespaceId, service.getName());
break;
}
}
} else {
throw new IllegalArgumentException("health check mode 'client' and 'server' are not supported, service: " + serviceName);
}
throw new IllegalArgumentException("health check is still working, service: " + serviceName);
}
return "ok";
}
}

View File

@ -112,6 +112,7 @@ public class InstanceController {
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
Loggers.SRV_LOG.warn("remove instance from non-exist service: {}", serviceName);
return "ok";
}
@ -187,7 +188,8 @@ public class InstanceController {
List<Instance> ips = service.allIPs(clusters);
if (ips == null || ips.isEmpty()) {
throw new IllegalStateException("no ips found for cluster " + cluster + " in service " + serviceName);
throw new NacosException(NacosException.NOT_FOUND,
"no ips found for cluster " + cluster + " in service " + serviceName);
}
for (Instance instance : ips) {
@ -205,7 +207,7 @@ public class InstanceController {
}
}
throw new IllegalStateException("no matched ip found!");
throw new NacosException(NacosException.NOT_FOUND, "no matched ip found!");
}
@CanDistro
@ -216,15 +218,15 @@ public class InstanceController {
result.put("clientBeatInterval", switchDomain.getClientBeatInterval());
// ignore client beat in CP mode:
if (ServerMode.CP.name().equals(switchDomain.getServerMode())) {
return result;
}
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
String beat = WebUtils.required(request, "beat");
RsInfo clientBeat = JSON.parseObject(beat, RsInfo.class);
if (!switchDomain.isDefaultInstanceEphemeral() && !clientBeat.isEphemeral()) {
return result;
}
if (StringUtils.isBlank(clientBeat.getCluster())) {
clientBeat.setCluster(UtilsAndCommons.DEFAULT_CLUSTER_NAME);
}
@ -323,16 +325,6 @@ public class InstanceController {
throw new NacosException(NacosException.INVALID_PARAM, "instance format invalid:" + instance);
}
if ((ServerMode.AP.name().equals(switchDomain.getServerMode()) && !instance.isEphemeral())) {
throw new NacosException(NacosException.INVALID_PARAM, "wrong instance type: " + instance.isEphemeral()
+ " in " + switchDomain.getServerMode() + " mode.");
}
if ((ServerMode.CP.name().equals(switchDomain.getServerMode()) && instance.isEphemeral())) {
throw new NacosException(NacosException.INVALID_PARAM, "wrong instance type: " + instance.isEphemeral()
+ " in " + switchDomain.getServerMode() + " mode.");
}
return instance;
}
@ -341,12 +333,14 @@ public class InstanceController {
String ip = WebUtils.required(request, "ip");
String port = WebUtils.required(request, "port");
String weight = WebUtils.optional(request, "weight", "1");
String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, UtilsAndCommons.DEFAULT_CLUSTER_NAME);
String cluster = WebUtils.optional(request, CommonParams.CLUSTER_NAME, StringUtils.EMPTY);
if (StringUtils.isBlank(cluster)) {
cluster = WebUtils.optional(request, "cluster", UtilsAndCommons.DEFAULT_CLUSTER_NAME);
}
boolean healthy = BooleanUtils.toBoolean(WebUtils.optional(request, "healthy", "true"));
boolean enabled = BooleanUtils.toBoolean(WebUtils.optional(request, "enable", "true"));
// If server running in CP mode, we set this flag to false:
boolean ephemeral = BooleanUtils.toBoolean(WebUtils.optional(request, "ephemeral",
String.valueOf(!ServerMode.CP.name().equals(switchDomain.getServerMode()))));
String.valueOf(switchDomain.isDefaultInstanceEphemeral())));
Instance instance = new Instance();
instance.setPort(Integer.parseInt(port));

View File

@ -23,6 +23,7 @@ import com.alibaba.nacos.core.utils.SystemUtils;
import com.alibaba.nacos.core.utils.WebUtils;
import com.alibaba.nacos.naming.cluster.ServerListManager;
import com.alibaba.nacos.naming.cluster.ServerStatusManager;
import com.alibaba.nacos.naming.consistency.persistent.raft.RaftCore;
import com.alibaba.nacos.naming.core.DistroMapper;
import com.alibaba.nacos.naming.core.Service;
import com.alibaba.nacos.naming.core.ServiceManager;
@ -73,6 +74,9 @@ public class OperatorController {
@Autowired
private DistroMapper distroMapper;
@Autowired
private RaftCore raftCore;
@RequestMapping("/push/state")
public JSONObject pushState(HttpServletRequest request) {
@ -144,6 +148,7 @@ public class OperatorController {
result.put("status", serverStatusManager.getServerStatus().name());
result.put("serviceCount", serviceCount);
result.put("instanceCount", ipCount);
result.put("raftNotifyTaskCount", raftCore.getNotifyTaskCount());
result.put("responsibleServiceCount", responsibleDomCount);
result.put("responsibleInstanceCount", responsibleIPCount);
result.put("cpu", SystemUtils.getCPU());

View File

@ -50,7 +50,7 @@ import java.util.List;
import java.util.Map;
/**
* HTTP interfaces for Raft consistency protocol. These interfaces should only be invoked by Nacos server itself.
* Methods for Raft consistency protocol. These methods should only be invoked by Nacos server itself.
*
* @author nkorange
* @since 1.0.0
@ -83,7 +83,6 @@ public class RaftController {
public JSONObject beat(HttpServletRequest request, HttpServletResponse response) throws Exception {
String entity = new String(IoUtils.tryDecompress(request.getInputStream()), "UTF-8");
// String value = Arrays.asList(entity).toArray(new String[1])[0];
String value = URLDecoder.decode(entity, "UTF-8");
value = URLDecoder.decode(value, "UTF-8");
@ -240,7 +239,6 @@ public class RaftController {
response.setHeader("Content-Encode", "gzip");
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
// String value = Arrays.asList(entity).toArray(new String[1])[0];
String value = URLDecoder.decode(entity, "UTF-8");
value = URLDecoder.decode(value, "UTF-8");
JSONObject jsonObject = JSON.parseObject(value);

View File

@ -31,6 +31,7 @@ import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import com.alibaba.nacos.naming.selector.LabelSelector;
import com.alibaba.nacos.naming.selector.NoneSelector;
import com.alibaba.nacos.naming.selector.Selector;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -39,6 +40,7 @@ import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.*;
@ -105,15 +107,6 @@ public class ServiceController {
Constants.DEFAULT_NAMESPACE_ID);
String serviceName = WebUtils.required(request, CommonParams.SERVICE_NAME);
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
throw new IllegalArgumentException("specified service not exist, serviceName : " + serviceName);
}
if (!service.allIPs().isEmpty()) {
throw new IllegalArgumentException("specified service has instances, serviceName : " + serviceName);
}
serviceManager.easyRemoveService(namespaceId, serviceName);
return "ok";
@ -299,13 +292,19 @@ public class ServiceController {
}
@RequestMapping(value = "/status", method = RequestMethod.POST)
public String serviceStatus(HttpServletRequest request) {
public String serviceStatus(HttpServletRequest request) throws Exception {
String entity = IOUtils.toString(request.getInputStream(), "UTF-8");
String value = URLDecoder.decode(entity, "UTF-8");
JSONObject json = JSON.parseObject(value);
//format: service1@@checksum@@@service2@@checksum
String statuses = WebUtils.required(request, "statuses");
String serverIP = WebUtils.optional(request, "clientIP", "");
String statuses = json.getString("statuses");
String serverIP = json.getString("clientIP");
if (!serverListManager.contains(serverIP)) {
throw new IllegalArgumentException("ip: " + serverIP + " is not in serverlist");
throw new NacosException(NacosException.INVALID_PARAM,
"ip: " + serverIP + " is not in serverlist");
}
try {
@ -345,7 +344,7 @@ public class ServiceController {
}
@RequestMapping(value = "/checksum", method = RequestMethod.PUT)
public JSONObject checksum(HttpServletRequest request) {
public JSONObject checksum(HttpServletRequest request) throws Exception {
String namespaceId = WebUtils.optional(request, CommonParams.NAMESPACE_ID,
Constants.DEFAULT_NAMESPACE_ID);
@ -353,7 +352,8 @@ public class ServiceController {
Service service = serviceManager.getService(namespaceId, serviceName);
if (service == null) {
throw new IllegalArgumentException("serviceName not found: " + serviceName);
throw new NacosException(NacosException.NOT_FOUND,
"serviceName not found: " + serviceName);
}
service.recalculateChecksum();

View File

@ -54,6 +54,9 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
@JSONField(serialize = false)
private Service service;
@JSONField(serialize = false)
private volatile boolean inited = false;
private Map<String, String> metadata = new ConcurrentHashMap<>();
public Cluster() {
@ -87,8 +90,12 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
}
public void init() {
if (inited) {
return;
}
checkTask = new HealthCheckTask(this);
HealthCheckReactor.scheduleCheck(checkTask);
inited = true;
}
public void destroy() {
@ -105,6 +112,7 @@ public class Cluster extends com.alibaba.nacos.api.naming.pojo.Cluster implement
public void setService(Service service) {
this.service = service;
this.setServiceName(service.getName());
}
@Override

View File

@ -181,7 +181,7 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
}
if (ip == null) {
throw new IllegalArgumentException("malfomed ip config: " + json);
throw new IllegalArgumentException("malformed ip config: " + json);
}
if (ip.getWeight() > MAX_WEIGHT_VALUE) {
@ -195,7 +195,7 @@ public class Instance extends com.alibaba.nacos.api.naming.pojo.Instance impleme
}
if (!ip.validate()) {
throw new IllegalArgumentException("malfomed ip config: " + json);
throw new IllegalArgumentException("malformed ip config: " + json);
}
return ip;

View File

@ -25,6 +25,7 @@ import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@ -40,7 +41,7 @@ public class Instances implements Record {
private long lastCalculateTime = 0L;
private List<Instance> instanceList;
private List<Instance> instanceList = new ArrayList<>();
public List<Instance> getInstanceList() {
return instanceList;

View File

@ -18,8 +18,8 @@ package com.alibaba.nacos.naming.core;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.annotation.JSONField;
import com.alibaba.nacos.naming.boot.SpringContext;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
import com.alibaba.nacos.naming.consistency.RecordListener;
import com.alibaba.nacos.naming.healthcheck.ClientBeatCheckTask;
import com.alibaba.nacos.naming.healthcheck.ClientBeatProcessor;
import com.alibaba.nacos.naming.healthcheck.HealthCheckReactor;
@ -44,9 +44,9 @@ import java.util.*;
* Service of Nacos server side
* <p>
* We introduce a 'service --> cluster --> instance' model, in which service stores a list of clusters,
* which contains a list of instances.
* which contain a list of instances.
* <p>
* This class inherits from Service in API module and stores some fields that do not expose to client.
* This class inherits from Service in API module and stores some fields that do not have to expose to client.
*
* @author nkorange
*/
@ -154,14 +154,19 @@ public class Service extends com.alibaba.nacos.api.naming.pojo.Service implement
Loggers.SRV_LOG.info("[NACOS-RAFT] datum is changed, key: {}, value: {}", key, value);
for (Instance ip : value.getInstanceList()) {
for (Instance instance : value.getInstanceList()) {
if (ip.getWeight() > 10000.0D) {
ip.setWeight(10000.0D);
if (instance == null) {
// Reject this abnormal instance list:
throw new RuntimeException("got null instance " + key);
}
if (ip.getWeight() < 0.01D && ip.getWeight() > 0.0D) {
ip.setWeight(0.01D);
if (instance.getWeight() > 10000.0D) {
instance.setWeight(10000.0D);
}
if (instance.getWeight() < 0.01D && instance.getWeight() > 0.0D) {
instance.setWeight(0.01D);
}
}

View File

@ -21,7 +21,6 @@ import com.alibaba.fastjson.JSONObject;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.cluster.ServerListManager;
import com.alibaba.nacos.naming.cluster.ServerMode;
import com.alibaba.nacos.naming.cluster.servers.Server;
import com.alibaba.nacos.naming.consistency.ConsistencyService;
import com.alibaba.nacos.naming.consistency.Datum;
@ -79,6 +78,8 @@ public class ServiceManager implements RecordListener<Service> {
@Autowired
private PushService pushService;
private final Object putServiceLock = new Object();
@PostConstruct
public void init() {
@ -155,9 +156,15 @@ public class ServiceManager implements RecordListener<Service> {
public void onDelete(String key) throws Exception {
String namespace = KeyBuilder.getNamespace(key);
String name = KeyBuilder.getServiceName(key);
Service service = chooseServiceMap(namespace).remove(name);
Service service = chooseServiceMap(namespace).get(name);
Loggers.RAFT.info("[RAFT-NOTIFIER] datum is deleted, key: {}", key);
// check again:
if (service != null && !service.allIPs().isEmpty()) {
Loggers.SRV_LOG.warn("service not empty, key: {}", key);
return;
}
if (service != null) {
service.destroy();
consistencyService.remove(KeyBuilder.buildInstanceListKey(namespace, name, true));
@ -165,6 +172,8 @@ public class ServiceManager implements RecordListener<Service> {
consistencyService.unlisten(KeyBuilder.buildServiceMetaKey(namespace, name), service);
Loggers.SRV_LOG.info("[DEAD-SERVICE] {}", service.toJSON());
}
chooseServiceMap(namespace).remove(name);
}
private class UpdatedServiceProcessor implements Runnable {
@ -321,16 +330,28 @@ public class ServiceManager implements RecordListener<Service> {
}
public void easyRemoveService(String namespaceId, String serviceName) throws Exception {
Service service = getService(namespaceId, serviceName);
if (service == null) {
throw new IllegalArgumentException("specified service not exist, serviceName : " + serviceName);
}
if (!service.allIPs().isEmpty()) {
throw new IllegalArgumentException("specified service has instances, serviceName : " + serviceName);
}
consistencyService.remove(KeyBuilder.buildServiceMetaKey(namespaceId, serviceName));
}
public void addOrReplaceService(Service service) throws Exception {
public void addOrReplaceService(Service service) throws NacosException {
consistencyService.put(KeyBuilder.buildServiceMetaKey(service.getNamespaceId(), service.getName()), service);
}
public void createEmptyService(String namespaceId, String serviceName) throws NacosException {
public void createEmptyService(String namespaceId, String serviceName, boolean local) throws NacosException {
Service service = getService(namespaceId, serviceName);
if (service == null) {
Loggers.SRV_LOG.info("creating empty service {}:{}", namespaceId, serviceName);
service = new Service();
service.setName(serviceName);
service.setNamespaceId(namespaceId);
@ -339,10 +360,14 @@ public class ServiceManager implements RecordListener<Service> {
service.setLastModifiedMillis(System.currentTimeMillis());
service.recalculateChecksum();
service.validate();
putService(service);
service.init();
consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service);
consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service);
if (local) {
putService(service);
service.init();
consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), true), service);
consistencyService.listen(KeyBuilder.buildInstanceListKey(service.getNamespaceId(), service.getName(), false), service);
} else {
addOrReplaceService(service);
}
}
}
@ -358,9 +383,7 @@ public class ServiceManager implements RecordListener<Service> {
*/
public void registerInstance(String namespaceId, String serviceName, Instance instance) throws NacosException {
if (ServerMode.AP.name().equals(switchDomain.getServerMode())) {
createEmptyService(namespaceId, serviceName);
}
createEmptyService(namespaceId, serviceName, instance.isEphemeral());
Service service = getService(namespaceId, serviceName);
@ -466,6 +489,7 @@ public class ServiceManager implements RecordListener<Service> {
if (!service.getClusterMap().containsKey(instance.getClusterName())) {
Cluster cluster = new Cluster(instance.getClusterName());
cluster.setService(service);
cluster.init();
service.getClusterMap().put(instance.getClusterName(), cluster);
Loggers.SRV_LOG.warn("cluster: {} not found, ip: {}, will create new cluster with default configuration.",
instance.getClusterName(), instance.toJSON());
@ -522,7 +546,11 @@ public class ServiceManager implements RecordListener<Service> {
public void putService(Service service) {
if (!serviceMap.containsKey(service.getNamespaceId())) {
serviceMap.put(service.getNamespaceId(), new ConcurrentHashMap<>(16));
synchronized (putServiceLock) {
if (!serviceMap.containsKey(service.getNamespaceId())) {
serviceMap.put(service.getNamespaceId(), new ConcurrentHashMap<>(16));
}
}
}
serviceMap.get(service.getNamespaceId()).put(service.getName(), service);
}

View File

@ -27,6 +27,8 @@ import org.apache.commons.lang3.StringUtils;
import org.apache.http.*;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpEntityEnclosingRequestBase;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.entity.ContentType;
@ -41,6 +43,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.util.*;
@ -285,6 +288,29 @@ public class HttpClient {
}
}
public static void asyncHttpGetLarge(String url, Map<String, String> headers, byte[] content, AsyncCompletionHandler handler) throws Exception {
AsyncHttpClient.BoundRequestBuilder builder = asyncHttpClient.prepareGet(url);
if (!headers.isEmpty()) {
for (String headerKey : headers.keySet()) {
builder.setHeader(headerKey, headers.get(headerKey));
}
}
builder.setBody(content);
builder.setHeader("Content-Type", "application/json; charset=UTF-8");
builder.setHeader("Accept-Charset", "UTF-8");
builder.setHeader("Accept-Encoding", "gzip");
builder.setHeader("Content-Encoding", "gzip");
if (handler != null) {
builder.execute(handler);
} else {
builder.execute();
}
}
public static HttpResult httpPutLarge(String url, Map<String, String> headers, byte[] content) {
try {
HttpClientBuilder builder = HttpClients.custom();
@ -313,6 +339,35 @@ public class HttpClient {
}
}
public static HttpResult httpGetLarge(String url, Map<String, String> headers, String content) {
try {
HttpClientBuilder builder = HttpClients.custom();
builder.setUserAgent(UtilsAndCommons.SERVER_VERSION);
builder.setConnectionTimeToLive(500, TimeUnit.MILLISECONDS);
CloseableHttpClient httpClient = builder.build();
HttpGetWithEntity httpGetWithEntity = new HttpGetWithEntity();
httpGetWithEntity.setURI(new URI(url));
for (Map.Entry<String, String> entry : headers.entrySet()) {
httpGetWithEntity.setHeader(entry.getKey(), entry.getValue());
}
httpGetWithEntity.setEntity(new StringEntity(content, ContentType.create("application/json", "UTF-8")));
HttpResponse response = httpClient.execute(httpGetWithEntity);
HttpEntity entity = response.getEntity();
HeaderElement[] headerElements = entity.getContentType().getElements();
String charset = headerElements[0].getParameterByName("charset").getValue();
return new HttpResult(response.getStatusLine().getStatusCode(),
IOUtils.toString(entity.getContent(), charset), Collections.<String, String>emptyMap());
} catch (Exception e) {
return new HttpResult(500, e.toString(), Collections.<String, String>emptyMap());
}
}
public static HttpResult httpPostLarge(String url, Map<String, String> headers, String content) {
try {
HttpClientBuilder builder = HttpClients.custom();
@ -442,4 +497,14 @@ public class HttpClient {
return respHeaders.get(name);
}
}
public static class HttpGetWithEntity extends HttpEntityEnclosingRequestBase {
public final static String METHOD_NAME = "GET";
@Override
public String getMethod() {
return METHOD_NAME;
}
}
}

View File

@ -79,8 +79,8 @@ public class NamingProxy {
Map<String, String> params = new HashMap<>(8);
params.put("keys", StringUtils.join(keys, ","));
HttpClient.HttpResult result = HttpClient.httpGet("http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL, new ArrayList<>(), params);
HttpClient.HttpResult result = HttpClient.httpGetLarge("http://" + server + RunningConfig.getContextPath()
+ UtilsAndCommons.NACOS_NAMING_CONTEXT + DATA_GET_URL, new HashMap<>(8), JSON.toJSONString(params));
if (HttpURLConnection.HTTP_OK == result.code) {
return result.content.getBytes();

View File

@ -15,6 +15,7 @@
*/
package com.alibaba.nacos.naming.misc;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.naming.boot.RunningConfig;
import com.ning.http.client.AsyncCompletionHandler;
import com.ning.http.client.Response;
@ -49,7 +50,7 @@ public class ServiceStatusSynchronizer implements Synchronizer {
}
try {
HttpClient.asyncHttpPost(url, null, params, new AsyncCompletionHandler() {
HttpClient.asyncHttpPostLarge(url, null, JSON.toJSONString(params), new AsyncCompletionHandler() {
@Override
public Integer onCompleted(Response response) throws Exception {
if (response.getStatusCode() != HttpURLConnection.HTTP_OK) {

View File

@ -89,7 +89,7 @@ public class SwitchDomain implements Record, Cloneable {
private String overriddenServerStatus = null;
private String serverMode = "AP";
private boolean defaultInstanceEphemeral = true;
public boolean isEnableAuthentication() {
return enableAuthentication;
@ -342,12 +342,12 @@ public class SwitchDomain implements Record, Cloneable {
this.overriddenServerStatus = overriddenServerStatus;
}
public String getServerMode() {
return serverMode;
public boolean isDefaultInstanceEphemeral() {
return defaultInstanceEphemeral;
}
public void setServerMode(String serverMode) {
this.serverMode = serverMode;
public void setDefaultInstanceEphemeral(boolean defaultInstanceEphemeral) {
this.defaultInstanceEphemeral = defaultInstanceEphemeral;
}
@Override

View File

@ -59,5 +59,5 @@ public class SwitchEntry {
public static final String PARAM_JSON = "json";
public static final String OVERRIDDEN_SERVER_STATUS = "overriddenServerStatus";
public static final String SERVER_MODE = "serverMode";
public static final String DEFAULT_INSTANCE_EPHEMERAL = "defaultInstanceEphemeral";
}

View File

@ -18,7 +18,6 @@ package com.alibaba.nacos.naming.misc;
import com.alibaba.fastjson.JSON;
import com.alibaba.nacos.api.common.Constants;
import com.alibaba.nacos.api.exception.NacosException;
import com.alibaba.nacos.naming.cluster.ServerMode;
import com.alibaba.nacos.naming.consistency.ConsistencyService;
import com.alibaba.nacos.naming.consistency.Datum;
import com.alibaba.nacos.naming.consistency.KeyBuilder;
@ -263,9 +262,9 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
switchDomain.setOverriddenServerStatus(status);
}
if (entry.equals(SwitchEntry.SERVER_MODE)) {
String mode = value;
switchDomain.setServerMode(ServerMode.valueOf(mode).name());
if (entry.equals(SwitchEntry.DEFAULT_INSTANCE_EPHEMERAL)) {
String defaultEphemeral = value;
switchDomain.setDefaultInstanceEphemeral(Boolean.parseBoolean(defaultEphemeral));
}
if (debug) {
@ -308,7 +307,7 @@ public class SwitchManager implements RecordListener<SwitchDomain> {
switchDomain.setPushCVersion(newSwitchDomain.getPushCVersion());
switchDomain.setEnableAuthentication(newSwitchDomain.isEnableAuthentication());
switchDomain.setOverriddenServerStatus(newSwitchDomain.getOverriddenServerStatus());
switchDomain.setServerMode(newSwitchDomain.getServerMode());
switchDomain.setDefaultInstanceEphemeral(newSwitchDomain.isDefaultInstanceEphemeral());
}
public SwitchDomain getSwitchDomain() {

View File

@ -237,4 +237,34 @@ public class UtilsAndCommons {
public static String assembleFullServiceName(String namespaceId, String serviceName) {
return namespaceId + UtilsAndCommons.NAMESPACE_SERVICE_CONNECTOR + serviceName;
}
/**
* 根据指定的字符串计算出一个0{@code upperLimit}不含之间的数字本方法会试图让不同的字符串较均匀的分布在0到{@code upperLimit}之间
* (Provide a number between 0(include) and {@code upperLimit}(exclude) for the given {@code string}, the number will be nearly uniform distribution.)
* <p>
* <p>
* 举个例子假设有N个提供相同服务的服务器地址被存在一个数组中为了实现负载均衡可以根据调用者的名字决定使用哪个服务器
* (e.g. Assume there's an array which contains some IP of the servers provide the same service, the caller name can be used to choose the server to achieve load balance.)
* <blockquote><pre>
* String[] serverIps = new String[10];
* int index = shakeUp("callerName", serverIps.length);
* String targetServerIp = serverIps[index];
* </pre></blockquote>
*
* @param string 字符串如果为null会固定返回0 (a string. the number 0 will be returned if it's null)
* @param upperLimit 返回值的上限必须为正整数(>0) (the upper limit of the returned number, must be a positive integer, which means > 0)
* @return 0到upperLimit不含之间的一个数字 (a number between 0(include) and upperLimit(exclude))
* @throws IllegalArgumentException if the upper limit equals or less than 0
* @since 1.0.0
*/
public static int shakeUp(String string, int upperLimit) {
if (upperLimit < 1) {
throw new IllegalArgumentException("upper limit must be greater than 0");
}
if (string == null) {
return 0;
}
return (string.hashCode() & Integer.MAX_VALUE) % upperLimit;
}
}

View File

@ -22,7 +22,6 @@ import com.alibaba.nacos.naming.misc.HttpClient;
import com.alibaba.nacos.naming.misc.Loggers;
import com.alibaba.nacos.naming.misc.SwitchDomain;
import com.alibaba.nacos.naming.misc.UtilsAndCommons;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
@ -57,7 +56,6 @@ public class DistroFilter implements Filter {
}
@SuppressFBWarnings("HRS_REQUEST_PARAMETER_TO_HTTP_HEADER")
@Override
public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
HttpServletRequest req = (HttpServletRequest) servletRequest;
@ -68,6 +66,10 @@ public class DistroFilter implements Filter {
try {
String path = new URI(req.getRequestURI()).getPath();
String serviceName = req.getParameter(CommonParams.SERVICE_NAME);
// For client under 0.8.0:
if (StringUtils.isBlank(serviceName)) {
serviceName = req.getParameter("dom");
}
Method method = filterBase.getMethod(req.getMethod(), path);
if (method == null) {

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.naming.consistency.ephemeral.distro;
import com.alibaba.nacos.naming.misc.GlobalConfig;
import org.junit.Before;
import org.junit.Test;
import org.springframework.test.util.ReflectionTestUtils;
/**
* @author jifengnan
*/
public class TaskDispatcherTest {
private TaskDispatcher taskDispatcher;
@Before
public void init() {
taskDispatcher = new TaskDispatcher();
GlobalConfig conf = new GlobalConfig();
ReflectionTestUtils.setField(conf, "taskDispatchThreadCount", 3);
ReflectionTestUtils.setField(taskDispatcher, "partitionConfig", conf);
taskDispatcher.init();
}
@Test
public void testAddTask() {
char[] chars = new char[]{2325, 9, 30, 12, 2};
taskDispatcher.addTask(new String(chars));
}
}

View File

@ -40,4 +40,16 @@ public class UtilsAndCommonsTest {
Assert.assertEquals("/nacos/v1/ns", DEFAULT_NACOS_NAMING_CONTEXT);
}
@Test(expected = IllegalArgumentException.class)
public void testShakeUpException() {
UtilsAndCommons.shakeUp(null, 0);
}
@Test
public void testShakeUp() {
Assert.assertEquals(0, UtilsAndCommons.shakeUp(null, 1));
char[] chars = new char[]{2325, 9, 30, 12, 2};
Assert.assertEquals(0, UtilsAndCommons.shakeUp(new String(chars), 1));
}
}

19
pom.xml
View File

@ -21,7 +21,7 @@
<inceptionYear>2018</inceptionYear>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<packaging>pom</packaging>
<name>Alibaba NACOS ${project.version}</name>
@ -680,23 +680,22 @@
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.6</version>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.8</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.9.10</version>
</dependency>
<dependency>
<groupId>com.github.spotbugs</groupId>
<artifactId>spotbugs-annotations</artifactId>
<version>3.1.3</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>

View File

@ -17,7 +17,7 @@
<parent>
<groupId>com.alibaba.nacos</groupId>
<artifactId>nacos-all</artifactId>
<version>1.0.0-RC2</version>
<version>1.0.0-RC3</version>
<relativePath>../pom.xml</relativePath>
</parent>
<modelVersion>4.0.0</modelVersion>

View File

@ -32,6 +32,8 @@ import com.alibaba.nacos.api.naming.NamingFactory;
import com.alibaba.nacos.api.naming.NamingService;
import com.alibaba.nacos.api.naming.pojo.Instance;
import com.alibaba.nacos.api.naming.pojo.ListView;
import com.alibaba.nacos.config.server.utils.TimeUtils;
import com.alibaba.nacos.config.server.utils.TimeoutUtils;
import com.alibaba.nacos.naming.NamingApp;
import org.junit.After;
@ -79,8 +81,6 @@ public class CPInstancesAPI_ITCase {
public void setUp() throws Exception {
String url = String.format("http://localhost:%d/", port);
this.base = new URL(url);
NamingBase.prepareServer(port, "UP", "CP");
TimeUnit.SECONDS.sleep(5L);
naming = NamingFactory.createNamingService("127.0.0.1" + ":" + port);
@ -105,7 +105,7 @@ public class CPInstancesAPI_ITCase {
* @TestStep :
* @ExpectResult :
*/
@Test(expected = IllegalStateException.class)
@Test
public void registerInstance_ephemeral_true() throws Exception {
String serviceName = NamingBase.randomDomainName();
namingServiceCreate(serviceName, TEST_NAMESPACE_1, TEST_GROUP_1);
@ -116,6 +116,9 @@ public class CPInstancesAPI_ITCase {
instance.setIp("11.11.11.11");
instance.setPort(80);
naming1.registerInstance(serviceName, TEST_GROUP_1, instance);
TimeUnit.SECONDS.sleep(3L);
naming1.deregisterInstance(serviceName, TEST_GROUP_1, instance);
namingServiceDelete(serviceName, TEST_NAMESPACE_1, TEST_GROUP_1);
}
/**
@ -154,7 +157,8 @@ public class CPInstancesAPI_ITCase {
instance.setIp("11.11.11.11");
instance.setPort(80);
naming1.registerInstance(serviceName, TEST_GROUP_1, instance);
naming1.deregisterInstance(serviceName, TEST_GROUP_1, "11.11.11.11", 80, "c1");
naming1.deregisterInstance(serviceName, TEST_GROUP_1, instance);
TimeUnit.SECONDS.sleep(3L);
namingServiceDelete(serviceName, TEST_NAMESPACE_1, TEST_GROUP_1);
}

View File

@ -165,10 +165,10 @@ public class NamingBase {
}
public static void prepareServer(int localPort) {
prepareServer(localPort, "UP", "AP");
prepareServer(localPort, "UP");
}
public static void prepareServer(int localPort, String status, String mode) {
public static void prepareServer(int localPort, String status) {
String url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=overriddenServerStatus&value=" + status;
List<String> headers = new ArrayList<String>();
headers.add("User-Agent");
@ -177,15 +177,5 @@ public class NamingBase {
HttpClient.request(url, headers, new HashMap<String, String>(), "UTF-8", "PUT");
Assert.assertEquals(HttpStatus.SC_OK, result.code);
url = "http://127.0.0.1:" + localPort + "/nacos/v1/ns/operator/switches?entry=serverMode&value=" + mode;
headers = new ArrayList<String>();
headers.add("User-Agent");
headers.add("Nacos-Server");
result =
HttpClient.request(url, headers, new HashMap<String, String>(), "UTF-8", "PUT");
Assert.assertEquals(HttpStatus.SC_OK, result.code);
}
}