1、加入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>1.8.1.RELEASE</version>
</dependency>
2、application.yml:
redis:
clusterNodes: 10.10.39.11:7111,10.10.39.11:7112,10.10.39.11:7113,10.10.39.11:7114,10.10.39.11:7115,10.10.39.11:7116
maxWaitMillis: 5000
maxTotal: 1000
minIdle: 10
maxIdle: 20
3、读取配置:
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisProperties {
private String clusterNodes;
private int maxWaitMillis;
private int maxTotal;
private int minIdle;
private int maxIdle;
public String getClusterNodes() {
return clusterNodes;
}
public void setClusterNodes(String clusterNodes) {
this.clusterNodes = clusterNodes;
}
public int getMaxWaitMillis() {
return maxWaitMillis;
}
public void setMaxWaitMillis(int maxWaitMillis) {
this.maxWaitMillis = maxWaitMillis;
}
public int getMaxTotal() {
return maxTotal;
}
public void setMaxTotal(int maxTotal) {
this.maxTotal = maxTotal;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
}
4、构造Bean:
@Configuration
public class JedisClusterConfig {
@Autowired
private RedisProperties redisProperties;
@Bean
public JedisPoolConfig getJedisPoolConfig(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(redisProperties.getMaxTotal());
jedisPoolConfig.setMaxIdle(redisProperties.getMaxIdle());
jedisPoolConfig.setMinIdle(redisProperties.getMinIdle());
jedisPoolConfig.setMaxWaitMillis(redisProperties.getMaxWaitMillis());
return jedisPoolConfig;
}
@Bean
public JedisCluster getJedisCluster(){
String[] serverArray = redisProperties.getClusterNodes().split(",");
Set<HostAndPort> nodes = new HashSet<>();
for(String server:serverArray){
String[] ipPortPair = server.split(":");
nodes.add(new HostAndPort(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim())));
}
return new JedisCluster(nodes, getJedisPoolConfig());
}
@Bean
public JedisConnectionFactory getJedisConnectionFactory(){
RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
HashSet<RedisNode> nodes = new HashSet<>();
String[] serverArray = redisProperties.getClusterNodes().split(",");
for(String server:serverArray){
String[] ipPortPair = server.split(":");
RedisNode redisNode = new RedisNode(ipPortPair[0].trim(), Integer.valueOf(ipPortPair[1].trim()));
nodes.add(redisNode);
}
redisClusterConfiguration.setClusterNodes(nodes);
JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, getJedisPoolConfig());
return jedisConnectionFactory;
}
}
5、直接操作Redis集群:
@Component
public class TestRedis {
@Autowired
private JedisCluster jedisCluster;
@PostConstruct
public void run() {
for (int i = 0; i < 100; i++) {
//jedisCluster.set("redis"+i, "key"+i);
String value= jedisCluster.get("redis"+i);
System.err.println(value);
}
}
}
6、作为缓存使用:
@Service
public class TestService {
//用来模拟对数据库的操作
private Map<Integer, String> dataMap = new HashMap<>();
@PostConstruct
public void init(){
for(int i=1; i<4; i++){
dataMap.put(i, "value"+i);
}
}
/**
* 查询
* 如果数据没有缓存,那么从dataMap里面获取
* 如果缓存了,那么从RedisCache里面获取
* 并且将缓存的数据存入到 RedisCache里面
* 其中key 为 dataMap_ + #id
*/
@Cacheable(value = "RedisCache", key = "'dataMap_' + #id")
public String query(int id){
System.out.println("["+getDateNow()+"]:"+id);
return dataMap.get(id);
}
/**
* 插入或更新数据到dataMap中
* 并且缓存到 RedisCache中
* 如果存在了那么更新缓存中的值
* 其中key 为 dataMap_ + #id
*/
@CachePut(value = "RedisCache", key = "'dataMap_' + #id")
public String add(int id, String value){
System.out.println("["+getDateNow()+"]:"+id);
dataMap.put(id, value);
return value;
}
/**
* 删除dataMap里面的数据
* 并且删除缓存RedisCache中的数据
* 其中key 为 dataMap_ + #id
*/
@CacheEvict(value = "RedisCache", key = "'dataMap_' + #id")
public void delete(int id){
System.out.println("["+getDateNow()+"]:"+id);
dataMap.remove(id);
}
private static String getDateNow(){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(new Date());
}
}
@RestController
@EnableCaching
public class TestCache {
@Autowired
private TestService testService;
@RequestMapping("get")
public String query(int id){
return "["+getDateNow()+"]:"+testService.query(id);
}
@RequestMapping("put")
public String add(int id, String value){
return "["+getDateNow()+"]:"+testService.add(id ,value);
}
@RequestMapping("del")
public String delete(int id){
testService.delete(id);
return "["+getDateNow()+"]:"+id+" delete success";
}
private static String getDateNow(){
DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
return dateFormat.format(new Date());
}
}
项目源码:点击打开链接