1、依赖jar
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2、yml配置:
spring
redis:
port: 6380
password: 123456
host: 192.168.2.207
timeout: 2000
pool:
maxActice: 300
3、创建RedisPoolFactory:
package com.greattao.egtcp.purchase.common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Component
@Slf4j
@Configuration
public class RedisPoolFactory {
/**
* redis配置属性读取
*/
@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.pool.maxActice}")
private int maxActive;
/**
* JedisPoolConfig配置
* @return
*/
@Bean
public JedisPool jedisPoolConfig() {
log.info("初始化JedisPoolConfig");
JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
jedisPoolConfig.setMaxTotal(maxActive);
JedisPool jp=new JedisPool(jedisPoolConfig,host,port,timeout,password);
return jp;
}
}
4、RedisTool中的获取jedis:
@Autowired
private JedisPool jedisPool;
Jedis jedis=jedisPool.getResource()