Spring integration Redis, and configure the connection pool Jedis

table of Contents


 

Word or two

single vision

Creating redis profile connection pool

spring integration redis (using JedisPool)

Distributed version

Creating redis profile connection pool

spring integration redis (using SharedJedisPool)

 

 

 

 


 

 

 

Word or two  

  Spring integration Redis, nothing more than an object to create Jedis manual process to Spring to create and use Spring IoC and DI were injected where needed JedisPool then get Jedis object before proceeding.

  The following would be a stand-alone version of Redis, and Redis cluster are configured.

 

single vision

  Stand-alone, redis server refers to only one, this time redis configuration is + port to access the server via redis host.

Creating redis profile connection pool

  Connection pool configuration file named redis_pool.properties, located in the classpath directory, as follows:

redis_maxTotal = 30 
redis_maxIdle = 15 
redis_minIdle = 5 
redis_ip = 127.0.0.1 
redis_port = 6379 
redis_timeout = 2000 
redis_database = 0

  

Spring Redis integration

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context.xsd">

	<context:component-scan base-package="cn.ganlixin.test"></context:component-scan>

	<!-- 读取redis pool的配置文件 -->
	<context:property-placeholder location="classpath:redis_pool.properties" />

	<bean id="jedisPoolConfig" class="redis.clients.jedis.JedisPoolConfig">
		<property name="maxTotal" value="${redis_maxTotal}"></property>
		<property name="minIdle" value="${redis_minIdle}"></property>
		<property name="maxIdle" value="${redis_maxIdle}"></property>
	</bean>

	<bean id="jedisPool" class="redis.clients.jedis.JedisPool">
		<constructor-arg name="poolConfig" ref="jedisPoolConfig"></constructor-arg>
		<constructor-arg name="host" value="${redis_host}"></constructor-arg>
		<constructor-arg name="port" value="${redis_port}"></constructor-arg>
		<constructor-arg name="timeout" value="${redis_timeout}"></constructor-arg>
		<constructor-arg name="database" value="${redis_database}"></constructor-arg>
	</bean>
</beans>

  

test

package cn.ganlixin.test;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

@Component
public class TestJedisPool {

	@Autowired
	private JedisPool jedisPool;
	
	public void doSomeAction() {
		// 获取jedis连接
		Jedis jedis = jedisPool.getResource();
		String name = jedis.get("name");
		System.out.println(name);
		
		jedis.close();
	}
}

  

 

Distributed version

  Distributed Version Redis means more than one server, rather than a single Redis receives all the requests.

 

Guess you like

Origin www.cnblogs.com/-beyond/p/10991428.html