springcloud 整合 redis ,并部分关于 StringRedisTemplate 的常用api

参考文档: https://docs.spring.io/spring-data/redis/docs/

Redis中文教程: https://www.redis.net.cn/tutorial/3501.html   (十分详细)

Redis官方中文文档之Redis集群教程: http://ifeve.com/redis-cluster-tutorial/

1.pom.xml 需要引入的jar包  (如果有父项目可以不写版本号)

<dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-data-redis</artifactId>
   <version>2.2.4.RELEASE</version>
</dependency>

<dependency>
   <groupId>org.apache.commons</groupId>
   <artifactId>commons-pool2</artifactId>
   <version>2.7.0</version>
</dependency>

2.application.yml的相关配置 (我这里redis是没设置密码的)

spring:
  application:
    # 服务名称 - 服务之间使用名称进行通讯
    name: test-client-b
  redis:
    host: 192.168.4.6  #(此处填写自己redis服务端的ip地址)
    port: 6379
    lettuce:
      pool:
        max-wait: 100000
        max-idle: 10
        max-active: 100
    timeout: 5000

3.修改redis的配置文件redis.conf,允许外部访问,修改之后需要重启redis服务端

1.将以下注释掉
#bind 127.0.0.1
2.将以下参数的值设置为no
protected-mode no

4.关闭相关的防火墙

参考:https://blog.csdn.net/j253507692/article/details/78704542

//查看开放了哪些端口号
firewall-cmd --list-ports

//开放一个端口号,最好将常用的都开放,如80,6379,3306,5672,8080
firewall-cmd --zone=public --add-port=80/tcp --permanent    (--permanent永久生效,没有此参数重启后失效)

//重新加载防火墙(修改完成后需要重新加载才能生效,不需要重启redis)
firewall-cmd --reload

执行命令后的相关图例

无开放任何端口号

关闭相关端口号的防火墙,即开放部分端口号(success表示开放端口号成功)

重新加载之后即可看到刚刚开放了的端口号

5.在cloud项目中使用redis (将手机验证码存储在redis中)

StringRedisTemplate可以自动关闭redis连接

package com.redis.blogtool.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @Author: 
 * @Despriction:
 * @Package: com.redis.blogtool.utils
 * @Date:Created in 2020/3/2 19:04
 * @Modify By:
 */
@Component
public class RedisUtil {

    @Autowired
    private StringRedisTemplate redisTemplate;

    private static String[] randomNum = {"q","w","e","r","t","y","u","i","o","p","a","s","d","f","g","h","j","k","l","z","x","c","v","b","n","m","1","2","3","4","5","6","7","8","9","0"};

    /**
     * 随机创建一个验证码且保存在redis中
     * key的格式为 verificationCode:phoneNum:code
     * @param phoneNum
     * @return
     */
    public String creatVerificationCode (String phoneNum) {
        ValueOperations<String, String> ops = redisTemplate.opsForValue();
        String key = "verificationCode:"+ phoneNum +":code";
        String code = "";
        Long expire = redisTemplate.getExpire(key);
        //expire等於-2为已过期,等于-1为永不过期,等于别的值为还剩余的过期时间
        if (expire != -2) {
            //key尚未过期,不允许重复生成
            return "00";
        }
        //生成随机的6位数的验证码
        Random random = new Random();
        for (int i = 0; i < 6; i++) {
            int redInt = random.nextInt(randomNum.length);
            code += randomNum[redInt];
        }
        System.err.println("生成的验证码" + code);
        //存储该验证码到redis中,过期时长为60秒
        ops.set(key ,code,60,TimeUnit.SECONDS);
        return code;
    }

    /**
     * 通过手机号从redis中获取验证码,用于进行检验客户端传入的验证码的合法性
     * @param phoneNum
     * @return
     */
    public String getVerificationCode (String phoneNum) {
        //ValueOperations<String, String> ops = redisTemplate.opsForValue();
        //String code = ops.get("verificationCode:" + phoneNum + ":code");
        //bound方法就是将该对象绑定一个key,之后对这个key的操作就可以使用这个对象进行操作,不再需要每次都填写key了。
        BoundValueOperations<String, String> boundValueOps = redisTemplate.boundValueOps("verificationCode:" + phoneNum + ":code");
        String code = boundValueOps.get();
        return code;
    }

}

6.通过StringRedisTemplate 获取五中操作类型的对象

//操作字符串
redisTemplate.opsForValue();
//操作hash  
redisTemplate.opsForHash();
//操作list   
redisTemplate.opsForList();
//操作set   
redisTemplate.opsForSet();
//操作zset    
redisTemplate.opsForZSet();   

7.部分常用操作

//向redis里存入数据和设置缓存时间  
stringRedisTemplate.opsForValue().set("test", "100",60*10,TimeUnit.SECONDS);

//val做-1操作
stringRedisTemplate.boundValueOps("test").increment(-1);

//根据key获取缓存中的val
stringRedisTemplate.opsForValue().get("test")

//val +1
stringRedisTemplate.boundValueOps("test").increment(1);

//根据key获取过期时间
stringRedisTemplate.getExpire("test");

//根据key获取过期时间并换算成指定单位 
stringRedisTemplate.getExpire("test",TimeUnit.SECONDS)

//根据key删除缓存
stringRedisTemplate.delete("test");

//检查key是否存在,返回boolean值 
stringRedisTemplate.hasKey("546545");

//向指定key中存放set集合
stringRedisTemplate.opsForSet().add("red_123", "1","2","3");

//设置过期时间
stringRedisTemplate.expire("red_123",1000 , TimeUnit.MILLISECONDS);

//根据key查看集合中是否存在指定数据
stringRedisTemplate.opsForSet().isMember("red_123", "1")

//根据key获取set集合
stringRedisTemplate.opsForSet().members("red_123");

另外也可以参考这篇博客:https://www.cnblogs.com/snake107/p/12143234.html

8.未完待续~~~若大家有新的api可以向我进行补充,实在太多了无法一一进行实验操作,唯有在真实项目中遇到了才可以

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值