spring boot 自学笔记(四) Redis集成—Jedis

上一篇笔记Reddis集成,操作Redis使用的是RedisTemplate,但实际中还是有一大部分人习惯使用JedisPool和Jedis来操作Redis, 下面使用Jedis集成示例。

修改RedisConfig类如下:

[java]  view plain  copy
  1. package com.vic.config;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.boot.autoconfigure.EnableAutoConfiguration;  
  5. import org.springframework.boot.context.properties.ConfigurationProperties;  
  6. import org.springframework.context.annotation.Bean;  
  7. import org.springframework.context.annotation.Configuration;  
  8.   
  9. import redis.clients.jedis.JedisPool;  
  10. import redis.clients.jedis.JedisPoolConfig;  
  11.   
  12. /** 
  13.  *  
  14.  * @author vic 
  15.  * @desc redis config bean 
  16.  * 
  17.  */  
  18. @Configuration  
  19. @EnableAutoConfiguration  
  20. @ConfigurationProperties(prefix = "spring.redis", locations = "classpath:application.properties")  
  21. public class RedisConfig {  
  22.   
  23.     private static Logger logger = Logger.getLogger(RedisConfig.class);  
  24.       
  25.     private String hostName;  
  26.   
  27.     private int port;  
  28.   
  29.     private String password;  
  30.   
  31.     private int timeout;  
  32.       
  33.     @Bean  
  34.     public JedisPoolConfig getRedisConfig(){  
  35.         JedisPoolConfig config = new JedisPoolConfig();  
  36.         return config;  
  37.     }  
  38.       
  39.     @Bean  
  40.     public JedisPool getJedisPool(){  
  41.         JedisPoolConfig config = getRedisConfig();  
  42.         JedisPool pool = new JedisPool(config,hostName,port,timeout,password);  
  43.         logger.info("init JredisPool ...");  
  44.         return pool;  
  45.     }  
  46.   
  47.     public String getHostName() {  
  48.         return hostName;  
  49.     }  
  50.   
  51.     public void setHostName(String hostName) {  
  52.         this.hostName = hostName;  
  53.     }  
  54.   
  55.     public int getPort() {  
  56.         return port;  
  57.     }  
  58.   
  59.     public void setPort(int port) {  
  60.         this.port = port;  
  61.     }  
  62.   
  63.     public String getPassword() {  
  64.         return password;  
  65.     }  
  66.   
  67.     public void setPassword(String password) {  
  68.         this.password = password;  
  69.     }  
  70.   
  71.     public int getTimeout() {  
  72.         return timeout;  
  73.     }  
  74.   
  75.     public void setTimeout(int timeout) {  
  76.         this.timeout = timeout;  
  77.     }  
  78. }  

因为JedisPool实例化对象,是将host,password等参数通过构造传入,所以在这里将整个RedisConfig定义为一个配置类,定义host,password等配置属性,通过spring boot属性文件自动注入。

接下来看看Service中如何使用:

修改IRedisService接口:

[java]  view plain  copy
  1. /** 
  2.  *  
  3.  * @author vic 
  4.  * @desc redis service 
  5.  */  
  6. public interface IRedisService {  
  7.   
  8.     public Jedis getResource();  
  9.   
  10.     public void returnResource(Jedis jedis);  
  11.   
  12.     public void set(String key, String value);  
  13.   
  14.     public String get(String key);  
  15.   
  16. }  


RedisService实现类代码:

[java]  view plain  copy
  1. package com.vic.service.impl;  
  2.   
  3. import org.apache.log4j.Logger;  
  4. import org.springframework.beans.factory.annotation.Autowired;  
  5. import org.springframework.stereotype.Service;  
  6.   
  7. import com.vic.service.IRedisService;  
  8.   
  9. import redis.clients.jedis.Jedis;  
  10. import redis.clients.jedis.JedisPool;  
  11.   
  12. /** 
  13.  *  
  14.  * @author vic 
  15.  * @desc resdis service 
  16.  * 
  17.  */  
  18. @Service  
  19. public class RedisServiceImpl implements IRedisService {  
  20.       
  21.     private static Logger logger = Logger.getLogger(RedisServiceImpl.class);  
  22.   
  23.     @Autowired  
  24.     private JedisPool jedisPool;  
  25.       
  26.     @Override  
  27.     public Jedis getResource() {  
  28.         return jedisPool.getResource();  
  29.     }  
  30.   
  31.     @SuppressWarnings("deprecation")  
  32.     @Override  
  33.     public void returnResource(Jedis jedis) {  
  34.         if(jedis != null){  
  35.             jedisPool.returnResourceObject(jedis);  
  36.         }  
  37.     }  
  38.   
  39.     @Override  
  40.     public void set(String key, String value) {  
  41.         Jedis jedis=null;  
  42.         try{  
  43.             jedis = getResource();  
  44.             jedis.set(key, value);  
  45.             logger.info("Redis set success - " + key + ", value:" + value);  
  46.         } catch (Exception e) {  
  47.             e.printStackTrace();  
  48.             logger.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + value);  
  49.         }finally{  
  50.             returnResource(jedis);  
  51.         }  
  52.     }  
  53.       
  54.     @Override  
  55.     public String get(String key) {  
  56.         String result = null;  
  57.         Jedis jedis=null;  
  58.         try{  
  59.             jedis = getResource();  
  60.             result = jedis.get(key);  
  61.             logger.info("Redis get success - " + key + ", value:" + result);  
  62.         } catch (Exception e) {  
  63.             e.printStackTrace();  
  64.             logger.error("Redis set error: "+ e.getMessage() +" - " + key + ", value:" + result);  
  65.         }finally{  
  66.             returnResource(jedis);  
  67.         }  
  68.         return result;  
  69.     }  
  70.   
  71. }  

JedisPool对象使用自动注入,手动获取Jedis对象进行Redis操作,ExampleController进行测试:
[java]  view plain  copy
  1. @RequestMapping("/redis/set")  
  2. public ResponseModal redisSet(@RequestParam("value")String value){  
  3.     redisService.set("name", value);  
  4.     return new ResponseModal(200, true, "success", null);  
  5. }  
  6.   
  7. @RequestMapping("/redis/get")  
  8. public ResponseModal redisGet(){  
  9.     String name = redisService.get("name");  
  10.     return new ResponseModal(200, true,"success",name);  
  11. }  
测试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"}

点击下载示例
 

猜你喜欢

转载自www.cnblogs.com/williamjie/p/9121908.html