From 27e6838c3bcef807950d0fce19c8b6a7321103f6 Mon Sep 17 00:00:00 2001 From: carlvine500 Date: Wed, 28 Jun 2023 20:30:45 +0800 Subject: [PATCH] add spring boot interceptor to copy header(dubbo-tag)/cookie(dubbo.tag) (#12514) * add spring boot interceptor to copy header(dubbo-tag)/cookie(dubbo.tag) * copy dubbo.tag in urlParameter * copy dubbo.tag in urlParameter * remove dubbo.tag in interceptor * add dubbo-spring-boot-interceptor to dubbo-dependencies-all/.artifacts/dubbo-bom --------- Co-authored-by: liutingfeng Co-authored-by: songxiaosheng Co-authored-by: Albumen Kevin --- .artifacts | 1 + .../common/constants/CommonConstants.java | 2 + dubbo-distribution/dubbo-bom/pom.xml | 5 ++ .../dubbo-spring-boot-interceptor/README.md | 31 +++++++++++ .../dubbo-spring-boot-interceptor/pom.xml | 53 +++++++++++++++++++ .../DubboTagCookieInterceptor.java | 53 +++++++++++++++++++ .../DubboTagHeaderOrParameterInterceptor.java | 45 ++++++++++++++++ dubbo-spring-boot/pom.xml | 1 + dubbo-test/dubbo-dependencies-all/pom.xml | 5 ++ 9 files changed, 196 insertions(+) create mode 100644 dubbo-spring-boot/dubbo-spring-boot-interceptor/README.md create mode 100644 dubbo-spring-boot/dubbo-spring-boot-interceptor/pom.xml create mode 100644 dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagCookieInterceptor.java create mode 100644 dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagHeaderOrParameterInterceptor.java diff --git a/.artifacts b/.artifacts index 6467d70a70..70701e2ce3 100644 --- a/.artifacts +++ b/.artifacts @@ -110,6 +110,7 @@ dubbo-spring-boot-tracing-otel-otlp-starter dubbo-spring-boot-observability-starter dubbo-spring-boot-starter dubbo-spring-boot-starters +dubbo-spring-boot-interceptor dubbo-nacos-spring-boot-starter dubbo-zookeeper-spring-boot-starter dubbo-zookeeper-curator5-spring-boot-starter diff --git a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java index 3642089f63..780dd0fad0 100644 --- a/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java +++ b/dubbo-common/src/main/java/org/apache/dubbo/common/constants/CommonConstants.java @@ -632,6 +632,8 @@ public interface CommonConstants { String DUBBO_PACKABLE_METHOD_FACTORY = "dubbo.application.parameters." + PACKABLE_METHOD_FACTORY_KEY; + String DUBBO_TAG_HEADER = "dubbo-tag"; + String REST_SERVICE_DEPLOYER_URL_ATTRIBUTE_KEY = "restServiceDeployerAttributeKey"; } diff --git a/dubbo-distribution/dubbo-bom/pom.xml b/dubbo-distribution/dubbo-bom/pom.xml index 379d40096a..967c6143a3 100644 --- a/dubbo-distribution/dubbo-bom/pom.xml +++ b/dubbo-distribution/dubbo-bom/pom.xml @@ -495,6 +495,11 @@ dubbo-spring-boot-starter ${project.version} + + org.apache.dubbo + dubbo-spring-boot-interceptor + ${project.version} + org.apache.dubbo dubbo-dependencies-zookeeper diff --git a/dubbo-spring-boot/dubbo-spring-boot-interceptor/README.md b/dubbo-spring-boot/dubbo-spring-boot-interceptor/README.md new file mode 100644 index 0000000000..842ed8534f --- /dev/null +++ b/dubbo-spring-boot/dubbo-spring-boot-interceptor/README.md @@ -0,0 +1,31 @@ +# Dubbo Spring Boot interceptor + +`dubbo-spring-boot-interceptor` copy the header(dubbo-tag)/cookie(dubbo.tag)/urlParameter(dubbo.tag) to dubbo . + + + + +## Integrate with spring boot + +### copy the header(dubbo-tag=tagx)/urlParameter(dubbo.tag=tagx) to dubbo +if there is no header(dubbo-tag) , downgrade use urlParameter(dubbo.tag) +``` +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new DubboTagHeaderOrParameterInterceptor()).addPathPatterns("/*").excludePathPatterns("/admin"); + } +} +``` +### copy the cookie(dubbo.tag=tagx) to dubbo +``` +@Configuration +public class WebMvcConfig implements WebMvcConfigurer { + @Override + public void addInterceptors(InterceptorRegistry registry) { + registry.addInterceptor(new DubboTagCookieInterceptor()).addPathPatterns("/*").excludePathPatterns("/admin"); + } +} +``` + diff --git a/dubbo-spring-boot/dubbo-spring-boot-interceptor/pom.xml b/dubbo-spring-boot/dubbo-spring-boot-interceptor/pom.xml new file mode 100644 index 0000000000..e097175e7e --- /dev/null +++ b/dubbo-spring-boot/dubbo-spring-boot-interceptor/pom.xml @@ -0,0 +1,53 @@ + + + + + org.apache.dubbo + dubbo-spring-boot + ${revision} + ../pom.xml + + jar + 4.0.0 + dubbo-spring-boot-interceptor + Apache Dubbo Spring Boot Interceptor + + + + + org.apache.dubbo + dubbo + ${project.version} + + + org.apache.dubbo + dubbo-common + ${project.version} + true + + + + org.springframework.boot + spring-boot-starter-web + true + + + + + diff --git a/dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagCookieInterceptor.java b/dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagCookieInterceptor.java new file mode 100644 index 0000000000..ad91025c5f --- /dev/null +++ b/dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagCookieInterceptor.java @@ -0,0 +1,53 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.dubbo.spring.boot.interceptor; + +import org.apache.dubbo.common.constants.CommonConstants; +import org.apache.dubbo.rpc.RpcContext; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class DubboTagCookieInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String tag = getSingleCookieValue(request.getCookies(), CommonConstants.TAG_KEY); + RpcContext.getClientAttachment().setAttachment(CommonConstants.TAG_KEY,tag); + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + RpcContext.getClientAttachment().removeAttachment(CommonConstants.TAG_KEY); + } + + private static String getSingleCookieValue(Cookie[] cookies, String name){ + if (cookies == null || cookies.length == 0) { + return null; + } + for (Cookie cookie: cookies) { + if (name.equals(cookie.getName())) { + return cookie.getValue(); + } + } + return null; + } + +} diff --git a/dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagHeaderOrParameterInterceptor.java b/dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagHeaderOrParameterInterceptor.java new file mode 100644 index 0000000000..df6a614f4e --- /dev/null +++ b/dubbo-spring-boot/dubbo-spring-boot-interceptor/src/main/java/org/apache/dubbo/spring/boot/interceptor/DubboTagHeaderOrParameterInterceptor.java @@ -0,0 +1,45 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * 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 org.apache.dubbo.spring.boot.interceptor; + +import org.apache.dubbo.common.constants.CommonConstants; +import org.apache.dubbo.rpc.RpcContext; +import org.springframework.web.servlet.HandlerInterceptor; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +public class DubboTagHeaderOrParameterInterceptor implements HandlerInterceptor { + + @Override + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { + String tag = request.getHeader(CommonConstants.DUBBO_TAG_HEADER); + if (tag == null) { + tag = request.getParameter(CommonConstants.TAG_KEY); + } + if (tag != null) { + RpcContext.getClientAttachment().setAttachment(CommonConstants.TAG_KEY, tag); + } + return true; + } + + @Override + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception { + RpcContext.getClientAttachment().removeAttachment(CommonConstants.TAG_KEY); + } + +} diff --git a/dubbo-spring-boot/pom.xml b/dubbo-spring-boot/pom.xml index 17d648e32f..4b24b5671c 100644 --- a/dubbo-spring-boot/pom.xml +++ b/dubbo-spring-boot/pom.xml @@ -37,6 +37,7 @@ dubbo-spring-boot-compatible dubbo-spring-boot-starter dubbo-spring-boot-starters + dubbo-spring-boot-interceptor diff --git a/dubbo-test/dubbo-dependencies-all/pom.xml b/dubbo-test/dubbo-dependencies-all/pom.xml index bc255a4221..3fbd8d38a1 100644 --- a/dubbo-test/dubbo-dependencies-all/pom.xml +++ b/dubbo-test/dubbo-dependencies-all/pom.xml @@ -391,6 +391,11 @@ dubbo-spring-boot-starter ${project.version} + + org.apache.dubbo + dubbo-spring-boot-interceptor + ${project.version} + org.apache.dubbo