SpringBoot整合Redis Cluster实现增删改

原文链接: https://blog.csdn.net/qq_19636353/article/details/50988423

SpringBoot整合Redis Cluster

SpringBoot2.1.5
Redis5.0.3

Redis集群搭建

Redis5.0集群搭建(Redis Cluster)
https://blog.csdn.net/qq_19636353/article/details/72810960

引入依赖配置

pom.xml


	org.springframework.boot
	spring-boot-starter-data-redis

appliction.properties

属性文件配置

# Redis服务器连接密码
spring.redis.password=123456
## Redis数据库索引(默认为0)
spring.redis.database=0

###连接池设置:初始连接数、最小、最大连接数、最大超时时间
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=1000
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=200
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=3000
spring.redis.commandTimeout=5000

# redis.cluster
spring.redis.cluster.nodes=192.168.236.128:6379,192.168.236.128:6380,192.168.236.128:6381,192.168.236.128:6382,192.168.236.128:6383,192.168.236.128:6384
spring.redis.cluster.max-attempts=100
spring.redis.cluster.test-on-borrow=true

RedisTemplate

RedisTemplate 封装了 RedisConnection,具有连接管理,序列化和 Redis 操作等功能。

Redis操作视图接口类用的是ValueOperations,对应的是Redis String/Value 操作。还有其他的操作视图,ListOperations、SetOperations、ZSetOperations 和 HashOperations 。ValueOperations 插入缓存是可以设置失效时间.

序列化处理类

RedisConf.java

import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;

/**
 * Redis对json序列化处理
 */
@Configuration
public class RedisConfig_JacksonSerializer
{
	@Bean
	public RedisTemplate  getRedisTemplate(RedisConnectionFactory connectionFactory)
	{
		RedisTemplate redisTemplate = new RedisTemplate<>();
		redisTemplate.setConnectionFactory(connectionFactory);
		// 使用Jackson2JsonRedisSerialize替换默认序列化方式
		Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
		StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
		ObjectMapper om = new ObjectMapper();
		om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
		//启用默认的类型
		om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
		//序列化类,对象映射设置
		jackson2JsonRedisSerializer.setObjectMapper(om);
		redisTemplate.setKeySerializer(stringRedisSerializer);
		redisTemplate.setValueSerializer(jackson2JsonRedisSerializer);
		redisTemplate.setHashKeySerializer(stringRedisSerializer);
		redisTemplate.setHashValueSerializer(jackson2JsonRedisSerializer);
		redisTemplate.afterPropertiesSet();
		return redisTemplate;
	}
}

测试

Controller层

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class RedisController {
	@Autowired
	private RedisService redisService;

	@RequestMapping(value = "setUsr")
	public String setUsr(String key, String id, String name, String pwd) {
		Usr usr = new Usr();
		usr.id = id;
		usr.name = "Lyndon-"+id;
		usr.pwd = "123456";
		System.out.println(usr);
		this.redisService.set(id, usr);
		return "true";
	}

	@RequestMapping(value = "getUsr")
	public List getUsr(String key) {
		return this.redisService.get(key);
	}
}
 
  

Service层

import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisServiceImpl implements RedisService
{
	@Autowired
	RedisTemplate redisTemplate;
	
	/** 增删改. */
	@Override
	public boolean set(String key, Usr usr)
	{
		if(this.redisTemplate.hasKey(key))
			return false;
		ListOperations usrs = redisTemplate.opsForList();
		usrs.leftPush(key, usr.id);
		usrs.leftPush(key, usr.name);
		usrs.leftPush(key, usr.pwd);
		System.out.println("【SERVICE】set success, id="+key);
		return true;
	}

	@Override
	public List get(String key)	{
		List usr = redisTemplate.opsForList().range(key, 0, -1);
		return usr;
	}
}

 
  

参考 资料

Spring boot 配置 Redis集群模式
https://blog.csdn.net/qq_20698983/article/details/83056626

Redis学习系列(二)–spring boot整合Redis集群
https://blog.csdn.net/baidu_41669919/article/details/79148203

SpringBoot整合Redis集群
https://blog.csdn.net/qq_26440803/article/details/82724935

Spring Data Redis(Redis Cluster)
https://blog.csdn.net/zlfprogram/article/details/75383864

SpringBoot整合Lettuce Redis
https://blog.csdn.net/Winter_chen001/article/details/80614331

Jedis操作单节点redis,集群及redisTemplate操作redis集群(三)
https://blog.csdn.net/qq_36305027/article/details/80686229

Redis cluster注意的问题
https://blog.csdn.net/duyuanhai/article/details/53198355

秒杀业务
https://www.jianshu.com/p/e759eaba2d07

其他

RedisClusterConnection

集群的支持是基于非集群通讯构建的。RedisClusterConnection 是RedisConnection 的一个扩展,用来处理和Redis Cluster的通讯,转换错误信息到Spring DAO异常层。RedisClusterConnection 是通过RedisConnectionFactory 创建的,该工程的创建要依据于RedisClusterConfiguration配置。

像跨节点收集信息,或发送命令到集群的所有节点上,都由RedisClusterConnection 来处理。

Spring Data Redis(Redis Cluster)
https://blog.csdn.net/zlfprogram/article/details/75383864

import org.springframework.data.redis.connection.RedisClusterConnection;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = { App.class })
public class AppTest
{
	@Autowired
	private RedisConnectionFactory factory;

	@Test
	public void test2() throws Exception
	{
		RedisClusterConnection conn = factory.getClusterConnection();
		System.out.println(conn.keys("dept".getBytes()));
		System.out.println(conn.hKeys("dept".getBytes()));
		System.out.println(conn.clusterGetNodes());
	}
}

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