springboot接入redis
文章目录
一、引入依赖
<!--redis集成-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
<!--序列化-->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.38</version>
</dependency>
二、添加配置application.properties
#redis远程服务器redis
redis.host=47.112.17.9
redis.port=6379
redis.timeout=3
redis.password=haibo1118
redis.poolMaxTotal=10
redis.poolMaxIdle=10
redis.poolMaxWait=3
三、读取配置文件
通过以下注解读取redis开头的配置文件(二中配置文件)
@ConfigurationProperties(prefix = "redis")
读取配置完整代码如下:
package xyz.haibofaith.miaosha.redis;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @author:haibo.xiong
* @date:2019/5/6
* @description:
*/
@Component
@ConfigurationProperties(prefix = "redis")
public class RedisConfig {
private String host;
private int port;
private int timeout;//秒
private String password;
private int poolMaxTotal;
private int poolMaxIdle;
private int poolMaxWait;//秒
//略set get方法
}
四、Jedis工厂
package xyz.haibofaith.miaosha.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
&