From f08fe2417402706d6526e84e948397a5c719fe8e Mon Sep 17 00:00:00 2001 From: YunaiV Date: Sat, 11 Jun 2022 23:13:58 +0800 Subject: [PATCH] =?UTF-8?q?2.=20tenant=20=E7=BB=84=E4=BB=B6=EF=BC=9Afeign?= =?UTF-8?q?=20=E8=B0=83=E7=94=A8=E6=97=B6=EF=BC=8C=E9=80=9A=E8=BF=87=20hea?= =?UTF-8?q?der=20=E9=80=8F=E4=BC=A0=20Tenant=20=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- http-client.env.json | 1 + .../framework/common/enums/RpcConstants.java | 17 +++++++++++++ .../pom.xml | 7 ++++++ .../YudaoTenantRpcAutoConfiguration.java | 17 +++++++++++++ .../core/rpc/TenantRequestInterceptor.java | 25 +++++++++++++++++++ .../security/TenantSecurityWebFilter.java | 8 ++++++ .../core/web/TenantContextWebFilter.java | 2 -- .../main/resources/META-INF/spring.factories | 1 + .../pom.xml | 14 +++++------ .../core/rpc/LoginUserRequestInterceptor.java | 5 ++++ .../web/core/util/WebFrameworkUtils.java | 2 +- yudao-gateway/pom.xml | 4 +-- .../src/main/resources/application.yaml | 8 ++++++ .../module/infra/enums/ApiConstants.java | 4 ++- .../yudao-module-infra-biz/pom.xml | 4 +++ .../infra/framework/TmpConfiguration.java | 22 ++++++++++++---- .../module/system/enums/ApiConstants.java | 4 ++- 17 files changed, 126 insertions(+), 19 deletions(-) create mode 100644 yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/enums/RpcConstants.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantRpcAutoConfiguration.java create mode 100644 yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/rpc/TenantRequestInterceptor.java diff --git a/http-client.env.json b/http-client.env.json index a731bdb36..77c59303c 100644 --- a/http-client.env.json +++ b/http-client.env.json @@ -14,6 +14,7 @@ "gateway": { "baseUrl": "http://127.0.0.1:48080/admin-api", "systemBaseUrl": "http://127.0.0.1:48080/admin-api", + "infaBaseUrl": "http://127.0.0.1:48080/admin-api", "token": "test1", "adminTenentId": "1", diff --git a/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/enums/RpcConstants.java b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/enums/RpcConstants.java new file mode 100644 index 000000000..b1c53dbfe --- /dev/null +++ b/yudao-framework/yudao-common/src/main/java/cn/iocoder/yudao/framework/common/enums/RpcConstants.java @@ -0,0 +1,17 @@ +package cn.iocoder.yudao.framework.common.enums; + +/** + * RPC 相关的枚举 + * + * 虽然放在 yudao-spring-boot-starter-rpc 会相对合适,但是每个 API 模块需要使用到,所以暂时只好放在此处 + * + * @author 芋道源码 + */ +public class RpcConstants { + + /** + * RPC API 的前缀 + */ + public static final String RPC_API_PREFIX = "/rpc-api"; + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml b/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml index e6926cca2..634b323dc 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/pom.xml @@ -38,6 +38,13 @@ yudao-spring-boot-starter-redis + + + cn.iocoder.cloud + yudao-spring-boot-starter-rpc + true + + cn.iocoder.cloud diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantRpcAutoConfiguration.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantRpcAutoConfiguration.java new file mode 100644 index 000000000..cb3b3f073 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantRpcAutoConfiguration.java @@ -0,0 +1,17 @@ +package cn.iocoder.yudao.framework.tenant.config; + +import cn.iocoder.yudao.framework.tenant.core.rpc.TenantRequestInterceptor; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +@Configuration +@ConditionalOnProperty(prefix = "yudao.tenant", value = "enable", matchIfMissing = true) // 允许使用 yudao.tenant.enable=false 禁用多租户 +public class YudaoTenantRpcAutoConfiguration { + + @Bean + public TenantRequestInterceptor tenantRequestInterceptor() { + return new TenantRequestInterceptor(); + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/rpc/TenantRequestInterceptor.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/rpc/TenantRequestInterceptor.java new file mode 100644 index 000000000..ac82fb9f1 --- /dev/null +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/rpc/TenantRequestInterceptor.java @@ -0,0 +1,25 @@ +package cn.iocoder.yudao.framework.tenant.core.rpc; + +import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder; +import cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils; +import feign.RequestInterceptor; +import feign.RequestTemplate; + +import static cn.iocoder.yudao.framework.web.core.util.WebFrameworkUtils.HEADER_TENANT_ID; + +/** + * Tenant 的 RequestInterceptor 实现类:Feign 请求时,将 {@link TenantContextHolder} 设置到 header 中,继续透传给被调用的服务 + * + * @author 芋道源码 + */ +public class TenantRequestInterceptor implements RequestInterceptor { + + @Override + public void apply(RequestTemplate requestTemplate) { + Long tenantId = TenantContextHolder.getTenantId(); + if (tenantId != null) { + requestTemplate.header(HEADER_TENANT_ID, String.valueOf(tenantId)); + } + } + +} diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java index 9a047312c..7e1f1dc5a 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/security/TenantSecurityWebFilter.java @@ -1,6 +1,8 @@ package cn.iocoder.yudao.framework.tenant.core.security; import cn.hutool.core.collection.CollUtil; +import cn.hutool.core.util.StrUtil; +import cn.iocoder.yudao.framework.common.enums.RpcConstants; import cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants; import cn.iocoder.yudao.framework.common.pojo.CommonResult; import cn.iocoder.yudao.framework.common.util.servlet.ServletUtils; @@ -53,6 +55,12 @@ public class TenantSecurityWebFilter extends ApiRequestFilter { this.tenantFrameworkService = tenantFrameworkService; } + @Override + protected boolean shouldNotFilter(HttpServletRequest request) { + return super.shouldNotFilter(request) && + !StrUtil.startWithAny(request.getRequestURI(), RpcConstants.RPC_API_PREFIX); // 因为 RPC API 也会透传租户编号 + } + @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/web/TenantContextWebFilter.java b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/web/TenantContextWebFilter.java index 272419c09..8e37727d9 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/web/TenantContextWebFilter.java +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/core/web/TenantContextWebFilter.java @@ -18,8 +18,6 @@ import java.io.IOException; */ public class TenantContextWebFilter extends OncePerRequestFilter { - private static final String HEADER_TENANT_ID = "tenant-id"; - @Override protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws ServletException, IOException { diff --git a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/resources/META-INF/spring.factories b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/resources/META-INF/spring.factories index 95ab510a4..f8e45e6c8 100644 --- a/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/resources/META-INF/spring.factories +++ b/yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/resources/META-INF/spring.factories @@ -1,2 +1,3 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ + cn.iocoder.yudao.framework.tenant.config.YudaoTenantRpcAutoConfiguration,\ cn.iocoder.yudao.framework.tenant.config.YudaoTenantAutoConfiguration diff --git a/yudao-framework/yudao-spring-boot-starter-security/pom.xml b/yudao-framework/yudao-spring-boot-starter-security/pom.xml index e28424430..2cbae6d00 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/pom.xml +++ b/yudao-framework/yudao-spring-boot-starter-security/pom.xml @@ -44,19 +44,19 @@ spring-boot-starter-security - - - cn.iocoder.cloud - yudao-module-system-api - ${revision} - - cn.iocoder.cloud yudao-spring-boot-starter-rpc true + + + + cn.iocoder.cloud + yudao-module-system-api + ${revision} + diff --git a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/rpc/LoginUserRequestInterceptor.java b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/rpc/LoginUserRequestInterceptor.java index f855a17ca..cb6c33ccf 100644 --- a/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/rpc/LoginUserRequestInterceptor.java +++ b/yudao-framework/yudao-spring-boot-starter-security/src/main/java/cn/iocoder/yudao/framework/security/core/rpc/LoginUserRequestInterceptor.java @@ -6,6 +6,11 @@ import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils; import feign.RequestInterceptor; import feign.RequestTemplate; +/** + * LoginUser 的 RequestInterceptor 实现类:Feign 请求时,将 {@link LoginUser} 设置到 header 中,继续透传给被调用的服务 + * + * @author 芋道源码 + */ public class LoginUserRequestInterceptor implements RequestInterceptor { @Override diff --git a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/util/WebFrameworkUtils.java b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/util/WebFrameworkUtils.java index f5ac676fa..e11a3236a 100644 --- a/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/util/WebFrameworkUtils.java +++ b/yudao-framework/yudao-spring-boot-starter-web/src/main/java/cn/iocoder/yudao/framework/web/core/util/WebFrameworkUtils.java @@ -23,7 +23,7 @@ public class WebFrameworkUtils { private static final String REQUEST_ATTRIBUTE_COMMON_RESULT = "common_result"; - private static final String HEADER_TENANT_ID = "tenant-id"; + public static final String HEADER_TENANT_ID = "tenant-id"; private static WebProperties properties; diff --git a/yudao-gateway/pom.xml b/yudao-gateway/pom.xml index 50f5155fe..8eef8c358 100644 --- a/yudao-gateway/pom.xml +++ b/yudao-gateway/pom.xml @@ -37,8 +37,8 @@ - cn.iocoder.cloud - yudao-spring-boot-starter-rpc + org.springframework.cloud + spring-cloud-starter-loadbalancer diff --git a/yudao-gateway/src/main/resources/application.yaml b/yudao-gateway/src/main/resources/application.yaml index 59b5db671..6d4439553 100644 --- a/yudao-gateway/src/main/resources/application.yaml +++ b/yudao-gateway/src/main/resources/application.yaml @@ -15,3 +15,11 @@ spring: uri: grayLb://system-server predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 - Path=/app-api/system/** + - id: infra-admin-api # 路由的编号 + uri: grayLb://infra-server + predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 + - Path=/admin-api/infra/** + - id: infra-app-api # 路由的编号 + uri: grayLb://infra-server + predicates: # 断言,作为路由的匹配条件,对应 RouteDefinition 数组 + - Path=/app-api/infra/** diff --git a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ApiConstants.java b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ApiConstants.java index 94c56e8fb..7f02d0161 100644 --- a/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ApiConstants.java +++ b/yudao-module-infra/yudao-module-infra-api/src/main/java/cn/iocoder/yudao/module/infra/enums/ApiConstants.java @@ -1,5 +1,7 @@ package cn.iocoder.yudao.module.infra.enums; +import cn.iocoder.yudao.framework.common.enums.RpcConstants; + /** * API 相关的枚举 * @@ -14,7 +16,7 @@ public class ApiConstants { */ public static final String NAME = "infra-server"; - public static final String PREFIX = "/rpc-api/system"; + public static final String PREFIX = RpcConstants.RPC_API_PREFIX + "/system"; public static final String VERSION = "1.0.0"; diff --git a/yudao-module-infra/yudao-module-infra-biz/pom.xml b/yudao-module-infra/yudao-module-infra-biz/pom.xml index 023b13b2b..40d0fef26 100644 --- a/yudao-module-infra/yudao-module-infra-biz/pom.xml +++ b/yudao-module-infra/yudao-module-infra-biz/pom.xml @@ -42,6 +42,10 @@ cn.iocoder.cloud yudao-spring-boot-starter-biz-operatelog + + cn.iocoder.cloud + yudao-spring-boot-starter-biz-tenant + diff --git a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/TmpConfiguration.java b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/TmpConfiguration.java index e052e5988..1c1316e8d 100644 --- a/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/TmpConfiguration.java +++ b/yudao-module-infra/yudao-module-infra-biz/src/main/java/cn/iocoder/yudao/module/infra/framework/TmpConfiguration.java @@ -1,15 +1,12 @@ package cn.iocoder.yudao.module.infra.framework; -import cn.iocoder.yudao.framework.apilog.core.service.ApiAccessLogFrameworkService; -import cn.iocoder.yudao.framework.apilog.core.service.ApiErrorLogFrameworkService; -import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiAccessLogCreateReqDTO; -import cn.iocoder.yudao.framework.apilog.core.service.dto.ApiErrorLogCreateReqDTO; import cn.iocoder.yudao.framework.operatelog.core.dto.OperateLogCreateReqDTO; import cn.iocoder.yudao.framework.operatelog.core.service.OperateLogFrameworkService; -import cn.iocoder.yudao.module.infra.api.file.FileApi; +import cn.iocoder.yudao.framework.tenant.core.service.TenantFrameworkService; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import java.util.List; import java.util.concurrent.Future; @Configuration @@ -25,4 +22,19 @@ public class TmpConfiguration { }; } + @Bean + public TenantFrameworkService tenantFrameworkService() { + return new TenantFrameworkService() { + @Override + public List getTenantIds() { + return null; + } + + @Override + public void validTenant(Long id) { + + } + }; + } + } diff --git a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ApiConstants.java b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ApiConstants.java index 7610a6f25..8f7decf59 100644 --- a/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ApiConstants.java +++ b/yudao-module-system/yudao-module-system-api/src/main/java/cn/iocoder/yudao/module/system/enums/ApiConstants.java @@ -1,5 +1,7 @@ package cn.iocoder.yudao.module.system.enums; +import cn.iocoder.yudao.framework.common.enums.RpcConstants; + /** * API 相关的枚举 * @@ -14,7 +16,7 @@ public class ApiConstants { */ public static final String NAME = "system-server"; - public static final String PREFIX = "/rpc-api/system"; + public static final String PREFIX = RpcConstants.RPC_API_PREFIX + "/system"; public static final String VERSION = "1.0.0";