diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/component/BloomRedisService.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/component/BloomRedisService.java deleted file mode 100644 index 83e8e256c..000000000 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/component/BloomRedisService.java +++ /dev/null @@ -1,50 +0,0 @@ -package com.youlai.mall.pms.component; - -import com.google.common.base.Preconditions; -import com.youlai.mall.pms.utils.BloomFilterUtils; -import org.springframework.data.redis.core.RedisTemplate; - -/** - * @Author DaniR - * @Description - * @Date 2021/6/26 9:38 - **/ -public class BloomRedisService { - - private RedisTemplate redisTemplate; - - private BloomFilterUtils bloomFilterUtils; - - public void setBloomFilterUtils(BloomFilterUtils bloomFilterUtils) { - this.bloomFilterUtils = bloomFilterUtils; - } - - public void setRedisTemplate(RedisTemplate redisTemplate) { - this.redisTemplate = redisTemplate; - } - - /** - * 根据给定的布隆过滤器添加值 - */ - public void addByBloomFilter(String key, T value) { - Preconditions.checkArgument(bloomFilterUtils != null, "bloomFilterHelper不能为空"); - int[] offset = bloomFilterUtils.murmurHash(value); - for (int i : offset) { - redisTemplate.opsForValue().setBit(key, i, true); - } - } - - /** - * 根据给定的布隆过滤器判断值是否存在 - */ - public boolean includeByBloomFilter(String key, T value) { - Preconditions.checkArgument(bloomFilterUtils != null, "bloomFilterHelper不能为空"); - int[] offset = bloomFilterUtils.murmurHash(value); - for (int i : offset) { - if (!redisTemplate.opsForValue().getBit(key, i)) { - return false; - } - } - return true; - } -} diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/BloomFilterConfig.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/BloomFilterConfig.java deleted file mode 100644 index 6167d9a32..000000000 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/BloomFilterConfig.java +++ /dev/null @@ -1,57 +0,0 @@ -package com.youlai.mall.pms.config; - -import com.google.common.base.Charsets; -import com.google.common.hash.Funnel; -import com.youlai.mall.pms.common.constant.PmsConstants; -import com.youlai.mall.pms.component.BloomRedisService; -import com.youlai.mall.pms.pojo.entity.PmsSpu; -import com.youlai.mall.pms.service.IPmsSpuService; -import com.youlai.mall.pms.utils.BloomFilterUtils; -import lombok.AllArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.InitializingBean; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.data.redis.core.RedisTemplate; -import org.springframework.util.CollectionUtils; -import java.util.List; - -/** - * @Author DaniR - * @Description - * @Date 2021/6/26 9:39 - **/ -@Slf4j -@Configuration -@AllArgsConstructor -public class BloomFilterConfig implements InitializingBean { - - private final IPmsSpuService pmsSpuServiced; - private final RedisTemplate redisTemplate; - - @Bean - public BloomFilterUtils initBloomFilterHelper() { - return new BloomFilterUtils<>((Funnel) (from, into) -> into.putString(from, Charsets.UTF_8) - .putString(from, Charsets.UTF_8), 1000000, 0.01); - } - - - @Bean - public BloomRedisService bloomRedisService() { - BloomRedisService bloomRedisService = new BloomRedisService(); - bloomRedisService.setBloomFilterUtils(initBloomFilterHelper()); - bloomRedisService.setRedisTemplate(redisTemplate); - return bloomRedisService; - } - - @Override - public void afterPropertiesSet() throws Exception { - List list = pmsSpuServiced.list(); - log.info("加载产品到布隆过滤器当中,size:{}", list.size()); - if (!CollectionUtils.isEmpty(list)) { - list.stream().filter(item -> item.getId() > 0).forEach(item -> { - bloomRedisService().addByBloomFilter(PmsConstants.GOODS_BLOOM_FILTER, item.getId() + ""); - }); - } - } -} diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/InterceptorConfiguration.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/InterceptorConfiguration.java deleted file mode 100644 index 7465e6146..000000000 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/config/InterceptorConfiguration.java +++ /dev/null @@ -1,29 +0,0 @@ -package com.youlai.mall.pms.config; - -import com.youlai.mall.pms.interceptor.BloomFilterInterceptor; -import org.springframework.context.annotation.Bean; -import org.springframework.context.annotation.Configuration; -import org.springframework.web.servlet.config.annotation.InterceptorRegistry; -import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; - -/** - * @Author DaniR - * @Description - * @Date 2021/6/26 9:39 - **/ -@Configuration -public class InterceptorConfiguration implements WebMvcConfigurer { - - @Override - public void addInterceptors(InterceptorRegistry registry) { - //注册拦截器 - registry.addInterceptor(bloomInterceptorHandler()) - .addPathPatterns("/app-api/v1/goods/**") - .excludePathPatterns("/app-api/v1/goods/sku/**"); - } - - @Bean - public BloomFilterInterceptor bloomInterceptorHandler() { - return new BloomFilterInterceptor(); - } -} diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/common/constant/PmsConstants.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/constant/PmsConstants.java similarity index 88% rename from mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/common/constant/PmsConstants.java rename to mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/constant/PmsConstants.java index 0f83dceea..6a4f96236 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/common/constant/PmsConstants.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/constant/PmsConstants.java @@ -1,4 +1,4 @@ -package com.youlai.mall.pms.common.constant; +package com.youlai.mall.pms.constant; /** * 商品模块常量 diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/common/enums/AttributeTypeEnum.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/enums/AttributeTypeEnum.java similarity index 94% rename from mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/common/enums/AttributeTypeEnum.java rename to mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/enums/AttributeTypeEnum.java index d418691f3..a53293601 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/common/enums/AttributeTypeEnum.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/enums/AttributeTypeEnum.java @@ -1,4 +1,4 @@ -package com.youlai.mall.pms.common.enums; +package com.youlai.mall.pms.enums; import lombok.Getter; diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/interceptor/BloomFilterInterceptor.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/interceptor/BloomFilterInterceptor.java deleted file mode 100644 index 25b822ddd..000000000 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/interceptor/BloomFilterInterceptor.java +++ /dev/null @@ -1,55 +0,0 @@ -package com.youlai.mall.pms.interceptor; - -import com.fasterxml.jackson.databind.ObjectMapper; -import com.youlai.common.result.Result; -import com.youlai.mall.pms.common.constant.PmsConstants; -import com.youlai.mall.pms.component.BloomRedisService; -import lombok.extern.slf4j.Slf4j; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.util.AntPathMatcher; -import org.springframework.util.PathMatcher; -import org.springframework.web.servlet.HandlerInterceptor; - -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import java.nio.charset.StandardCharsets; -import java.util.Map; - - -/** - * @author DaniR - * @version 1.0 - * @description 布隆过滤器拦截器 - * @createDate 2021/6/23 20:50 - */ -@Slf4j -public class BloomFilterInterceptor implements HandlerInterceptor { - - @Autowired - private BloomRedisService bloomRedisService; - - @Override - public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { - String currentUrl = request.getRequestURI(); - PathMatcher matcher = new AntPathMatcher(); - Map pathVariable; - try { - pathVariable = matcher.extractUriTemplateVariables("/app-api/v1/goods/{id}", currentUrl); - } catch (IllegalStateException e) { - // 路径不匹配则放行 - return true; - } - if (bloomRedisService.includeByBloomFilter(PmsConstants.GOODS_BLOOM_FILTER, pathVariable.get("id"))) { - return true; - } - - response.setContentType(MediaType.APPLICATION_JSON_VALUE); - response.setStatus(HttpStatus.BAD_REQUEST.value()); - response.setCharacterEncoding(StandardCharsets.UTF_8.name()); - String result = new ObjectMapper().writeValueAsString(Result.failed("商品不存在!")); - response.getWriter().print(result); - return false; - } -} diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java index 822bce331..1018ee706 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSkuServiceImpl.java @@ -7,7 +7,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.youlai.common.web.exception.ApiException; -import com.youlai.mall.pms.common.constant.PmsConstants; +import com.youlai.mall.pms.constant.PmsConstants; import com.youlai.mall.pms.mapper.PmsSkuMapper; import com.youlai.mall.pms.pojo.dto.CheckPriceDTO; import com.youlai.mall.pms.pojo.dto.SkuInfoDTO; diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java index 4721237c0..b804345ac 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/service/impl/PmsSpuServiceImpl.java @@ -10,8 +10,8 @@ import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.youlai.common.security.util.SecurityUtils; -import com.youlai.mall.pms.common.constant.PmsConstants; -import com.youlai.mall.pms.common.enums.AttributeTypeEnum; +import com.youlai.mall.pms.constant.PmsConstants; +import com.youlai.mall.pms.enums.AttributeTypeEnum; import com.youlai.mall.pms.converter.SpuAttributeConverter; import com.youlai.mall.pms.converter.SpuConverter; import com.youlai.mall.pms.mapper.PmsSpuMapper; diff --git a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/utils/BloomFilterUtils.java b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/util/BloomFilterUtils.java similarity index 98% rename from mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/utils/BloomFilterUtils.java rename to mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/util/BloomFilterUtils.java index 83bc1fd20..8786b60c9 100644 --- a/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/utils/BloomFilterUtils.java +++ b/mall-pms/pms-boot/src/main/java/com/youlai/mall/pms/util/BloomFilterUtils.java @@ -1,4 +1,4 @@ -package com.youlai.mall.pms.utils; +package com.youlai.mall.pms.util; import com.google.common.hash.Funnel; import com.google.common.hash.Hashing;