这个demo是用了jedis操作的redis,springboot2.0之后,对redis连接的支持默认采用了lettuce。所以首先在pom文件中加入如下依赖。
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
yml配置如下:
server:
port: 8081
tomcat:
uri-encoding: UTF-8
spring:
redis:
host: 192.168.31.18
password: 1534864446366szxg
jedis:
pool:
max-idle: 6
max-active: 10
min-idle: 2
port: 6379
timeout: 2000
mvc:
view:
prefix: templates/
suffix: .html
然后编写jedis自动配置类来向容器中注入jedisPool。
package com.example.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class JedisConfig {
private Logger logger = LoggerFactory.getLogger(JedisConfig.class);
@Value("${spring.redis.host}")
private String host;
@Value("${spring.redis.port}")
private int port;
@Value("${spring.redis.password}")
private String password;
@Value("${spring.redis.timeout}")
private int timeout;
@Value("${spring.redis.jedis.pool.max-active}")
private int maxActive;
@Value("${spring.redis.jedis.pool.max-idle}")
private int maxIdle;
@Value("${spring.redis.jedis.pool.min-idle}")
private int minIdle;
@Bean
public JedisPool jedisPool(){
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxIdle(maxIdle);
jedisPoolConfig.setMinIdle(minIdle);
jedisPoolConfig.setMaxTotal(maxActive);
JedisPool jedisPool = new JedisPool(jedisPoolConfig,host,port,timeout,password);
logger.info("jedis连接成功:"+host+"\t"+port);
return jedisPool;
}
}
为了操作方便,写一个jedis的util,封装了获取jedis资源的方法和关闭jedis的方法。当然,里面可以接着封装一些其他的jedis操作redis的方法。
package com.example.config;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
public class JedisUtil {
@Autowired
private JedisPool jedisPool;
//获取jedis资源
public Jedis getJedis(){
return jedisPool.getResource();
}
//关闭jedis资源
public void close(Jedis jedis){
if(jedis!=null){
jedis.close();
}
}
//....可以封装其他方法。
}
接着是controller层逻辑代码了。应该写在service实现层的,但是写demo为了方便,大家理解就可以了。
@Autowired
private JedisUtil jedisUtil;
@RequestMapping("/haha/getnum")
public void getNum(@RequestParam("phoneNumber") String phoneNumber){
logger.info("手机号为:"+phoneNumber);
Jedis jedis = jedisUtil.getJedis();
String countKey = phoneNumber+":count";
String codeKet = phoneNumber+"code";
String num = jedis.get(countKey);
if(num==null){
jedis.setex(countKey,60*60*2,"1");
StringBuffer code = new StringBuffer();
for(int i = 0;i<6;i++){
code.append(new Random().nextInt(10));
}
logger.info("随机验证码:"+code);
jedis.setex(codeKet,60*60*2,code.toString());
} else {
if(Integer.parseInt(num)<3){
jedis.incr(countKey);
StringBuffer code = new StringBuffer();
for (int i=0;i<6;i++){
code.append(new Random().nextInt(10));
}
logger.info("随机验证码:"+code);
jedis.setex(codeKet,60*60*2,code.toString());
} else {
logger.info("发送次数过多,请稍后再试!");
}
}
if(jedis!=null){
logger.info("关闭jedis");
jedis.close();
}
}
@RequestMapping("/haha/setnum")
public void chickkey(@RequestParam("phone")String phoneNumber,@RequestParam("yanzhengma")String yanzhengma) {
logger.info("输入的验证码为:" + yanzhengma);
logger.info("输入的手机号为:" + phoneNumber);
String countKey = phoneNumber + ":count";
Jedis jedis = jedisUtil.getJedis();
String validation = jedis.get(countKey);
if (validation == null) {
System.out.println("验证码未发送或者失效");
} else {
if (validation.equals(yanzhengma)) {
System.out.println("验证成功");
} else {
System.out.println("验证失败");
}
}
if (jedis != null) {
jedis.close();
}
}