0
点赞
收藏
分享

微信扫一扫

Redis布隆过滤器

一、日常问题说明

在项目开发的时候,会遇到如下问题:

  • 现有1亿个电话号码,如何要快速准确的判断这些电话号是否存在?面试时会问布隆过滤器了解过吗?
  • 安全连接网址,全球数10亿的网址判断黑名单校验,识别垃圾邮件怎么解决?
  • 白名单校验,识别出合法用户进行后续处理

二、布隆过滤器

布隆过滤器(Bloom Filter)是 Redis 4.0 版本提供的新功能,它被作为插件加载到 Redis 服务器中,给 Redis 提供强大的去重功能。
相比于 Set 集合的去重功能而言,布隆过滤器在空间上能节省 90% 以上,但是它的不足之处是去重率大约在 99% 左右,也就是说有 1% 左右的误判率,这种误差是由布隆过滤器的自身结构决定的。俗话说“鱼与熊掌不可兼得”,如果想要节省空间,就需要牺牲 1% 的误判率,而且这种误判率,在处理海量数据时,几乎可以忽略。

工作原理

布隆过滤器(Bloom Filter)是一个高空间利用率的概率性数据结构,由二进制向量(即位数组)和一系列随机映射函数(即哈希函数)两部分组成。
布隆过滤器使用exists()判断某个元素是否存在于自身结构中。当布隆过滤器判定某个值存在时,其实这个值只是有可能存在;当它说某个值不存在时,那这个值肯定不存在,这个误判概率大约在 1% 左右。

1) 工作流程-添加元素

布隆过滤器主要由位数组和一系列 hash 函数构成,其中位数组的初始状态都为 0。下面对布隆过滤器工作流程做简单描述,如下图所示:

Redis布隆过滤器_布隆过滤器

当使用布隆过滤器添加 key 时,会使用不同的 hash 函数对 key 存储的元素值进行哈希计算,从而会得到多个哈希值。根据哈希值计算出一个整数索引值,将该索引值与位数组长度做取余运算,最终得到一个位数组位置,并将该位置的值变为 1。每个 hash 函数都会计算出一个不同的位置,然后把数组中与之对应的位置变为 1。通过上述过程就完成了元素添加(add)操作。

2) 工作流程-判定元素是否存在

当我们需要判断一个元素是否存时,其流程如下:首先对给定元素再次执行哈希计算,得到与添加元素时相同的位数组位置,判断所得位置是否都为 1,如果其中有一个为 0,那么说明元素不存在,若都为 1,则说明元素有可能存在。

3) 为什么是可能“存在”

您可能会问,为什么是有可能存在?其实原因很简单,那些被置为 1 的位置也可能是由于其他元素的操作而改变的。比如,元素1 和 元素2,这两个元素同时将一个位置变为了 1(图1所示)。在这种情况下,我们就不能判定“元素 1”一定存在,这是布隆过滤器存在误判的根本原因。

三、布隆过滤器特点

布隆过滤器具有高效地插入和查询,占用空间少的特点,但是存在返回结果不确定,不是绝对的完美

Redis布隆过滤器_布隆过滤器_02

布隆过滤器可以减少内存占用,采用的实现思路是不保存数据信息,只是在内存中做一个是否存在的标记flag。

布隆过滤器作用:判断元素是否存在结果,存在时,元素不一定存在,不存在时,元素一定不存在。

布隆过滤器可以添加元素,但是不能删除元素,因为涉及到hashcode判断依据,删掉元素会导致误判率增加,原因如下:

  • 布隆过滤器的误判是指多个输入经过哈希之后在相同的bit位置1了,这样就无法判断究竟是哪个输入产生的,因此误判的根源在于相同的 bit 位被多次映射且置 1。
  • 这种情况也造成了布隆过滤器的删除问题,因为布隆过滤器的每一个 bit 并不是独占的,很有可能多个元素共享了某一位。如果我们直接删除这一位的话,会影响其他的元素。
  • 因为他是有多个 hash 函数,对一个值进行多次 hash 运算,将获得的每个值,在对应位置存 1 ,容易导致这个 1 也代表别的值,一旦删除,另一个值也无法通过。

综述:

  • 布隆过滤器,做了判断某个数据有,是很可能有,但是无.则是100%不存在
  • 布隆过滤器,使用时最好不要让实际元素数量远大于初始化数量,一次给够避免扩容。
  • 当实际元素数量超过初始化数量时,应该对布隆过滤器进行重建,重新分配一个size更大的过滤器,再将所有的历史元素批量add 进行

四、布隆过滤器使用场景说明

4.1.解决缓存穿透的问题,和redis结合bitmap一起使用

缓存穿透是什么?

一般情况下,先查询缓存redis是否有该条数据,缓存中没有时,再查询数据库。当数据库也不存在该条数据时,每次查询都要访问数据库,这就是缓存穿透。缓存透带来的问题是,当有大量请求查询数据库不存在的数据时,就会给数据库带来压力,甚至会拖垮数据库。

使用布隆过滤器解决缓存穿透的问题

把已存在数据的key存在布隆过滤器中,相当于redis前面挡着一个布隆过滤器。

  1. 当有新的请求时,先到布隆过滤器中查询是否存在:
  2. 如果布隆过滤器中不存在该条数据则直接返回;
  3. 如果布隆过滤器中已存在,才去查询缓存redis,如果redis里没查询到则再查询Mysql数据库

 

Redis布隆过滤器_布隆过滤器_03

4.2.黑名单校验,识别垃圾邮箱或者安全连接网址的判断,全球几十亿的网址判断

发现存在黑名单中的,就执行特定操作。比如:识别垃圾邮件,只要是邮箱在黑名单中的邮件,就识别为垃圾邮件。

假设黑名单的数量是数以亿计的,存放起来就是非常耗费存储空间的,布隆过滤器则是一个较好的解决方案。

把所有黑名单都放在布隆过滤器中,在收到邮件时,判断邮件地址是否在布隆过滤器中即可。

五、手写布隆过滤器结合bitmap使用

布隆过滤器工作原理,本质上就是在redis前面有加了一层保护伞,如果白名单中不存在,就不用去查询redis,以及后面的MySQL。起到保护作用,架构设计思路如下:

Redis布隆过滤器_bc_04

步骤设计

  • setBit的构建过程
  • @PostConstruct 初始化白名单数据
  • 计算元素hash值
  • 得到hash值算出对应的二进制数组的坑位
  • 将对应坑位的值修改为数字1,表示存在
  • getBit查询是否存在
  • 计算元素hash值
  • 得到hash值算出对应的二进制数组的坑位
  • 返回对应坑位的值,0表示无,1表示存在

六、使用mybatis通用mapper4生成代码

6.1.先创建一个空项目

这里创建空项目,后续案例以该项目的模块出现

Redis布隆过滤器_spring_05

6.2.在MySQL中建表sql如下:

注意这里我是创建在db2022这个库中,后续在项目配置文件要写对

CREATE TABLE `t_customer` (
  `id` int(20) NOT NULL AUTO_INCREMENT,
  `cname` varchar(50) NOT NULL,
  `age` int(10) NOT NULL,
  `phone` varchar(20) NOT NULL,
  `sex` tinyint(4) NOT NULL,
  `birth` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_cname` (`cname`)
) ENGINE=InnoDB AUTO_INCREMENT=10 DEFAULT CHARSET=utf8mb4

6.3.创建 mybatis-generator 工程

创建 mybatis-generator 工程主要是为了去自动生成mapper文件

Redis布隆过滤器_布隆过滤器_06

6.4.在pom.xml中引入依赖如下:

需要注意的是,本次案例使用的MySQL8.0,所以对应的驱动版本也要对应上

<properties>
        <!--  依赖版本号 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <java.version>1.8</java.version>
        <hutool.version>5.5.8</hutool.version>
        <druid.version>1.1.18</druid.version>
        <mapper.version>4.1.5</mapper.version>
        <pagehelper.version>5.1.4</pagehelper.version>
        <mysql.version>8.0.21</mysql.version>
        <swagger2.version>2.9.2</swagger2.version>
        <swagger-ui.version>2.9.2</swagger-ui.version>
        <mybatis.spring.version>2.1.3</mybatis.spring.version>
    </properties>


    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--Mybatis 通用mapper tk单独使用,自己带着版本号-->
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.4.6</version>
        </dependency>
        <!--mybatis-spring-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>${mybatis.spring.version}</version>
        </dependency>

        <!-- Mybatis Generator -->
        <dependency>
            <groupId>org.mybatis.generator</groupId>
            <artifactId>mybatis-generator-core</artifactId>
            <version>1.4.0</version>
            <scope>compile</scope>
            <optional>true</optional>
        </dependency>

        <!--通用Mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>${mapper.version}</version>
        </dependency>
        <!--persistence-->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <build>
        <resources>
            <resource>
                <directory>${basedir}/src/main/java</directory>
                <includes>
                    <include>**/*.xml</include>
                </includes>
            </resource>
            <resource>
                <directory>${basedir}/src/main/resources</directory>
            </resource>
        </resources>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mybatis.generator</groupId>
                <artifactId>mybatis-generator-maven-plugin</artifactId>
                <version>1.3.6</version>
                <configuration>
                    <configurationFile>${basedir}/src/main/resources/generatorConfig.xml</configurationFile>
                    <overwrite>true</overwrite>
                    <verbose>true</verbose>
                </configuration>
                <dependencies>
                    <dependency>
                        <groupId>mysql</groupId>
                        <artifactId>mysql-connector-java</artifactId>
                        <version>${mysql.version}</version>
                    </dependency>
                    <dependency>
                        <groupId>tk.mybatis</groupId>
                        <artifactId>mapper</artifactId>
                        <version>${mapper.version}</version>
                    </dependency>
                </dependencies>
            </plugin>
        </plugins>
    </build>

6.5.在src/main/resources目录下创建配置文件

  • 创建 config.properties 文件内容如下:

#t_customer表包名
#com.xfcy 输自己用的包名 要不然不能直接cv到自己用的工程里
package.name=com.redis.demo

jdbc.driverClass = com.mysql.cj.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/db2022
jdbc.user = root
jdbc.password =123456

  • 创建 generatorConfig.xml 内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE generatorConfiguration
        PUBLIC "-//mybatis.org//DTD MyBatis Generator Configuration 1.0//EN"
        "http://mybatis.org/dtd/mybatis-generator-config_1_0.dtd">

<generatorConfiguration>
    <properties resource="config.properties"/>

    <context id="Mysql" targetRuntime="MyBatis3Simple" defaultModelType="flat">
        <property name="beginningDelimiter" value="`"/>
        <property name="endingDelimiter" value="`"/>

        <plugin type="tk.mybatis.mapper.generator.MapperPlugin">
            <property name="mappers" value="tk.mybatis.mapper.common.Mapper"/>
            <property name="caseSensitive" value="true"/>
        </plugin>

        <jdbcConnection driverClass="${jdbc.driverClass}"
                        connectionURL="${jdbc.url}"
                        userId="${jdbc.user}"
                        password="${jdbc.password}">
        </jdbcConnection>

        <javaModelGenerator targetPackage="${package.name}.entities" targetProject="src/main/java"/>

        <sqlMapGenerator targetPackage="${package.name}.mapper" targetProject="src/main/java"/>

        <javaClientGenerator targetPackage="${package.name}.mapper" targetProject="src/main/java" type="XMLMAPPER"/>

        <!--        输入表名  t_customer  -->
        <table tableName="t_customer" domainObjectName="Customer">
            <generatedKey column="id" sqlStatement="JDBC"/>
        </table>
    </context>
</generatorConfiguration>

6.6.一键生成代码

  • 操作如下图:

Redis布隆过滤器_布隆过滤器_07

  • 生成后如下图:

Redis布隆过滤器_spring_08

  • 需要注意,我使用的MySQL8.0出现了异常信息如下:

idea创建spring-boot出现[ERROR]Failed to execute goal org.mybatis.generator:mybatis-generator-maven-plugi

解决方式如下:

# 通过以下命令查询Time Zone,显示如图(SYSTEM为SQL默认美国时间,而我们中国要比他们迟8小时)
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone |        |
| time_zone        | SYSTEM |
+------------------+--------+
2 rows in set, 1 warning (0.00 sec)
mysql>
# 修改默认时间:set global time_zone=’+8:00’;
mysql> set global time_zone='+8:00';
Query OK, 0 rows affected (0.00 sec)
# 设置完重新打卡命令行,进入mysql,查询time,看是否修改成功
mysql> show variables like '%time_zone%';
+------------------+--------+
| Variable_name    | Value  |
+------------------+--------+
| system_time_zone |        |
| time_zone        | +08:00 |
+------------------+--------+
2 rows in set, 1 warning (0.00 sec)

七、springboot 、redis 和mybatis结合布隆过滤器实战编码

7.1.创建springboot模块

这里使用springboot lnitializr创建,模块名为BloomFilter,如下图

Redis布隆过滤器_spring_09

7.2.导入依赖

在pom.xml中添加依赖文件如下:

<properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <junit.version>4.12</junit.version>
        <log4j.version>1.2.17</log4j.version>
        <lombok.version>1.16.18</lombok.version>
        <mysql.version>8.0.21</mysql.version>
    </properties>

    <dependencies>
        <!--SpringBoot通用依赖模块-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--jedis-->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>4.3.1</version>
        </dependency>

        <!--SpringBoot与Redis整合依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
        <!--swagger2-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.9.2</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.9.2</version>
        </dependency>
        <!--Mysql数据库驱动-->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <!--SpringBoot集成druid连接池-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.1.10</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.16</version>
        </dependency>
        <!--mybatis和springboot整合-->
        <dependency>
            <groupId>org.mybatis.spring.boot</groupId>
            <artifactId>mybatis-spring-boot-starter</artifactId>
            <version>1.3.0</version>
        </dependency>
        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.2.3</version>
        </dependency>
        <!--persistence-->
        <dependency>
            <groupId>javax.persistence</groupId>
            <artifactId>persistence-api</artifactId>
            <version>1.0.2</version>
        </dependency>
        <!--通用Mapper-->
        <dependency>
            <groupId>tk.mybatis</groupId>
            <artifactId>mapper</artifactId>
            <version>4.1.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-autoconfigure</artifactId>
        </dependency>
        <!--通用基础配置junit/devtools/test/log4j/lombok/-->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>${junit.version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>${log4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>${lombok.version}</version>
            <optional>true</optional>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

7.3.创建配置文件

在resources目录下的 application.properties中添加如下内容:

server.port=7070

# ========================swagger=====================
spring.swagger2.enabled=true
#在springboot2.6.X结合swagger2.9.X会提示documentationPluginsBootstrapper空指针异常,
#原因是在springboot2.6.X中将SpringMVC默认路径匹配策略从AntPathMatcher更改为PathPatternParser,
# 导致出错,解决办法是matching-strategy切换回之前ant_path_matcher
spring.mvc.pathmatch.matching-strategy=ant_path_matcher

# ========================redis单机=====================
spring.redis.database=0
## 修改为自己真实IP
spring.redis.host=192.168.42.132
spring.redis.port=6379
spring.redis.password=123456
spring.redis.lettuce.pool.max-active=8
spring.redis.lettuce.pool.max-wait=-1ms
spring.redis.lettuce.pool.max-idle=8
spring.redis.lettuce.pool.min-idle=0

# ========================logging=====================
logging.level.root=info
logging.level.com.atguigu.redis7=info
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n 

logging.file.name=D:/mylogs2023/redis7_study.log
logging.pattern.file=%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger- %msg%n

# ========================alibaba.druid=====================
spring.datasource.type=com.alibaba.druid.pool.DruidDataSource
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/db2022?useUnicode=true&characterEncoding=utf-8&useSSL=false
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.druid.test-while-idle=false

# ========================mybatis===================
mybatis.mapper-locations=classpath:mapper/*.xml
mybatis.type-aliases-package=com.xfcy.entities

7.3.创建config包

创建:RedisConfig,内容如下:

package com.redis.demo.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;

@Configuration
public class RedisConfig {
    /**
     * redis序列化的工具配置类,下面这个请一定开启配置
     * 127.0.0.1:6379> keys *
     * 1) "ord102"  序列化过
     * 2) "\xac\xed\x00\x05t\x00\aord102"   没有序列化过
     * this.redisTemplate.opsForValue(); //提供了操作string类型的所有方法
     * this.redisTemplate.opsForList(); // 提供了操作list类型的所有方法
     * this.redisTemplate.opsForSet(); //提供了操作set的所有方法
     * this.redisTemplate.opsForHash(); //提供了操作hash表的所有方法
     * this.redisTemplate.opsForZSet(); //提供了操作zset的所有方法
     * @param lettuceConnectionFactory
     * @return
     */
    @Bean
    public RedisTemplate<String, Object> redisTemplate(LettuceConnectionFactory lettuceConnectionFactory) {
        RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();

        redisTemplate.setConnectionFactory(lettuceConnectionFactory);
        //设置key序列化方式string
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        //设置value的序列化方式json,使用GenericJackson2JsonRedisSerializer替换默认序列化
        redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

        redisTemplate.afterPropertiesSet();

        return redisTemplate;
    }
}

创建:SwaggerConfig,内容如下:

package com.redis.demo.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

@Configuration
@EnableSwagger2 //开启swagger功能
public class SwaggerConfig {
    @Value("${spring.swagger2.enabled}")
    private Boolean enabled;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enabled)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.redis.demo")) //你自己的package
                .paths(PathSelectors.any())
                .build();
    }
    public ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("springboot利用swagger2构建api接口文档 "+"\t"+ DateTimeFormatter.ofPattern("yyyy-MM-dd").format(LocalDateTime.now()))
                .description("springboot项目整合")
                .version("1.0")
                .termsOfServiceUrl("https://www.augus.com/")
                .build();
    }
}

7.4.复制生成的文件

将章节6中mybatis-generator文件中的生成的实体类和mapper文件复制到模块中

Redis布隆过滤器_spring_10

7.5.创建包filter

在下面创建 BloomFilterInit,内容如下:

package com.redis.demo.filter;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;

@Component
@Slf4j
public class BloomFilterInit {

    @Resource
    private RedisTemplate redisTemplate;

    @PostConstruct //spring初始化的时候执行白名单数据
    public void init(){
        //白名单客户加载到布隆过滤器
        String uid = "customer:11";

        //1.计算hashValue,由于存在计算出来的负数的可能性,取绝对值
        int hashValue = Math.abs(uid.hashCode());

        //2.通过hashValue和2的32次方取余后,获得对应的慒位
        long index = (long) (hashValue % Math.pow(2, 32));

        log.info(uid+" 对应------慒位index:{}",index);

        //3.设置redis里面的bitmap对应坑位,该有值时设为1
        redisTemplate.opsForValue().setBit("whitelistCustomer",index,true);
    }

}

7.6.创建包utils

在下面创建 CheckUtils,内容如下:

package com.redis.demo.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
@Slf4j
public class CheckUtils {

    @Resource
    private RedisTemplate redisTemplate;

    public boolean checkWithBloomFilter(String checkItem,String key){
        int hashValue = Math.abs(key.hashCode());

        //通过hashValue和2的32次方取余后,获得对应的慒位
        long index = (long) (hashValue%Math.pow(2,32));

        //检查是否存在某个值
        Boolean aBoolean = redisTemplate.opsForValue().getBit(checkItem, index);

        log.info("----->key:"+key+"\t对应坑位index:"+index+"\t是否存在:"+aBoolean);
        return aBoolean;

    }
}

7.7.创建包service

在下面创建CustomerService,内容如下:

package com.redis.demo.service;

import com.redis.demo.entities.Customer;
import com.redis.demo.mapper.CustomerMapper;
import com.redis.demo.utils.CheckUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

@Service
@Slf4j
public class CustomerService {
    public static final String CACHE_KEY_CUSTOMER = "customer:";

    @Resource
    private CustomerMapper customerMapper;

    @Resource
    private RedisTemplate redisTemplate;

    @Resource
    private CheckUtils checkUtils;

    /**
     * 将数据插入到MySQL数据库,同时回写入redis
     * @param customer 员工信息
     */
    public void addCustomer(Customer customer){
        int i = customerMapper.insertSelective(customer);

        if(i>0){
            //MySQL插入成功,需要重新查询一次数据,写入到redis中
            Customer result = customerMapper.selectByPrimaryKey(customer.getId());
            //构造redis中存储的key
            String key = CACHE_KEY_CUSTOMER + customer.getId();
            //写入redis
            redisTemplate.opsForValue().set(key,result);

        }
    }

    /**
     * 读取操作,通过双检加锁机制完成
     * @param customerId
     * @return
     */

    public Customer findCustomerById(Integer customerId){
        Customer customer = null;

        //缓存redis的key名称
        String key = CACHE_KEY_CUSTOMER + customerId;

        //1.先去redis查询
        customer = (Customer) redisTemplate.opsForValue().get(key);

        //2.如果redis存在数据,直接返回,如果不存在,就去MySQL中查询
        if(customer == null){
            // 3.对于高QPS的优化,进来就先加锁,保证一个请求操作,让外面的redis等待一下,避免击穿mysql
            synchronized (CustomerService.class){
                //3.1第二次查询redis 加锁
                customer = (Customer) redisTemplate.opsForValue().get(key);

                //4.在去查询MySQL
                customer = customerMapper.selectByPrimaryKey(customerId);

                //5.MySQL存在数据,redis不存在,先从MySQL查询出来的内容写入到redis中
                if(customer != null){
                    //6.把mysql查询到的数据会写到到redis, 保持双写一致性  7天过期
                    redisTemplate.opsForValue().set(key, customer, 7L, TimeUnit.DAYS);

                }
            }

        }
        return customer;
    }

    public Customer findCustomerByIdWithBloomFilter(Integer customerId){
        Customer customer = null;

        //1.缓存redis的key名称
        String key = CACHE_KEY_CUSTOMER + customerId;

        //判断布隆过滤器白名单是否存在该信息
        if(!checkUtils.checkWithBloomFilter("whitelistCustomer", key)){
            log.info("白名单没有此信息,不可以访问" + key);
            return null;
        }


        //1.先去redis查询
        customer = (Customer) redisTemplate.opsForValue().get(key);

        //2.如果redis存在数据,直接返回,如果不存在,就去MySQL中查询
        if(customer == null){
            // 3.对于高QPS的优化,进来就先加锁,保证一个请求操作,让外面的redis等待一下,避免击穿mysql
            synchronized (CustomerService.class){
                //3.1第二次查询redis 加锁
                customer = (Customer) redisTemplate.opsForValue().get(key);

                //4.在去查询MySQL
                customer = customerMapper.selectByPrimaryKey(customerId);

                //5.MySQL存在数据,redis不存在,先从MySQL查询出来的内容写入到redis中
                if(customer != null){
                    //6.把mysql查询到的数据会写到到redis, 保持双写一致性  7天过期
                    redisTemplate.opsForValue().set(key, customer, 7L, TimeUnit.DAYS);
                }
            }

        }
        return customer;
    }

}

7.8.创建包controller

在下面创建CustomerController,内容如下:

package com.redis.demo.controller;

import com.redis.demo.entities.Customer;
import com.redis.demo.service.CustomerService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.Random;

@Api(tags = "客户Customer接口和布隆过滤器讲解")
@RestController
@Slf4j
public class CustomerController {
    @Resource
    private CustomerService customerService;

    @ApiOperation("数据库初始化10条customer记录插入")
    @RequestMapping(value = "/customer/add", method = RequestMethod.POST)
    public void addCustomer(){
        for(int i=0;i<10;i++){
            Customer customer = new Customer();
            customer.setCname("customer" +i);
            customer.setAge(new Random().nextInt(30)+1);
            customer.setPhone("12345678910");
            customer.setSex((byte) new Random().nextInt(2));
            customer.setBirth(Date.from(LocalDateTime.now().atZone(ZoneId.systemDefault()).toInstant()));

            customerService.addCustomer(customer);
        }
    }

    @ApiOperation("单个customer查询操作,按照customerId查询")
    @RequestMapping(value = "/customer/{customerId}", method = RequestMethod.GET)
    public Customer findCustomerById(@PathVariable("customerId") Integer customerId){
        return customerService.findCustomerById(customerId);
    }

    @ApiOperation("布隆过滤器单个customer查询操作,按照customerId查询")
    @RequestMapping(value = "/bloomfilter/{customerId}", method = RequestMethod.GET)
    public Customer findCustomerByIdWithBloomFilter(@PathVariable("customerId") Integer customerId){
        return customerService.findCustomerById(customerId);
    }
}

7.9.测试

保证redis和MySQL是启动状态,然后启动项目模块,在浏览器访问swagger地址:http://localhost:7070/swagger-ui.html#/ ,操作添加customer数据接口如下图:

Redis布隆过滤器_spring_11

查看数据库,会发现已经添加了10条数据

Redis布隆过滤器_bc_12

同时上面的数据也写入到了redis

Redis布隆过滤器_bc_13

执行布隆过滤器单个customer查询操作,查询customerId为11的数据

Redis布隆过滤器_布隆过滤器_14

 

 查出来的信息和redis中保存的一致

Redis布隆过滤器_布隆过滤器_15

八、布隆过滤器优缺点

优点:高效地插入和查询,内存占用 bit 空间少

缺点

  • 不能删除元素
  • 因为删除元素会导致误判率增加,因为hash冲突同一个位置可能存的东西是多个共有的,你删除一个元素的同时可能也把其他的删除了。
  • 存在误判,不能精准过滤
  • 数据存在,可能有。
  • 数据不存在,绝对是没有的。

为了解决布隆过滤器不能删除元素的问题,布谷鸟过滤器横空出世。



举报

相关推荐

0 条评论