Redisson的使用

学习Redisson,首先看文档:
github地址

使用:
1.在pom.xml中引入maven依赖

        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-data-redisartifactId>
        dependency>
        
        <dependency>
            <groupId>org.redissongroupId>
            <artifactId>redissonartifactId>
            <version>3.7.4version>
        dependency>

2.在application.properties中配置redis相关的参数:

#redis
spring.redis.host=XXX.XXX.XX.XX(host地址)
spring.redis.password=password(密码)
spring.redis.port=6060(端口号)
spring.redis.database=0
spring.redis.timeout=10000

3.配置redis和redisson 的config
RedisConfig类:

@Configuration
@EnableCaching
public class RedisConfig extends CachingConfigurerSupport {
    @Value("${spring.redis.host}")
    private String hostName;
    @Value("${spring.redis.port}")
    private int port;
    @Value("${spring.redis.timeout}")
    private int timeOut;
    @Value("${spring.redis.password}")
    private String password;

    @Bean
    public KeyGenerator wiselyKeyGenerator(){
        return new KeyGenerator() {
            @Override
            public Object generate(Object o, Method method, Object... objects) {
                StringBuilder sb = new StringBuilder();
                sb.append(o.getClass().getName());
                sb.append(method.getName());
                for(Object object : objects){
                    sb.append(object.toString());
                }
                return sb.toString();
            }
        };
    }

    @Bean
    public JedisConnectionFactory redisConnectionFactory(){
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory();
        jedisConnectionFactory.setHostName(hostName);
        jedisConnectionFactory.setPort(port);
        jedisConnectionFactory.setTimeout(timeOut);
        jedisConnectionFactory.setPassword(password);
        return jedisConnectionFactory;
    }

    @Bean
    public RedisCacheManager redisCacheManager(RedisTemplate redisTemplate){
        RedisCacheManager redisCacheManager = new RedisCacheManager(redisTemplate);
        redisCacheManager.setDefaultExpiration(10);
        return redisCacheManager;
    }

    @Bean
    public RedisTemplate dataRedisTemplate(JedisConnectionFactory jedisConnectionFactory){
        RedisTemplate redisTemplate = new RedisTemplate();
        StringRedisSerializer keyRedisSerializer = new StringRedisSerializer();
        JdkSerializationRedisSerializer jdkSerializationRedisSerializer = new JdkSerializationRedisSerializer();
        redisTemplate.setKeySerializer(keyRedisSerializer);
        redisTemplate.setValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.setHashKeySerializer(keyRedisSerializer);
        redisTemplate.setHashValueSerializer(jdkSerializationRedisSerializer);
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

RedissonConfig类 :

@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password}")
    private String password;

    @Bean("redissonClient")
    RedissonClient redissonClient() {

//创建配置
    Config config = new Config();

//指定编码,默认编码为org.redisson.codec.JsonJacksonCodec
//之前使用的spring-data-redis,用的客户端jedis,编码为org.springframework.data.redis.serializer.StringRedisSerializer
//改用redisson后为了之间数据能兼容,这里修改编码为org.redisson.client.codec.StringCodec
//        config.setCodec(new org.redisson.client.codec.StringCodec());

//        config.setCodec(new org.redisson.codec.JsonJacksonCodec());

//指定使用单节点部署方式
        SingleServerConfig singleConfig = config.useSingleServer();
//        singleConfig.setAddress("redis://dev.jingzheit.com:6060");
        singleConfig.setAddress("redis://"+host+":"+port);
        //设置密码
        singleConfig.setPassword(password);
//设置对于master节点的连接池中连接数最大为500
        singleConfig.setConnectionPoolSize(500);
        //如果当前连接池里的连接数量超过了最小空闲连接数,而同时有连接空闲时间超过了该数值,
        // 那么这些连接将会自动被关闭,并从连接池里去掉。时间单位是毫秒。
        singleConfig.setIdleConnectionTimeout(10000);
        //同任何节点建立连接时的等待超时。时间单位是毫秒。
        singleConfig.setConnectTimeout(30000);
        //等待节点回复命令的时间。该时间从命令发送成功时开始计时。
        singleConfig.setTimeout(3000);
        singleConfig.setPingTimeout(30000);
        //当与某个节点的连接断开时,等待与其重新建立连接的时间间隔。时间单位是毫秒。
        singleConfig.setReconnectionTimeout(3000);

        //创建客户端(发现创建RedissonClient非常耗时,基本在2秒-4秒左右)
        RedissonClient redissonClient = Redisson.create(config);
        return redissonClient;
    }
}

4.创建实体类:

public class FamilyBo {
    private String name;
    private int gender;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getGender() {
        return gender;
    }

    public void setGender(int gender) {
        this.gender = gender;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public FamilyBo(String name, int gender, int age) {
        this.name = name;
        this.gender = gender;
        this.age = age;
    }

    public FamilyBo() {
    }

    @Override
    public String toString() {
        return "FamilyBo{" +
                "name='" + name + '\'' +
                ", gender=" + gender +
                ", age=" + age +
                '}';
    }
}

5.调用:


    public void setFamilyBo() {
        //从redis里存值 ,RBucket 普通的管道
        FamilyBo familyBo = new FamilyBo("jingliyuan",0,26);
        RBucket serRBucket = redissonClient.getBucket("Family");
        serRBucket .set(familyBo);
        //从redis里取值
        RBucket getRBucket = redissonClient.getBucket("Family");
        FamilyBo result = getRBucket .get();
    }

6.在redis里保存的值为:
Redisson的使用_第1张图片

你可能感兴趣的:(java)