Dowemo
0 0 0 0

The springboot integrated redis cluster is much easier than spring mvc. This example uses a redis cluster cluster built with 3 machines for 3 main 3 from the configuration, and the collective build details don't do this. Concrete integration redis steps are as follows:

First, you need to introduce dependencies:

Spring boot provides data access frameworks spring data jedis based on. You can configure dependencies by introducing spring boot starter redis.

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

Parameter configuration:

In application. Block ( ps: if you're using the application. Properties configuration ), add the following configuration:

spring:
 redis:
 cluster:
 nodes:
 - 10.7.28.179:6379
 - 10.7.28.180:6379
 - 10.7.28.181:6379
 - 10.7.28.179:6380
 - 10.7.28.180:6380
 - 10.7.28.181:6380
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active: 8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait: -1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle: 8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle: 0
# 连接超时时间(毫秒)
spring.redis.timeout: 0

Test access:

@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
 @Autowired
 private StringRedisTemplate stringRedisTemplate;
/**
 * redis缓存测试放入普通的字符串
 * @throws Exception
 */
 @Test
 public void testRedisByString() throws Exception {
//保存字符串
 stringRedisTemplate.opsForValue().set("aaa","122");
 Assert.assertEquals("111", stringRedisTemplate.opsForValue().get("aaa"));
 }
}
With the simple tests above, you can store data above redis, but the above approach only supports the storage of string types, and we usually have a common storage object
Create an object to store:
package tf56.SpringBoot.domain;
import java.io.Serializable;
/**
 * 创建需要放入redis的对象
 */
public class UserRedis implements Serializable {
 private static final long serialVersionUID = -1L;
 private String username;
 private Integer age;
 public UserRedis(String username, Integer age) {
 this.username = username;
 this.age = age;
 }
 public String getUsername() {
 return username;
 }
 public void setUsername(String username) {
 this.username=username;
 }
 public Integer getAge() {
 return age;
 }
 public void setAge(Integer age) {
 this.age=age;
 }
}
Implements the serialization interface for an object:
package tf56.SpringBoot.utils;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.serializer.support.DeserializingConverter;
import org.springframework.core.serializer.support.SerializingConverter;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.data.redis.serializer.SerializationException;
/**
 * 实现对象的序列化接口
 */
public class RedisObjectSerializer implements RedisSerializer<Object> {
 private Converter<Object, byte[]> serializer = new SerializingConverter();
 private Converter<byte[], Object> deserializer = new DeserializingConverter();
 static final byte[] EMPTY_ARRAY = new byte[0];
 public Object deserialize(byte[] bytes) {
 if (isEmpty(bytes)) {
 return null;
 }
 try {
 return deserializer.convert(bytes);
 } catch (Exception ex) {
 throw new SerializationException("Cannot deserialize", ex);
 }
 }
 public byte[] serialize(Object object) {
 if (object == null) {
 return EMPTY_ARRAY;
 }
 try {
 return serializer.convert(object);
 } catch (Exception ex) {
 return EMPTY_ARRAY;
 }
 }
 private boolean isEmpty(byte[] data) {
 return (data == null || data.length == 0);
 }
}
Configure I & tances:
package tf56.SpringBoot.utils;
import org.springframework.beans.factory.annotation.Autowired;
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.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.repository.configuration.EnableRedisRepositories;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import tf56.SpringBoot.domain.UserRedis;
import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
/**
 * 配置针对UserRedis的RedisTemplate实例
 */
@Configuration
@EnableRedisRepositories
public class RedisConfig {
/**
 * 如果这里注入了JedisConnectionFactory,加载的时候application.yml中的配置不会起作用,所以
 * 初始化JedisConnectionFactory对象的时候需要加入clusterNodes节点信息
 */
 List<String> clusterNodes = Arrays.asList("10.7.28.179:6379","10.7.28.179:6380","10.7.28.180:6379","10.7.28.180:6380","10.7.28.181:6379","10.7.28.181:6380");
 @Bean
 JedisConnectionFactory jedisConnectionFactory() {
 return new JedisConnectionFactory(new RedisClusterConfiguration(clusterNodes));
 }
 @Bean
 public RedisTemplate<String, UserRedis> redisTemplate(RedisConnectionFactory factory) {
 RedisTemplate<String, UserRedis> template = new RedisTemplate<String, UserRedis>();
 template.setConnectionFactory(jedisConnectionFactory());
 template.setKeySerializer(new StringRedisSerializer());
 template.setValueSerializer(new RedisObjectSerializer());
 return template;
 }
}
After the configuration is complete, write the test implementation effect:
package tf56.SpringBoot.controller;
import org.junit.Assert;
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.data.redis.core.StringRedisTemplate;
import org.springframework.test.context.junit4.SpringRunner;
import tf56.SpringBoot.domain.UserRedis;
/**
 * Created by Administrator on 2017/8/30.
 */
@RunWith(SpringRunner.class)
@SpringBootTest
public class TestRedis {
 @Autowired
 private RedisTemplate<String, UserRedis> redisTemplate;
/**
 * redis 缓存测试存放对象
 * @throws Exception
 */
 @Test
 public void testRedisByObject() throws Exception {
//保存对象
 UserRedis user = new UserRedis("超人", 20);
 redisTemplate.opsForValue().set(user.getUsername(), user);
 user = new UserRedis("蝙蝠侠", 30);
 redisTemplate.opsForValue().set(user.getUsername(), user);
 user = new UserRedis("蜘蛛侠", 40);
 redisTemplate.opsForValue().set(user.getUsername(), user);
 System.out.println(redisTemplate.opsForValue().get("超人").getAge());
 System.out.println(redisTemplate.opsForValue().get("蝙蝠侠").getAge());
 System.out.println(redisTemplate.opsForValue().get("蜘蛛侠").getAge());
 }
}









Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs