如果是通过外网访问Redis一定要开放防火墙的端口权限,不然不能访问
导入pom.xml
1 2 3 4 5
| <dependency> <groupId>redis.clients</groupId> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency>
|
Java代码
1 2
| Jedis jedis = new Jedis("127.0.0.1",6379); jedis.set("test","这是一个测试");
|
创建对象还有一些其他方法,这里不赘述
通过JedisPool连接
设置JedisPool,参照JedisPoolConfig配置
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| JedisPoolConfig config = new JedisPoolConfig();
config.setBlockWhenExhausted(true);
config.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy");
config.setJmxEnabled(true);
config.setJmxNamePrefix("pool");
config.setLifo(true);
config.setMaxIdle(8);
config.setMaxTotal(8);
config.setMaxWaitMillis(-1);
config.setMinEvictableIdleTimeMillis(1800000);
config.setMinIdle(0);
config.setNumTestsPerEvictionRun(3);
config.setSoftMinEvictableIdleTimeMillis(1800000);
config.setTestOnBorrow(false);
config.setTestWhileIdle(false);
config.setTimeBetweenEvictionRunsMillis(-1);
|
注:Jedis低版本可能不存在maxTotal
和maxWaitMillis
,使用maxActive
和maxWait
1 2 3 4 5 6 7 8 9 10 11
| JedisPoolConfig jedisPoolConfig = new JedisPoolConfig(); jedisPoolConfig.setMaxIdle(5); jedisPoolConfig.setBlockWhenExhausted(true); jedisPoolConfig.setEvictionPolicyClassName("org.apache.commons.pool2.impl.DefaultEvictionPolicy"); jedisPoolConfig.setJmxEnabled(true); jedisPoolConfig.setMinIdle(2); jedisPoolConfig.setMaxWaitMillis(3000); JedisPool jedisPool = new JedisPool(jedisPoolConfig,"127.0.0.1",6379,3000); Jedis jedis = jedisPool.getResource(); String aa = jedis.get("aa"); System.out.println(aa);
|
Jedis集成Spring
配置文件加载
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| <context:property-placeholder location="classpath:config/redis.properties" ignore-unresolvable="true" file-encoding="utf-8"/>
<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig"> <property name="minIdle" value="${jedis.minIdle}"></property> <property name="lifo" value="${jedis.lifo}"></property> <property name="maxIdle" value="${jedis.maxIdle}"></property> <property name="maxTotal" value="${jedis.maxTotal}"></property> <property name="maxWaitMillis" value="${jedis.maxWaitMillis}"></property> <property name="minEvictableIdleTimeMillis" value="${jedis.minEvictableIdleTimeMillis}"></property> <property name="numTestsPerEvictionRun" value="${jedis.numTestsPerEvictionRun}"></property> <property name="softMinEvictableIdleTimeMillis" value="${jedis.softMinEvictableIdleTimeMillis}"></property> <property name="testOnBorrow" value="${jedis.testOnBorrow}"></property> <property name="testWhileIdle" value="${jedis.testWhileIdle}"></property> <property name="timeBetweenEvictionRunsMillis" value="${jedis.timeBetweenEvictionRunsMillis}"></property> <property name="blockWhenExhausted" value="${jedis.blockWhenExhausted}"></property> <property name="jmxEnabled" value="${jedis.jmxEnabled}"></property> </bean> <bean id="jedisPool" class="redis.clients.jedis.JedisPool" scope="singleton"> <constructor-arg index="0" ref="jedisPoolConfig" /> <constructor-arg index="1" value="${jedis.address}" /> <constructor-arg index="2" value="${jedis.port}" type="int" /> <constructor-arg index="3" value="0" type="int" /> </bean>
|
通过上面的方式,使用@Autowired
注入的时候会自动注入jedispool
并且使用配置文件中的配置
redis.properties:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| jedis.minIdle=1
jedis.maxIdle=5
jedis.lifo=true
jedis.maxTotal=10
jedis.maxWaitMillis=3000
jedis.minEvictableIdleTimeMillis=1800000
jedis.numTestsPerEvictionRun=3
jedis.softMinEvictableIdleTimeMillis=1800000
jedis.testOnBorrow=true
jedis.testWhileIdle=true
jedis.timeBetweenEvictionRunsMillis=1000
jedis.blockWhenExhausted=true
jedis.jmxEnabled=true
jedis.address=127.0.0.1 jedis.port=6379
|
上面设置只能为参考,不具有实际项目的应用能力
Java代码
1 2 3 4 5 6 7 8 9 10 11 12 13
| @Autowired private JedisPool jedisPool;
public String get(String key){ try { Jedis jedis = jedisPool.getResource(); return jedis.get(key); } catch (Exception e) { e.printStackTrace(); log.error("get Error!"+e); } return null; }
|
也可以只注入JedisPoolConfig,手动创建JedisPool
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| @Component public class JedisUtil { @Autowired JedisPoolConfig jedisPoolConfig;
private static JedisPool jedisPool;
@Value("${jedis.address}") private String redisAddress;
@Value("${jedis.port}") private int redisPort;
private static final Log log = LogFactory.getLog(JedisUtil.class); private JedisPool getPool(){ if (jedisPool==null){ synchronized (JedisUtil.class){ if (jedisPool==null){ jedisPool = new JedisPool(jedisPoolConfig,redisAddress,redisPort); } } } return jedisPool; }
public String get(String key){ try{ Jedis jedis = getPool().getResource(); return jedis.get(key); }catch (Exception e){ log.error("get Error"+e); } return null; } }
|