上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例。
修改RedisConfig类如下:
- package com.vic.config;
- import org.apache.log4j.Logger;
- import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
- import org.springframework.boot.context.properties.ConfigurationProperties;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import redis.clients.jedis.JedisPool;
- import redis.clients.jedis.JedisPoolConfig;
- /**
- *
- * @author vic
- * @desc redis config bean
- *
- */
- @Configuration
- @EnableAutoConfiguration
- @ConfigurationProperties(prefix = "spring.redis", locations = "classpath:application.properties")
- public class RedisConfig {
- private static Logger logger = Logger.getLogger(RedisConfig.class);
- private String hostName;
- private int port;
- private String password;
- private int timeout;
- @Bean
- public JedisPoolConfig getRedisConfig(){
- JedisPoolConfig config = new JedisPoolConfig();
- return config;
- }
- @Bean
- public JedisPool getJedisPool(){
- JedisPoolConfig config = getRedisConfig();
- JedisPool pool = new JedisPool(config,hostName,port,timeout,password);
- logger.info("init JredisPool ...");
- return pool;
- }
- public String getHostName() {
- return hostName;
- }
- public void setHostName(String hostName) {
- this.hostName = hostName;
- }
- public int getPort() {
- return port;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getPassword() {
- return password;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public int getTimeout() {
- return timeout;
- }
- public void setTimeout(int timeout) {
- this.timeout = timeout;
- }
- }
因为JedisPool实例化对象,是将host,password等参数通过构造传入,所以在这里将整个RedisConfig定义为一个配置类,定义host,password等配置属性,通过spring boot属性文件自动注入。
接下来看看Service中如何使用:
修改IRedisService接口:
- /**
- *
- * @author vic
- * @desc redis service
- */
- public interface IRedisService {
- public Jedis getResource();
- public void returnResource(Jedis jedis);
- public void set(String key, String value);
- public String get(String key);
- }
RedisService实现类代码:
- package com.vic.service.impl;
- import org.apache.log4j.Logger;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import com.vic.service.IRedisService;
- import redis.clients.jedis.Jedis;
- import redis.clients.jedis.JedisPool;
- /**
- *
- * @author vic
- * @desc resdis service
- *
- */
- @Service
- public class RedisServiceImpl implements IRedisService {
- private static Logger logger = Logger.getLogger(RedisServiceImpl.class);
- @Autowired
- private JedisPool jedisPool;
- @Override
- public Jedis getResource() {
- return jedisPool.getResource();
- }
- @SuppressWarnings("deprecation")
- @Override
- public void returnResource(Jedis jedis) {
- if(jedis != null){
- jedisPool.returnResourceObject(jedis);
- }
- }
- @Override
- public void set(String key, String value) {
- Jedis jedis=null;
- try{
- jedis = getResource();
- jedis.set(key, value);
- logger.info("Redis set success - " + key + ", value:" + value);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + value);
- }finally{
- returnResource(jedis);
- }
- }
- @Override
- public String get(String key) {
- String result = null;
- Jedis jedis=null;
- try{
- jedis = getResource();
- result = jedis.get(key);
- logger.info("Redis get success - " + key + ", value:" + result);
- } catch (Exception e) {
- e.printStackTrace();
- logger.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + result);
- }finally{
- returnResource(jedis);
- }
- return result;
- }
- }
JedisPool对象使用自动注入,手动获取Jedis对象进行Redis操作,ExampleController进行测试:
- @RequestMapping("/redis/set")
- public ResponseModal redisSet(@RequestParam("value")String value){
- redisService.set("name", value);
- return new ResponseModal(200, true, "success", null);
- }
- @RequestMapping("/redis/get")
- public ResponseModal redisGet(){
- String name = redisService.get("name");
- return new ResponseModal(200, true,"success",name);
- }
测试URL:http://localhost:8080/redis/set?value=vic 响应结果:
{"code":200,"success":true,"message":"success","response":null}
测试URL:http://localhost:8080/redis/get 响应结果:
{"code":200,"success":true,"message":"success","response":"vic"}