由于代码中用不到jedis了,做个记录,万一以后还用的着呢,对吧
由于我是要用到双机热备,所以用到了主从redis ,服务器中的配置详见上一篇文章,这里是springboot中的使用和配置
首先是在application.properties文件中配置redis
spring.redis.host=10.5.133.213
spring.redis.port=6379
spring.redis.pool.max-idle=8
spring.redis.pool.min-idle=0
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
spring.redis.sentinel.master=mymaster
spring.redis.sentinel.nodes=10.5.133.219:26380
spring.redis.timeout=0
spring.redis.sentinel.master=mymaster是主redis的名字,spring.redis.sentinel.nodes=10.5.133.219:26380是sentinel的ip和端口,这里可以配置多个,但是我只用到一个,所以就配置了一个
spring.reids.timout 这个我一开始是放到前面的,可以代码运行不起来,报错,最后放到了最后一行,就可以了,没有详细研究这个是为什么
用jedis,还要配置他的连接池,这个是在代码中配置的
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.JedisShardInfo;
import redis.clients.jedis.ShardedJedis;
import redis.clients.jedis.ShardedJedisPool;
import java.util.ArrayList;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@Configuration
@EnableCaching
public class RedisCacheConfiguration extends CachingConfigurerSupport {
Logger logger = LoggerFactory.getLogger(RedisCacheConfiguration.class);
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.pool.max-wait}")
private long maxWaitMillis;
@Value("${spring.redis.sentinel.nodes}")
private String sentinelMaster;
/* @Value("${spring.redis.password}")
private String password;*/
@Bean
public ShardedJedisPool redisPoolFactory() {
// 生成多机连接信息列表
int m=sentinelMaster.toString().indexOf(":");
List<JedisShardInfo> shards = new ArrayList<JedisShardInfo>();
shards.add( new JedisShardInfo("10.5.133.213", 6379) );
shards.add( new JedisShardInfo(sentinelMaster.toString().substring(0, m),Integer.parseInt(sentinelMaster.toString().substring(m+2))) );
// 生成连接池配置信息
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxIdle(10);
config.setMaxTotal(30);
config.setMaxWaitMillis(3*1000);
// 在应用初始化的时候生成连接池
ShardedJedisPool pool = new ShardedJedisPool(config, shards);
return pool;
}
}
这里是分布式的连接池,两个,一个主一个从,当从主切换到从的时候,一开始出现了一个错误,因为从是只读,所以不能写入,然后将主从的redis.conf中的
slave-read-only yes 改为no
这样就可以写入了。
然后使用jedis连接池
@Autowired
ShardedJedisPool jedisPool;
public void moveAllMap(){
ShardedJedis jedis = jedisPool.getResource();
TrainTraceData trainTraceData=new TrainTraceData();
trainTraceData.setServTag(servTag);
byte[] ll = SerializeUtil. serialize(trainTraceData);
jedis.set( "good".getBytes(),ll);
byte[] value = jedis.get( "good".getBytes());
Object object = SerializeUtil. unserialize(value);
if(object!= null){
TrainTraceData goods=(TrainTraceData) object;
System. out.println(goods.getServTag());
}
System. out.println(jedis.del( "good".getBytes()));
}
import java.io.ObjectOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
public class SerializeUtil {
public static byte[] serialize(Object object) {
ObjectOutputStream oos = null;
ByteArrayOutputStream baos = null;
try {
// 序列化
baos = new ByteArrayOutputStream();
oos = new ObjectOutputStream(baos);
oos.writeObject(object);
byte[] bytes = baos.toByteArray();
return bytes;
} catch (Exception e) {
System.out.println("====错误===="+e);
}
return null;
}
public static Object unserialize( byte[] bytes) {
ByteArrayInputStream bais = null;
try {
// 反序列化
bais = new ByteArrayInputStream(bytes);
ObjectInputStream ois = new ObjectInputStream(bais);
return ois.readObject();
} catch (Exception e) {
}
return null;
}
}