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
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;
import org.reflections.ReflectionUtils;

@Extension(ThreadWorkerPool.TYPE)
public class ThreadWorkerPool extends AbstractWorkerPool
Expand Down Expand Up @@ -104,7 +103,7 @@
try {
// Use JDK 21+ method Executors.newThreadPerTaskExecutor(ThreadFactory threadFactory)
// to create a virtual thread executor service
Class<?> executorsClazz = ReflectionUtils.forName(EXECUTORS_CLASS_NAME);
Class<?> executorsClazz = Class.forName(EXECUTORS_CLASS_NAME);
Method newThreadPerTaskExecutorMethod = executorsClazz
.getDeclaredMethod(NEW_THREAD_PER_TASK_EXECUTOR_NAME, ThreadFactory.class);
ThreadPerTaskExecutorWrapper wrappedThreadPool = ThreadPerTaskExecutorWrapper
Expand All @@ -115,7 +114,8 @@
logger.info("Successfully created an executor that assigns each task to a "
+ "new virtual thread for processing");
return;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) {
} catch (NoSuchMethodException | InvocationTargetException |

Check warning on line 117 in trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] reported by reviewdog 🐶 '|' 应另起一行。 Raw Output: /github/workspace/./trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java:117:72: warning: '|' 应另起一行。 (com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck)
IllegalAccessException | ClassNotFoundException exception) {
logger.warn("The current JDK version does not support virtual threads, please use OpenJDK 21+, "
+ "or remove use_virtual_thread_per_task_executor config, error: ", exception);
}
Expand Down Expand Up @@ -219,7 +219,7 @@
// introducing the "java.lang.Thread.Builder.OfVirtual" dependency will result in an error,
// so we create virtual threads through reflection, which is compatible with JDKs that do not support
// virtual threads. When the JDK does not support virtual threads, it downgrades to thread.
Class<?> threadClazz = ReflectionUtils.forName(THREAD_CLASS_NAME);
Class<?> threadClazz = Class.forName(THREAD_CLASS_NAME);
Method ofVirtualMethod = threadClazz.getDeclaredMethod(OF_VIRTUAL_NAME);
Object virtual = ofVirtualMethod.invoke(threadClazz);
Class<?> virtualClazz = ofVirtualMethod.getReturnType();
Expand All @@ -238,7 +238,8 @@
threadFactory = (ThreadFactory) factoryMethod.invoke(virtual);
logger.info("Successfully created virtual thread factory");
return threadFactory;
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException exception) {
} catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException |

Check warning on line 241 in trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java

View workflow job for this annotation

GitHub Actions / checkstyle

[checkstyle] reported by reviewdog 🐶 '|' 应另起一行。 Raw Output: /github/workspace/./trpc-core/src/main/java/com/tencent/trpc/core/worker/support/thread/ThreadWorkerPool.java:241:97: warning: '|' 应另起一行。 (com.puppycrawl.tools.checkstyle.checks.whitespace.OperatorWrapCheck)
ClassNotFoundException exception) {
logger.error("The current JDK version cannot use virtual threads, please use OpenJDK 21+ or "
+ "Tencent Kona JDK FIBER 8+ version, error: ", exception);
}
Expand Down
38 changes: 19 additions & 19 deletions trpc-dependencies/trpc-dependencies-bom/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,31 +63,31 @@
<assertj-core.version>3.24.2</assertj-core.version>
<apollo.version>2.1.0</apollo.version>
<byte.buddy.version>1.14.9</byte.buddy.version>
<caffeine.version>2.9.3</caffeine.version>
<caffeine.version>3.1.8</caffeine.version>
<central-publishing-maven-plugin.version>0.7.0</central-publishing-maven-plugin.version>
<commons.codec.version>1.15</commons.codec.version>
<commons.codec.version>1.17.1</commons.codec.version>
<commons.collections4.version>4.4</commons.collections4.version>
<commons.configurations.version>2.10.1</commons.configurations.version>
<commons.configurations.version>2.11.0</commons.configurations.version>
<commons.lang.version>2.6</commons.lang.version>
<commons.lang3.version>3.12.0</commons.lang3.version>
<commons.pool2.version>2.3</commons.pool2.version>
<commons.io.version>2.15.1</commons.io.version>
<commons.text.version>1.10.0</commons.text.version>
<commons.lang3.version>3.17.0</commons.lang3.version>
<commons.pool2.version>2.12.0</commons.pool2.version>
<commons.io.version>2.18.0</commons.io.version>
<commons.text.version>1.13.0</commons.text.version>
<commons.beanutils.version>1.9.4</commons.beanutils.version>
<nacos.config.sdk.version>0.2.12</nacos.config.sdk.version>
<curator.version>5.5.0</curator.version>
<curator.version>5.7.1</curator.version>
<consul-api.version>1.4.5</consul-api.version>
<disruptor.version>3.4.2</disruptor.version>
<error.prone.annotations.version>2.10.0</error.prone.annotations.version>
<freemarker.version>2.3.32</freemarker.version>
<gson.version>2.8.9</gson.version>
<gson.version>2.11.0</gson.version>
<guava.version>32.1.3-jre</guava.version>
<httpclient.version>4.5.14</httpclient.version>
<httpclient5.version>5.3.1</httpclient5.version>
<httpclient5.version>5.4.1</httpclient5.version>
<httpcore5.version>5.4</httpcore5.version>
<httpcore.version>4.4.15</httpcore.version>
<httpcore5.version>5.2.5</httpcore5.version>
<hutool.all.version>5.8.28</hutool.all.version>
<jackson.version>2.15.0-rc2</jackson.version>
<jackson.version>2.18.2</jackson.version>
<jakarta.servlet.version>6.0.0</jakarta.servlet.version>
<jakarta.annotation-api.version>2.1.1</jakarta.annotation-api.version>
<jakarta.activation-api.version>2.1.3</jakarta.activation-api.version>
Expand All @@ -101,7 +101,7 @@
<joda-time.version>2.7</joda-time.version>
<jprotobuf.version>2.4.14</jprotobuf.version>
<junit.version>5.14.2</junit.version>
<javassist.version>3.28.0-GA</javassist.version>
<javassist.version>3.30.2-GA</javassist.version>
<javax.servlet-api.version>3.1.0</javax.servlet-api.version>
<jsr305.version>3.0.2</jsr305.version>
<logback.version>1.4.14</logback.version>
Expand All @@ -119,7 +119,7 @@
<nacos.version>1.4.2</nacos.version>
<nacos.client.version>2.2.1</nacos.client.version>
<objenesis.version>3.3</objenesis.version>
<okhttp3.version>4.10.0</okhttp3.version>
<okhttp3.version>4.12.0</okhttp3.version>
<opentracing.version>0.33.0</opentracing.version>
<openfeign.version>12.2</openfeign.version>
<openfeign.form.version>3.8.0</openfeign.form.version>
Expand All @@ -132,24 +132,24 @@
<pgv.version>0.4.1</pgv.version>
<pgv.bin.version>0.6.13</pgv.bin.version>
<quasar.version>0.7.9</quasar.version>
<reflections.version>0.9.12</reflections.version>
<reflections.version>0.10.2</reflections.version>
<resteasy.version>6.2.15.Final</resteasy.version>
<sentinel-transport-simple-http.version>1.8.6</sentinel-transport-simple-http.version>
<sentinel.version>1.8.6</sentinel.version>
<slf4j.version>2.0.17</slf4j.version>
<snakeyaml.version>2.0</snakeyaml.version>
<snappy.version>1.1.10.4</snappy.version>
<snappy.version>1.1.10.7</snappy.version>
<spring.version>6.2.7</spring.version>
<springboot.version>3.5.0</springboot.version>
<spring.cloud.gateway.version>4.3.0</spring.cloud.gateway.version>
<transmittable.version>2.12.4</transmittable.version>
<zookeeper.version>3.8.4</zookeeper.version>
<zookeeper.version>3.9.3</zookeeper.version>

<!-- netty-bom, reactor-bom, io.grpc, auto.value.annotations should be upgraded simultaneously -->
<auto.value.annotations.version>1.9</auto.value.annotations.version>
<io.grpc.version>1.56.0</io.grpc.version>
<reactor-bom.version>2022.0.9</reactor-bom.version>
<netty-bom.version>4.1.79.Final</netty-bom.version>
<reactor-bom.version>2023.0.11</reactor-bom.version>
<netty-bom.version>4.1.115.Final</netty-bom.version>
</properties>

<dependencyManagement>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand Down Expand Up @@ -65,7 +65,6 @@ public List<String> shortcutFieldOrder() {

@Override
public GatewayFilter apply(TrpcGatewayFilterFactory.Config config) {
logger.info("config.toString():" + config.toString());
loadRequestRewriter(config);
loadResponseRewriter(config);
return new TrpcRoutingFilter(requestRewriter, responseRewriter, config);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,17 @@ public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
return DataBufferUtils.join(request.getBody())
.map(body -> requestRewriter.resolver(exchange, route, body))
.map(body -> client.asyncInvoke(request, route, body.asByteBuffer().array()))
.map(body -> {
java.nio.ByteBuffer byteBuffer = body.asByteBuffer();
byte[] bytes;
if (byteBuffer.hasArray()) {
bytes = byteBuffer.array();
} else {
bytes = new byte[byteBuffer.remaining()];
byteBuffer.get(bytes);
}
return client.asyncInvoke(request, route, bytes);
})
.flatMap(result -> this.responseRewriter.write(exchange, route.getMetadata(), result));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand Down Expand Up @@ -30,11 +30,25 @@ public Mono<Void> write(ServerWebExchange exchange,
Mono<byte[]> result) {
ServerHttpResponse response = exchange.getResponse();
if (result != null) {
DataBuffer dataBuffer = response.bufferFactory().wrap(result.block());
logger.info("dataBuffer :{}", dataBuffer.toString(StandardCharsets.UTF_8));
// Content-Type uses application/json by default
response.getHeaders().add("Content-Type", MimeTypeUtils.APPLICATION_JSON_VALUE);
return response.writeWith(Mono.justOrEmpty(dataBuffer));
return result.flatMap(bytes -> {
if (bytes == null || bytes.length == 0) {
if (logger.isDebugEnabled()) {
logger.debug("Empty response body, skipping write");
}
return Mono.empty();
}

if (logger.isDebugEnabled()) {
logger.debug("Writing response: {}", new String(bytes, StandardCharsets.UTF_8));
}

DataBuffer dataBuffer = response.bufferFactory().wrap(bytes);
response.getHeaders().setContentLength(bytes.length);
response.getHeaders().set("Content-Type", MimeTypeUtils.APPLICATION_JSON_VALUE);

return response.writeWith(Mono.just(dataBuffer))
.doOnError(error -> logger.error("Failed to write response", error));
});
}
return Mono.empty();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand All @@ -11,8 +11,8 @@

package com.tencent.trpc.spring.cloud.gateway.filter;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import com.tencent.trpc.spring.cloud.gateway.TrpcGatewayApplication;
import java.io.IOException;
Expand All @@ -22,6 +22,7 @@
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;
import okhttp3.ResponseBody;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.jupiter.api.AfterEach;
Expand All @@ -34,52 +35,60 @@ public class TrpcRoutingFilterTest {

private final String requestBody = "{\"msg\":\"hello gateway!\",\"id\":\"\"}";

ConfigurableApplicationContext application;
private ConfigurableApplicationContext application;
private OkHttpClient httpClient;

@BeforeEach
void setUp() {
// Start the Spring container, start the gateway, and backend services
void setUp() throws InterruptedException {
application = new SpringApplicationBuilder().sources(TrpcGatewayApplication.class).run(new String[0]);
TimeUnit.SECONDS.sleep(5);

httpClient = new OkHttpClient().newBuilder()
.readTimeout(10, TimeUnit.SECONDS)
.connectTimeout(10, TimeUnit.SECONDS)
.writeTimeout(10, TimeUnit.SECONDS)
.build();
}

@AfterEach
void tearDown() {
// Stop the gateway and simulated backend services
application.stop();
if (application != null) {
application.close();
}
}

@Test
void filter() {
// Initiate an HTTP request and verify the normal TRPC forwarding scenario.
trpcTest();

// Initiate an HTTP request and verify the normal HTTP forwarding scenario.
httpTest();
}

private void httpTest() {
try {
JSONObject response = gateway(getHttpRequest());
assertEquals(response.toString(), requestBody);
assertNotNull(response);
assertEquals(requestBody, response.toString());
} catch (JSONException | IOException e) {
assertNull(e);
throw new AssertionError("httpTest failed", e);
}
}

private void trpcTest() {
try {
JSONObject response = gateway(getTRPCRequest(requestBody));
assertEquals(response.toString(), requestBody);
assertNotNull(response);
assertEquals(requestBody, response.toString());
} catch (JSONException | IOException e) {
assertNull(e);
throw new AssertionError("trpcTest failed", e);
}
}

private JSONObject gateway(Request httpRequest) throws JSONException, IOException {
Response response = new OkHttpClient().newBuilder().readTimeout(2, TimeUnit.SECONDS).build()
.newCall(httpRequest).execute();
// Format is as follows: {"message":"","id":""}
return new JSONObject(response.body().string());
try (Response response = httpClient.newCall(httpRequest).execute()) {
ResponseBody body = response.body();
assertNotNull(body);
return new JSONObject(body.string());
}
}

private Request getHttpRequest() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@
import com.tencent.trpc.core.rpc.Response;
import com.tencent.trpc.spring.context.TRpcConfigAutoRegistryTest.TestService;
import java.util.concurrent.CompletionStage;
import javax.annotation.Resource;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;

public class AutoInjectTestClientFilter implements Filter {

Expand All @@ -31,6 +32,7 @@ public TestService getMyTestService1() {
}

@Autowired
@Qualifier("myTestService1")
public void setMyTestService1(TestService myTestService1) {
this.myTestService1 = myTestService1;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import com.tencent.trpc.spring.context.TRpcConfigAutoRegistryTest.InjectByFieldBean;
import com.tencent.trpc.spring.context.TRpcConfigAutoRegistryTest.InjectBySetterBean;
import java.util.concurrent.CompletionStage;
import javax.annotation.Resource;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;

public class AutoInjectTestServerFilter implements Filter {
Expand Down
Loading
Loading