diff --git a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AbstractOpenApiService.java b/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AbstractOpenApiService.java index 464bb1f9..a70120d0 100644 --- a/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AbstractOpenApiService.java +++ b/apollo-openapi/src/main/java/com/ctrip/framework/apollo/openapi/client/service/AbstractOpenApiService.java @@ -18,10 +18,13 @@ import com.ctrip.framework.apollo.openapi.client.exception.ApolloOpenApiException; import com.ctrip.framework.apollo.openapi.client.url.OpenApiPathBuilder; +import com.ctrip.framework.foundation.Foundation; import com.google.common.base.Preconditions; import com.google.common.base.Strings; +import com.google.common.net.HttpHeaders; import com.google.gson.Gson; import org.apache.http.HttpResponse; +import org.apache.http.ProtocolVersion; import org.apache.http.StatusLine; import org.apache.http.client.methods.*; import org.apache.http.entity.ContentType; @@ -74,6 +77,9 @@ private CloseableHttpResponse execute(HttpEntityEnclosingRequestBase requestBase } private CloseableHttpResponse execute(HttpUriRequest request) throws IOException { + + addClientIP(request); + CloseableHttpResponse response = client.execute(request); checkHttpResponseStatus(response); @@ -97,6 +103,12 @@ private void checkHttpResponseStatus(HttpResponse response) { throw new ApolloOpenApiException(status.getStatusCode(), status.getReasonPhrase(), message); } + private void addClientIP(HttpUriRequest request) { + ProtocolVersion protocolVersion = request.getProtocolVersion(); + request.addHeader(HttpHeaders.FORWARDED, String.format("for=%s; proto=%s", Foundation.net().getHostAddress(), protocolVersion.getProtocol())); + request.addHeader(HttpHeaders.X_FORWARDED_FOR, Foundation.net().getHostAddress()); + } + protected void checkNotNull(Object value, String name) { Preconditions.checkArgument(null != value, name + " should not be null"); }