1、引入spring-boot-starter-data-redis依赖
<dependency>
<groupId></groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
由于SpringBoot2.0默认采用Lettuce客户端来连接Redis服务端的。并且默认是不使用连接池的,如果是在老项目的基础上进行改造redis的工具类且原连接池是jedis的话,那需要引入Jedis客户端依赖。
<dependency>
<groupId></groupId>
<artifactId>jedis</artifactId>
</dependency>
如果存入redis的key和value都是String类型,并且value值是实例对象的Json形式,那么就需要将实例对象通过(实例对象)进行json化。那么也需要引入阿里的fastJson依赖。
<dependency>
<groupId></groupId>
<artifactId>fastjson</artifactId>
<version>1.2.46</version>//版本可自己定义,不是非要用此版本
</dependency>
2、RedisConfig配置类
由于我是对项目中的redis进行优化,并且redis配置项是放在apollo中统一进行管理的。所以redis配置项是不能动的,那就无法通过在或者yml文件中添加标准的配置了。所以只能自己重新写一个配置类去加载配置。(使用spring-boot-starter-data-redis时,redis参数配置项是固定的key,如果key不一致就需要自己写配置类去加载配置)由于原redis配置使用的是Jedis连接池,所以配置类中的连接池用的也是Jedis.
@Configuration
public class RedisConfig {
@Value("${}")
private String host;
@Value("${}")
private int port;
@Value("${}")
private String password;
@Value("${}")
private int dbIndex;
@Value("${}")
private int timeout;
@Value("${}")
private boolean ssl;
@Value("${}")
private int maxTotal;
@Value("${}")
private int maxIdle;
@Value("${}")
private int minIdle;
@Value("${}")
private long maxWaitMillis;
@Value("${}")
private boolean testOnBorrow;
@Value("${}")
private boolean testOnReturn;
@Value("${}")
private boolean testWhileIdle;
@Bean
public JedisPoolConfig jedisPoolConfig() {
JedisPoolConfig poolConfig = new JedisPoolConfig();
(maxTotal);
(maxIdle);
(minIdle);
(maxWaitMillis);
(testOnBorrow);
(testOnReturn);
(testWhileIdle);
return poolConfig;
}
@Bean
public RedisConnectionFactory redisConnectionFactory(JedisPoolConfig jedisPoolConfig) {
RedisStandaloneConfiguration redisStandaloneConfiguration =
new RedisStandaloneConfiguration();
(host);
(dbIndex);
((password));
(port);
jpcb =
()();
(jedisPoolConfig);
JedisClientConfiguration jedisClientConfiguration = ();
return new JedisConnectionFactory(redisStandaloneConfiguration, jedisClientConfiguration);
}
//key为String,value值为序列化的数据
/*@Bean
public RedisTemplate<String, Serializable> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>();
(new StringRedisSerializer());
(new GenericJackson2JsonRedisSerializer());
(redisConnectionFactory);
return redisTemplate;
}*/
RedisTemplate默认有两个实现,一个是RedisTemplate<Object,Object>,一个是StringRedisTemplate,当然如果不想使用这两种实现可以自己在配置类中实现自己想要的RedisTemplate,编写一个新的Bean实例即可。
如果你的项目中第一次使用spring-boot-starter-data-redis,只想用默认的配置,那么只需要在或者yml中配置对应的配置即可。如下所示,如果需要其他的配置,自己加上即可。
spring:
redis:
host: 127.0.0.1
port: 6379
password:
database: 0
lettuce:
pool:
max-active: 32
max-wait: 300ms
max-idle: 16
min-idle: 8
3、如何使用
@RestController
public class RedisController {
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping(value = "/redis/set")
public void setKey (@RequestParam("key") String key, @RequestParam("value") String value) {
().set(key, value);
}
@RequestMapping(value = "/redis/get")
public List<LocationInfoResponse> getList(@RequestParam("key") String key) {
List<LocationInfoResponse> locationInfoResponseList = new ArrayList<>();
String value = ().get(key);
locationInfoResponseList = (value, );
return locationInfoResponseList;
}
}