当前位置:网站首页>20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)
20:第三章:开发通行证服务:3:在程序中,打通redis服务器;(仅仅是打通redis服务器,不涉及具体的业务开发)
2022-06-26 16:39:00 【小枯林】
说明:
(1)本篇博客需要注意的点:
● 本篇博客仅仅是打通redis服务(也就是我们的程序,可以操作redis服务器),不涉及【redis服务,在项目中的具体应用】,也不涉及【具体业务的开发】;
● 其中遇到了一款不错的redis图形化工具:rdm(Redis Desktop Manager)工具,感觉不错;
● redis服务,作为一个第三方的“工具”,我们把其写在了【imooc-news-dev-common】通用工程中;
(2)有关Redis的内容,如有需要可以参考【(13)Linux基础】、【(14)Redis】专栏中的内容;
目录
1.在【imooc-news-dev-common】通用工程中,引入redis等相关依赖;
2.在【imooc-news-dev-common】通用工程中,创建一个操作redis的工具类;
3.在【imooc-news-dev-user】这个(实际需要使用到redis的)用户微服务中,在application*.yml配置文件中去配置redis服务的url、密码等信息;
4.在【imooc-news-dev-user】这个(实际需要使用redis的)用户微服务中,在HelloController中,编写代码,以测试“我们的程序,是否可以连上redis”;
一:我们这个项目,为什么需要用到redis;
(1)生成验证码后,验证码是需要发给用户的;;;用户把收到的验证码在前端输入,就会进入后端验证;;;即,在后端,我们要把这个原生的验证码进行存储;;;而,对于分布式微服务项目,我们一般会存放在redis中(而不会像单体应用中那样,存在session中);
二:安装redis、redis简介;
如有需要可以参考【(13)Linux基础】、【(14)Redis】专栏中的内容;
同时,在【Spring Boot电商项目31:商品分类模块十:利用Redis缓存加速响应;】这个Spring Boot项目中,也用到过redis;
我们这儿,就使用在【Redis入门二:Linux系统下安装Redis;】中,创建的那台虚拟机中安装的redis了;
(1)把下载的包,上传到Linux系统,似乎可以借助FileZill工具;(自己没用过,自己用的是Xftp)
(2)命令行工具,也可以使用SecureCRT;(自己没用过,自己用的是Xshell)
(3)可以使用【rdm(Redis Desktop Manager)工具】这款redis可视化工具,来辅助开发;
● rdm的下载安装,可以参考【Redis Desktop Manager(Redis可视化工具)安装及使用教程】;
三:在项目中,整合redis,打通redis服务器;
1.在【imooc-news-dev-common】通用工程中,引入redis等相关依赖;
<!-- 引入 redis 依赖 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> <!--<version>2.1.5.RELEASE</version>--> </dependency> <!--okhttp依赖.作用是,服务与服务之间发送rest请求--> <dependency> <groupId>com.squareup.okhttp3</groupId> <artifactId>okhttp</artifactId> </dependency> <!-- jackson三件套 --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> </dependency> <!-- apache 工具类 【commons-codec】是apache提供的数据的编码/解码组件,可以使用【Commons Codec】来实现MD5的; 【commons-lang3】是Apache提供的一个java.lang包的增强版本,Lang3为java.lang API提供了许多帮助程序实用程序,特别是字符串操作方法,基本数值方法,对象反射,并发,创建和序列化以及系统属性。 【commons-fileupload】是一款apache提供的文件上传组件; --> <dependency> <groupId>commons-codec</groupId> <artifactId>commons-codec</artifactId> </dependency> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> </dependency> <!-- google 工具类 --> <dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> </dependency> <!-- joda-time 时间工具,一个时间工具类 --> <dependency> <groupId>joda-time</groupId> <artifactId>joda-time</artifactId> </dependency>说明:
(1)对这些依赖,如果有不清楚的,可以参考【9:第二章:架构后端项目:5:【聚合工程】简介;创建顶级父工程;】;
(2)还是那句话,根据我们的分布式微服务项目,各个module的分工和关系;;;edis服务,作为一个第三方的“工具”,我们把其写在了【imooc-news-dev-common】通用工程中;
2.在【imooc-news-dev-common】通用工程中,创建一个操作redis的工具类;
RedisOperator工具类:
package com.imooc.utils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.dao.DataAccessException; import org.springframework.data.redis.connection.RedisConnection; import org.springframework.data.redis.connection.StringRedisConnection; import org.springframework.data.redis.core.RedisCallback; import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Component; import java.util.List; import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; /** * @Title: Redis 工具类 * @author 风间影月 */ @Component public class RedisOperator { @Autowired private StringRedisTemplate redisTemplate; // Key(键),简单的key-value操作 /** * 判断key是否存在 * @param key * @return */ public boolean keyIsExist(String key) { return redisTemplate.hasKey(key); } /** * 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。 * * @param key * @return */ public long ttl(String key) { return redisTemplate.getExpire(key); } /** * 实现命令:expire 设置过期时间,单位秒 * * @param key * @return */ public void expire(String key, long timeout) { redisTemplate.expire(key, timeout, TimeUnit.SECONDS); } /** * 实现命令:increment key,增加key一次 * * @param key * @return */ public long increment(String key, long delta) { return redisTemplate.opsForValue().increment(key, delta); } /** * 实现命令:decrement key,减少key一次 * * @param key * @return */ public long decrement(String key, long delta) { return redisTemplate.opsForValue().decrement(key, delta); } /** * 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key */ public Set<String> keys(String pattern) { return redisTemplate.keys(pattern); } /** * 实现命令:DEL key,删除一个key * * @param key */ public void del(String key) { redisTemplate.delete(key); } // String(字符串) /** * 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key) * * @param key * @param value */ public void set(String key, String value) { redisTemplate.opsForValue().set(key, value); } /** * 实现命令:SET key value EX seconds,设置key-value和超时时间(秒) * * @param key * @param value * @param timeout * (以秒为单位) */ public void set(String key, String value, long timeout) { redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS); } /** * 如果key不存在,则设置,如果存在,则报错 * @param key * @param value */ public void setnx60s(String key, String value) { redisTemplate.opsForValue().setIfAbsent(key, value, 60, TimeUnit.SECONDS); } /** * 如果key不存在,则设置,如果存在,则报错 * @param key * @param value */ public void setnx(String key, String value) { redisTemplate.opsForValue().setIfAbsent(key, value); } /** * 实现命令:GET key,返回 key所关联的字符串值。 * * @param key * @return value */ public String get(String key) { return (String)redisTemplate.opsForValue().get(key); } /** * 批量查询,对应mget * @param keys * @return */ public List<String> mget(List<String> keys) { return redisTemplate.opsForValue().multiGet(keys); } /** * 批量查询,管道pipeline * @param keys * @return */ public List<Object> batchGet(List<String> keys) { // nginx -> keepalive // redis -> pipeline List<Object> result = redisTemplate.executePipelined(new RedisCallback<String>() { @Override public String doInRedis(RedisConnection connection) throws DataAccessException { StringRedisConnection src = (StringRedisConnection)connection; for (String k : keys) { src.get(k); } return null; } }); return result; } // Hash(哈希表) /** * 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value * * @param key * @param field * @param value */ public void hset(String key, String field, Object value) { redisTemplate.opsForHash().put(key, field, value); } /** * 实现命令:HGET key field,返回哈希表 key中给定域 field的值 * * @param key * @param field * @return */ public String hget(String key, String field) { return (String) redisTemplate.opsForHash().get(key, field); } /** * 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。 * * @param key * @param fields */ public void hdel(String key, Object... fields) { redisTemplate.opsForHash().delete(key, fields); } /** * 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。 * * @param key * @return */ public Map<Object, Object> hgetall(String key) { return redisTemplate.opsForHash().entries(key); } // List(列表) /** * 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头 * * @param key * @param value * @return 执行 LPUSH命令后,列表的长度。 */ public long lpush(String key, String value) { return redisTemplate.opsForList().leftPush(key, value); } /** * 实现命令:LPOP key,移除并返回列表 key的头元素。 * * @param key * @return 列表key的头元素。 */ public String lpop(String key) { return (String)redisTemplate.opsForList().leftPop(key); } /** * 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。 * * @param key * @param value * @return 执行 LPUSH命令后,列表的长度。 */ public long rpush(String key, String value) { return redisTemplate.opsForList().rightPush(key, value); } }说明:
(1)这个工具类,不是我写的;;里面定义了很多操作redis的方法;可以大略的瞅一瞅;
(2)有关java操作redis的内容,如有需要可以先参考下【(14)Redis】专栏中的内容;
3.在【imooc-news-dev-user】这个(实际需要使用到redis的)用户微服务中,在application*.yml配置文件中去配置redis服务的url、密码等信息;
4.在【imooc-news-dev-user】这个(实际需要使用redis的)用户微服务中,在HelloController中,编写代码,以测试“我们的程序,是否可以连上redis”;
5.测试;
(1)首先,整个项目全局install一下;
(2)然后,启动【user】的主启动类;
(3)然后,访问【user】的【"/redis"】接口;
至此,我们的redis就好了,也就是我们的项目是可以连接到redis服务器了;
边栏推荐
- Multiply the values of the upper triangular elements of the array by M
- 数字藏品与NFT到底有何区别
- Stm32f103c8t6 realize breathing lamp code
- Day10 daily 3 questions (1): sum gradually to get the minimum value of positive numbers
- [Error] ld returned 1 exit status
- Teach you to learn dapr - 8 binding
- Scala Basics (II): variables and data types
- Redis Guide (8): principle and implementation of Qianfan Jingfa distributed lock
- Niuke Xiaobai monthly race 50
- 知道这几个命令让你掌握Shell自带工具
猜你喜欢
![[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram](/img/75/440e515c82b5613b117728ba760786.png)
[Li Kou brush question] monotone stack: 84 The largest rectangle in the histogram

构造函数和析构函数

【毕业季】致毕业生的一句话:天高任鸟飞,海阔凭鱼跃

基於Kubebuilder開發Operator(入門使用)

用Attention和微调BERT进行自然语言推断-PyTorch

Leetcode 1170. 比较字符串最小字母出现频次(可以,已解决)
![[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)](/img/75/2fb1a4e6215e404df34849e9e4f21a.png)
[from database deletion to running] JDBC conclusion (finish the series in one day!! run as soon as you finish learning!)
![[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul](/img/26/c0c8a406ff4ffe0ae37d277f730bd0.png)
[Blue Bridge Cup training 100 questions] scratch distinguishing prime numbers and composite numbers Blue Bridge Cup scratch competition special prediction programming question intensive training simul

对话长安马自达高层,全新产品将在Q4发布,空间与智能领跑日系

Learn about common functional interfaces
随机推荐
Niuke Xiaobai monthly race 50
Count the number of words in a line of string and take it as the return value of the function
[from deleting the database to running] the end of MySQL Foundation (the first step is to run.)
y=1/100*100+1/200*200+1/300*300+.....+ 1/m*m
国内首款开源 MySQL HTAP 数据库即将发布,三大看点提前告知
JUnit unit test
Fgetc() reads content from file
Toupper function
Qt 5.9.8 安装教程
108. simple chat room 11: realize client group chat
Multiply the values of the upper triangular elements of the array by M
108. 简易聊天室11:实现客户端群聊
Find all primes less than or equal to Lim, store them in AA array, and return the number of primes
JS tutorial - printing stickers / labels using the electronjs desktop application
【从删库到跑路】MySQL基础 完结篇(入个门先跑路了。。)
网页课程设计大作业——华山旅游网
GUI+SQLServer考试系统
Knowing these commands allows you to master shell's own tools
100+数据科学面试问题和答案总结 - 基础知识和数据分析
# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍









