diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/autoconfigure/EnableTrpcRouting.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/autoconfigure/EnableTrpcRouting.java index a5958de15d..011dbe538a 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/autoconfigure/EnableTrpcRouting.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/autoconfigure/EnableTrpcRouting.java @@ -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, @@ -11,19 +11,16 @@ package com.tencent.trpc.spring.cloud.gateway.autoconfigure; -import com.tencent.trpc.spring.cloud.gateway.TrpcGatewayApplication; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; -import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.context.annotation.Import; /** - * When using, you can add annotations to the {@link SpringApplicationBuilder} startup. - * Example code: {@link TrpcGatewayApplication} + * When using it, you need to add the `@EnableTrpcRouting` annotation to your Spring application's startup class. * The application will automatically load the `Bean` objects in {@link TrpcGatewayConfig} at startup. */ @Target({ElementType.TYPE}) diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/META-INF/spring.factories b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/META-INF/spring.factories deleted file mode 100644 index d5b5d253ae..0000000000 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/META-INF/spring.factories +++ /dev/null @@ -1 +0,0 @@ -#org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.tencent.trpc.spring.cloud.gateway.autoconfigure.TrpcGatewayConfig \ No newline at end of file diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/application.yml b/trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/application.yml deleted file mode 100644 index 4731627a82..0000000000 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/resources/application.yml +++ /dev/null @@ -1,56 +0,0 @@ -logging: - level: - org.springframework.cloud.gateway: DEBUG - reactor.netty.http.client: DEBUG - -server: - port: 8080 -spring: - main: - web-application-type: reactive - application: - name: TrpcHttpGateway - cloud: - gateway: - default-filters: - - AddRequestHeader=gateway-env, TrpcHttpGateway - routes: - - id: "serviceA" - uri: "http://127.0.0.1:8091" - predicates: - - Path=/cgi-bin/** - filters: - - RewritePath=/cgi-bin/ , / - - PreserveHostHeader - - id: "serviceB" - uri: "trpc://testTrpcClient/trpc.test.demo.Hello/SayHello" - predicates: - - Path=/trpc/** - filters: - - name: Trpc - args: - metadata: "valueB" - - id: "serviceC" - uri: "trpc://testTrpcClient/trpc.test.demo.Hello/SayHello" - predicates: - - Path=/trpc1/** - filters: - - name: Trpc - args: - metadata: "valueC" - responseRewriter: "com.tencent.trpc.spring.cloud.gateway.rewriter.DefaultTrpcRequestRewriter" - -trpc: - client: - service: - - name: testHttpClient - interface: com.tencent.trpc.core.rpc.GenericClient - naming_url: ip://127.0.0.1:8091 - protocol: http - transporter: jetty - serialization: json - - name: testTrpcClient - interface: com.tencent.trpc.core.rpc.GenericClient - naming_url: ip://127.0.0.1:8090 # polaris://trpc.trpc.test.demo.Hello.TestPolarisTRPC - protocol: trpc - serialization: json \ No newline at end of file diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/TrpcGatewayApplication.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TestSpringApplication.java similarity index 62% rename from trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/TrpcGatewayApplication.java rename to trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TestSpringApplication.java index 106330fc0c..fcb649f9a2 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/main/java/com/tencent/trpc/spring/cloud/gateway/TrpcGatewayApplication.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TestSpringApplication.java @@ -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, @@ -9,18 +9,19 @@ * A copy of the Apache 2.0 License can be found in the LICENSE file. */ -package com.tencent.trpc.spring.cloud.gateway; +package com.tencent.trpc.spring.cloud.gateway.filter; +import com.tencent.trpc.spring.boot.starters.annotation.EnableTRpc; import com.tencent.trpc.spring.cloud.gateway.autoconfigure.EnableTrpcRouting; +import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.boot.builder.SpringApplicationBuilder; @EnableTrpcRouting +@EnableTRpc @SpringBootApplication -public class TrpcGatewayApplication { +public class TestSpringApplication { public static void main(String[] args) { - new SpringApplicationBuilder().sources(TrpcGatewayApplication.class).run(args); + SpringApplication.run(TestSpringApplication.class, args); } - -} \ No newline at end of file +} diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java index b1d0eb482e..8c6e0fea6c 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/java/com/tencent/trpc/spring/cloud/gateway/filter/TrpcRoutingFilterTest.java @@ -14,7 +14,6 @@ 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; import java.util.concurrent.TimeUnit; import okhttp3.MediaType; @@ -25,23 +24,22 @@ import okhttp3.ResponseBody; import org.json.JSONException; import org.json.JSONObject; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.springframework.boot.builder.SpringApplicationBuilder; +import org.springframework.boot.SpringApplication; import org.springframework.context.ConfigurableApplicationContext; public class TrpcRoutingFilterTest { - private final String requestBody = "{\"msg\":\"hello gateway!\",\"id\":\"\"}"; + private static final String REQUEST_BODY = "{\"msg\":\"hello gateway!\",\"id\":\"\"}"; + private static ConfigurableApplicationContext application; + private static OkHttpClient httpClient; - private ConfigurableApplicationContext application; - private OkHttpClient httpClient; - - @BeforeEach - void setUp() throws InterruptedException { - application = new SpringApplicationBuilder().sources(TrpcGatewayApplication.class).run(new String[0]); - TimeUnit.SECONDS.sleep(5); + @BeforeAll + static void setUp() throws InterruptedException { + application = SpringApplication.run(TestSpringApplication.class); + Thread.sleep(3000); httpClient = new OkHttpClient().newBuilder() .readTimeout(10, TimeUnit.SECONDS) @@ -50,37 +48,25 @@ void setUp() throws InterruptedException { .build(); } - @AfterEach - void tearDown() { + @AfterAll + static void tearDown() { if (application != null) { application.close(); } } @Test - void filter() { - trpcTest(); - httpTest(); - } - - private void httpTest() { - try { - JSONObject response = gateway(getHttpRequest()); - assertNotNull(response); - assertEquals(requestBody, response.toString()); - } catch (JSONException | IOException e) { - throw new AssertionError("httpTest failed", e); - } + void httpTest() throws JSONException, IOException { + JSONObject response = gateway(getHttpRequest()); + assertNotNull(response); + assertEquals(REQUEST_BODY, response.toString()); } - private void trpcTest() { - try { - JSONObject response = gateway(getTRPCRequest(requestBody)); - assertNotNull(response); - assertEquals(requestBody, response.toString()); - } catch (JSONException | IOException e) { - throw new AssertionError("trpcTest failed", e); - } + @Test + void trpcTest() throws JSONException, IOException { + JSONObject response = gateway(getTRPCRequest(REQUEST_BODY)); + assertNotNull(response); + assertEquals(REQUEST_BODY, response.toString()); } private JSONObject gateway(Request httpRequest) throws JSONException, IOException { diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/application.yml b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/application.yml index 4731627a82..159db37083 100644 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/application.yml +++ b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/application.yml @@ -41,6 +41,21 @@ spring: responseRewriter: "com.tencent.trpc.spring.cloud.gateway.rewriter.DefaultTrpcRequestRewriter" trpc: + server: + local_ip: 127.0.0.1 + service: + - name: Hello + impls: + - com.tencent.trpc.spring.cloud.gateway.filter.test.server.HelloServiceImpl + port: 8090 + - name: HelloHttpService + impls: + - com.tencent.trpc.spring.cloud.gateway.filter.test.server.HelloServiceImpl + port: 8091 + transporter: jetty + protocol: http + base_path: /test + serialization: json client: service: - name: testHttpClient diff --git a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/trpc_java.yaml b/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/trpc_java.yaml deleted file mode 100644 index c8c640e991..0000000000 --- a/trpc-spring-support/trpc-spring-cloud-gateway/src/test/resources/trpc_java.yaml +++ /dev/null @@ -1,15 +0,0 @@ -server: - local_ip: 127.0.0.1 - service: - - name: Hello - impls: - - com.tencent.trpc.spring.cloud.gateway.filter.test.server.HelloServiceImpl - port: 8090 - - name: HelloHttpService - impls: - - com.tencent.trpc.spring.cloud.gateway.filter.test.server.HelloServiceImpl - port: 8091 - transporter: jetty - protocol: http - base_path: /test - serialization: json \ No newline at end of file