From 73793a506c409c964ce95becc54ec55d8da2758c Mon Sep 17 00:00:00 2001 From: SkyeBeFreeman <928016560@qq.com> Date: Wed, 18 Aug 2021 16:37:11 +0800 Subject: [PATCH] feat:support gateway metadata delivery --- spring-cloud-tencent-dependencies/pom.xml | 6 ++ .../gateway-callee-service/pom.xml | 27 +++++++ .../callee/GatewayCalleeApplication.java | 33 ++++++++ .../callee/GatewayCalleeController.java | 62 +++++++++++++++ .../src/main/resources/application.yaml | 9 +++ .../gateway-scg-service/pom.xml | 32 ++++++++ .../example/scg/GatewayScgApplication.java | 33 ++++++++ .../src/main/resources/application.yml | 31 ++++++++ .../gateway-zuul-service/pom.xml | 38 +++++++++ .../example/zuul/GatewayZuulApplication.java | 35 ++++++++ .../src/main/resources/application.yaml | 20 +++++ .../polaris-gateway-example/pom.xml | 70 ++++++++++++++++ spring-cloud-tencent-examples/pom.xml | 1 + spring-cloud-tencent-starters/pom.xml | 1 + .../pom.xml | 1 + .../config/MetadataConfiguration.java | 4 +- .../core/filter/MetadataReactiveFilter.java | 7 -- .../core/filter/MetadataServletFilter.java | 7 -- .../cloud/metadata/util/JacksonUtils.java | 2 +- .../filter/MetadataReactiveFilterTest.java | 9 +-- .../filter/MetadataServletFilterTest.java | 5 +- .../pom.xml | 42 ++++++++++ .../PolarisGatewayAutoConfiguration.java | 55 +++++++++++++ .../core/scg/filter/AbstractGlobalFilter.java | 49 ++++++++++++ .../core/scg/filter/MetadataScgFilter.java | 77 ++++++++++++++++++ .../core/zuul/filter/MetadataZuulFilter.java | 79 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 2 + 27 files changed, 709 insertions(+), 28 deletions(-) create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/resources/application.yaml create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/java/com/tencent/cloud/polaris/gateway/example/scg/GatewayScgApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/application.yml create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yaml create mode 100644 spring-cloud-tencent-examples/polaris-gateway-example/pom.xml create mode 100644 spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml create mode 100644 spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java create mode 100644 spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/AbstractGlobalFilter.java create mode 100644 spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/MetadataScgFilter.java create mode 100644 spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataZuulFilter.java create mode 100644 spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/resources/META-INF/spring.factories diff --git a/spring-cloud-tencent-dependencies/pom.xml b/spring-cloud-tencent-dependencies/pom.xml index 468c9e36..a90bf71e 100644 --- a/spring-cloud-tencent-dependencies/pom.xml +++ b/spring-cloud-tencent-dependencies/pom.xml @@ -121,6 +121,12 @@ ${revision} + + com.tencent.cloud + spring-cloud-tencent-polaris-gateway + ${revision} + + com.tencent.cloud spring-cloud-tencent-metadata diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/pom.xml new file mode 100644 index 00000000..c2198299 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/pom.xml @@ -0,0 +1,27 @@ + + + + polaris-gateway-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + gateway-callee-service + Spring Cloud Starter Tencent Polaris Gateway Callee Example + + + + spring-cloud-starter-tencent-polaris-discovery + com.tencent.cloud + + + + org.springframework.boot + spring-boot-starter-web + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeApplication.java b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeApplication.java new file mode 100644 index 00000000..de8dae89 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeApplication.java @@ -0,0 +1,33 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.example.callee; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author Haotian Zhang + */ +@SpringBootApplication +public class GatewayCalleeApplication { + + public static void main(String[] args) { + SpringApplication.run(GatewayCalleeApplication.class, args); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java new file mode 100644 index 00000000..6356ef26 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/java/com/tencent/cloud/polaris/gateway/example/callee/GatewayCalleeController.java @@ -0,0 +1,62 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.example.callee; + +import com.tencent.cloud.metadata.constant.MetadataConstant; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.RequestHeader; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.io.UnsupportedEncodingException; +import java.net.URLDecoder; + +/** + * @author Haotian Zhang + */ +@RestController +@RequestMapping("/gateway/example/callee") +public class GatewayCalleeController { + + private static Logger logger = LoggerFactory.getLogger(GatewayCalleeController.class); + + /** + * Get info string + * + * @return 返回服务信息 + */ + @RequestMapping("/info") + public String info() { + return "Gateway Example Callee"; + } + + /** + * Get metadata in HTTP header + * + * @param metadataStr + * @return + * @throws UnsupportedEncodingException + */ + @RequestMapping("/echo") + public String echoHeader(@RequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA) String metadataStr) throws UnsupportedEncodingException { + logger.info(URLDecoder.decode(metadataStr, "UTF-8")); + return URLDecoder.decode(metadataStr, "UTF-8"); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/resources/application.yaml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/resources/application.yaml new file mode 100644 index 00000000..e93c697e --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-callee-service/src/main/resources/application.yaml @@ -0,0 +1,9 @@ +server: + session-timeout: 1800 + port: 48081 +spring: + application: + name: GatewayCalleeService + cloud: + polaris: + address: grpc://127.0.0.1:8081 \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml new file mode 100644 index 00000000..71183a5b --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/pom.xml @@ -0,0 +1,32 @@ + + + + polaris-gateway-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + gateway-scg-service + Spring Cloud Starter Tencent Polaris Gateway SCG Example + + + + spring-cloud-starter-tencent-polaris-discovery + com.tencent.cloud + + + + com.tencent.cloud + spring-cloud-tencent-polaris-gateway + + + + org.springframework.cloud + spring-cloud-starter-gateway + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/java/com/tencent/cloud/polaris/gateway/example/scg/GatewayScgApplication.java b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/java/com/tencent/cloud/polaris/gateway/example/scg/GatewayScgApplication.java new file mode 100644 index 00000000..e397463f --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/java/com/tencent/cloud/polaris/gateway/example/scg/GatewayScgApplication.java @@ -0,0 +1,33 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.example.scg; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +/** + * @author Haotian Zhang + */ +@SpringBootApplication +public class GatewayScgApplication { + + public static void main(String[] args) { + SpringApplication.run(GatewayScgApplication.class, args); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/application.yml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/application.yml new file mode 100644 index 00000000..7809bb23 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-scg-service/src/main/resources/application.yml @@ -0,0 +1,31 @@ +server: + session-timeout: 1800 + port: 48083 +spring: + application: + name: GatewayScgService + cloud: + polaris: + address: grpc://127.0.0.1:8081 + tencent: + metadata: + content: + a: 1 + transitive: + - a + gateway: + discovery: + locator: + enabled: true + lowerCaseServiceId: false + routes: + - id: GatewayCalleeService + uri: lb://GatewayCalleeService + predicates: + - Path=/GatewayCalleeService/** + filters: + - StripPrefix=1 + +logging: + level: + org.springframework.cloud.gateway: trace diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml new file mode 100644 index 00000000..0c149c8d --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/pom.xml @@ -0,0 +1,38 @@ + + + + polaris-gateway-example + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + gateway-zuul-service + Spring Cloud Starter Tencent Polaris Gateway Zuul Example + + + + spring-cloud-starter-tencent-polaris-discovery + com.tencent.cloud + + + + com.tencent.cloud + spring-cloud-tencent-polaris-gateway + + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java new file mode 100644 index 00000000..b748b986 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/java/com/tencent/cloud/polaris/gateway/example/zuul/GatewayZuulApplication.java @@ -0,0 +1,35 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.example.zuul; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.cloud.netflix.zuul.EnableZuulProxy; + +/** + * @author Haotian Zhang + */ +@SpringBootApplication +@EnableZuulProxy +public class GatewayZuulApplication { + + public static void main(String[] args) { + SpringApplication.run(GatewayZuulApplication.class, args); + } + +} diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yaml b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yaml new file mode 100644 index 00000000..bff70581 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/gateway-zuul-service/src/main/resources/application.yaml @@ -0,0 +1,20 @@ +server: + session-timeout: 1800 + port: 48082 +spring: + application: + name: GatewayZuulService + cloud: + polaris: + address: grpc://127.0.0.1:8081 + tencent: + metadata: + content: + a: 1 + transitive: + - a +zuul: + routes: + GatewayCalleeService: + serviceId: GatewayCalleeService + path: /GatewayCalleeService/** diff --git a/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml b/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml new file mode 100644 index 00000000..073bd0e6 --- /dev/null +++ b/spring-cloud-tencent-examples/polaris-gateway-example/pom.xml @@ -0,0 +1,70 @@ + + + + spring-cloud-tencent-examples + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + polaris-gateway-example + pom + Spring Cloud Starter Tencent Polaris Gateway Example + + + gateway-zuul-service + gateway-scg-service + gateway-callee-service + + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + org.springframework.cloud + spring-cloud-starter-openfeign + + + + + org.springframework.cloud + spring-cloud-starter-netflix-hystrix + + + + + org.springframework.cloud + spring-cloud-starter-netflix-ribbon + + + + + org.springframework.retry + spring-retry + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + repackage + + + + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-examples/pom.xml b/spring-cloud-tencent-examples/pom.xml index d2d05641..f198c04c 100644 --- a/spring-cloud-tencent-examples/pom.xml +++ b/spring-cloud-tencent-examples/pom.xml @@ -19,6 +19,7 @@ polaris-discovery-example polaris-ratelimit-example polaris-circuitbreaker-example + polaris-gateway-example diff --git a/spring-cloud-tencent-starters/pom.xml b/spring-cloud-tencent-starters/pom.xml index 9ae3ce3d..6b6325b5 100644 --- a/spring-cloud-tencent-starters/pom.xml +++ b/spring-cloud-tencent-starters/pom.xml @@ -24,6 +24,7 @@ spring-cloud-starter-tencent-polaris-ratelimit spring-cloud-starter-tencent-polaris-circuitbreaker spring-cloud-starter-tencent-polaris-router + spring-cloud-tencent-polaris-gateway diff --git a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml index bb58b906..5072ede2 100644 --- a/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml +++ b/spring-cloud-tencent-starters/spring-cloud-starter-tencent-polaris-discovery/pom.xml @@ -1,3 +1,4 @@ + diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/config/MetadataConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/config/MetadataConfiguration.java index ef4bc2eb..eb6e4c3f 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/config/MetadataConfiguration.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/config/MetadataConfiguration.java @@ -81,7 +81,7 @@ public class MetadataConfiguration { @Bean public MetadataServletFilter metadataServletFilter(MetadataLocalProperties metadataLocalProperties) { - return new MetadataServletFilter(metadataLocalProperties); + return new MetadataServletFilter(); } } @@ -94,7 +94,7 @@ public class MetadataConfiguration { @Bean public MetadataReactiveFilter metadataReactiveFilter(MetadataLocalProperties metadataLocalProperties) { - return new MetadataReactiveFilter(metadataLocalProperties); + return new MetadataReactiveFilter(); } } diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilter.java index a4c50e51..12b9f6f6 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilter.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilter.java @@ -17,7 +17,6 @@ package com.tencent.cloud.metadata.core.filter; -import com.tencent.cloud.metadata.config.MetadataLocalProperties; import com.tencent.cloud.metadata.constant.MetadataConstant; import com.tencent.cloud.metadata.context.MetadataContextHolder; import com.tencent.cloud.metadata.util.JacksonUtils; @@ -50,12 +49,6 @@ public class MetadataReactiveFilter implements WebFilter, Ordered { private static final Logger LOG = LoggerFactory.getLogger(MetadataReactiveFilter.class); - private MetadataLocalProperties metadataLocalProperties; - - public MetadataReactiveFilter(MetadataLocalProperties metadataLocalProperties) { - this.metadataLocalProperties = metadataLocalProperties; - } - @Override public int getOrder() { return MetadataConstant.OrderConstant.FILTER_ORDER; diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilter.java index f6953cb6..a478bb17 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilter.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilter.java @@ -19,7 +19,6 @@ package com.tencent.cloud.metadata.core.filter; import com.tencent.cloud.metadata.constant.MetadataConstant; import com.tencent.cloud.metadata.context.MetadataContextHolder; -import com.tencent.cloud.metadata.config.MetadataLocalProperties; import com.tencent.cloud.metadata.util.JacksonUtils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -49,12 +48,6 @@ public class MetadataServletFilter extends OncePerRequestFilter { private static final Logger LOG = LoggerFactory.getLogger(MetadataServletFilter.class); - private MetadataLocalProperties metadataLocalProperties; - - public MetadataServletFilter(MetadataLocalProperties metadataLocalProperties) { - this.metadataLocalProperties = metadataLocalProperties; - } - @Override protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/util/JacksonUtils.java b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/util/JacksonUtils.java index 010c893e..14cfe468 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/util/JacksonUtils.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/main/java/com/tencent/cloud/metadata/util/JacksonUtils.java @@ -67,7 +67,7 @@ public class JacksonUtils { } return new HashMap<>(); } catch (JsonProcessingException e) { - LOG.error("Json to map failed. {}", jsonStr, e); + LOG.error("Json to map failed. check if the format of the json string[{}] is correct.", jsonStr, e); throw new RuntimeException("Json to map failed.", e); } } diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilterTest.java b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilterTest.java index 2b43ff05..75f6de1f 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilterTest.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataReactiveFilterTest.java @@ -52,7 +52,7 @@ public class MetadataReactiveFilterTest { @Before public void setUp() { - this.metadataReactiveFilter = new MetadataReactiveFilter(this.metadataLocalProperties); + this.metadataReactiveFilter = new MetadataReactiveFilter(); } @Test @@ -64,12 +64,7 @@ public class MetadataReactiveFilterTest { @Test public void test2() { // Create mock WebFilterChain - WebFilterChain webFilterChain = new WebFilterChain() { - @Override - public Mono filter(ServerWebExchange serverWebExchange) { - return Mono.empty(); - } - }; + WebFilterChain webFilterChain = serverWebExchange -> Mono.empty(); // Mock request MockServerHttpRequest request = MockServerHttpRequest diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilterTest.java b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilterTest.java index 945ff4c3..d50174d5 100644 --- a/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilterTest.java +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-metadata/src/test/java/com/tencent/cloud/metadata/core/filter/MetadataServletFilterTest.java @@ -57,11 +57,8 @@ public class MetadataServletFilterTest { @Test public void test1() throws ServletException, IOException { // Create mock FilterChain - FilterChain filterChain = new FilterChain() { - @Override - public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse) { + FilterChain filterChain = (servletRequest, servletResponse) -> { - } }; // Mock request diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml new file mode 100644 index 00000000..c08a48ef --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/pom.xml @@ -0,0 +1,42 @@ + + + + spring-cloud-tencent-starters + com.tencent.cloud + ${revision} + ../pom.xml + + 4.0.0 + + spring-cloud-tencent-polaris-gateway + Spring Cloud Tencent Polaris Gateway + + + + + com.tencent.cloud + spring-cloud-tencent-commons + + + + com.tencent.cloud + spring-cloud-tencent-metadata + + + + + org.springframework.cloud + spring-cloud-starter-netflix-zuul + true + + + + org.springframework.cloud + spring-cloud-starter-gateway + true + + + + \ No newline at end of file diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java new file mode 100644 index 00000000..63e54b8e --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/config/PolarisGatewayAutoConfiguration.java @@ -0,0 +1,55 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.config; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.http.ZuulServlet; +import com.tencent.cloud.polaris.gateway.core.scg.filter.MetadataScgFilter; +import com.tencent.cloud.polaris.gateway.core.zuul.filter.MetadataZuulFilter; +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * Polaris Gateway Auto Configuration + * + * @author skyehtzhang + */ +@Configuration +public class PolarisGatewayAutoConfiguration { + + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(ZuulServlet.class) + static class PolarisGatewayZuulAutoConfiguration { + @Bean + public ZuulFilter metadataZuulFilter() { + return new MetadataZuulFilter(); + } + } + + @Configuration(proxyBeanMethods = false) + @ConditionalOnClass(GlobalFilter.class) + static class PolarisGatewayScgAutoConfiguration { + @Bean + public GlobalFilter metadataScgFilter() { + return new MetadataScgFilter(); + } + } + +} diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/AbstractGlobalFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/AbstractGlobalFilter.java new file mode 100644 index 00000000..bb47812a --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/AbstractGlobalFilter.java @@ -0,0 +1,49 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.core.scg.filter; + +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.cloud.gateway.filter.GlobalFilter; +import org.springframework.core.Ordered; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +/** + * Abstract Global filter for ordering and checking if shouldFilter. + * + * @author Haotian Zhang + */ +public abstract class AbstractGlobalFilter implements GlobalFilter, Ordered { + + @Override + public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { + if (shouldFilter(exchange, chain)) { + return doFilter(exchange, chain); + } else { + return chain.filter(exchange); + } + } + + @Override + abstract public int getOrder(); + + abstract public boolean shouldFilter(ServerWebExchange exchange, GatewayFilterChain chain); + + abstract public Mono doFilter(ServerWebExchange exchange, GatewayFilterChain chain); +} + diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/MetadataScgFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/MetadataScgFilter.java new file mode 100644 index 00000000..08c48420 --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/scg/filter/MetadataScgFilter.java @@ -0,0 +1,77 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.core.scg.filter; + +import com.tencent.cloud.metadata.constant.MetadataConstant; +import com.tencent.cloud.metadata.context.MetadataContext; +import com.tencent.cloud.metadata.context.MetadataContextHolder; +import com.tencent.cloud.metadata.util.JacksonUtils; +import org.springframework.cloud.gateway.filter.GatewayFilterChain; +import org.springframework.http.server.reactive.ServerHttpRequest; +import org.springframework.util.CollectionUtils; +import org.springframework.web.server.ServerWebExchange; +import reactor.core.publisher.Mono; + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.Map; + +/** + * Scg filter used for writing metadata in HTTP request header. + * + * @author Haotian Zhang + */ +public class MetadataScgFilter extends AbstractGlobalFilter { + + private static final int METADATA_SCG_FILTER_ORDER = 10151; + + @Override + public int getOrder() { + return METADATA_SCG_FILTER_ORDER; + } + + @Override + public boolean shouldFilter(ServerWebExchange exchange, GatewayFilterChain chain) { + return true; + } + + @Override + public Mono doFilter(ServerWebExchange exchange, GatewayFilterChain chain) { + // get request builder + ServerHttpRequest.Builder builder = exchange.getRequest().mutate(); + + // get metadata of current thread + MetadataContext metadataContext = exchange.getAttribute(MetadataConstant.HeaderName.METADATA_CONTEXT); + + // add new metadata and cover old + if (metadataContext == null) { + metadataContext = MetadataContextHolder.get(); + } + Map customMetadata = metadataContext.getAllTransitiveCustomMetadata(); + if (!CollectionUtils.isEmpty(customMetadata)) { + String metadataStr = JacksonUtils.serializeToJson(customMetadata); + try { + builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, URLEncoder.encode(metadataStr, "UTF-8")); + } catch (UnsupportedEncodingException e) { + builder.header(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr); + } + } + + return chain.filter(exchange.mutate().request(builder.build()).build()); + } +} diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataZuulFilter.java b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataZuulFilter.java new file mode 100644 index 00000000..727f7a58 --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/java/com/tencent/cloud/polaris/gateway/core/zuul/filter/MetadataZuulFilter.java @@ -0,0 +1,79 @@ +/* + * Tencent is pleased to support the open source community by making Spring Cloud Tencent available. + * + * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved. + * + * Licensed under the BSD 3-Clause License (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://opensource.org/licenses/BSD-3-Clause + * + * Unless required by applicable law or agreed to in writing, software distributed + * under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR + * CONDITIONS OF ANY KIND, either express or implied. See the License for the + * specific language governing permissions and limitations under the License. + */ + +package com.tencent.cloud.polaris.gateway.core.zuul.filter; + +import com.netflix.zuul.ZuulFilter; +import com.netflix.zuul.context.RequestContext; +import com.tencent.cloud.metadata.constant.MetadataConstant; +import com.tencent.cloud.metadata.context.MetadataContext; +import com.tencent.cloud.metadata.context.MetadataContextHolder; +import com.tencent.cloud.metadata.util.JacksonUtils; +import org.springframework.util.CollectionUtils; + + +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; +import java.util.Map; + +import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.RIBBON_ROUTING_FILTER_ORDER; +import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.ROUTE_TYPE; + +/** + * Zuul filter used for writing metadata in HTTP request header. + * + * @author skyehtzhang + */ +public class MetadataZuulFilter extends ZuulFilter { + + @Override + public String filterType() { + return ROUTE_TYPE; + } + + @Override + public int filterOrder() { + return RIBBON_ROUTING_FILTER_ORDER - 1; + } + + @Override + public boolean shouldFilter() { + return true; + } + + @Override + public Object run() { + // get request context + RequestContext requestContext = RequestContext.getCurrentContext(); + + // get metadata of current thread + MetadataContext metadataContext = MetadataContextHolder.get(); + + // add new metadata and cover old + Map customMetadata = metadataContext.getAllTransitiveCustomMetadata(); + if (!CollectionUtils.isEmpty(customMetadata)) { + String metadataStr = JacksonUtils.serializeToJson(customMetadata); + try { + requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, + URLEncoder.encode(metadataStr, "UTF-8")); + } catch (UnsupportedEncodingException e) { + requestContext.addZuulRequestHeader(MetadataConstant.HeaderName.CUSTOM_METADATA, metadataStr); + } + } + return null; + } +} diff --git a/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/resources/META-INF/spring.factories b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/resources/META-INF/spring.factories new file mode 100644 index 00000000..bc2e30e1 --- /dev/null +++ b/spring-cloud-tencent-starters/spring-cloud-tencent-polaris-gateway/src/main/resources/META-INF/spring.factories @@ -0,0 +1,2 @@ +org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + com.tencent.cloud.polaris.gateway.config.PolarisGatewayAutoConfiguration