什么是NoSQL 先来看看百度百科对NoSQL的描述。
NoSQL,泛指非关系型的数据库。随着互联网web2.0网站的兴起,传统的关系数据库在处理web2.0网站,特别是超大规模和高并发的SNS类型的web2.0纯动态网站已经显得力不从心,出现了很多难以克服的问题,而非关系型的数据库则由于其本身的特点得到了非常迅速的发展。NoSQL数据库的产生就是为了解决大规模数据集合多重数据种类带来的挑战,特别是大数据应用难题。
NoSQL维基百科称其为Not Only SQL
,在当今的大数据年代,仅仅是关系型数据库已经不能满足当前一些流量较大的Web应用,所以NoSQL就应运而生,就是用来解决数据量较大的问题。
NoSQL可以分成四个大类。
键值存储数据库
列存储数据库
文档型数据库
图形数据库
这里我就用百度百科上的表格,来简单了解一下这四个分类。
分类 Examples举例 典型应用场景 数据模型 优点 缺点 键值存储数据库 Tokyo Cabinet/Tyrant, Redis, Voldemort, Oracle BDB 内容缓存,主要用于处理大量数据的高访问负载,也用于一些日志系统等等。 Key 指向 Value 的键值对,通常用hash table来实现 查找速度快 数据无结构化,通常只被当作字符串或者二进制数据 列存储数据库 Cassandra, HBase, Riak 分布式的文件系统 以列簇式存储,将同一列数据存在一起 查找速度快,可扩展性强,更容易进行分布式扩展 功能相对局限 文档型数据库 CouchDB, MongoDb Web应用(与Key-Value类似,Value是结构化的,不同的是数据库能够了解Value的内容) Key-Value对应的键值对,Value为结构化数据 数据结构要求不严格,表结构可变,不需要像关系型数据库一样需要预先定义表结构 查询性能不高,而且缺乏统一的查询语法。 图形(Graph)数据库 Neo4J, InfoGrid, Infinite Graph 社交网络,推荐系统等。专注于构建关系图谱 图结构 利用图结构相关算法。比如最短路径寻址,N度关系查找等 很多时候需要对整个图做计算才能得出需要的信息,而且这种结构不太好做分布式的集群方案。
这次学习的就是键值存储数据库中的Redis。
什么是Redis Redis是Remote Dictionary Server
的缩写,翻译为远程字典调用,是一个开源的使用ANSI C语言编写、支持网络、可基于内存亦可持久化的日志型、Key-Value数据库,并提供多种语言的API。
安装 windows windows的只需要下载他们提供的压缩包使用就好了。
打开服务后就能看到服务的信息,默认端口为6379
接下来来确认一下服务是否真的开启了,打开客户端,使用ping
命令来测试,出现PONG
表示已经连接到了Redis的服务。
windows的版本活跃度很低,而且官网也不推荐使用windows,所以还是尽量使用Linux,之后的使用我也是使用Linux来测试。
Linux 去到redis的github就能下载到压缩包使用。
因为Redis是使用C/C++写的,所以要想使用,还是需要先安装一下C/C++的环境。
1 2 3 4 5 yum install gcc-c++ make make install
要是make命令时出现了很多的error,大概率就是因为redis6版本是需要gcc版本是5以上的才行,所以需要先升级gcc的版本,这里是升级到了9。
1 2 3 4 5 yum -y install centos-release-scl yum -y install devtoolset-9-gcc devtoolset-9-gcc-c++ devtoolset-9-binutils scl enable devtoolset-9 bash echo "source /opt/rh/devtoolset-9/enable" >>/etc/profile
Redis默认会安装到/usr/local/bin
的目录里。
Redis不是默认启动的,为了方便学习,要设置成默认启动的,所以去修改redis.conf
这个文件。
1 2 # 将这个选项设置为yes,默认为no daemonize yes
启动Redis的服务。
1 2 3 4 5 6 redis-server [conf的路径] redis-cli -p 6379 ping
关闭Redis的服务
性能测试 Redis的目录下还会有一个命令redis-benchmark
。
这个命令是官方自带的性能测试的命令,用来测试Redis的一些基础命令读写的速度,以下是命令的参数。
参数选项 描述 默认值 -h 指定服务器主机名 127.0.0.1 -p 指定服务器端口 6379 -s 指定服务器 socket -c 指定并发连接数 50 -n 指定请求数 10000 -d 以字节的形式指定 SET/GET 值的数据大小 2 -k 1=keep alive 0=reconnect 1 -r SET/GET/INCR 使用随机 key, SADD 使用随机值 -P 通过管道传输请求 1 -q 强制退出 redis。仅显示 query/sec 值 –csv 以 CSV 格式输出 -l 生成循环,永久执行测试 -t 仅运行以逗号分隔的测试命令列表 -I Idle 模式。仅打开 N 个 idle 连接并等待。
数据类型 Redis支持的数据类型有很多,官网也写的很清楚了。
Redis provides data structures such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperloglogs, geospatial indexes, and streams.
String(字符串) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 set [key] [value] get [key] keys * select [index] flushdb flushall exists [key] append [key] [value] strlen [key] incr [key] incrby [key] [length] decr [key] decrby [key] [length] getrange [key] [index1] [index2] setrange [key] [index] [value] expire [key] [秒] setex [key] [秒] [value] ttl [key] setnx [key] [value] mset [key1] [value1] [key2] [value3] ... mget [key1] [key2] ... msetnx [key1] [value1] [key2] [value3] ... getset [key] [value]
List(列表) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 lpush [key] [value] rpush [key] [value] lrange [key] [index1] [index2] lrange [key] 0 -1 lpop [key] rpop [key] lindex [key] [index] llen [key] lrem [key] [个数] [value] ltrim [key] [index1] [index2] rpoplpush [key1] [value] [key2] lset [key] [index] [value] exists [key] linsert [key] before [value1] [value2] linsert [key] after [value1] [value2]
Set(集合) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 sadd [key] [value] smembers [key] sismember [key] [value] scard [key] srem [key] [value] srandmember [key] srandmember [key] [length] spop [key] smove [key1] [value] [key2] sdiff [key1] [key2] sinter [key1] [key2] sunion [key1] [key2]
Hash(哈希) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 hset [key] [key1] [value1] hget [key] [key1] hmset [key] [key1] [value1] [key2] [value2]... hmget [key] [key1] [key2]... hgetall [key] hdel [key] [key1] hlen [key] hexists [key] [key1] hkeys [key] hvals [key] hsetnx [key] [key1] [value1]
Zset(有序的集合) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 zadd [key] [index] [value] zadd [key] [index1] [value1] [index2] [value2].. zrangebyscore [key] [min] [max] zrangebyscore [key] -inf +inf zrevrange [key] 0 -1 zcrad [key] zount [key] [index1] [index2]
geospatial(地理位置) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 geoadd [key] [经度] [纬度] [value] geopos [key] [value] geodist [key] [value1] [value2] [单位] georadius [key] [经度] [纬度] [半径] [单位] georadius [key] [经度] [纬度] [半径] [单位] withdist georadius [key] [经度] [纬度] [半径] [单位] withcoord georadiusbymember [key] [value] [半径] [单位] geohash [key] [value]
Hyperloglog(基数统计) 1 2 3 4 5 6 7 8 pfadd [key] [value1] [value2] [value3]... pfmerge [key] [key1] [key2] pfcount [key]
Bitmaps(位图) 1 2 3 4 5 6 7 8 setbit [key] [index] [1或0] getbit [key] [index] bitcount sign
总结 对Redis的数据类型进行一个小的总结。
Redis的数据类型其实大概分为两类。
五大数据类型
String Set Hash List Zset 三个特殊的数据类型
geospatial hyperloglog bitmaps 事务 1 2 3 4 5 6 7 8 9 10 11 multi exec discard
1. 若是事务中存在错误的命令,那么整个事务的命令都不会被执行 2. 若是事务中只是命令语法有问题,那么该命令会抛出异常,但是其他命令会正常执行
监视 首先了解悲观锁和乐观锁这两个概念。
悲观锁
悲观锁,顾名思义,什么时候都很悲观,每次访问公共数据,都会觉得别人会进行修改,所以每次获取该数据的时候,都会对数据加锁
乐观锁
乐观锁,顾名思义,什么时候都很乐观,每次访问公共数据,都觉得别人不会进行修改,所以每次都不会去上锁,只有在更新该数据后会判断一下使用期间其他线程有没有修改该数据。可以使用版本号来实现。每次更新数据后,判断该数据的版本号是否发生变更,发生了变更,就会导致修改失效,若是没有就会修改成功并修改版本号。
1 2 3 4 5 6 7 8 watch [key] multi ... exec unwatch [key] watch [key]
Jedis Jedis是官网推荐的Java连接工具,类似就是在Java和Redis中增加一层辅助层,辅助你用Java使用Redis。
简单的测试一下使用。
首先创建一个maven项目,导入相关的依赖。
1 2 3 4 5 6 <dependency > <groupId > redis.clients</groupId > <artifactId > jedis</artifactId > <version > 3.5.1</version > </dependency >
连接使用Redis
1 2 3 4 5 6 7 8 9 10 11 12 13 14 package com.yww;import redis.clients.jedis.Jedis;public class config { public static void main (String[] args) { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); System.out.println(jedis.ping()); jedis.close(); } }
事务的使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.yww;import redis.clients.jedis.Jedis;import redis.clients.jedis.Transaction;public class config { public static void main (String[] args) { Jedis jedis = new Jedis("127.0.0.1" , 6379 ); Transaction multi = jedis.multi(); try { multi.exec(); } catch (Exception e) { multi.discard(); e.printStackTrace(); } finally { jedis.close(); } } }
整合SpringBoot 整合就先创建一个SpringBoot的项目,导入SpringData项目的redis依赖。
1 2 3 4 5 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-data-redis</artifactId > <version > 2.4.2</version > </dependency >
在比较新的SpringBoot版本中,放弃了Jedis对redis的直接连接,因为多个线程会出现线程不安全的情况,所以官方已经替换成了lettuce。 lettuce采用netty,实例可以在多个线程中进行共享,减少线程数量,不存在线程不安全的情况。
在配置文件中,配置Redis的连接。
1 2 3 spring.redis.host =127.0.0.1 spring.redis.port =6379
简单的测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 package com.yww;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest class RedisTestApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void contextLoads () { redisTemplate.opsForValue().set("name" ,"yww" ); System.out.println(redisTemplate.opsForValue().get("name" )); RedisConnection connection = redisTemplate.getConnectionFactory().getConnection(); connection.flushDb(); } }
上述的测试其实是没有经过序列化的,这是一个很不安全的操作,特别是传输对象的时候,没有序列化就会报错。
先简单的创建一个对象来测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 package com.yww;import lombok.AllArgsConstructor;import lombok.Data;import lombok.NoArgsConstructor;import org.springframework.stereotype.Component;@Component @AllArgsConstructor @NoArgsConstructor @Data public class User implements Serializable { private String name; private int age; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 package com.yww;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.connection.RedisConnection;import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest class RedisTestApplicationTests { @Autowired private RedisTemplate redisTemplate; @Test void contextLoads () throws JsonProcessingException { User user = new User("yww" , 20 ); String json = new ObjectMapper().writeValueAsString(user); redisTemplate.opsForValue().set("user" ,json); System.out.println(redisTemplate.opsForValue().get("user" )); } }
默认是会使用jdk的序列化方式,在后期使用会出现一些麻烦,所以开发中一般会自己定义RedisTemplate的序列化方式,所以就需要自己定义一个RedisTemplate来使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 package com.yww.config;import com.fasterxml.jackson.annotation.JsonAutoDetect;import com.fasterxml.jackson.annotation.PropertyAccessor;import com.fasterxml.jackson.databind.ObjectMapper;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.data.redis.connection.RedisConnectionFactory;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;import org.springframework.data.redis.serializer.StringRedisSerializer;import java.net.UnknownHostException;@Configuration public class RedisConfig { @Bean @SuppressWarnings("all") public RedisTemplate<String,Object> redisTemplate (RedisConnectionFactory redisConnectionFactory) { RedisTemplate<String, Object> template = new RedisTemplate<>(); template.setConnectionFactory(redisConnectionFactory); Jackson2JsonRedisSerializer jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer(Object.class); ObjectMapper objectMapper = new ObjectMapper(); objectMapper.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY); objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL); jackson2JsonRedisSerializer.setObjectMapper(objectMapper); StringRedisSerializer stringRedisSerializer = new StringRedisSerializer(); template.setKeySerializer(stringRedisSerializer); template.setHashKeySerializer(stringRedisSerializer); template.setValueSerializer(jackson2JsonRedisSerializer); template.setHashValueSerializer(jackson2JsonRedisSerializer); template.afterPropertiesSet(); return template; } }
测试。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 package com.yww;import com.fasterxml.jackson.core.JsonProcessingException;import com.fasterxml.jackson.databind.ObjectMapper;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.beans.factory.annotation.Qualifier;import org.springframework.boot.test.context.SpringBootTest;import org.springframework.data.redis.core.RedisTemplate;@SpringBootTest class RedisTestApplicationTests { @Autowired @Qualifier("redisTemplate") private RedisTemplate redisTemplate; @Test void contextLoads () throws JsonProcessingException { User user = new User("yww" , 20 ); redisTemplate.opsForValue().set("user" ,json); System.out.println(redisTemplate.opsForValue().get("user" )); } }
每次使用redisTemplate都要确定类型,所以一般都会对操作进行封装,所以我们使用一个封装的工具类。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 package com.yww.utils;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.data.redis.core.RedisTemplate;import org.springframework.stereotype.Component;import org.springframework.util.CollectionUtils;import java.util.List;import java.util.Map;import java.util.Set;import java.util.concurrent.TimeUnit;@Component public final class RedisUtil { @Autowired private RedisTemplate<String, Object> redisTemplate; public boolean expire (String key, long time) { try { if (time > 0 ) { redisTemplate.expire(key, time, TimeUnit.SECONDS); } return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public long getExpire (String key) { return redisTemplate.getExpire(key, TimeUnit.SECONDS); } public boolean hasKey (String key) { try { return redisTemplate.hasKey(key); } catch (Exception e) { e.printStackTrace(); return false ; } } @SuppressWarnings("unchecked") public void del (String... key) { if (key != null && key.length > 0 ) { if (key.length == 1 ) { redisTemplate.delete(key[0 ]); } else { redisTemplate.delete(CollectionUtils.arrayToList(key)); } } } public Object get (String key) { return key == null ? null : redisTemplate.opsForValue().get(key); } public boolean set (String key, Object value) { try { redisTemplate.opsForValue().set(key, value); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean set (String key, Object value, long time) { try { if (time > 0 ) { redisTemplate.opsForValue().set(key, value, time, TimeUnit.SECONDS); } else { set(key, value); } return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public long incr (String key, long delta) { if (delta < 0 ) { throw new RuntimeException("递增因子必须大于0" ); } return redisTemplate.opsForValue().increment(key, delta); } public long decr (String key, long delta) { if (delta < 0 ) { throw new RuntimeException("递减因子必须大于0" ); } return redisTemplate.opsForValue().increment(key, -delta); } public Object hget (String key, String item) { return redisTemplate.opsForHash().get(key, item); } public Map<Object, Object> hmget (String key) { return redisTemplate.opsForHash().entries(key); } public boolean hmset (String key, Map<String, Object> map) { try { redisTemplate.opsForHash().putAll(key, map); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean hmset (String key, Map<String, Object> map, long time) { try { redisTemplate.opsForHash().putAll(key, map); if (time > 0 ) { expire(key, time); } return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean hset (String key, String item, Object value) { try { redisTemplate.opsForHash().put(key, item, value); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean hset (String key, String item, Object value, long time) { try { redisTemplate.opsForHash().put(key, item, value); if (time > 0 ) { expire(key, time); } return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public void hdel (String key, Object... item) { redisTemplate.opsForHash().delete(key, item); } public boolean hHasKey (String key, String item) { return redisTemplate.opsForHash().hasKey(key, item); } public double hincr (String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, by); } public double hdecr (String key, String item, double by) { return redisTemplate.opsForHash().increment(key, item, -by); } public Set<Object> sGet (String key) { try { return redisTemplate.opsForSet().members(key); } catch (Exception e) { e.printStackTrace(); return null ; } } public boolean sHasKey (String key, Object value) { try { return redisTemplate.opsForSet().isMember(key, value); } catch (Exception e) { e.printStackTrace(); return false ; } } public long sSet (String key, Object... values) { try { return redisTemplate.opsForSet().add(key, values); } catch (Exception e) { e.printStackTrace(); return 0 ; } } public long sSetAndTime (String key, long time, Object... values) { try { Long count = redisTemplate.opsForSet().add(key, values); if (time > 0 ) { expire(key, time); } return count; } catch (Exception e) { e.printStackTrace(); return 0 ; } } public long sGetSetSize (String key) { try { return redisTemplate.opsForSet().size(key); } catch (Exception e) { e.printStackTrace(); return 0 ; } } public long setRemove (String key, Object... values) { try { Long count = redisTemplate.opsForSet().remove(key, values); return count; } catch (Exception e) { e.printStackTrace(); return 0 ; } } public List<Object> lGet (String key, long start, long end) { try { return redisTemplate.opsForList().range(key, start, end); } catch (Exception e) { e.printStackTrace(); return null ; } } public long lGetListSize (String key) { try { return redisTemplate.opsForList().size(key); } catch (Exception e) { e.printStackTrace(); return 0 ; } } public Object lGetIndex (String key, long index) { try { return redisTemplate.opsForList().index(key, index); } catch (Exception e) { e.printStackTrace(); return null ; } } public boolean lSet (String key, Object value) { try { redisTemplate.opsForList().rightPush(key, value); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean lSet (String key, Object value, long time) { try { redisTemplate.opsForList().rightPush(key, value); if (time > 0 ) { expire(key, time); } return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean lSet (String key, List<Object> value) { try { redisTemplate.opsForList().rightPushAll(key, value); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean lSet (String key, List<Object> value, long time) { try { redisTemplate.opsForList().rightPushAll(key, value); if (time > 0 ) { expire(key, time); } return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public boolean lUpdateIndex (String key, long index, Object value) { try { redisTemplate.opsForList().set(key, index, value); return true ; } catch (Exception e) { e.printStackTrace(); return false ; } } public long lRemove (String key, long count, Object value) { try { Long remove = redisTemplate.opsForList().remove(key, count, value); return remove; } catch (Exception e) { e.printStackTrace(); return 0 ; } } }
测试使用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 package com.yww;import com.yww.utils.RedisUtil;import org.junit.jupiter.api.Test;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTest class RedisTestApplicationTests { @Autowired private RedisUtil redisUtil; @Test public void contextLoads () { redisUtil.set("name" , "yww" ); System.out.println(redisUtil.get("name" )); } }
持久化 存储在内存中的数据,一旦机器出现问题,那么内存中的数据就会丢失,所以为了尽量避免这个问题,Redis提供了两种持久化的方法,接下来来了解一下这两种持久化的方式。
RDB RDB持久化机制是将某个时刻的数据快照写入磁盘,也就是将某个时刻的数据保存下来,等到Redis服务启动,就会自动加载这个快照文件进行数据恢复。
手动触发 自动触发
RDB文件 从配置文件可以看到配置文件的默认路径和默认名。
1 2 3 4 5 dbfilename dump.rdb dir ./
AOF AOF持久化全称Append Only File
,当我们执行的改变数据的操作时就会将该命令追加到一个AOF文件的末尾,当Redis服务重新启动的时候,就会重新执行AOF文件内的命令,用来同步数据。
AOF不是默认的持久化方式,故默认关闭的,需要去配置文件手动开启。
AOF触发策略 重写机制 当命令不断被追加到AOF文件内,文件会越来越大,这对使用来说很不好,所以Redis提供了一个AOF的重写机制来解决这个问题,将AOF文件内的命令优化,重写为可以恢复到当前数据的最小指令集,从而减少文件的大小,达到压缩AOF文件的目的。
触发流程如下。
手动触发 手动输入bgrewriteaof
命令触发重写机制。
自动触发 自动触发就需要自行修改配置文件内AOF重写的配置。
1 2 3 4 auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
持久化流程
发布订阅 发布订阅是一种消息通信模式。
发送者发送消息,订阅者接受发送者的消息。
接下来进行一个简单的测试。
订阅一个频道,这里订阅yww
这个频道
1 2 3 4 5 127.0.0.1:6379> subscribe yww Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "yww" 3) (integer ) 1
在打开一个Redis的客户端,然后往yww
频道发送两条消息
1 2 3 4 5 127.0.0.1:6379> publish yww "Hello Redis" (integer ) 1 127.0.0.1:6379> publish yww "Hello World" (integer ) 1 127.0.0.1:6379>
在订阅频道的订阅者的客户端就能接收到频道的消息了
1 2 3 4 5 6 7 8 9 10 11 127.0.0.1:6379> subscribe yww Reading messages... (press Ctrl-C to quit) 1) "subscribe" 2) "yww" 3) (integer ) 1 1) "message" 2) "yww" 3) "Hello Redis" 1) "message" 2) "yww" 3) "Hello World"
以下是一些常用的API
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 psubscribe pattern[pattern...] punsubscribe [pattern[pattern]] pubsub subcommand [argument[argument...]] publish channel message subscribe channel[channel ...] unsubscribe [channel[channel...]]
主从复制,读写分离 当数据量过大的时候,服务器的压力就会提高,为了解决这个问题,Redis提供了主从复制,读写分离
的方案,因为大部分的压力是读操作,所以可以搭建一个Redis集群(最低要求三台,一主二从),主节点负责写操作,从节点用来提供读的服务,这样就能减少服务器的压力了。
当然还可以有以下这种情况
节点是既可以当主节点,又可以当子节点的。
Redis的主从复制表示只有主节点能进行写操作,从节点是不能进行写操作的,只能进行读操作,主节点的数据会同步到子节点,达到整个集群的数据一致。
同步机制 上边说到的同步方式根据是否是全量来分为全量同步
和增量同步
。
模拟Redis集群 这里使用同一个服务器,不同端口搭建的Redis集群进行学习。
当然你有多台服务器可以使用不同服务器来搭建集群来学习。
再不然可以直接开三个容器来搭建Redis集群来学习。
首先是了解一个基本的命令。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 info replication 127.0.0.1:6379> info replication role:master connected_slaves:0 master_replid:fb2dce24da3d78a04f01a6f55abdd48e1751dda0 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:0 second_repl_offset:-1 repl_backlog_active:0 repl_backlog_size:1048576 repl_backlog_first_byte_offset:0 repl_backlog_histlen:0 127.0.0.1:6379>
创建三个配置文件,用来开启三个服务。
1 redis6379.conf redis6380.conf redis6381.conf
然后配置这三个文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 logfile "6379log.log" dbfilename dump6379.rdb port 6380 pidfile /var/run/redis_6380.pid logfile "6380log.log" dbfilename dump80.rdb port 6381 pidfile /var/run/redis_6381.pid logfile "6381log.log dbfilename dump6381.rdb # 开启redis集群服务 # redis-server ./myconf/redis6379.conf # redis-server ./myconf/redis6380.conf # redis-server ./myconf/redis6381.conf # ps -ef |grep redis root 19536 19481 0 20:19 pts/1 00:00:00 redis-cli root 32040 1 0 22:30 ? 00:00:00 redis-server 127.0.0.1:6379 root 32052 1 0 22:30 ? 00:00:00 redis-server 127.0.0.1:6380 root 32062 1 0 22:30 ? 00:00:00 redis-server 127.0.0.1:6381 root 32106 30264 0 22:30 pts/2 00:00:00 grep --color=auto redis # 开启三个客户端,这里使用主节点的客户端演示,子节点客户端另开窗口 # redis-cli -p 6379
然后是建立主从联系,这里使用6379的服务当成主节点,建立联系有两种方法。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 127.0.0.1:6380> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6380> info replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:3 master_sync_in_progress:0 slave_repl_offset:28 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:8ed59f23d41ec553e33eae6b5fb13cfd1d807e80 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:28 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:28 127.0.0.1:6381> SLAVEOF 127.0.0.1 6379 OK 127.0.0.1:6381> info replication role:slave master_host:127.0.0.1 master_port:6379 master_link_status:up master_last_io_seconds_ago:9 master_sync_in_progress:0 slave_repl_offset:42 slave_priority:100 slave_read_only:1 connected_slaves:0 master_replid:8ed59f23d41ec553e33eae6b5fb13cfd1d807e80 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:42 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:15 repl_backlog_histlen:28 127.0.0.1:6379> info replication role:master connected_slaves:2 slave0:ip=127.0.0.1,port=6380,state=online,offset=70,lag=1 slave1:ip=127.0.0.1,port=6381,state=online,offset=70,lag=1 master_replid:8ed59f23d41ec553e33eae6b5fb13cfd1d807e80 master_replid2:0000000000000000000000000000000000000000 master_repl_offset:70 second_repl_offset:-1 repl_backlog_active:1 repl_backlog_size:1048576 repl_backlog_first_byte_offset:1 repl_backlog_histlen:70
然后在主机设置的数据,可以在从机中获取数据。
哨兵模式(Sentinel) 通过配置连接的子节点出现故障,重启服务后,因为配置连接的缘故那还是子节点,所以子节点出现故障的情况很简单。
要是这个子节点有子节点,那么主机故障后,子节点的role依旧是salve,所以还是不能进行写操作。
接下来重点了解一下主节点出现故障的情况。
当主节点出现故障后,它的子节点身份是不会变的,当主节点的服务重新启动后,集群依旧存在。
当我们不清楚主节点何时恢复,总不能一直不进行写操作,所以就需要重新推出一个主节点。
这是手动配置主节点,这其实还是挺麻烦的,所以为了解决这个问题,Redis从2.8的版本后就提供了哨兵模式
这个方案。
哨兵模式其实就是自动推选主节点的一种方案。
先来简单了解一下哨兵模式。
哨兵是一个独立的进程,在Redis的命令中也能看到它redis-sentinel
。
该进程通过向各个节点发送命令,通过节点返回的信息来监控节点的使用情况。
当主节点宕机了,哨兵向主节点发送命令却得不到返回信息,过段时间哨兵确认主节点宕机后,就会随机给从节点随机投票,获得投票的节点就会当选为主节点,然后通过发布订阅模式通知其他的从节点修改配置,让它们切换主节点对象,从而实现集群正常服务。
那要是哨兵宕机了怎么办呢?这样的设置还是会出现问题。所以哨兵不能只有一台,哨兵也要形成一个集群(最好三个起步),于是最终的解决方案就如下图。
多个哨兵进行监控,当有一个哨兵检测到主节点宕机,并不会马上切换,因为有可能是哨兵的问题,所以哨兵只会主观认为主节点宕机(这种情况叫主观下线)。
当一定个数的哨兵都认为主节点宕机后,那大概率就不是哨兵的问题了,那么哨兵之前就会对剩余的从节点投票,投票结束后,随机一个哨兵进行failover
操作(这种情况叫客观下线)。
failover
又称故障转移,它会从从节点中挑选一个作为Redis集群中的新的主节点。
选择票数高的从节点当为主节点,若是不存在(同票的情况),就继续判断。 选择主从复制,同步数据最完整的节点成为主节点,若是不存在就继续判断。 选择启动最早的子节点当为新的主节点。 对选出来的主节点执行slaveof no one
将身份转换成主节点,然后向其他的从节点发送订阅模式通知,各个哨兵就会让他们的节点切换主节点的对象为新的主节点。最后更新之前宕掉的节点的身份为从节点,当宕机恢复后,就自动成为该集群的从节点。
哨兵的启动需要先配置哨兵启动的配置文件sentinel.conf
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 port 26379 dir /tmp sentinel monitor mymaster 127.0.0.1 6379 2
1 2 redis-sentinel ./sentinel.conf
redis.conf 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 bind 127.0.0.1protected-mode yes port 6379 daemonize no pidfile /var/run/redis_6379.pid loglevel notice logfile "" databases 16 save 900 1 save 300 10 save 60 10000 stop-writes-on-bgsave-error yes rdbcompression yes rdbchecksum yes dbfilename dump.rdb dir ./ appendonly no appendfilename "appendonly.aof" appendfsync everysec no-appendfsync-on-rewrite no auto-aof-rewrite-percentage 100 auto-aof-rewrite-min-size 64mb
一些参考链接