Current location - Loan Platform Complete Network - Big data management - 3 minutes to get springboot integration redis
3 minutes to get springboot integration redis
Without further ado, here's the code:

<! -- redis -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>. spring-boot-starter-data-redis</artifactId>

<exclusions>

<exclusion>

<groupId>redis. clients</groupId>

<artifactId>jedis</artifactId>

</exclusion>

<exclusion>

<exclusion>

<groupId>redis. lt;groupId>io.lettuce</groupId>

<artifactId>lettuce-core</artifactId>

</exclusion>

</exclusion>

& lt;/exclusions>

</dependency>

<! -- Add jedis client -->

<dependency>

<groupId>redis.clients</groupId>

<artifactId>jedis</ artifactId>

</dependency>

<! -- spring2.0 integration redis required common-pool2 -->

<! -- Must be added, jedis depends on this -->

<dependency>

<groupId>org.apache.commons</groupId>

<artifactId>. commons-pool2</artifactId>

<version>2.5.0</version>

</dependency>

<! -- will act as a Redis object serializer -->

<dependency>

<groupId>com.alibaba</groupId>

<artifactId>fastjson. lt;/artifactId>

<version>1.2.47</version>

</dependency>

#Matser's ip address

redis.hostName= 172.16.44.148

#Port number

redis.port=6379

#If there is a password

redis.password=

#Client timeout in milliseconds Default is 2000

redis.timeout= 10000

# Maximum number of idle

redis.maxIdle=300

# Maximum number of database connections for the connection pool. Set to 0 means unlimited, if jedis 2.4 and later use redis.maxTotal

#redis.maxActive=600

#Controls how many jedis instances can be allocated to a pool, used to replace redis.maxActive, if jedis 2.4 and later use this property

#redis.maxActive=600

#controls how many jedis instances can be allocated to a pool, used to replace redis.maxActive, if jedis 2.4 and later Use this property

redis.maxTotal=1000

# Maximum time to wait for a connection to be established. If exceeded, an exception will be thrown. Set to -1 for unlimited.

redis.maxWaitMillis=1000

#Minimum idle time for a connection Default 1800000 milliseconds (30 minutes)

redis.minEvictableIdleTimeMillis=300000

#Maximum number of connections to release each time. default 3

redis.numTestsPerEvictionRun=1024

#Interval between eviction scans (milliseconds) If negative, no eviction threads are run, default -1

redis.timeBetweenEvictionRunsMillis= 30000

#Whether to test the connection before taking it out of the pool, if the test fails, remove the connection from the pool and try to take out another

redis.testOnBorrow=true

#Check validity at idle, default false

redis. testWhileIdle=true

package com.junsi.demo.config;

import org.springframework.beans.factory.annotation.Value;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation. org.springframework.context.annotation.PropertySource;

import org.springframework.data.redis.connection. RedisConnectionFactory;

import org.springframework.data.redis.connection.jedis. springframework.data.redis.core.RedisTemplate;

import org.springframework.data.redis.serializer. GenericJackson2JsonRedisSerializer;

import org.springframework.data.redis.serializer.StringRedisSerializer;

import com. junsi.demo.utils.RedisUtil;

import redis.clients.jedis.JedisPoolConfig;

/**

* Redis Configuration Classes

* @author pzx

* June 8, 2019

*/

@Configuration

@PropertySource("classpath:redis.properties")

public class RedisConfig {

@ Value("${redis.maxIdle}")

private Integer maxIdle;

@Value("${redis.maxTotal}")

private Integer maxTotal;

@ Value("${redis.maxWaitMillis}")

private Integer maxWaitMillis;

@Value("${redis.minEvictableIdleTimeMillis}")

private Integer minEvictableIdleTimeMillis;

@Value("${redis.numTestsPerEvictionRun}")

private Integer numTestsPerEvictionRun;

@Value("${redis.timeBetweenEvictionRunsMillis}")

private long timeBetweenEvictionRunsMillis;

@Value("${redis.testOnBorrow}")

private boolean testOnBorrow;

@Value("${redis.testWhileIdle}")

private boolean testWhileIdle;

@Value("${spring.redis.cluster.nodes}")

private String clusterNodes;

@Value("${spring.redis.cluster. .max-redirects}")

private Integer mmaxRedirectsac;

/**

* JedisPoolConfig Connection Pool

* @return

*/

@Bean

public JedisPoolConfig jedisPoolConfig() {

JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();

// Maximum number of idleness

jedisPoolConfig.setMaxIdle(maxIdle);

// Maximum number of database connections for the connection pool

jedisPoolConfig.setMaxTotal(maxTotal);

// Maximum time to wait for a connection to be established

jedisPoolConfig.setMaxWaitMillis(maxWaitMillis);

// Minimum idle time for evictable connections Default 1800000 milliseconds (30 minutes)

jedisPoolConfig. setMinEvictableIdleTimeMillis(minEvictableIdleTimeMillis);

// // Maximum number of evictions per eviction check If negative it is : 1/abs(n), default 3

jedisPoolConfig. setNumTestsPerEvictionRun(numTestsPerEvictionRun);

// Time between eviction scans (milliseconds) If negative, then no eviction threads are run, default -1

jedisPoolConfig. setTimeBetweenEvictionRunsMillis(timeBetweenEvictionRunsMillis);

// Whether or not to test the connection before taking it out of the pool, if the test fails, remove the connection from the pool and try to take another one out

jedisPoolConfig.setTestOnBorrow(testOnBorrow);

// Check validity at idle, default false

jedisPoolConfig.setTestWhileIdle(testWhileIdle);

return jedisPoolConfig;

}

/**

* @param jedisPoolConfig

* @return

*/

@Bean

public JedisConnectionFactory JedisConnectionFactory(JedisPoolConfig jedisPoolConfig){

JedisConnectionFactory JedisConnectionFactory = new JedisConnectionFactory(jedisPoolConfig);

// Connection Pool

JedisConnectionFactory.setPoolConfig(jedisPoolConfig);

// IP Address < /p>

JedisConnectionFactory.setHostName("172.16.44.148");

//Port number

JedisConnectionFactory.setPort(6379);

//If Redis is set with a password

//JedisConnectionFactory.setPassword(password);

//client timeout in milliseconds

JedisConnectionFactory.setTimeout(5000);

return JedisConnectionFactory;

}

/**

* Instantiate the RedisTemplate object

*

* @return

*/

@Bean

public RedisTemplate<String, Object> functionDomainRedisTemplate(RedisConnectionFactory redisConnectionFactory) {

RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();

initDomainRedisTemplate(redisTemplate, redisConnectionFactory);

return redisTemplate;

}

/**

* Set the way data is serialized into redis and enable transactions

*

* @param redisTemplate

* @param factory

*/

private void initDomainRedisTemplate(RedisTemplate<String, Object>. redisTemplate, RedisConnectionFactory factory) {

//If you don't configure Serializer, then String will be used by default when storing, and if you store it with User type, then it will prompt error User can't cast to String! /p>

redisTemplate.setKeySerializer(new StringRedisSerializer());

redisTemplate.setHashKeySerializer(new StringRedisSerializer());

redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());

redisTemplate .setValueSerializer(new GenericJackson2JsonRedisSerializer());

// Enable transactions

redisTemplate.setEnableTransactionSupport(true);< /p>

redisTemplate.setConnectionFactory(factory);

}

@Bean(name = "redisUtil")

public RedisUtil redisUtil( RedisTemplate<String, Object> redisTemplate) {

RedisUtil redisUtil = new RedisUtil();

redisUtil.setRedisTemplate( redisTemplate);

return redisUtil;

}

}

}