Redisson配置类:
package com.example.demo.configuration;
import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
/**
* 修改日期:2020/3/29
* 修改人:牟松
* redisson 配置类
**/
@Configuration
public class RedissonConfig {
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private String port;
@Bean
public RedissonClient getRedisson(){
Config config = new Config();
config.useSingleServer().setAddress("redis://" + host + ":" + port);
//添加主从配置
//config.useMasterSlaveServers().setMasterAddress("").setPassword("").addSlaveAddress(new String[]{"",""});
return Redisson.create(config);
}
}
RedissonService操作类:
package com.example.demo.configuration;
import org.redisson.api.*;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.IOException;
/**
* 修改日期:2020/3/29
* 修改人:牟松
* Redisson操作类
**/
@Service("redissonService")
public class RedissonService {
@Autowired
private RedissonClient redissonClient;
public void getRedissonClient() throws IOException {
Config config = redissonClient.getConfig();
System.out.println(config.toJSON().toString());
}
/**
* 获取字符串对象
*
* @param objectName
* @return
*/
public
RBucket
return bucket;
}
/**
* 获取Map对象
*
* @param objectName
* @return
*/
public <K, V> RMap<K, V> getRMap(String objectName) {
RMap<K, V> map = redissonClient.getMap(objectName);
return map;
}
}
获取结果:
针对Rdis的字符串(String)类型数据
RBucket rBucket=redissonService.getRBucket("piao");
System.out.println(rBucket.get());
针对Rdis的hash类型数据
RMap<String, String> bucket = redissonService.getRMap("objectname"); //缓存名称
System.out.println(bucket.get("key")); //字段名称