当前位置:网站首页>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服务器了;
边栏推荐
- Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
- JS tutorial - printing stickers / labels using the electronjs desktop application
- 【小5聊】毕业8年,一直在追梦的路上
- What is flush software? Is it safe to open an account online?
- Redis migration (recommended operation process)
- Scala Foundation (2): variables et types de données
- [understanding of opportunity -31]: Guiguzi - Daoyu [x ī] Crisis is the coexistence of danger and opportunity
- The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
- STM32F103C8T6实现呼吸灯代码
- TCP拥塞控制详解 | 1. 概述
猜你喜欢
![[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

心情不好,我就这样写代码

Kubecon China 2021 Alibaba cloud special session is coming! These first day highlights should not be missed

Cloud platform monitoring system based on stm32+ Huawei cloud IOT design

Develop operator based on kubebuilder (for getting started)

Science | 红树林中发现的巨型细菌挑战传统无核膜观念

MS | Xie Liwei group found that mixed probiotics and their metabolites could alleviate colitis

No manual prior is required! HKU & Tongji & lunarai & Kuangshi proposed self supervised visual representation learning based on semantic grouping, which significantly improved the tasks of target dete

C语言 头哥习题答案截图

100+数据科学面试问题和答案总结 - 基础知识和数据分析
随机推荐
进军AR领域,这一次罗永浩能成吗?
Several forms of buffer in circuit
Screenshot of the answers to C language exercises
# 补齐短板-开源IM项目OpenIM关于初始化/登录/好友接口文档介绍
Find out the maximum value of each column element of NxN matrix and store it in the one-dimensional array indicated by formal parameter B in order
Niuke programming problem -- dynamic programming of must brush 101 (a thorough understanding of dynamic programming)
去中心化NFT交易协议将击败OpenSea
【MATLAB项目实战】基于卷积神经网络与双向长短时(CNN-LSTM)融合的锂离子电池剩余使用寿命预测
Binary array command of redis
108. 简易聊天室11:实现客户端群聊
Scala Basics (II): variables and data types
Notes on key review of software engineering at the end of the term
Acid of redis
Qt 5.9.8 安装教程
Développer un opérateur basé sur kubebuilder (démarrer)
用Attention和微调BERT进行自然语言推断-PyTorch
The first open source MySQL HTAP database in China will be released soon, and the three highlights will be notified in advance
1-12Vmware新增SSH功能
Junit单元测试
[from deleting the database to running] the end of MySQL Foundation (the first step is to run.)









