Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Apollo Java 2.5.0

* [Feature Provide a new open APl to return the organization list](https://github.com/apolloconfig/apollo-java/pull/102)
* [Feature Added a new feature to get instance count by namespace.](https://github.com/apolloconfig/apollo-java/pull/103)
* [Feature Support retry in open api client.](https://github.com/apolloconfig/apollo-java/pull/105)

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/5?closed=1)
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package com.ctrip.framework.apollo.openapi.client;

import com.ctrip.framework.apollo.openapi.client.constant.ApolloOpenApiConstants;
import com.ctrip.framework.apollo.openapi.client.extend.ApolloStandardHttpRequestRetryHandler;
import com.ctrip.framework.apollo.openapi.client.extend.IdempotentHttpMethod;
import com.ctrip.framework.apollo.openapi.client.service.AppOpenApiService;
import com.ctrip.framework.apollo.openapi.client.service.ClusterOpenApiService;
import com.ctrip.framework.apollo.openapi.client.service.ItemOpenApiService;
Expand Down Expand Up @@ -55,10 +57,13 @@ public class ApolloOpenApiClient {
private final InstanceOpenApiService instanceService;
private static final Gson GSON = new GsonBuilder().setDateFormat(ApolloOpenApiConstants.JSON_DATE_FORMAT).create();

private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig) {
private ApolloOpenApiClient(String portalUrl, String token, RequestConfig requestConfig,
int retryCount, IdempotentHttpMethod[] idempotentHttpMethods) {
this.portalUrl = portalUrl;
this.token = token;
CloseableHttpClient client = HttpClients.custom().setDefaultRequestConfig(requestConfig)
.setRetryHandler(retryCount > 0 ?
new ApolloStandardHttpRequestRetryHandler(retryCount, idempotentHttpMethods) : null)
.setDefaultHeaders(Lists.newArrayList(new BasicHeader("Authorization", token))).build();

String baseUrl = this.portalUrl + ApolloOpenApiConstants.OPEN_API_V1_PREFIX;
Expand Down Expand Up @@ -273,6 +278,8 @@ public static class ApolloOpenApiClientBuilder {
private String token;
private int connectTimeout = -1;
private int readTimeout = -1;
private int retryCount = -1;
private IdempotentHttpMethod[] idempotentHttpMethods;

/**
* @param portalUrl The apollo portal url, e.g http://localhost:8070
Expand Down Expand Up @@ -306,6 +313,22 @@ public ApolloOpenApiClientBuilder withReadTimeout(int readTimeout) {
return this;
}

/**
* @param retryCount execute retry when an exception occurs, default no retry
*/
public ApolloOpenApiClientBuilder withRetryCount(int retryCount) {
this.retryCount = retryCount;
return this;
}

/**
* @param idempotentHttpMethods idempotent HTTP methods will directly execute retries when exception
*/
public ApolloOpenApiClientBuilder withIdempotentHttpMethods(IdempotentHttpMethod... idempotentHttpMethods) {
this.idempotentHttpMethods = idempotentHttpMethods;
return this;
}

public ApolloOpenApiClient build() {
Preconditions.checkArgument(!Strings.isNullOrEmpty(portalUrl), "Portal url should not be null or empty!");
Preconditions.checkArgument(portalUrl.startsWith("http://") || portalUrl.startsWith("https://"), "Portal url should start with http:// or https://" );
Expand All @@ -322,7 +345,7 @@ public ApolloOpenApiClient build() {
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout)
.setSocketTimeout(readTimeout).build();

return new ApolloOpenApiClient(portalUrl, token, requestConfig);
return new ApolloOpenApiClient(portalUrl, token, requestConfig, retryCount, idempotentHttpMethods);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.openapi.client.extend;

import com.google.common.collect.Collections2;
import com.google.common.collect.Lists;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import org.apache.http.HttpRequest;
import org.apache.http.impl.client.DefaultHttpRequestRetryHandler;

import javax.net.ssl.SSLException;
import java.net.ConnectException;
import java.net.NoRouteToHostException;
import java.net.UnknownHostException;
import java.util.Arrays;
import java.util.Locale;

/**
* @author zth9
* @date 2025-05-07
*/
public class ApolloStandardHttpRequestRetryHandler extends DefaultHttpRequestRetryHandler {

private final Set<String> idempotentMethods;

public ApolloStandardHttpRequestRetryHandler(int retryCount, IdempotentHttpMethod[] httpMethods) {
super(retryCount, false, Arrays.asList(
UnknownHostException.class,
ConnectException.class,
NoRouteToHostException.class,
SSLException.class));
this.idempotentMethods = new HashSet<>();
if (httpMethods == null || httpMethods.length == 0) {
// default set safe idempotent http method
httpMethods = IdempotentHttpMethod.safe();
}
for (IdempotentHttpMethod httpMethod : httpMethods) {
if (httpMethod == null) {
continue;
}
idempotentMethods.add(httpMethod.name());
}
}

@Override
protected boolean handleAsIdempotent(final HttpRequest request) {
String method = request.getRequestLine().getMethod().toUpperCase(Locale.ROOT);
return idempotentMethods.contains(method);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright 2022 Apollo Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.ctrip.framework.apollo.openapi.client.extend;

/**
* @author zth9
* @date 2025-05-11
*/
public enum IdempotentHttpMethod {
GET, HEAD, PUT, DELETE, OPTIONS, TRACE;

/**
* Usually, these methods are idempotent
*/
public static IdempotentHttpMethod[] safe() {
return new IdempotentHttpMethod[]{GET, HEAD, OPTIONS, TRACE};
}

/**
* Standard HTTP idempotent method. While PUT and DELETE are technically idempotent, repeated
* requests can yield different responses—such as a 404 on a second delete
*/
public static IdempotentHttpMethod[] standard() {
return new IdempotentHttpMethod[]{GET, HEAD, PUT, DELETE, OPTIONS, TRACE};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.ctrip.framework.apollo.openapi.client.extend.IdempotentHttpMethod;
import com.ctrip.framework.apollo.openapi.dto.NamespaceReleaseDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenAppDTO;
import com.ctrip.framework.apollo.openapi.dto.OpenAppNamespaceDTO;
Expand Down Expand Up @@ -63,6 +64,8 @@ ApolloOpenApiClient newClient() {
.withToken(someToken)
.withReadTimeout(2000 * 1000)
.withConnectTimeout(2000 * 1000)
.withRetryCount(3)
.withIdempotentHttpMethods(IdempotentHttpMethod.safe())
.build();
}

Expand Down