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

View File

@ -1,81 +1,83 @@
///* /*
// * Copyright 1999-2018 Alibaba Group Holding Ltd. * Copyright 1999-2018 Alibaba Group Holding Ltd.
// * *
// * Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at * You may obtain a copy of the License at
// * *
// * http://www.apache.org/licenses/LICENSE-2.0 * http://www.apache.org/licenses/LICENSE-2.0
// * *
// * Unless required by applicable law or agreed to in writing, software * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS, * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and * See the License for the specific language governing permissions and
// * limitations under the License. * limitations under the License.
// */ */
//
//package com.alibaba.nacos.client; package com.alibaba.nacos.client;
//
//import com.alibaba.nacos.api.NacosFactory; import com.alibaba.nacos.api.NacosFactory;
//import com.alibaba.nacos.api.PropertyKeyConst; import com.alibaba.nacos.api.PropertyKeyConst;
//import com.alibaba.nacos.api.config.ConfigService; import com.alibaba.nacos.api.config.ConfigService;
//import com.alibaba.nacos.api.config.listener.Listener; import com.alibaba.nacos.api.config.listener.Listener;
//import com.alibaba.nacos.api.exception.NacosException; import com.alibaba.nacos.api.exception.NacosException;
//import org.junit.Before; import org.junit.Before;
//import org.junit.Test; import org.junit.Ignore;
// import org.junit.Test;
//import java.util.Properties;
//import java.util.concurrent.Executor; import java.util.Properties;
// import java.util.concurrent.Executor;
///**
// * @author liaochuntao /**
// * @date 2019-06-07 16:37 * @author liaochuntao
// **/ * @date 2019-06-07 16:37
//public class ConfigLongPollTest { **/
// @Ignore
// private ConfigService configService; public class ConfigLongPollTest {
//
// @Before private ConfigService configService;
// public void init() throws NacosException {
// Properties properties = new Properties(); @Before
// properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848"); public void init() throws NacosException {
// properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, 2000); Properties properties = new Properties();
// properties.put(PropertyKeyConst.CONFIG_RETRY_TIME, 5000); properties.put(PropertyKeyConst.SERVER_ADDR, "127.0.0.1:8848");
// configService = NacosFactory.createConfigService(properties); 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 {
// @Test
// synchronized (this) { public void test() throws InterruptedException, NacosException {
//
// configService.addListener("test", "DEFAULT_GROUP", new Listener() { synchronized (this) {
// @Override
// public Executor getExecutor() { configService.addListener("test", "DEFAULT_GROUP", new Listener() {
// return null; @Override
// } public Executor getExecutor() {
// return null;
// @Override }
// public void receiveConfigInfo(String configInfo) {
// System.out.println(configInfo); @Override
// } public void receiveConfigInfo(String configInfo) {
// }); System.out.println(configInfo);
// }
// configService.addListener("test-1", "DEFAULT_GROUP", new Listener() { });
// @Override
// public Executor getExecutor() { configService.addListener("test-1", "DEFAULT_GROUP", new Listener() {
// return null; @Override
// } public Executor getExecutor() {
// return null;
// @Override }
// public void receiveConfigInfo(String configInfo) {
// System.out.println(configInfo); @Override
// } public void receiveConfigInfo(String configInfo) {
// }); System.out.println(configInfo);
// }
// wait(); });
// }
// 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 // I think there is a problem with the task delay setting here that could easily cause a client read timeout
scheduler.execute( scheduler.execute(
new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, delayTime, appName, tag)); new ClientLongPolling(asyncContext, clientMd5Map, ip, probeRequestSize, timeout, appName, tag));
} }
@Override @Override

View File

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