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 ...
随机推荐
- 阻塞I/O、非阻塞I/O和I/O多路复用、怎样理解阻塞非阻塞与同步异步的区别?
“阻塞”与"非阻塞"与"同步"与“异步"不能简单的从字面理解,提供一个从分布式系统角度的回答.1.同步与异步 同步和异步关注的是消息通信机制 (syn ...
- python3.7 利用pyhive 连接上hive(亲测可用)
来python爬虫中,经常会遇到数据的存储问题,如果有大量数据,hive存储是个不错的选择. 那么python如何来连接hive呢?网上有各种教程但是都不是很好用,亲自测试pyhive可用 要求:可用 ...
- postgres外部表
在创建外部表的时候遇见: CREATE EXTENSION file_fdw;2018-12-21 17:32:23.822 CST [31237] ERROR: could not open ex ...
- commons Collections4 MultiMap
MultiMap<String, Integer> multiMap = new MultiValueMap<>(); multiMap.put("A", ...
- Ubuntu与centos的区别小用法
给root设置密码 更新软件下载的地址 安装指令apt 使用ssh登录Ubuntu 使用ssh登录Ubuntu必须注意的地方,要先配置 sudo vi /etc/ssh/sshd_config 找到: ...
- django优化--ORM优缺点
谈Django绕不开ORM ORM : ORM概念,ORM特点,ORM 的优点,ORM 的缺点 orm : 对象关系映射 (Object Relational Mapping) ,用于实现面向对象编程 ...
- Ubuntu环境变量设置注意点
设置环境变量时,有一点要注意: /etc/bash.bashrc与/etc/profile是有区别的 什么区别呢? 打开一个新的shell时,会读取/etc/bash.bashrc和~/.bashrc ...
- CentOS7使用阿里云的yum源
替换成阿里云的yum源速度更快一些,替换很简单,简单记录一下步骤 1.备份原来的yum源 sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repo ...
- android studio 下载sdk
一.打开android studio 的"setting",搜索 "proxy",选中"No Proxy"
- adb简介
Android 调试桥 (adb) 是一种功能多样的命令行工具,可让您与设备进行通信.adb 命令便于执行各种设备操作(例如安装和调试应用),并提供对 Unix shell(可用来在设备上运行各种命令 ...