赞
踩
1、首先在pom中添加Jedis依赖
<!-- jedis配置 -->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.8.2</version>
</dependency>
2、在aplication.properties配置文件中加入Jedis的参数配置
#jedis配置===============================
#redis服务器地址
jedis.pool.host=192.168.184.100
#redis服务器端口
jedis.pool.port=6379
#最大连接数
jedis.pool.config.maxTotal=100
#最小空闲连接数
jedis.pool.config.maxIdle=10
#获取连接时的最大等待毫秒数
jedis.pool.config.maxWaitMillis=100000
#redis的auth密码
jedis.pool.password=0.0.0.
3、创建Jedis的连接配置文件
package com.cy.example.config;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.cy.example.utils.RedisClient;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;
@Configuration
public class RedisConfig {
private Logger logger = LoggerFactory.getLogger(RedisClient.class);
@Bean(name= "jedis.pool")
@Autowired
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${jedis.pool.host}")String host,
@Value("${jedis.pool.port}")int port,
@Value("${jedis.pool.password}")String password) {
logger.info("缓存服务器的地址:"+host+":"+port);
return new JedisPool(config, host, port,Protocol.DEFAULT_TIMEOUT,password);
}
@Bean(name= "jedis.pool.config")
public JedisPoolConfig jedisPoolConfig (@Value("${jedis.pool.config.maxTotal}")int maxTotal,
@Value("${jedis.pool.config.maxIdle}")int maxIdle,
@Value("${jedis.pool.config.maxWaitMillis}")int maxWaitMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
return config;
}
}
4、编写Jedis操作的两个简单接口
package com.cy.example.utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.cy.example.config.LoginInterceptor;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
public class RedisClient {
private Logger logger = LoggerFactory.getLogger(RedisClient.class);
@Autowired
private JedisPool jedisPool;
public void set(String key, String value){
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
} catch(Exception e){
logger.info("缓存服务器连接失败!");
}finally {
//返还到连接池
jedis.close();
}
}
public String get(String key){
String value = "";
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
value = jedis.get(key);
} catch(Exception e){
logger.info("缓存服务器连接异常!");
}finally {
//返还到连接池
jedis.close();
}
return value;
}
}
5、写一个简单的测试demo
package com.cy.example.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import redis.clients.jedis.Jedis;
import com.cy.example.utils.RedisClient;
/*
* 测试redis
*/
@Controller
@RequestMapping("/redis")
public class RedisController {
@Autowired
private RedisClient redisClinet;
@RequestMapping("/set")
public String set(String key, String value) throws Exception{
redisClinet.set(key, value);
return "success";
}
@RequestMapping("/get")
public String get(String key) throws Exception {
return redisClinet.get(key);
}
}
项目代码请看我的GitHub
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。