Stand-alone configuration
Redis.properties Configuration
#redis的服务器地址redis.host=127.0.0.1#redis的服务端口redis.port=6379#客户端超时时间单位是毫秒redis.timeout=100000#最大建立连接等待时间redis.maxWaitMillis=1000#最小空闲数redis.minIdle=5#最大空闲数redis.maxIdle=20#最大连接数redis.maxTotal=100
XML configuration
<!--read the properties file--><bean id= "PropertyConfigurerForProject1" class= " Org.springframework.beans.factory.config.PropertyPlaceholderConfigurer "> <property name=" Order "value=" 1 "/ > <property name= "systempropertiesmodename" value= "System_properties_mode_override"/> <property name= " Ignoreresourcenotfound "value=" true "/> <property name=" Ignoreunresolvableplaceholders "value=" true "/> < Property Name= "Locations" > <list> <value>classpath*:redis.properties</value> </list> </property></bean><!--Configuration object for Jedis connection pool--><bean id= "Jedispoolconfig" class= " Redis.clients.jedis.JedisPoolConfig "> <!--max idle--<property name=" Maxidle "value=" ${redis.maxidle} "/& Gt <!--minimum idle number--<property name= "Minidle" value= "${redis.minidle}"/> <!--Maximum Connections--<property N Ame= "Maxtotal" value= "${redis.maxtotal}"/> <property name= "Testonborrow" value= "true" /> <!--maximum connection waiting time-<property name= "Maxwaitmillis" value= "${redis.maxwaitmillis}"/></bean>& lt;! --jedis Server information--><bean id= "Jedisshardinfo" class= "Redis.clients.jedis.JedisShardInfo" > <constructor-arg index= "0" value= "${redis.host}"/> <constructor-arg index= "1" value= "${redis.port}" type= "int"/> <const Ructor-arg index= "2" value= "${redis.timeout}" type= "int"/></bean><!--Jedis Connection pool--><bean id= "Shard Edjedispool "class=" Redis.clients.jedis.ShardedJedisPool "> <constructor-arg index=" 0 "ref=" jedispoolconfig "/ > <constructor-arg index= "1" > <list> <ref bean= "Jedisshardinfo"/> </l ist> </constructor-arg></bean><!--Redis Connection Factory--><bean id= "Jedisconnectionfactory" class= "or G.springframework.data.redis.connection.jedis.jedisconnectionfactory "> <property name=" shardInfo "ref=" Jedisshardinfo "/> <property NAme= "Poolconfig" ref= "Jedispoolconfig"/></bean><!--cache serialization Mode--><!--The default serializer for key. The default value is Stringserializer--><bean id= "Keyserializer" class= " Org.springframework.data.redis.serializer.StringRedisSerializer "/><!--is the default serializer for value, The default value is the Jdkserializationredisserializer taken from Defaultserializer. --><bean id= "ValueSerializer" class= " Org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer "/><!--redis action template, Common API operations for Jedis--><bean id= "redistemplate" class= "Org.springframework.data.redis.core.RedisTemplate" > <property name= "ConnectionFactory" ref= "jedisconnectionfactory"/> <property name= "KeySerializer" ref= " Keyserializer "/> <property name=" ValueSerializer "ref=" ValueSerializer "/> <property name=" HashKeySeria Lizer "ref=" Keyserializer "/> <property name=" Hashvalueserializer "ref=" ValueSerializer "/></bean>
As of 2.0, the RedisStandaloneConfiguration
JedisConnectionFactory
HostName
Port
Password
Configuration of the properties is introduced and deprecated for the single-machine configuration.
/** * 连接工厂类 * @return */@Beanpublic JedisConnectionFactory jedisConnectionFactory(){ // 单机配置 RedisStandaloneConfiguration standaloneConfig = new RedisStandaloneConfiguration(); standaloneConfig.setHostName("127.0.01"); standaloneConfig.setPort(6379); JedisConnectionFactory connectionFactory = new JedisConnectionFactory(standaloneConfig); return connectionFactory;}
Sentinel Configuration
In order to handle high availability Redis
, support can be used RedisSentinelConfiguration
Redis Sentinel
.
/*** jedis*/@Beanpublic RedisConnectionFactory jedisConnectionFactory() { RedisSentinelConfiguration sentinelConfig = new RedisSentinelConfiguration() .master("mymaster") .sentinel("127.0.0.1", 26379) .sentinel("127.0.0.1", 26380); return new JedisConnectionFactory(sentinelConfig);}
Properties that can be configured:
The name of the Spring.redis.sentinel.master:master node
Spring.redis.sentinel.nodes: Host: comma-separated list of ports
Cluster configuration
@Beanpublic jedisconnectionfactory redisconnectionfactory () {jedisconnectionfactory connectionfactory = new JedisConn Ectionfactory (); Cluster configuration Redisclusterconfiguration redisclusterconfiguration = new Redisclusterconfiguration (); Redisnode redisNode7000 = new Redisnode ("10.1.21.4", 7000); Redisnode redisNode7001 = new Redisnode ("10.1.21.4", 7001); Redisnode redisNode7002 = new Redisnode ("10.1.21.4", 7002); Redisnode redisNode7003 = new Redisnode ("10.1.21.4", 7003); Redisnode redisNode7004 = new Redisnode ("10.1.21.4", 7004); Redisnode redisNode7005 = new Redisnode ("10.1.21.4", 7005); Redisclusterconfiguration.addclusternode (redisNode7000); Redisclusterconfiguration.addclusternode (redisNode7001); Redisclusterconfiguration.addclusternode (redisNode7002); Redisclusterconfiguration.addclusternode (redisNode7003); Redisclusterconfiguration.addclusternode (redisNode7004); Redisclusterconfiguration.addclusternode (redisNode7005); ConnectionFactory = new JedisconnecTionfactory (redisclusterconfiguration); Set the connection pool configuration connectionfactory.setpoolconfig (Jedisconfig ()); Set the database, default 0 connectionfactory.setdatabase (2); Set use Connection pool Connectionfactory.setusepool (true); return connectionfactory;}
Configuration Instructions
1. 以上配置是基于Jedis客户端的2. 使用javaCode或者xml的配置方式依据个人喜好
Spring Data Redis Getting Started Sample: Program configuration (v)