Java 实现 Redis集群操作

Java 实现 Redis集群操作

1. 导入pom依赖

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
	<groupId>redis.clients</groupId>
	<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
	<groupId>commons-io</groupId>
	<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>

2. 写入配置文件并创建配置文件管理类

这里是在application.properties核心配置文件中写入配置

# redis集群配置
# 连接池最大阻塞等待时间(使用负值表示没有限制
spring.redis.jedis.pool.max-wait = 5000
# 连接池中的最大空闲连接
spring.redis.jedis.pool.max-Idle = 100
# 连接池中的最小空闲连接
spring.redis.jedis.pool.min-Idle = 10
# 连接超时时间(毫秒)
spring.redis.timeout = 10
# 集群地址
spring.redis.cluster.nodes = 192.168.17.101:6379,192.168.17.101:6380,192.168.17.101:6381
# 集群最大跳转数
spring.redis.cluster.max-redirects=5
package com.dbh123.dubbo.api.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @description: 配置文件管理类
 * @author: DBH123
 * @date: 2021/6/29 22:42
 */
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisClusterConfigurationProperties {
    private List<String> nodes;
    private Integer maxRedirects;

    public List<String> getNodes() {
        return nodes;
    }

    public void setNodes(List<String> nodes) {
        this.nodes = nodes;
    }

    public Integer getMaxRedirects() {
        return maxRedirects;
    }

    public void setMaxRedirects(Integer maxRedirects) {
        this.maxRedirects = maxRedirects;
    }
}

4. 创建RedisTemple对象

package com.dbh123.dubbo.api.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

import java.util.List;

/**
 * @description: Redis对象注入
 * @author: DBH123
 * @date: 2021/6/29 22:44
 */
@Configuration
public class RedisClusterConfig {
    @Autowired
    private RedisClusterConfigurationProperties redisClusterConfigurationProperties;

    private List<String> nodes;
    private Integer maxRedirects;

    @Bean
    public RedisConnectionFactory getConnectionFactory(){
        RedisClusterConfiguration clusterConfiguration = new RedisClusterConfiguration(redisClusterConfigurationProperties.getNodes());
        clusterConfiguration.setMaxRedirects(redisClusterConfigurationProperties.getMaxRedirects());
        return new JedisConnectionFactory(clusterConfiguration);
    }

    @Bean
    public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<String, String> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.afterPropertiesSet();
        return redisTemplate;
    }
}

5. 测试

package com.dbh123.dubbo.api;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Set;

/**
 * @description: redis 测试
 * @author: DBH123
 * @date: 2021/6/29 22:35
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
    @Autowired
     RedisTemplate<String, String> redisTemplate;
    @Test
    public void testSave(){
        for (int i = 0 ; i < 100 ; i++){
            redisTemplate.opsForValue().set("key_" + i , "value_" + i);
        }
        Set<String> keys = redisTemplate.keys("key_*");
        for (String key : keys){
            String value = redisTemplate.opsForValue().get(key);
            System.out.println(value);
        }
    }
}

6. RedisTemplate操作详细见转载文章
RedisTemplate操作Redis,这一篇文章就够了(一)

你可能感兴趣的:(java,redis,java)