Removing OkHttp3 dependencies (#1313)

Signed-off-by: Artur Ciocanu <ciocanu@adobe.com>
Co-authored-by: Artur Ciocanu <ciocanu@adobe.com>
This commit is contained in:
artur-ciocanu 2025-04-15 01:44:49 +03:00 committed by GitHub
parent ef1fc2242a
commit 128cfdeb4b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
11 changed files with 73 additions and 117 deletions

View File

@ -111,12 +111,6 @@
<version>${springboot.version}</version> <version>${springboot.version}</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.gmazzo.okhttp.mock</groupId>
<artifactId>mock-client</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -88,18 +88,6 @@ dependencies {
{{< /tabs >}} {{< /tabs >}}
If you are also using Spring Boot, you may run into a common issue where the `OkHttp` version that the Dapr SDK uses conflicts with the one specified in the Spring Boot _Bill of Materials_.
You can fix this by specifying a compatible `OkHttp` version in your project to match the version that the Dapr SDK uses:
```xml
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>1.14.0</version>
</dependency>
```
## Try it out ## Try it out
Put the Dapr Java SDK to the test. Walk through the Java quickstarts and tutorials to see Dapr in action: Put the Dapr Java SDK to the test. Walk through the Java quickstarts and tutorials to see Dapr in action:

View File

@ -133,11 +133,6 @@
<artifactId>protobuf-java</artifactId> <artifactId>protobuf-java</artifactId>
<version>${protobuf.version}</version> <version>${protobuf.version}</version>
</dependency> </dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.12.0</version>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -19,11 +19,11 @@ import com.evanlennick.retry4j.config.RetryConfig;
import com.evanlennick.retry4j.config.RetryConfigBuilder; import com.evanlennick.retry4j.config.RetryConfigBuilder;
import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -34,7 +34,11 @@ import static java.time.temporal.ChronoUnit.SECONDS;
*/ */
final class Validation { final class Validation {
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient(); private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
private static final String TRACES_URL = "http://localhost:9411/api/v2/traces";
private static final RetryConfig RETRY_CONFIG = new RetryConfigBuilder() private static final RetryConfig RETRY_CONFIG = new RetryConfigBuilder()
.withMaxNumberOfTries(5) .withMaxNumberOfTries(5)
@ -56,27 +60,16 @@ final class Validation {
static void validate() { static void validate() {
Status<Void> result = new CallExecutorBuilder().config(RETRY_CONFIG).build().execute(() -> doValidate()); Status<Void> result = new CallExecutorBuilder().config(RETRY_CONFIG).build().execute(() -> doValidate());
if (!result.wasSuccessful()) { if (!result.wasSuccessful()) {
throw new RuntimeException(result.getLastExceptionThatCausedRetry()); throw new RuntimeException(result.getLastExceptionThatCausedRetry());
} }
} }
private static Void doValidate() throws Exception { private static Void doValidate() throws Exception {
System.out.println("Performing validation of tracing events ..."); HttpRequest request = HttpRequest.newBuilder().GET().uri(URI.create(TRACES_URL)).build();
HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
HttpUrl.Builder urlBuilder = new HttpUrl.Builder(); DocumentContext documentContext = JsonPath.parse(response.body());
urlBuilder.scheme("http")
.host("localhost")
.port(9411);
urlBuilder.addPathSegments("api/v2/traces");
Request.Builder requestBuilder = new Request.Builder()
.url(urlBuilder.build());
requestBuilder.method("GET", null);
Request request = requestBuilder.build();
Response response = HTTP_CLIENT.newCall(request).execute();
DocumentContext documentContext = JsonPath.parse(response.body().string());
String mainSpanId = readOne(documentContext, "$..[?(@.name == \"example's main\")]['id']").toString(); String mainSpanId = readOne(documentContext, "$..[?(@.name == \"example's main\")]['id']").toString();
// Validate echo // Validate echo
@ -104,13 +97,14 @@ final class Validation {
.toString(); .toString();
readOne(documentContext, readOne(documentContext,
String.format(JSONPATH_SLEEP_SPAN_ID, proxySleepSpanId2)); String.format(JSONPATH_SLEEP_SPAN_ID, proxySleepSpanId2));
System.out.println("Validation of tracing events has succeeded.");
return null; return null;
} }
private static Object readOne(DocumentContext documentContext, String path) { private static Object readOne(DocumentContext documentContext, String path) {
List<Map<String, Object>> arr = documentContext.read(path); List<Map<String, Object>> arr = documentContext.read(path);
if (arr.size() == 0) {
if (arr.isEmpty()) {
throw new RuntimeException("No record found for " + path); throw new RuntimeException("No record found for " + path);
} }
@ -119,6 +113,7 @@ final class Validation {
private static void assertCount(DocumentContext documentContext, String path, int expectedCount) { private static void assertCount(DocumentContext documentContext, String path, int expectedCount) {
List<Map<String, Object>> arr = documentContext.read(path); List<Map<String, Object>> arr = documentContext.read(path);
if (arr.size() != expectedCount) { if (arr.size() != expectedCount) {
throw new RuntimeException( throw new RuntimeException(
String.format("Unexpected count %d vs expected %d for %s", arr.size(), expectedCount, path)); String.format("Unexpected count %d vs expected %d for %s", arr.size(), expectedCount, path));

View File

@ -31,12 +31,6 @@
<artifactId>mockito-core</artifactId> <artifactId>mockito-core</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.gmazzo.okhttp.mock</groupId>
<artifactId>mock-client</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>org.junit.jupiter</groupId> <groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId> <artifactId>junit-jupiter</artifactId>

View File

@ -71,12 +71,6 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.gmazzo.okhttp.mock</groupId>
<artifactId>mock-client</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -142,7 +142,7 @@
</dependency> </dependency>
<dependency> <dependency>
<groupId>io.zipkin.reporter2</groupId> <groupId>io.zipkin.reporter2</groupId>
<artifactId>zipkin-sender-okhttp3</artifactId> <artifactId>zipkin-sender-urlconnection</artifactId>
<version>3.4.0</version> <version>3.4.0</version>
</dependency> </dependency>
<dependency> <dependency>

View File

@ -24,17 +24,18 @@ import io.dapr.config.Property;
import io.dapr.v1.AppCallbackHealthCheckGrpc; import io.dapr.v1.AppCallbackHealthCheckGrpc;
import io.grpc.ManagedChannel; import io.grpc.ManagedChannel;
import io.grpc.ManagedChannelBuilder; import io.grpc.ManagedChannelBuilder;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.apache.commons.lang3.tuple.ImmutablePair; import org.apache.commons.lang3.tuple.ImmutablePair;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.time.Duration;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.UUID; import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier; import java.util.function.Supplier;
@ -231,23 +232,21 @@ public class DaprRun implements Stoppable {
channel.shutdown(); channel.shutdown();
} }
} else { } else {
// Create an OkHttpClient instance with a custom timeout Duration waitDuration = Duration.ofMillis(maxWaitMilliseconds);
OkHttpClient client = new OkHttpClient.Builder() HttpClient client = HttpClient.newBuilder()
.connectTimeout(maxWaitMilliseconds, TimeUnit.MILLISECONDS) .version(HttpClient.Version.HTTP_1_1)
.readTimeout(maxWaitMilliseconds, TimeUnit.MILLISECONDS) .connectTimeout(waitDuration)
.build(); .build();
String url = "http://127.0.0.1:" + this.getAppPort() + "/health";
HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(url))
.build();
// Define the URL to probe try {
String url = "http://127.0.0.1:" + this.getAppPort() + "/health"; // Change to your specific URL HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
// Create a request to the URL if (response.statusCode() != 200) {
Request request = new Request.Builder()
.url(url)
.build();
// Execute the request
try (Response response = client.newCall(request).execute()) {
if (!response.isSuccessful()) {
throw new RuntimeException("error: HTTP service is not healthy."); throw new RuntimeException("error: HTTP service is not healthy.");
} }
} catch (IOException e) { } catch (IOException e) {

View File

@ -21,9 +21,6 @@ import io.dapr.client.domain.State;
import io.dapr.config.Properties; import io.dapr.config.Properties;
import io.dapr.testcontainers.DaprContainer; import io.dapr.testcontainers.DaprContainer;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag; import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -34,6 +31,10 @@ import static org.testcontainers.shaded.org.awaitility.Awaitility.await;
import org.testcontainers.shaded.org.awaitility.core.ConditionTimeoutException; import org.testcontainers.shaded.org.awaitility.core.ConditionTimeoutException;
import java.io.IOException; import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.util.Map; import java.util.Map;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@ -51,7 +52,6 @@ import static com.github.tomakehurst.wiremock.client.WireMock.verify;
import static io.dapr.it.testcontainers.ContainerConstants.DAPR_IMAGE_TAG; import static io.dapr.it.testcontainers.ContainerConstants.DAPR_IMAGE_TAG;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail; import static org.junit.jupiter.api.Assertions.fail;
@ -155,20 +155,21 @@ public class DaprContainerIT {
} }
private String checkSidecarMetadata() throws IOException { private String checkSidecarMetadata() throws IOException, InterruptedException {
OkHttpClient okHttpClient = new OkHttpClient.Builder() String url = DAPR_CONTAINER.getHttpEndpoint() + "/v1.0/metadata";
.build(); HttpClient client = HttpClient.newBuilder()
Request request = new Request.Builder() .version(HttpClient.Version.HTTP_1_1)
.url(DAPR_CONTAINER.getHttpEndpoint() + "/v1.0/metadata") .build();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create(url))
.build(); .build();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
try (Response response = okHttpClient.newCall(request).execute()) { if (response.statusCode() != 200) {
if (response.isSuccessful() && response.body() != null) { throw new IOException("Unexpected response: " + response.statusCode());
return response.body().string();
} else {
throw new IOException("Unexpected response: " + response.code());
}
} }
return response.body();
} }
@Test @Test

View File

@ -16,13 +16,14 @@ package io.dapr.it.tracing;
import com.jayway.jsonpath.DocumentContext; import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath; import com.jayway.jsonpath.JsonPath;
import net.minidev.json.JSONArray; import net.minidev.json.JSONArray;
import okhttp3.HttpUrl;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Class used to verify that traces are present as expected. * Class used to verify that traces are present as expected.
@ -31,7 +32,11 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
*/ */
public final class Validation { public final class Validation {
private static final OkHttpClient HTTP_CLIENT = new OkHttpClient(); private static final HttpClient HTTP_CLIENT = HttpClient.newBuilder()
.version(HttpClient.Version.HTTP_1_1)
.build();
private static final String TRACES_URL = "http://localhost:9411/api/v2/traces?limit=100";
/** /**
* JSON Path for main span Id. * JSON Path for main span Id.
@ -47,31 +52,28 @@ public final class Validation {
public static void validate(String spanName, String sleepSpanName) throws Exception { public static void validate(String spanName, String sleepSpanName) throws Exception {
// Must wait for some time to make sure Zipkin receives all spans. // Must wait for some time to make sure Zipkin receives all spans.
Thread.sleep(10000); Thread.sleep(10000);
HttpUrl.Builder urlBuilder = new HttpUrl.Builder();
urlBuilder.scheme("http")
.host("localhost")
.port(9411)
.addPathSegments("api/v2/traces")
.addQueryParameter("limit", "100");
Request.Builder requestBuilder = new Request.Builder()
.url(urlBuilder.build());
requestBuilder.method("GET", null);
Request request = requestBuilder.build(); HttpRequest request = HttpRequest.newBuilder()
.GET()
.uri(URI.create(TRACES_URL))
.build();
Response response = HTTP_CLIENT.newCall(request).execute(); HttpResponse<String> response = HTTP_CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
DocumentContext documentContext = JsonPath.parse(response.body().string()); DocumentContext documentContext = JsonPath.parse(response.body());
String mainSpanId = readOne(documentContext, String.format(JSONPATH_MAIN_SPAN_ID, spanName)).toString(); String mainSpanId = readOne(documentContext, String.format(JSONPATH_MAIN_SPAN_ID, spanName)).toString();
assertNotNull(mainSpanId); assertNotNull(mainSpanId);
String sleepSpanId = readOne(documentContext, String.format(JSONPATH_SLEEP_SPAN_ID, mainSpanId, sleepSpanName)) String sleepSpanId = readOne(documentContext, String.format(JSONPATH_SLEEP_SPAN_ID, mainSpanId, sleepSpanName))
.toString(); .toString();
assertNotNull(sleepSpanId); assertNotNull(sleepSpanId);
} }
private static Object readOne(DocumentContext documentContext, String path) { private static Object readOne(DocumentContext documentContext, String path) {
JSONArray arr = documentContext.read(path); JSONArray arr = documentContext.read(path);
assertTrue(arr.size() > 0);
assertFalse(arr.isEmpty());
return arr.get(0); return arr.get(0);
} }

View File

@ -61,12 +61,6 @@
<version>1.9.0</version> <version>1.9.0</version>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<dependency>
<groupId>com.github.gmazzo.okhttp.mock</groupId>
<artifactId>mock-client</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>com.github.stefanbirkner</groupId> <groupId>com.github.stefanbirkner</groupId>
<artifactId>system-rules</artifactId> <artifactId>system-rules</artifactId>