springboot+redission实现布隆过滤器

布隆过滤器

介绍

为了防止缓存穿透,也就是缓存查不到,服务器也查不到,可以设置布隆过滤器,当然也可以直接将一些查不到的信息直接设置为空,这样也可以防止缓存穿透。布隆过滤器是一种概率性的查询,通过一个很大的Bitmap,然后将一个key经过hash运算,将bitmap对应的位置设置为1。后面如果有个Key打过来,这个key有可能不存在与bitmap,但是经过hash运算却存在,这种称为hash冲突。

软件架构

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.8</version>
        <relativePath/> <!-- lookup parent from repository -->
 </parent>  

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>org.redisson</groupId>
            <artifactId>redisson-spring-boot-starter</artifactId>
            <version>3.13.6</version>
        </dependency>
        <dependency>
            <groupId>com.github.ulisesbocchio</groupId>
            <artifactId>jasypt-spring-boot-starter</artifactId>
            <version>3.0.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-pool2</artifactId>
        </dependency>
 

 

配置

RedisSesionConfig

package com.bloomfiler.demo.config;

import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.net.URISyntaxException;


@Configuration
public class RedisSesionConfig {

    @Value("${spring.redis.cluster.nodes}")
    String add;
    @Value("${spring.redis.password}")
    String pass;

    public Config config() throws URISyntaxException {
        System.out.println("ok");
        System.out.println(add);
        System.out.println(pass);
        Config configs = new Config();
        String[] adds = add.split(",");
        for (int i = 0; i < adds.length; i++) {
            adds[i] = "redis://" + adds[i];
        }
        //#连接间隔 心跳 客户端长时间未使用,服务端会断开
        configs.useClusterServers().addNodeAddress(adds).setClientName("rediss").setPassword(pass).setMasterConnectionPoolSize(40);
        return configs;
    }

    @Bean
    public RedissonClient redissonClient() throws URISyntaxException {

        RedissonClient redissonClient = Redisson.create(config());

        return redissonClient;
    }

    @Bean
    public RLock rLock() throws URISyntaxException {

        return redissonClient().getLock("kk");
    }
}

 

 

RedissBloomFilerConfig

package com.bloomfiler.demo.config;

import org.redisson.api.RBloomFilter;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Configuration
public class RedissBloomFilerConfig {
    @Autowired
    RedissonClient redissonClient;

    @Bean
    public RBloomFilter<String>  bloomFilter(){
        RBloomFilter<String> bloomFilter =redissonClient.getBloomFilter("bloomtest");
        bloomFilter.tryInit((long) 1E8,0.003);

        return bloomFilter;
    }
}

 

 

简单的放置数据和查询数据

@RestController
public class RedisController {
    @Autowired
    RedissonClient redissonClient;

    @Autowired
    RBloomFilter bloomFilter;
    @GetMapping("/add/{name}")
    public  String addData(@PathVariable String name){

        return  bloomFilter.add(name)?"ok":"false";
    }
    @GetMapping("/list/{name}")
    public  boolean listData(@PathVariable String name){

        return bloomFilter.contains(name);
    }

}

 

 

结果

 

 

 

 代码地址

https://gitee.com/chenjie950907/boolean-filter

 

posted @ 2022-08-01 13:11  小陈子博客  阅读(1245)  评论(0编辑  收藏  举报