Skip to content

Commit 4152b46

Browse files
authored
fix:remove okhttp dep (#280)
1 parent 4114955 commit 4152b46

File tree

3 files changed

+29
-27
lines changed

3 files changed

+29
-27
lines changed

polaris-plugins/polaris-plugins-location/location-provider-remotehttp/pom.xml

-5
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,6 @@
2121
<artifactId>location-provider-base</artifactId>
2222
<version>${project.version}</version>
2323
</dependency>
24-
<dependency>
25-
<groupId>com.squareup.okhttp</groupId>
26-
<artifactId>okhttp</artifactId>
27-
<version>${okhttp.version}</version>
28-
</dependency>
2924
</dependencies>
3025

3126
</project>

polaris-plugins/polaris-plugins-location/location-provider-remotehttp/src/main/java/com/tencent/polaris/plugin/location/remotehttp/RemoteHttpLocationProvider.java

+28-21
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,14 @@
1717

1818
package com.tencent.polaris.plugin.location.remotehttp;
1919

20+
import java.io.BufferedReader;
2021
import java.io.IOException;
22+
import java.io.InputStreamReader;
23+
import java.net.HttpURLConnection;
24+
import java.net.URL;
25+
import java.util.concurrent.TimeUnit;
2126

2227
import com.google.protobuf.StringValue;
23-
import com.squareup.okhttp.Call;
24-
import com.squareup.okhttp.OkHttpClient;
25-
import com.squareup.okhttp.Request;
26-
import com.squareup.okhttp.Response;
2728
import com.tencent.polaris.api.utils.StringUtils;
2829
import com.tencent.polaris.client.pb.ModelProto;
2930
import com.tencent.polaris.logging.LoggerFactory;
@@ -37,8 +38,6 @@ public class RemoteHttpLocationProvider extends BaseLocationProvider<BaseLocatio
3738

3839
private static final Logger LOGGER = LoggerFactory.getLogger(RemoteHttpLocationProvider.class);
3940

40-
private final OkHttpClient httpClient = new OkHttpClient();
41-
4241
public RemoteHttpLocationProvider() {
4342
super(GetOption.class);
4443
}
@@ -67,31 +66,39 @@ public ModelProto.Location doGet(GetOption option) {
6766
.build();
6867
}
6968

70-
private String getResponse(final String url, String label) {
71-
if (StringUtils.isEmpty(url)) {
69+
private String getResponse(final String path, String label) {
70+
if (StringUtils.isEmpty(path)) {
7271
LOGGER.warn("[Location][Provider][RemoteHttp] get {} from remote url is empty", label);
7372
return "";
7473
}
7574

76-
Request request = new Request.Builder()
77-
.get()
78-
.url(url)
79-
.build();
80-
81-
Call call = httpClient.newCall(request);
82-
75+
HttpURLConnection conn = null;
8376
try {
84-
Response response = call.execute();
85-
byte[] ret = response.body().bytes();
86-
if (response.code() != 200) {
87-
LOGGER.error("[Location][Provider][RemoteHttp] get {} from remote {} fail: {}", label, url, new String(ret));
77+
URL url = new java.net.URL(path);
78+
conn = (HttpURLConnection) url.openConnection();
79+
80+
conn.setRequestMethod("GET");
81+
conn.setConnectTimeout((int) TimeUnit.SECONDS.toMillis(2));// 连接超时
82+
conn.setReadTimeout((int) TimeUnit.SECONDS.toMillis(2));// 读取超时
83+
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
84+
StringBuffer buffer = new StringBuffer();
85+
String str;
86+
while((str = reader.readLine())!= null){
87+
buffer.append(str);
88+
}
89+
if (conn.getResponseCode() != 200) {
90+
LOGGER.error("[Location][Provider][RemoteHttp] get {} from remote {} fail: {}", label, url, buffer);
8891
return "";
8992
}
90-
return new String(ret);
93+
return buffer.toString();
9194
}
9295
catch (IOException e) {
93-
LOGGER.error("[Location][Provider][RemoteHttp] get {} from remote {} fail : {}", label, url, e.getMessage());
96+
LOGGER.error("[Location][Provider][RemoteHttp] get {} from remote {} fail : {}", label, path, e);
9497
return "";
98+
} finally {
99+
if (null != conn) {
100+
conn.disconnect();
101+
}
95102
}
96103
}
97104
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
<properties>
6666
<!-- Project revision -->
67-
<revision>1.10.4</revision>
67+
<revision>1.10.5</revision>
6868

6969
<timestamp>${maven.build.timestamp}</timestamp>
7070
<skip.maven.deploy>false</skip.maven.deploy>

0 commit comments

Comments
 (0)