refactor(nacos-client:config): Rewrite the HTTP retry rule

This commit is contained in:
chuntaojun 2019-06-07 18:41:08 +08:00
parent 92ff0a0873
commit 456e84785f
5 changed files with 118 additions and 144 deletions

View File

@ -68,6 +68,9 @@ public class ServerHttpAgent implements HttpAgent {
boolean isSSL = false;
String currentServerAddr = serverListMgr.getCurrentServerAddr();
int maxRetry = 3;
do {
try {
List<String> newHeaders = getSpasHeaders(paramValues);
@ -75,7 +78,7 @@ public class ServerHttpAgent implements HttpAgent {
newHeaders.addAll(headers);
}
HttpResult result = HttpSimpleClient.httpGet(
getUrl(serverListMgr.getCurrentServerAddr(), path, isSSL), newHeaders, paramValues, encoding,
getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding,
readTimeoutMs, isSSL);
if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR
|| result.code == HttpURLConnection.HTTP_BAD_GATEWAY
@ -87,14 +90,23 @@ public class ServerHttpAgent implements HttpAgent {
}
} catch (ConnectException ce) {
LOGGER.error("[NACOS ConnectException httpGet] currentServerAddr:{}", serverListMgr.getCurrentServerAddr());
serverListMgr.refreshCurrentServerAddr();
} catch (SocketTimeoutException stoe) {
LOGGER.error("[NACOS SocketTimeoutException httpGet] currentServerAddr:{}", serverListMgr.getCurrentServerAddr());
serverListMgr.refreshCurrentServerAddr();
} catch (IOException ioe) {
LOGGER.error("[NACOS IOException httpGet] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), ioe);
throw ioe;
}
if (serverListMgr.hasNextServer()) {
currentServerAddr = serverListMgr.getNextServerAddr();
} else {
maxRetry --;
if (maxRetry < 0) {
throw new ConnectException("[NACOS HTTP-GET] The maximum number of tolerable server reconnection errors has been reached");
}
serverListMgr.refreshCurrentServerAddr();
}
} while (System.currentTimeMillis() <= endTime);
LOGGER.error("no available server");
@ -144,7 +156,7 @@ public class ServerHttpAgent implements HttpAgent {
} else {
maxRetry --;
if (maxRetry < 0) {
throw new ConnectException("The maximum number of tolerable server reconnection errors has been reached");
throw new ConnectException("[NACOS HTTP-POST] The maximum number of tolerable server reconnection errors has been reached");
}
serverListMgr.refreshCurrentServerAddr();
}
@ -160,6 +172,10 @@ public class ServerHttpAgent implements HttpAgent {
long readTimeoutMs) throws IOException {
final long endTime = System.currentTimeMillis() + readTimeoutMs;
boolean isSSL = false;
String currentServerAddr = serverListMgr.getCurrentServerAddr();
int maxRetry = 3;
do {
try {
List<String> newHeaders = getSpasHeaders(paramValues);
@ -167,7 +183,7 @@ public class ServerHttpAgent implements HttpAgent {
newHeaders.addAll(headers);
}
HttpResult result = HttpSimpleClient.httpDelete(
getUrl(serverListMgr.getCurrentServerAddr(), path, isSSL), newHeaders, paramValues, encoding,
getUrl(currentServerAddr, path, isSSL), newHeaders, paramValues, encoding,
readTimeoutMs, isSSL);
if (result.code == HttpURLConnection.HTTP_INTERNAL_ERROR
|| result.code == HttpURLConnection.HTTP_BAD_GATEWAY
@ -179,15 +195,23 @@ public class ServerHttpAgent implements HttpAgent {
}
} catch (ConnectException ce) {
LOGGER.error("[NACOS ConnectException httpDelete] currentServerAddr:{}", serverListMgr.getCurrentServerAddr());
serverListMgr.refreshCurrentServerAddr();
} catch (SocketTimeoutException stoe) {
LOGGER.error("[NACOS SocketTimeoutException httpDelete] currentServerAddr:{}", serverListMgr.getCurrentServerAddr());
serverListMgr.refreshCurrentServerAddr();
} catch (IOException ioe) {
LOGGER.error("[NACOS IOException httpDelete] currentServerAddr: " + serverListMgr.getCurrentServerAddr(), ioe);
throw ioe;
}
if (serverListMgr.hasNextServer()) {
currentServerAddr = serverListMgr.getNextServerAddr();
} else {
maxRetry --;
if (maxRetry < 0) {
throw new ConnectException("[NACOS HTTP-DELETE] The maximum number of tolerable server reconnection errors has been reached");
}
serverListMgr.refreshCurrentServerAddr();
}
} while (System.currentTimeMillis() <= endTime);
LOGGER.error("no available server");

View File

@ -1,81 +1,83 @@
///*
// * 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;
//
//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.Listener;
//import com.alibaba.nacos.api.exception.NacosException;
//import org.junit.Before;
//import org.junit.Test;
//
//import java.util.Properties;
//import java.util.concurrent.Executor;
//
///**
// * @author liaochuntao
// * @date 2019-06-07 16:37
// **/
//public class ConfigLongPollTest {
//
// private ConfigService configService;
//
// @Before
// public void init() throws NacosException {
// Properties properties = new Properties();
// properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
// properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, 2000);
// properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, 5000);
// configService = NacosFactory.createConfigService(properties);
// }
//
// @Test
// public void test() throws InterruptedException, NacosException {
//
// synchronized (this) {
//
// configService.addListener("test", "DEFAULT_GROUP", new Listener() {
// @Override
// public Executor getExecutor() {
// return null;
// }
//
// @Override
// public void receiveConfigInfo(String configInfo) {
// System.out.println(configInfo);
// }
// });
//
// configService.addListener("test-1", "DEFAULT_GROUP", new Listener() {
// @Override
// public Executor getExecutor() {
// return null;
// }
//
// @Override
// public void receiveConfigInfo(String configInfo) {
// System.out.println(configInfo);
// }
// });
//
// wait();
// }
//
// }
//
//}
/*
* 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;
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.Listener;
import com.alibaba.nacos.api.exception.NacosException;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import java.util.Properties;
import java.util.concurrent.Executor;
/**
* @author liaochuntao
* @date 2019-06-07 16:37
**/
@Ignore
public class ConfigLongPollTest {
private ConfigService configService;
@Before
public void init() throws NacosException {
Properties properties = new Properties();
properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, 2000);
properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, 5000);
configService = NacosFactory.createConfigService(properties);
}
@Test
public void test() throws InterruptedException, NacosException {
synchronized (this) {
configService.addListener("test", "DEFAULT_GROUP", new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println(configInfo);
}
});
configService.addListener("test-1", "DEFAULT_GROUP", new Listener() {
@Override
public Executor getExecutor() {
return null;
}
@Override
public void receiveConfigInfo(String configInfo) {
System.out.println(configInfo);
}
});
wait();
}
}
}

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.client;
import org.junit.Before;
import org.junit.Test;
import java.util.Random;
/**
* @author liaochuntao
* @date 2019-06-05 20:55
**/
public class RandomTest {
private Random random;
@Before
public void before() {
random = new Random();
}
@Test
public void test() throws InterruptedException {
long[] tmp = new long[10];
for (int i = 0; i < 10; i++) {
tmp[i] = random.nextInt(Integer.MAX_VALUE);
}
Thread.sleep(100);
for (int i = 0; i < 10; i++) {
System.out.println(tmp[i] == random.nextInt(Integer.MAX_VALUE));
}
}
}

View File

@ -243,7 +243,7 @@ public class LongPollingService extends AbstractEventListener {
// I think there is a problem with the task delay setting here that could easily cause a client read timeout
scheduler.execute(
new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, delayTime, appName, tag));
new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, timeout, appName, tag));
}
@Override

View File

@ -76,7 +76,7 @@ public class DeregisterInstance_ITCase {
*/
@Test
public void dregDomTest() throws Exception {
String serviceName = randomDomainName() + "-dom";
String serviceName = randomDomainName();
System.out.println(serviceName);
naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT);
naming.registerInstance(serviceName, "127.0.0.2", TEST_PORT);
@ -109,11 +109,10 @@ public class DeregisterInstance_ITCase {
*
* @throws Exception
*/
@Repeat(value = 20)
@Test
public void dregDomClusterTest() throws Exception {
String serviceName = randomDomainName() + "-cluster";
String serviceName = randomDomainName();
System.out.println(serviceName);
naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
@ -121,7 +120,6 @@ public class DeregisterInstance_ITCase {
List<Instance> instances;
instances = naming.getAllInstances(serviceName);
System.out.println("before : " + instances.toString());
verifyInstanceList(instances, 2, serviceName);
instances = naming.getAllInstances(serviceName);
@ -132,7 +130,6 @@ public class DeregisterInstance_ITCase {
TimeUnit.SECONDS.sleep(5);
instances = naming.getAllInstances(serviceName);
System.out.println("after : " + instances.toString());
Assert.assertEquals(1, instances.size());
@ -153,7 +150,7 @@ public class DeregisterInstance_ITCase {
@Test
public void dregLastDomTest() throws Exception {
String serviceName = randomDomainName() + "-last";
String serviceName = randomDomainName();
naming.registerInstance(serviceName, "127.0.0.1", TEST_PORT, "c1");
naming.registerInstance(serviceName, "127.0.0.2", TEST_PORT, "c2");