Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
@@ -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,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})
Expand Down

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,26 +1,27 @@
/*
* 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,
* please note that tRPC source code is licensed under the Apache 2.0 License,
* 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);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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)
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

This file was deleted.

Loading