Redis Cluster Cache with SpringBoot
前提:
根据 https://www.cnblogs.com/luffystory/p/12081074.html
创建好Redis集群

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.study</groupId>
<artifactId>SpringBootTest_RedisCluster</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>SpringBootTest-2</name>
<description>Demo project for Spring Boot</description> <properties>
<java.version>1.8</java.version>
</properties> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency> <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>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency> </dependencies> <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> </project>
RedisCacheManager 中使用 configMap 做如下 c1 和 redisCacheConfig 的mapping
configMap.put("c1" , redisCacheConfig);
如下
@Cacheable(value = "c1")
@Cacheable(vaule = "c2")
c1 存在于configMap 中,因此使用的缓存策略是 configMap 集合中 c1 所对应的缓存策略;
c2 不存在于 configMap 集合中,因此使用的缓存策略是默认的缓存策略。
RedisCacheManager redisCacheManager = new RedisCacheManager(cacheWriter,
RedisCacheConfiguration.defaultCacheConfig(), configMap);
package com.rediscluster.cache; import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Repository; @Repository
public class BookDao { @Cacheable(value = "c1")
public String getBookById(Integer id) {
System.out.println("getBookById");
return "Book : SanGuoYanYi";
} @CachePut(value = "c1")
public String updateBookById(Integer id) {
return "Brand new Book : SanGuoYanYi";
} @CacheEvict(value = "c1")
public void deleteById(Integer id) {
System.out.println("deleteById");
} @Cacheable(value = "c2")
public String getBookById2(Integer id) {
System.out.println("getBookById2");
return "Book : HongLouMeng";
} }
package com.rediscluster.cache; import java.time.Duration;
import java.util.HashMap;
import java.util.Map; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory; @Configuration
public class RedisCacheConfig { @Autowired
RedisConnectionFactory conFactory; @Bean
RedisCacheManager redisCacheManager() {
Map<String, RedisCacheConfiguration> configMap = new HashMap<>();
RedisCacheConfiguration redisCacheConfig = RedisCacheConfiguration.defaultCacheConfig().prefixKeysWith("xluffy:")
.disableCachingNullValues().entryTtl(Duration.ofMinutes(30)); configMap.put("c1" , redisCacheConfig); RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(conFactory); RedisCacheManager redisCacheManager = new RedisCacheManager(cacheWriter,
RedisCacheConfiguration.defaultCacheConfig(), configMap); return redisCacheManager;
}
}
package com.rediscluster.cache; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching; @SpringBootApplication
@EnableCaching
public class RedisClusterCacheApplication { public static void main(String[] args) {
SpringApplication.run(RedisClusterCacheApplication.class, args);
} }
package com.rediscluster.cache; import java.util.ArrayList;
import java.util.List; import org.springframework.boot.context.properties.ConfigurationProperties;
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.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer; import redis.clients.jedis.JedisPoolConfig; @Configuration
@ConfigurationProperties("spring.redis.cluster")
public class RedisConfig { List<Integer> ports;
String host;
JedisPoolConfig poolConfig; @Bean
RedisClusterConfiguration redisClusterConfiguarion() {
RedisClusterConfiguration configuration = new RedisClusterConfiguration();
List<RedisNode> nodes = new ArrayList<>();
for(Integer port : ports) {
nodes.add(new RedisNode(host,port));
}
configuration.setClusterNodes(nodes);
return configuration;
} @Bean
JedisConnectionFactory jedisConnectionFactory() {
JedisConnectionFactory factory = new JedisConnectionFactory(redisClusterConfiguarion(), poolConfig);
return factory;
} @Bean
RedisTemplate redisTempalte() {
RedisTemplate redisTempalte = new RedisTemplate();
redisTempalte.setConnectionFactory(jedisConnectionFactory());
redisTempalte.setKeySerializer(new StringRedisSerializer());
redisTempalte.setValueSerializer(new JdkSerializationRedisSerializer()); return redisTempalte;
} @Bean
StringRedisTemplate stringRedisTemplate() {
StringRedisTemplate stringRedisTemplate = new StringRedisTemplate(jedisConnectionFactory());
stringRedisTemplate.setKeySerializer(new StringRedisSerializer());
stringRedisTemplate.setValueSerializer(new StringRedisSerializer()); return stringRedisTemplate;
} public List<Integer> getPorts() {
return ports;
} public void setPorts(List<Integer> ports) {
this.ports = ports;
} public String getHost() {
return host;
} public void setHost(String host) {
this.host = host;
} public JedisPoolConfig getPoolConfig() {
return poolConfig;
} public void setPoolConfig(JedisPoolConfig poolConfig) {
this.poolConfig = poolConfig;
} }
server:
port: 8091
spring:
redis:
cluster:
ports:
- 8001
- 8002
- 8003
- 8004
- 8005
- 8006
- 8007
- 8008
host: 192.168.157.131
poolConfig:
max-total: 8
max-idle: 8
max-wait-millis: -1
min-idle: 0
package com.rediscluster.cache; 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.test.context.junit4.SpringRunner; @RunWith(SpringRunner.class)
@SpringBootTest
public class RedisClusterCacheApplicationTests { @Autowired
BookDao bookDao; @Test
public void contextLoad() {
bookDao.getBookById(100);
String book = bookDao.getBookById(100);
System.out.println(book); bookDao.updateBookById(100); String book2 = bookDao.getBookById(100);
System.out.println(book2); bookDao.deleteById(100);
bookDao.getBookById(100); bookDao.getBookById2(99);
}
}
代码配置好之后 run RedisClusterCacheApplicationTests as Junit Test, test 通过之后
可以看到方法的参数和返回值已被缓存到 Redis 中。

Redis Cluster Cache with SpringBoot的更多相关文章
- Redis Cluster with SpringBoot
前提: 按照 https://www.cnblogs.com/luffystory/p/12081074.html 配置好Redis Cluster in Ubuntu 按照如下结构搭建项目结构: P ...
- 实践篇 -- Redis客户端缓存在SpringBoot应用的探究
本文探究Redis最新特性--客户端缓存在SpringBoot上的应用实战. Redis Tracking Redis客户端缓存机制基于Redis Tracking机制实现的.我们先了解一下Redis ...
- Redis Cluster 集群搭建与扩容、缩容
说明:仍然是伪集群,所有的Redis节点,都在一个服务器上,采用不同配置文件,不同端口的形式实现 前提:已经安装好了Redis,本文的redis的版本是redis-6.2.3 Redis的下载.安装参 ...
- jedis处理redis cluster集群的密码问题
环境介绍:jedis:2.8.0 redis版本:3.2 首先说一下redis集群的方式,一种是cluster的 一种是sentinel的,cluster的是redis 3.0之后出来新的集群方式 本 ...
- Redis Cluster的搭建与部署,实现redis的分布式方案
前言 上篇Redis Sentinel安装与部署,实现redis的高可用实现了redis的高可用,针对的主要是master宕机的情况,我们发现所有节点的数据都是一样的,那么一旦数据量过大,redi也会 ...
- Redis Cluster 4.0 on CentOS 6.9 搭建
集群简介 Redis 集群是一个可以在多个 Redis 节点之间进行数据共享的设施(installation). Redis 集群不支持那些需要同时处理多个键的 Redis 命令, 因为执行这些命令需 ...
- Redis Cluster架构优化
Redis Cluster架构优化 在<全面剖析Redis Cluster原理和应用>中,我们已经详细剖析了现阶段Redis Cluster的缺点: 无中心化架构 Gossip消息的开销 ...
- Redis Cluster(集群)
一.概述 在前面的文章中介绍过了redis的主从和哨兵两种集群方案,redis从3.0版本开始引入了redis-cluster(集群).从主从-哨兵-集群可以看到redis的不断完善:主从复制是最简单 ...
- redis cluster介绍
讲解分布式数据存储的核心算法,数据分布的算法 hash算法 -> 一致性hash算法(memcached) -> redis cluster,hash slot算法 一.概述 1.我们的m ...
随机推荐
- springMvc接受单个文件,多个文件,多组文件
web端 <form id="iconForm" enctype="multipart/form-data"></form> JS:通过 ...
- Spring Boot 获取yaml配置文件信息
Spring boot 项目启动过程中: org.springframework.boot.SpringApplication#prepareEnvironment 当程序步入listeners.en ...
- 关于redis的几件小事(五)redis保证高并发以及高可用
如果你用redis缓存技术的话,肯定要考虑如何用redis来加多台机器,保证redis是高并发的,还有就是如何让Redis保证自己不是挂掉以后就直接死掉了,redis高可用 redis高并发:主从架构 ...
- 微信小程序swiper使用网络图片不显示问题
@ wxml代码: <view class="container"> <swiper indicator-dots="true}" autop ...
- electron localStorage的bug
在更新 electron 后有可能会读不到 localStorage 里的数据 推测是 localStorage 写在 Chromium 内核里,更新 electron 同时会更新 Chromium, ...
- IDEA打开光标是粗黑色,backspace键、insert键点击无效的解决办法
问题描述:打开IDEA的编译器之后,界面显示的光标变粗,点击backspace键和insert键盘之后无效 解决方法:打开File——Settings——Plugins,在右侧的搜索栏中搜索IdeaV ...
- Delphi 监视数据的值
- php关于系统环境配置的一些函数
disk_free_space() :返回指定目录的可用空间(以字节为单位)
- MST-prim ElogV
#include<bits/stdc++.h> #define ll long long using namespace std; ; ; struct node { int t;int ...
- context容器上下文件
在web项目中想要获取哪个bean,得先得到容器上下文context public class MyLoaderListener extends ContextLoader implements Se ...