出现问题:无法连接端口6379,connect timed out 解决: 1.修改redis配置文件 vi /etc/redis.conf
2.关闭redis防火墙 systemctl status firewalld systemctl stop firewalld 连接成功 jedis模拟验证码发送
package com.atguigu.jedis;
import redis.clients.jedis.Jedis;
import java.util.Random;
public class PhoneCode {
public static void main(String[] args) {
VerifyCodeGenerate verifyCodeGenerate = new VerifyCodeGenerate("15171284192");
verifyCodeGenerate.verifyCode();
}
}
class VerifyCodeGenerate{
private String codeKey;
private String countKey;
public VerifyCodeGenerate(String phone){
this.codeKey = "VerifyCode:" + phone + ":code";
this.countKey = "VerifyCode:" + phone + ":count";
}
public static String getCode() {
Random random = new Random();
String code = "";
for (int i = 0; i < 6; i++) {
code += random.nextInt(10);
}
return code;
}
public void verifyCode() {
Jedis jedis = new Jedis("192.168.2.130", 6379);
String count = jedis.get(countKey);
if (count == null) {
jedis.setex(countKey, 24 * 60 * 60, "1");
} else if (Integer.parseInt(count) <= 2) {
jedis.incr(countKey);
} else if (Integer.parseInt(count) > 2) {
System.out.println("今日验证次数已用完!");
jedis.close();
return;
}
jedis.setex(codeKey, 120, getCode());
System.out.println("验证码已发送");
jedis.close();
}
public void getRedisCode(String code) {
Jedis jedis = new Jedis("192.168.2.130", 6379);
String redisCode = jedis.get(codeKey);
if (redisCode.equals(code)) {
System.out.println("成功");
} else {
System.out.println("失败");
}
jedis.close();
}
}
|