This is an automated email from the ASF dual-hosted git repository.
albumenj pushed a commit to branch 3.3
in repository https://gitbox.apache.org/repos/asf/dubbo.git
The following commit(s) were added to refs/heads/3.3 by this push:
new 27e6838c3b add spring boot interceptor to copy
header(dubbo-tag)/cookie(dubbo.tag) (#12514)
27e6838c3b is described below
commit 27e6838c3bcef807950d0fce19c8b6a7321103f6
Author: carlvine500 <[email protected]>
AuthorDate: Wed Jun 28 20:30:45 2023 +0800
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 <[email protected]>
Co-authored-by: songxiaosheng <[email protected]>
Co-authored-by: Albumen Kevin <[email protected]>
---
.artifacts | 1 +
.../dubbo/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 ++++++++++++++++++++++
.../interceptor/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(+)
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 @@
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-spring-boot-interceptor</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>
<artifactId>dubbo-dependencies-zookeeper</artifactId>
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 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ 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.
+ -->
+<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
+ <parent>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-spring-boot</artifactId>
+ <version>${revision}</version>
+ <relativePath>../pom.xml</relativePath>
+ </parent>
+ <packaging>jar</packaging>
+ <modelVersion>4.0.0</modelVersion>
+ <artifactId>dubbo-spring-boot-interceptor</artifactId>
+ <description>Apache Dubbo Spring Boot Interceptor</description>
+
+ <dependencies>
+ <!-- Dubbo -->
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo</artifactId>
+ <version>${project.version}</version>
+ </dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-common</artifactId>
+ <version>${project.version}</version>
+ <optional>true</optional>
+ </dependency>
+ <!-- spring -->
+ <dependency>
+ <groupId>org.springframework.boot</groupId>
+ <artifactId>spring-boot-starter-web</artifactId>
+ <optional>true</optional>
+ </dependency>
+ </dependencies>
+
+
+</project>
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 @@
<module>dubbo-spring-boot-compatible</module>
<module>dubbo-spring-boot-starter</module>
<module>dubbo-spring-boot-starters</module>
+ <module>dubbo-spring-boot-interceptor</module>
</modules>
<properties>
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 @@
<artifactId>dubbo-spring-boot-starter</artifactId>
<version>${project.version}</version>
</dependency>
+ <dependency>
+ <groupId>org.apache.dubbo</groupId>
+ <artifactId>dubbo-spring-boot-interceptor</artifactId>
+ <version>${project.version}</version>
+ </dependency>
<dependency>
<groupId>org.apache.dubbo</groupId>