1.需在project maven的pom.xml增加依赖包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
<exclusions>
<exclusion>
<groupId>io.lettuce</groupId>
<artifactId>lettuce-core</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.在appliation.yml文件增加如下内容,主要用于连接redis cluster
spring:
redis:
cluster:
nodes:
- 127.0.0.1:7000
- 127.0.0.1:7001
- 127.0.0.1:7002
- 127.0.0.1:7003
- 127.0.0.1:7004
- 127.0.0.1:7005
4.增加如下rediscluster class
@Component
@ConfigurationProperties(prefix = "spring.redis.cluster")
public class RedisNodeConfig {
private List<String> nodes;
public List<String> getNodes() {
return nodes;
}
public void setNodes(List<String> nodes) {
this.nodes = nodes;
}
}
5.生成redisconnectionfactory bean
@Bean
public RedisConnectionFactory connectionFactory(@Autowired RedisNodeConfig redisConfig) {
JedisConnectionFactory redisFactory= new JedisConnectionFactory(
new RedisClusterConfiguration(redisConfig.getNodes()));
return redisFactory;
}
6.生成redistemplate bean,注意一定要设定串行化内容,要不然你在redis看key时你会完全看不懂
@Bean
public RedisTemplate redisTemplate(@Autowired RedisConnectionFactory redisConnectFactory) {
RedisTemplate template=new RedisTemplate();
template.setConnectionFactory(redisConnectFactory);
RedisSerializer serializer= template.getStringSerializer();
template.setValueSerializer(serializer);
template.setKeySerializer(serializer);
//template.setHashKeySerializer(serializer);
//template.setHashValueSerializer(template.getDefaultSerializer());
return template;
}
redistemplate主要通过以下方法对相关类型 的数据进行操作
Key Bound Operations | |
| Redis key bound geospatial operations |
| Redis hash key bound operations |
| Redis key bound operations |
| Redis list key bound operations |
| Redis set key bound operations |
| Redis string (or value) key bound operations |
| Redis zset (or sorted set) key bound operations |
7.在某个controller类中增加如下内容
@Autowired
private RedisTemplate redisTemplate;
@GetMapping("/redis/{key}/{value}")
public String redis(@PathVariable String key,@PathVariable String value) {
BoundSetOperations setOper= this.redisTemplate.boundSetOps(key);
this.redisTemplate.boundValueOps(key).set(value);
return this.redisTemplate.boundValueOps(key).get().toString();
}
8.在ie中输入如下内容就可以进行测试,我的port是9001,所以是http://localhost:9001/redis/hello/are%20you%20ok?
相应的返回内容如下:
9.在redis-cli也能看同样的内容
从以上方法看来spring 操作redis数据确实比较简单,如果涉及事务处理可以redistemplate.execute中调用SessionCallback接品实现,方法如下:
List<Object> txResults = redisTemplate.execute(
new SessionCallback<List<Object>>() {
public List<Object> execute(RedisOperations operations) throws DataAccessException {
operations.multi();
operations.opsForSet().add("key", "value1");
operations.exec();
}
});
System.out.println("Number of items added to set: " + txResults.get(0));
另外如果需要把某个对象保存hash数据,可以借助hashmap的方法来进行,具体的参考,如下:
@RequestMapping("/redis/hash/{firstname}/{lastname}")
public Person hashMap(@PathVariable String firstname,@PathVariable String lastname) {
BoundHashOperations hashOps= this.redisTemplate.boundHashOps("hash");
Person person=new Person();
person.setFirstname(firstname);
person.setLastname(lastname);
HashMapper<Object, byte[], byte[]> mapper = new ObjectHashMapper();
Map<byte[], byte[]> mapPerson= mapper.toHash(person);//将object转换成hash数据
hashOps.putAll( mapPerson);
Map<byte[], byte[]> loadedHash = hashOps.entries();
return (Person)mapper.fromHash(loadedHash); //将hash数据转换成某个class对象
//return null;
}
class Person{
private String firstname;
private String lastname;
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
}
测试一样,可以ie中输入http://localhost:9001/redis/hash/wang/linlin
另外对redis操作数据也可以借助于redisconnectfactory的connection来操作数据,不过方法原始,需要通过数据转换成二进制的方式来进行,如下参考,比较简单
@Bean
public String test(@Autowired RedisConnectionFactory redisfactory ) {
System.out.println("==================================");
RedisClusterConnection connect= redisfactory.getClusterConnection();
connect.set("test".getBytes(),"邹林林".getBytes());
connect.hSet("hash".getBytes(), "name".getBytes(), "tom".getBytes());
//System.out.println(new String(connect.get("test".getBytes())));
Set<byte[]> sets= connect.keys("*".getBytes());
for(byte[] set : sets) {
//System.out.println(new String(set)+" = "+new String(connect.get(set)));
System.out.println(new String(set));
}
return null;
}
用以下方法进行下来你在redis-cli看到擞 据内容根据无法看,
不如用redistemplate设定串行化后比较能看明白