Redis+SpringBoot企业版集群实战------【华为云版】

这篇具有很好参考价值的文章主要介绍了Redis+SpringBoot企业版集群实战------【华为云版】。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

目录

安装

复制及集群

bgsave

 rdb

aof

SpringBoot+Redis操作

操作string

操作hash

操作set

操作sorted set

获取所有key&删除

设置key的失效时间

SpringDataRedis整合使用哨兵机制


安装

下载地址

Redis

上传至服务器

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 解压

tar zxvf redis-5.0.3.tar.gz

安装依赖

yum -y install gcc-c++ autoconf automake

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 预编译

切换到解压目录

cd redis-5.0.3/
make

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

创建安装目录

mkdir -p /usr/local/redis

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

不使用:make install(make install默认安装到/usr/local/bin目录下)

使用:如果需要指定安装路径,需要添加PREFIX参数

make PREFIX=/usr/local/redis/ install

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

安装成功如图

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

Redis-cli :客户端

Redis-server :服务器端

安装的默认目标路径:/usr/local/redis/bin

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 启动

./redis-server

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 默认为前台启动,修改为后台启动

复制redis.conf至安装路径下

cp redis.conf /usr/local/redis/bin/

修改安装路径下的redis.conf,将 daemonize 修改为yes

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

启动时,指定配置文件路径即可

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 通过windows客户端访问

安装Redis客户端

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

建立连接->失败 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 修改配置文件redis.conf

注释掉 bind 127.0.0.1 可以使所有的ip访问redis,若是想指定多个ip访问,但并不是全部的ip访问,可以bind设置

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

关闭保护模式,修改为no

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

添加访问认证 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

修改后kill -9 XXXX杀死redis进程,重启redis 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 再次建立连接 -> 成功

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 我们可以修改默认数据库的数量 默认16

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 修改database 32则默认为32个数据库

修改后kill -9 XXXX杀死redis进程,重启redis即可看到效果

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

复制及集群

持久化方案

bgsave

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 rdb

redis.conf  中的 dbfilename dump.rdb  配置(rdb是默认开启的)

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 会生成一个 dump.rdb 文件

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 输入命令 进入 dump.rdb 文件(vim  dump.rdb

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

下面的意思是(可以根据自己需求进行添加):

 1、900秒之内有一个key发生变化就会把数据存入到磁盘里面

 2、300秒之内有十个key发生变化就会把数据存入到磁盘里面

 3、60秒之内有一万个key发生变化就会把数据存入到磁盘里面

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

aof

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 如何进行开启,把 appendonly 改成 yes

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

会发现多了一个 appendonly.aof 文件

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

添加一个key

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

打开 appendonly.aof 文件 ,如下图所示:

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

主从复用

读写分离

创建三个目录(数据文件、日志文件、配置文件)

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 复制redis.conf至/opt/redis/conf目录下

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

修改redis-common.conf公共配置文件 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

注释掉bind 127.0.0.1

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 关闭保护模式,修改为no

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

注释公共配置端口 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 修改为后台启动

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

注释进程编号记录文件 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 注释公共配置日志文件

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

注释公共配置数据文件、修改数据文件路径 

在默认情况下,Redis 将数据库快照保存在名字为 dump.rdb 的二进制文件中。当然,这里可以通过修改 redis.conf 配置文件来对数据存储条件进行定义,规定在“ N 秒内数据集至少有 M 个改动”这一条件被满足时,自动保存一次数据集。也可以通过调用save 或bgsave ,手动让Redis进行数据集保存操作

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 添加从服务器访问主服务器认证

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

添加访问认证 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

注释公共配置追加文件 

根据需求配置是否打开追加文件选项

appendonly yes -> 每当Redis执行一个改变数据集的命令时(比如 SET),这个命令就会被追加到 AOF 文件的末尾。这样的话,当Redis重新启时,程序就可以通过重新执行 AOF文件中的命令来达到重建数据集的目的

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

appendfilenamedir组合使用,找dir(/opt/redis/data)路径生成数据文件 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 从服务器默认是只读不允许写操作(不用修改)

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

添加3个服务的私有配置文件 

touch 或者 vi 都可以创建空白文件

touch 直接创建空白文件, vi 创建并且进入编辑模式, :wq 创建成功,否则不创建

cd /opt/redis/conf/

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划​​​​​​​ 

 redis-6379.conf

#引用公共配置
include /opt/redis/conf/redis-common.conf 
#进程编号记录文件
pidfile /var/run/redis-6379.pid 
#进程端口号
port 6379 
#日志记录文件
logfile "/opt/redis/log/redis-6379.log" 
#数据记录文件
dbfilename dump-6379.rdb 
#追加文件名称
appendfilename "appendonly-6379.aof" 
#下面的配置无需在6379里配置
#备份服务器从属于6379推荐配置配局域网IP 
slaveof 192.168.10.100 6379 

复制redis-6379.conf的内容至redis-6380.conf,redis-6381.conf并且修改其内容,将6379替换即可。

运行3个redis进程

cd /usr/local/redis/bin/

​​​​​​​Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 查看redis服务器主从状态

redis-6379

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划 

redis-6380

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

redis-6381

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

在主服务器下添加数据并测试从服务器数据是否正常显示 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

从服务器只读,不允许写操作

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 主备切换

主从节点redis.conf配置

参照 读写分离 的相应配置

修改sentinel-common.conf 哨兵公共配置文件

从redis解压目录下复制sentinel.conf至/opt/redis/conf/
cp sentinel.conf /opt/redis/conf/sentinel-common.conf

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

注释哨兵监听进程端口号

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

指示 Sentinel 去监视一个名为 master 的主服务器,这个主服务器的IP地址为 127.0.0.1,端口号为6379,而将这个主服务器判断为失效至少需要1个(一般设置为2个)。 Sentinel 同意 (只要同意 Sentinel 的数量不达标,自动故障迁移就不会执行)。 这个要配局域网IP,否则远程连不上。 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

设置masterslaves的密码

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

Sentinel 认为服务器已经断线所需的毫秒数 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 若 sentinel 在该配置值内未能完成 failover 操作(即故障时master/slave自动切换),则认为本次 failover 失败。

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 关闭保护模式,修改为no

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划​​​​​​​

 修改为后台启动

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 添加3个哨兵的私有配置文件

touch 或者 vi 都可以创建空白文件

touch 直接创建空白文件, vi 创建并且进入编辑模式, :wq 创建成功,否则不创建

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 sentinel-26379.conf

#引用公共配置
include /opt/redis/conf/sentinel-common.conf 
#进程端口号
port 26379 
#进程编号记录文件
pidfile /var/run/sentinel-26379.pid 
#日志记录文件(为了方便查看日志,先注释掉,搭好环境后再打开) 
logfile "/opt/redis/log/sentinel-26379.log" 

复制 sentinel-26379.conf 的内容至 sentinel-26380.conf , sentinel-26381.conf 并且修改其内容,将26379 替换即可。

启动测试 

启动3个redis服务

/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6379.conf 
/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6380.conf 
/usr/local/redis/bin/redis-server /opt/redis/conf/redis-6381.conf 

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

启动3个哨兵服务 

/usr/local/redis/bin/redis-sentinel /opt/redis/conf/sentinel-26379.conf 
/usr/local/redis/bin/redis-sentinel /opt/redis/conf/sentinel-26380.conf 
/usr/local/redis/bin/redis-sentinel /opt/redis/conf/sentinel-26381.conf

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

查看主从状态 

redis-6379

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

redis-6380 Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 redis-6381

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

检测哨兵功能是否配置成功 

kill -9 终止redis-6379,查看哨兵是否选举了新的主节点

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划​​​​​​​

 已选举6380为主节点,从节点目前只有6381

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 重新启动6379节点,再次查看主从状态

发现6379已被发现且成为从节点

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 6380之前不可以写操作,现在可以写操作,因为已成为主节点。

最后,公共配置文件修改为后台启动,私有配置文件打开日志记录文件,环境搭建成功。

SpringBoot+Redis操作

创建项目

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

 Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

Redis+SpringBoot企业版集群实战------【华为云版】,Spring全家桶,redis,spring boot,华为云,原力计划

添加依赖 

<dependencies> 
     <!-- spring data redis 组件 --> 
     <dependency> 
         <groupId>org.springframework.boot</groupId>
         <artifactId>spring-boot-starter-data-redis</artifactId> 
     </dependency> 

     <!-- commons-pool2 对象池依赖 --> 
     <dependency> 
         <groupId>org.apache.commons</groupId> 
         <artifactId>commons-pool2</artifactId> 
     </dependency> 

     <!-- web 组件 --> 
     <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-web</artifactId> 
     </dependency> 

     <!-- test 组件 --> 
     <dependency> 
         <groupId>org.springframework.boot</groupId> 
         <artifactId>spring-boot-starter-test</artifactId> 
     <scope>test</scope> 
   </dependency> 
</dependencies>

 添加application.yml配置文件

spring: 
   redis: 
     # Redis服务器地址
     host: 192.168.10.100 
     # Redis服务器端口
     port: 6379 
     # Redis服务器端口
     password: root 
     # Redis服务器端口
     database: 0 
     # 连接超时时间
     timeout: 10000ms 
     lettuce: 
       pool: 
         # 最大连接数,默认8 
         max-active: 1024 
         # 最大连接阻塞等待时间,单位毫秒,默认-1ms 
         max-wait: 10000ms 
         # 最大空闲连接,默认8 
         max-idle: 200 
         # 最小空闲连接,默认0 
         min-idle: 5 

测试环境测试环境是否搭建成功

@RunWith(SpringRunner.class) 
@SpringBootTest(classes = SpringDataRedisApplication.class) 
public class SpringDataRedisApplicationTests { 

     @Autowired 
     private RedisTemplate redisTemplate; 
     @Autowired 
     private StringRedisTemplate stringRedisTemplate; 

     @Test 
     public void initconn() { 
         ValueOperations<String, String> ops = stringRedisTemplate.opsForValue(); 
         ops.set("username","lisi"); 
         ValueOperations<String, String> value = redisTemplate.opsForValue(); 
         value.set("name","wangwu"); 
         System.out.println(ops.get("name")); 
     } 
} 

自定义模板解决序列化问题

默认情况下的模板 RedisTemplate,默认序列化使用的是 JdkSerializationRedisSerializer ,存储二进制字节码。这时需要自定义模板,当自定义模板后又想存储 String 字符串时,可以使StringRedisTemplate的方式,他们俩并不冲突。

序列化问题:

要把 domain object 做为 key-value 对保存在 redis 中,就必须要解决对象的序列化问题。Spring Data Redis给我们提供了一些现成的方案: 

JdkSerializationRedisSerializer 使用JDK提供的序列化功能。 优点是反序列化时不需要提供类型信息(class), 但缺点是序列化后的结果非常庞大,是JSON格式的5倍左右,这样就会消耗 Redis 服务器的大量内存。

Jackson2JsonRedisSerializer 使用 Jackson 库将对象序列化为JSON字符串。优点是速度快,序列化后的字符串短小精悍。但缺点也非常致命,那就是此类的构造函数中有一个类型参数,必须提供要序列化对象的类型信息(.class 对象)。通过查看源代码,发现其只在反序列化过程中用到了类型信息。 

GenericJackson2JsonRedisSerializer 通用型序列化,这种序列化方式不用自己手动指定对象的 Class。 

@Configuration 
public class RedisConfig { 
 @Bean 
 public RedisTemplate<String,Object> redisTemplate(LettuceConnectionFactory redisConnectionFactory){ 

     RedisTemplate<String,Object> redisTemplate = new RedisTemplate<>();
     //为string类型key设置序列器
     redisTemplate.setKeySerializer(new StringRedisSerializer()); 
     //为string类型value设置序列器
     redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); 
     //为hash类型key设置序列器
     redisTemplate.setHashKeySerializer(new StringRedisSerializer()); 
     //为hash类型value设置序列器
     redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); 
     redisTemplate.setConnectionFactory(redisConnectionFactory); 
     return redisTemplate; 
   } 
} 
//序列化
@Test 
public void testSerial(){ 
     User user = new User(); 
     user.setId(1); 
     user.setUsername("张三"); 
     user.setPassword("111"); 
     ValueOperations<String, Object> value = redisTemplate.opsForValue(); 
     value.set("userInfo",user); 
     System.out.println(value.get("userInfo")); 
} 

操作string

// 1.操作String 
@Test 
public void testString() { 
     ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); 
     // 添加一条数据
     valueOperations.set("username", "zhangsan"); 
     valueOperations.set("age", "18"); 

    // redis中以层级关系、目录形式存储数据
    valueOperations.set("user:01", "lisi"); 
    valueOperations.set("user:02", "wangwu"); 

     // 添加多条数据
     Map<String, String> userMap = new HashMap<>(); 
     userMap.put("address", "bj"); 
     userMap.put("sex", "1"); 
     valueOperations.multiSet(userMap); 

     // 获取一条数据
     Object username = valueOperations.get("username"); 
     System.out.println(username); 

     // 获取多条数据
     List<String> keys = new ArrayList<>(); 
     keys.add("username"); 
     keys.add("age"); 
     keys.add("address"); 
     keys.add("sex"); 
     List<Object> resultList = valueOperations.multiGet(keys); 
     for (Object str : resultList) { 
         System.out.println(str); 
     } 

     // 删除
     redisTemplate.delete("username"); 
} 

操作hash

// 2.操作Hash 
@Test 
public void testHash() { 
     HashOperations<String, String, String> hashOperations = redisTemplate.opsForHash(); 
     /* 
     * 添加一条数据
     * 参数一:redis的key 
     * 参数二:hash的key 
     * 参数三:hash的value 
     */ 
     hashOperations.put("userInfo","name","lisi"); 

     // 添加多条数据
     Map<String, String> map = new HashMap(); 
     map.put("age", "20"); 
     map.put("sex", "1"); 
     hashOperations.putAll("userInfo", map); 

     // 获取一条数据
     String name = hashOperations.get("userInfo", "name"); 
     System.out.println(name); 

     // 获取多条数据
     List<String> keys = new ArrayList<>(); 
     keys.add("age"); 
     keys.add("sex"); 
     List<String> resultlist =hashOperations.multiGet("userInfo", keys); 
     for (String str : resultlist) { 
         System.out.println(str); 
    } 

    // 获取Hash类型所有的数据
     Map<String, String> userMap = hashOperations.entries("userInfo"); 
     for (Entry<String, String> userInfo : userMap.entrySet()) { 
     System.out.println(userInfo.getKey() + "--" + userInfo.getValue()); 
   } 

     // 删除 用于删除hash类型数据
     hashOperations.delete("userInfo", "name"); 
}

操作list

// 3.操作list 
@Test 
public void testList() { 
    ListOperations<String, Object> listOperations = redisTemplate.opsForList(); 

    // 左添加(上) 
    // listOperations.leftPush("students", "Wang Wu"); 
    // listOperations.leftPush("students", "Li Si"); 

    // 左添加(上) 把value值放到key对应列表中pivot值的左面,如果pivot值存在的话
    //listOperations.leftPush("students", "Wang Wu", "Li Si"); 

    // 右添加(下) 
    // listOperations.rightPush("students", "Zhao Liu"); 

    // 获取 start起始下标 end结束下标 包含关系
    List<Object> students = listOperations.range("students", 0,2); 
    for (Object stu : students) { 
        System.out.println(stu); 
    } 

    // 根据下标获取
    Object stu = listOperations.index("students", 1); 
    System.out.println(stu); 

    // 获取总条数
    Long total = listOperations.size("students"); 
    System.out.println("总条数:" + total); 

    // 删除单条 删除列表中存储的列表中几个出现的Li Si。
    listOperations.remove("students", 1, "Li Si"); 

    // 删除多条
    redisTemplate.delete("students"); 
} 

操作set

// 4.操作set-无序
@Test 
public void testSet() { 

     SetOperations<String, Object> setOperations = redisTemplate.opsForSet(); 

     // 添加数据
     String[] letters = new String[]{"aaa", "bbb", "ccc", "ddd", "eee"}; 
     //setOperations.add("letters", "aaa", "bbb", "ccc", "ddd", "eee"); 
     setOperations.add("letters", letters); 

     // 获取数据
     Set<Object> let = setOperations.members("letters"); 
     for (Object letter: let) { 
         System.out.println(letter); 
     } 

     // 删除
     setOperations.remove("letters", "aaa", "bbb"); 
} 

操作sorted set

// 5.操作sorted set-有序
@Test 
public void testSortedSet() { 
     ZSetOperations<String, Object> zSetOperations = redisTemplate.opsForZSet(); 
     ZSetOperations.TypedTuple<Object> objectTypedTuple1 = new DefaultTypedTuple<Object>("zhangsan", 7D); 
     ZSetOperations.TypedTuple<Object> objectTypedTuple2 = new DefaultTypedTuple<Object>("lisi", 3D); 
     ZSetOperations.TypedTuple<Object> objectTypedTuple3 = new DefaultTypedTuple<Object>("wangwu", 5D); 
     ZSetOperations.TypedTuple<Object> objectTypedTuple4 = new DefaultTypedTuple<Object>("zhaoliu", 6D); 
     ZSetOperations.TypedTuple<Object> objectTypedTuple5 = new DefaultTypedTuple<Object>("tianqi", 2D); 
     Set<ZSetOperations.TypedTuple<Object>> tuples = new HashSet<ZSetOperations.TypedTuple<Object>>(); 
     tuples.add(objectTypedTuple1); 
     tuples.add(objectTypedTuple2); 
     tuples.add(objectTypedTuple3); 
     tuples.add(objectTypedTuple4); 
     tuples.add(objectTypedTuple5); 

     // 添加数据
     zSetOperations.add("score", tuples); 

     // 获取数据
     Set<Object> scores = zSetOperations.range("score", 0, 4); 
     for (Object score: scores) { 
             System.out.println(score); 
         } 
    // 获取总条数
     Long total = zSetOperations.size("score"); 
     System.out.println("总条数:" + total);
 
     // 删除
     zSetOperations.remove("score", "zhangsan", "lisi"); 
} 

获取所有key&删除

// 获取所有key 
@Test 
public void testAllKeys() { 
 // 当前库key的名称
 Set<String> keys = redisTemplate.keys("*"); 
 for (String key: keys) { 
     System.out.println(key); 
  } 
} 

// 删除
@Test 
public void testDelete() { 
     // 删除 通用 适用于所有数据类型
     redisTemplate.delete("score"); 
}

设置key的失效时间

@Test 
public void testEx() { 
     ValueOperations<String, Object> valueOperations = redisTemplate.opsForValue(); 
     // 方法一:插入一条数据并设置失效时间
     valueOperations.set("code", "abcd", 180, TimeUnit.SECONDS); 
     // 方法二:给已存在的key设置失效时间
     boolean flag = redisTemplate.expire("code", 180, TimeUnit.SECONDS); 
     // 获取指定key的失效时间
     Long l = redisTemplate.getExpire("code"); 
} 

SpringDataRedis整合使用哨兵机制

application.yml文章来源地址https://www.toymoban.com/news/detail-660611.html

spring: 
     redis: 
         # Redis服务器地址
         host: 192.168.10.100 
         # Redis服务器端口
         port: 6379 
         # Redis服务器端口
         password: root 
         # Redis服务器端口
         database: 0 
         # 连接超时时间
         timeout: 10000ms 
         lettuce: 
             pool: 
                 # 最大连接数,默认8 
                 max-active: 1024 
                 # 最大连接阻塞等待时间,单位毫秒,默认-1ms 
                 max-wait: 10000ms 
                 # 最大空闲连接,默认8 
                 max-idle: 200 
                 # 最小空闲连接,默认0 
                 min-idle: 5 
         #哨兵模式
         sentinel: 
             #主节点名称
             master: mymaster 
             #节点
             nodes: 192.168.10.100:26379,192.168.10.100:26380,192.168.10.100:26381 

到了这里,关于Redis+SpringBoot企业版集群实战------【华为云版】的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Docker Swarm集群企业案例实战

    Docker Swarm 和 Docker Compose 一样,都是 Docker 官方容器编排项目,但不同的是,Docker Compose 是一个在单个服务器或主机上创建多个容器的工具,而 Docker Swarm 则可以在多个服务器或主机上创建容器集群服务,对于微服务的部署,显然 Docker Swarm 会更加适合 。 Swarm是Docker公司自主研

    2024年02月19日
    浏览(30)
  • 在.NET Framework中使用RocketMQ(阿里云版)实战【第一章】

    章节 第一章:https://www.cnblogs.com/kimiliucn/p/17662052.html 第二章:https://www.cnblogs.com/kimiliucn/p/17667200.html 作者:西瓜程序猿 主页传送门:https://www.cnblogs.com/kimiliucn/ 在开发某一个需求的时候,领导要求使用RocketMQ(阿里云版) 作为消息队列。使用的版本是5.x,目前也已经没有4.x购买的

    2024年02月11日
    浏览(26)
  • Redis:原理速成+项目实战——Redis企业级项目实战终结篇(HyperLogLog实现UV统计)

    👨‍🎓作者简介:一位大四、研0学生,正在努力准备大四暑假的实习 🌌上期文章:Redis:原理速成+项目实战——Redis实战14(BitMap实现用户签到功能) 📚订阅专栏:Redis:原理速成+项目实战 希望文章对你们有所帮助 这篇是实战部分的终结篇,其实Redis的核心操作,主要是

    2024年01月17日
    浏览(36)
  • 在.NET Framework中使用RocketMQ(阿里云版)实战【第二章】

    章节 第一章:https://www.cnblogs.com/kimiliucn/p/17662052.html 第二章:https://www.cnblogs.com/kimiliucn/p/17667200.html 作者:西瓜程序猿 主页传送门:https://www.cnblogs.com/kimiliucn/ 上一章节主要介绍了RocketMQ基本介绍和前期准备,以及如何创建生产者。那这一章节主要介绍一下消费端的实现、如何

    2024年02月11日
    浏览(26)
  • SpringBoot Redis入门(四)——Redis单机、哨兵、集群模式

    单机模式:单台缓存服务器,开发、测试环境下使用; 哨兵模式:主-从模式,提高缓存服务器的 高可用 和 安全性 。所有缓存的数据在每个节点上都一致。每个节点添加监听器,不断监听节点可用状态,一旦主节点不能再提供服务。各监听器会立即在“从节点“中投票选择

    2024年01月20日
    浏览(28)
  • springboot如何集成redis哨兵集群?

    redis主从集群和redis sentinel集群都配置完毕了, 现在我们需要了解spring boot 如何连接上该集群 才能用上这两个集群带来的便利 为什么需要关注这个问题? 怎么配置? 记住. 本章是针对redis已经配置了主从集群和哨兵集群的, 而非cluster集群模式 没有 Redis Sentinel 架构之前,如果主节

    2024年02月09日
    浏览(41)
  • redis搭建主从集群模式+整合springboot

    建议先看教程哦!! 最简单的,最常见的模式。 在主从复制中,数据库分为两类: 主数据库(master) 从数据库(slave) 其中主从复制有如下** 特点 **: 主数据库 可以进行 读写 操作,当读写操作导致数据变化时会自动将数据同步给从数据库 复制的数据流是单向的,只能由主节点

    2024年01月20日
    浏览(23)
  • redis搭建哨兵集群模式+整合springboot

    Sentinel 哨兵模式是为了弥补主从复制集群中主机宕机后,主备切换的复杂性而演变出来的。哨兵顾名思义,就是用来监控的,主要作用就是监控主从集群,自动切换主备,完成集群故障转移。 ​ Sentinel 哨兵Sentinel 哨兵介绍 ​ Sentinel 哨兵本质上是一个运行在特殊模式下的Re

    2024年01月19日
    浏览(36)
  • 企业电子招标采购系统源码Spring Cloud + Spring Boot + MybatisPlus + Redis + Layui

      项目说明 随着公司的快速发展,企业人员和经营规模不断壮大,公司对内部招采管理的提升提出了更高的要求。在企业里建立一个公平、公开、公正的采购环境,最大限度控制采购成本至关重要。符合国家电子招投标法律法规及相关规范,以及审计监督要求;通过电子化

    2024年02月10日
    浏览(33)
  • 云原生系列之使用prometheus监控redis集群实战

    本次实战使用prometheus监控redis集群,如果你只想监控redis的某一个单机服务,可以参考: 超级实用,解密云原生监控技术,使用prometheus轻松搞定redis监控 本文中的是prometheus已经安装好,如果你还未安装,可以参考上一篇文章:prometheus安装及使用入门 若你想监控其他服务可以

    2023年04月14日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包