1. Build a Redis.properties Property file
# Redis settingredis.addr = 127.0.0.1redis.port = 6379redis.auth = Masterredis.maxidle = 200redis.maxactive = 1024redis.ma xwait = 10000redis.timeout = 10000redis.testonborrow = True
2, create a new Redisutil.java file, the file code is as follows:
Packagecn.com.taiji.sample.utils;Importjava.io.Serializable;Importjava.util.ArrayList;Importjava.util.List;ImportRedis.clients.jedis.Jedis;ImportRedis.clients.jedis.JedisPool;ImportRedis.clients.jedis.JedisPoolConfig;ImportRedis.clients.jedis.JedisShardInfo;ImportRedis.clients.jedis.ShardedJedis;ImportRedis.clients.jedis.ShardedJedisPool; Public classRedisutilImplementsserializable{Private Static Final LongSerialversionuid = -1149678082569464779l; //Redis Server IP Private StaticString addr; //the port number of the Redis Private Static intPort; //Access Password Private StaticString Auth; //the maximum number of available connection instances, with a default value of 8;//If the assignment is-1, it is unrestricted, and if the pool is already assigned a maxactive Jedis instance, the state of the pool is exhausted (exhausted) at this time. Private Static intmaxactive; //controls the maximum number of Jedis instances in a pool that have an idle (idle) state, and the default value is 8. Private Static intMaxidle; //The maximum time to wait for an available connection, in milliseconds, and the default value is-1, which means that never times out. If the waiting time is exceeded, the jedisconnectionexception is thrown directly; Private Static intmaxwait; Private Static intTimeOut; //whether validate operations are performed in advance when a Jedis instance is borrow, and if true, the resulting Jedis instances are available; Private Static BooleanTestonborrow; Public StaticJedis Jedis;//non-slice client connections Public StaticJedispool Jedispool;//non-tiled connection pool Public StaticShardedjedis Shardedjedis;//Slice amount Client Connection Public StaticShardedjedispool Shardedjedispool;//Slice Connection Pool /*** Initialize non-tile pool*/ Private Static voidInitialpool () {//Pool Basic ConfigurationJedispoolconfig config =NewJedispoolconfig (); Config.setmaxtotal (maxactive); Config.setmaxidle (Maxidle); Config.setmaxwaitmillis (maxwait); Config.settestonborrow (Testonborrow); Jedispool=Newjedispool (config, addr, port); } /*** Initialize the tile pool*/ Private Static voidInitialshardedpool () {//Pool Basic ConfigurationJedispoolconfig config =NewJedispoolconfig (); Config.setmaxtotal (maxactive); Config.setmaxidle (Maxidle); Config.setmaxwaitmillis (maxwait); Config.settestonborrow (Testonborrow); //Slave linkslist<jedisshardinfo> shards =NewArraylist<jedisshardinfo>(); Shards.add (Newjedisshardinfo (addr, port, auth)); //Construction PoolShardedjedispool =Newshardedjedispool (config, shards); } Public Static voidAfterpropertiesset ()throwsException {//TODO auto-generated Method StubInitialpool (); Initialshardedpool (); Try{Shardedjedis=Shardedjedispool.getresource (); } Catch(Exception e) {System.out.println ("Connection Shardedjedispool failed!"); } Try{Jedis=Jedispool.getresource (); } Catch(Exception e) {System.out.println ("Connection Jedispool failed!"); } } PublicString getaddr () {returnaddr; } Public voidsetaddr (String addr) { This. addr =addr; } Public intGetport () {returnPort; } Public voidSetport (intPort) { This. Port =Port; } PublicString GetAuth () {returnAuth; } Public voidSetauth (String auth) { This. Auth =Auth; } Public intgetmaxactive () {returnmaxactive; } Public voidSetmaxactive (intmaxactive) { This. maxactive =maxactive; } Public intGetmaxidle () {returnMaxidle; } Public voidSetmaxidle (intmaxidle) { This. Maxidle =Maxidle; } Public intgetmaxwait () {returnmaxwait; } Public voidSetmaxwait (intmaxwait) { This. maxwait =maxwait; } Public intgettimeout () {returnTimeOut; } Public voidSetTimeOut (intTimeOut) { This. TimeOut =TimeOut; } Public BooleanIstestonborrow () {returnTestonborrow; } Public voidSettestonborrow (BooleanTestonborrow) { This. Testonborrow =Testonborrow; }}
In the Set method, it will float yellow, without affecting the use.
3. In the Spring-servlet.xml configuration file, add The following code :
<Context:component-scanBase-package= "Cn.com.taiji.sample.config.web" /> <Context:property-placeholder Location= "file:#{systemproperties[' webapp.sample ']}/web-inf/conf/redis.properties" /> <BeanID= "Redisutil"class= "Cn.com.taiji.sample.utils.RedisUtil"> < Propertyname= "Addr"><value>${REDIS.ADDR}</value></ Property> < Propertyname= "Port"><value>${redis.port}</value></ Property> < Propertyname= "Auth"><value>${redis.auth}</value></ Property> < Propertyname= "Maxidle"><value>${redis.maxidle}</value></ Property> < Propertyname= "Maxactive"><value>${redis.maxactive}</value></ Property> < Propertyname= "Maxwait"><value>${redis.maxwait}</value></ Property> < Propertyname= "TimeOut"><value>${redis.timeout}</value></ Property> < Propertyname= "Testonborrow"><value>${redis.testonborrow}</value></ Property> </Bean>
<location = ""/> refers to the file path of redis.properties.
The spring configuration for Redis is now complete.
The next article describes the use of Redis in Java projects.
Redis's configuration in spring and Java code implementation