Spring Data Redis single node and cluster configuration and redistemplate usage
tags (space delimited): Spring-data
Using Springdata makes it easier for us to operate on relational and non-relational databases, encapsulating common code and making operations faster and easier.
One, the configuration of Spring Data Redis
Introduce the relevant jar packages and pay attention to dependencies and conflict issues.
Maven introduces Pom.xml:
<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <v Ersion>4.12</version> </dependency> <!--Spring-data-redis related, will automatically introduce relevant jar--> <dependency > <groupId>org.springframework.data</groupId> <artifactid>spring-data-redis</artifacti D> <version>1.7.4.RELEASE</version> </dependency> <!--spring Test related to <depe
Ndency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>4.2.8.RELEASE</version> </dependency> <!--Redis-related <!--https:// Mvnrepository.com/artifact/redis.clients/jedis-<dependency> <groupid>redis.clients</groupi
d> <artifactId>jedis</artifactId> <version>2.9.0</version> </dependency> </dependencies>
Manually add jar package related jar download address
Configuration file
You can use the introduction of external files or by configuring them in an XML file
How to configure a single node
Properties file
#JedisPoolConfig的参数
#最大连接数
redis.pool.maxtotal=30
#最大空闲时间
redis.pool.maxidle=10
#每次最大连接数
redis.pool.numtestsperevictionrun=1024
#释放扫描的扫描间隔
redis.pool.timebetweenevictionrunsmillis=30000
#连接的最小空闲时间
redis.pool.minevictableidletimemillis=1800000
#连接控歘按时间多久后释放, when idle time > This value and idle connection > The maximum number of idle connections is directly released
redis.pool.softminevictableidletimemillis=10000
#获得链接时的最大等待毫秒数, less than 0: blocking indeterminate time, default-1
redis.pool.maxwaitmillis=1500
#在获得链接的时候检查有效性, default false
redis.pool.testonborrow=true
#在空闲时检查有效性, Default false
Redis.pool.testwhileidle=true
#连接耗尽时是否阻塞, false to report exception, true to block timeout, default true
Redis.pool.blockwhenexhausted=false
#JedisConnectionFactory的参数
#主机地址, default: localhost
redis.hostname=192.168.200.128
#主机端口, default: 6379
redis.port=6379
#超时时间, default: +
redis.timeout=
#密码
#redis. Password
#是否使用连接池, default true
redis.usepool=true
#使用数据库的索引, 0-15 digits, default: 0
redis.dbindex=0
#是否使用数据类型的转换, default: True
#redis. Convertpipelineandtxresults
#哨兵配置
# Redis.sentinelconfig
#集群配置
#redis. clusterconfig
XML configuration file
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--Introducing configuration Files--<bean id=" Placeholderco Nfigurer "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" O Rder "value=" 1 "/> <property name=" Ignoreunresolvableplaceholders "value=" true "/> <property nam
E= "Locations" > <list> <value>classpath:redis.properties</value> </list> </property> </bean> <!--configuration Jedis pool--> <bean id= "Jedispoolcon Fig "class=" Redis.clients.jedis.JedisPoolConfig "> <!--Maximum number of connections--<property name=" Maxtotal "Val Ue= "${redis.pool.maxtotal}"/> <!--maximum idle time-<property name= "Maxidle" value= "${redis.pool.maxidle}"/> <!--maximum connections per connection-->
; <property name= "Numtestsperevictionrun" value= "${redis.pool.numtestsperevictionrun}"/> <!--release scan interval--&
Gt <property name= "Timebetweenevictionrunsmillis" value= "${redis.pool.timebetweenevictionrunsmillis}"/> <!- -Minimum idle time of connection--<property name= "Minevictableidletimemillis" value= "${redis.pool.minevictableidletimemillis}" /> <!--connection Control 歘 is released after a time, when idle time > This value and idle connection > maximum idle connection number is released directly--<property name= "Softminevictableidle Timemillis "value=" ${redis.pool.softminevictableidletimemillis} "/> <!--maximum wait milliseconds to get a link, less than 0: blocking indeterminate time, default-1-->
;
<property name= "Maxwaitmillis" value= "${redis.pool.maxwaitmillis}"/> <!--check validity when getting links, default false-- <property name= "Testonborrow" value= "${redis.pool.testonborrow}"/> <!--check validity at idle time, default false--<property name= "Testwhileidle" value= "${redis.pool.testwhileidle}"/> <!--If the connection is exhausted Blocking, false reporting exception, true blocking timeout default:true--> <property name= "blockwhenexhausted" value= "${redis.pool.blockwhenexhausted}" /> </bean> <!--spring data Redis--<bean id= "jedisconnectionfactory" class= "Org.springfra Mework.data.redis.connection.jedis.JedisConnectionFactory "> <property name=" hostName "value=" ${redis.hostna Me} "/> <property name=" port "value=" ${redis.port} "/> <property name=" Timeout "value=" ${redis.t Imeout} "/> <property name=" database "value=" ${redis.dbindex} "/> <property name=" Usepool "value = "${redis.usepool}"/> <!--can be injected in two ways via construction injection or set--<property name= "Poolconfig" ref= "Jedispoolcon Fig "/> </bean> <!--redistemplate--> <bean id=" redistemplate "class=" Org.springframework.dat A.redis.core.redistemplatE "> <property name=" connectionfactory "ref=" jedisconnectionfactory "/> </bean> </beans>
how the cluster is configured
Properties file
#JedisPoolConfig的参数
#最大连接数
redis.pool.maxtotal=30
#最大空闲时间
redis.pool.maxidle=10
#每次最大连接数
redis.pool.numtestsperevictionrun=1024
#释放扫描的扫描间隔
redis.pool.timebetweenevictionrunsmillis=30000
#连接的最小空闲时间
redis.pool.minevictableidletimemillis=1800000
#连接控歘按时间多久后释放, when idle time > This value and idle connection > The maximum number of idle connections is directly released
redis.pool.softminevictableidletimemillis=10000
#获得链接时的最大等待毫秒数, less than 0: blocking indeterminate time, default-1
redis.pool.maxwaitmillis=1500
#在获得链接的时候检查有效性, default false
redis.pool.testonborrow=true
#在空闲时检查有效性, Default false
Redis.pool.testwhileidle=true
#连接耗尽时是否阻塞, false to report exception, true to block timeout, default true
Redis.pool.blockwhenexhausted=false
#RedisClusterConfiguration配置
redis.maxredirects=5
#主机和端口号
redis.host1=192.168.200.128
redis.port1=7000
redis.host2=192.168.200.128
redis.port2=7001
redis.host3=192.168.200.128
redis.port3=7002
redis.host4=192.168.200.128
redis.port4=7003
redis.host5=192.168.200.128
redis.port5=7004
redis.host6=192.168.200.128
redis.port6=7005
XML configuration file
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" Xmlns:xs I= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://www.springframework.org/schema/beans Http://www.springframework.org/schema/beans/spring-beans.xsd "> <!--Introducing configuration Files--<bean id=" Placeholderco Nfigurer "class=" Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" O Rder "value=" 1 "/> <property name=" Ignoreunresolvableplaceholders "value=" true "/> <property nam
E= "Locations" > <list> <value>classpath:redis-cluster.properties</value> </list> </property> </bean> <!--configuration Jedis pool--> <bean id= "Jedi Spoolconfig "class=" Redis.clients.jedis.JedisPoolConfig "> <!--Maximum connections and <property name=" Maxto Tal "Value=" ${redis.pool.maxtoTal} "/> <!--maximum idle time-<property name=" Maxidle "value=" ${redis.pool.maxidle} "/> &L t;!
--Max connections per connection--<property name= "Numtestsperevictionrun" value= "${redis.pool.numtestsperevictionrun}"/> <!--release scan interval--<property name= "Timebetweenevictionrunsmillis" value= "${redis.pool.timebetweenevi Ctionrunsmillis} "/> <!--minimum idle time of connection--<property name=" Minevictableidletimemillis "value=" ${re Dis.pool.minEvictableIdleTimeMillis} "/> <!--the connection control 歘 is released after a period of time, when idle time > This value and idle connection > maximum number of idle connections is released directly--&L T;property name= "Softminevictableidletimemillis" value= "${redis.pool.softminevictableidletimemillis}"/> <!-
-Maximum wait milliseconds to get a link, less than 0: blocking indeterminate time, Default-1--<property name= "Maxwaitmillis" value= "${redis.pool.maxwaitmillis}"/> <!--check validity when getting links, default false---<property name= "Testonborrow" value= "${redis.pool.testonborrow}"/ > <!--inCheck validity at idle, default false--<property name= "Testwhileidle" value= "${redis.pool.testwhileidle}"/> <!-- Whether the connection is exhausted when blocking, false reporting exception, true blocking timeout default:true--> <property name= "blockwhenexhausted" value= "${redis.pool.blockwhenexh austed} "/> </bean> <!--configuration redisclusterconfiguration--> <bean id=" Redisclusterconfiguration " class= "Org.springframework.data.redis.connection.RedisClusterConfiguration" > <property name= "maxredirects"
Value= "${redis.maxredirects}" ></property> <property name= "Clusternodes" > <set> <bean class= "Org.springframework.data.redis.connection.RedisNode" > <constructo R-arg name= "host" value= "${redis.host1}"/> <constructor-arg name= "port" value= "${redis.port1}"/&
Gt
</bean> <bean class= "Org.springframework.data.redis.connection.RedisNode" > <Constructor-arg name= "host" value= "${redis.host2}"/> <constructor-arg name= "port" value= "${redis . Port2} "/> </bean> <bean class=" Org.springframework.data.redis.connection.Redi SNode "> <constructor-arg name=" host "value=" ${redis.host3} "/> <constru Ctor-arg name= "Port" value= "${redis.port3}"/> </bean> <bean class= "ORG.SPRINGF Ramework.data.redis.connection.RedisNode "> <constructor-arg name=" host "value=" ${redis.host4} "/&
Gt <constructor-arg name= "Port" value= "${redis.port4}"/> </bean> <bean class= " Org.springframework.data.redis.connection.RedisNode "> <constructor-arg name=" host "value=" ${redi S.HOST5} "/> <constructor-arg name=" port "value=" ${redis.port5} "/> </