"Spring Boot && Spring Cloud Series" How to use switch libraries in Spring-data-redis

Source: Internet
Author: User


Objective


Redis has 16 libraries by default, and the default connection is the one of index=0. These 16 libraries are directly independent of one another.


One, switch in the command line
Select 1;
Second, how to switch in spring


1, the use of Redisconnection.select (1) in redisconnectioncommands;



2. Set in config file (jedisconnectionfactory)


<!-jedis connection factory->
     <bean id = "connectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
         <property name = "hostName" value = "$ {redis.host}" />
         <property name = "port" value = "$ {redis.port}" />
         <property name = "database" value = "$ {redis.database}" />
         <property name = "password" value = "$ {redis.pass}" />
         <property name = "timeout" value = "2000" />
         <property name = "poolConfig" ref = "poolConfig" />
     </ bean>
/ **
  * Sets the index of the database used by this connection factory. Default is 0.
  *
  * @param index database index
  * /
public void setDatabase (int index) {
    Assert.isTrue (index> = 0, "invalid DB index (a positive index required)");
    this.dbIndex = index;
} 


Spring-redis.xml Full configuration file


1 <? Xml version = "1.0" encoding = "UTF-8"?>
 2 <beans xmlns = "http://www.springframework.org/schema/beans"
 3 xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
 4 xmlns: util = "http://www.springframework.org/schema/util"
 5 xsi: schemaLocation = "
 6 http://www.springframework.org/schema/beans
 7 http://www.springframework.org/schema/beans/spring-beans.xsd
 8 http://www.springframework.org/schema/util
 9 http://www.springframework.org/schema/util/spring-util.xsd
10 ">
11
12 <!-Jedis connection pool configuration->
13 <bean id = "poolConfig" class = "redis.clients.jedis.JedisPoolConfig">
14 <!-Maximum number of idle connections->
15 <property name = "maxIdle" value = "$ {redis.maxIdle}" />
16 <!-The minimum number of idle connections, the processing interval is timeBetweenEvictionRunsMillis->
17 <property name = "minIdle" value = "$ {redis.minIdle}" />
18 <!-The maximum number of connections held in the pool->
19 <property name = "maxTotal" value = "$ {redis.maxTotal}" />
20 <!-Maximum wait time for borrowObject method->
21 <property name = "maxWaitMillis" value = "$ {redis.maxWait}" />
22 <!-Whether the borrow method blocks waiting for maxWaitMillis milliseconds when the available resources in the pool are exhausted->
23 <property name = "blockWhenExhausted" value = "true" />
24 <!-Whether to perform detection when borrowObject->
25 <property name = "testOnBorrow" value = "$ {redis.testOnBorrow}" />
26 <!-Whether to check the validity of the idle connection link, the interval is timeBetweenEvictionRunsMillis->
27 <property name = "testWhileIdle" value = "true" />
28 <!-The minimum idle time required for an idle object to be cleared->
29 <property name = "minEvictableIdleTimeMillis" value = "$ {redis.minEvictableIdleTimeMillis}" />
30 <!-Idle detection thread, how long is the sleep interval, to handle idle related things->
31 <property name = "timeBetweenEvictionRunsMillis" value = "$ {redis.timeBetweenEvictionRunsMillis}" />
32 </ bean>
33 <!-Jedis connection factory->
34 <bean id = "connectionFactory" class = "org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
35 <property name = "hostName" value = "$ {redis.host}" />
36 <property name = "port" value = "$ {redis.port}" />
37 <property name = "database" value = "$ {redis.database}" />
38 <property name = "password" value = "$ {redis.pass}" />
39 <property name = "timeout" value = "2000" />
40 <property name = "poolConfig" ref = "poolConfig" />
41 </ bean>
42 <!-Redis actual template->
43 <bean id = "redisTemplate" class = "org.springframework.data.redis.core.RedisTemplate">
44 <property name = "connectionFactory" ref = "connectionFactory" />
45 <property name = "keySerializer">
46 <bean class = "org.springframework.data.redis.serializer.StringRedisSerializer" />
47 </ property>
48 <property name = "valueSerializer">
49 <bean class = "org.springframework.data.redis.serializer.JdkSerializationRedisSerializer" />
50 </ property>
51 </ bean>
52 <!-Spring session. To disable the automatic configuration->
53 <util: constant
54 static-field = "org.springframework.session.data.redis.config.ConfigureRedisAction.NO_OP" />
55 <!-Redis configuration for spring session->
56 <bean class = "org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration">
57 <property name = "maxInactiveIntervalInSeconds" value = "3600" />
58 </ bean>
59
60 </ beans> 





"Spring Boot && Spring Cloud Series" How to use switch libraries in Spring-data-redis


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.