springboot整合redis

redis的数据库在java里面提供的官网工具包为jedis,在springboot中依然使用此包。

在pom文件中加入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

在application.yml文件中编写以下代码

spring:
  redis:
    host: 192.168.68.166
    port: 6379
    password: helloworld
    timeout: 1000
    database: 0
    pool:
      max-active: 10
      max-idle: 8
      min-idle: 2
      max-wait: 100

然后就可以使用RedisTemplate了,如果application中默认不支持该缓存数据库,则可以使用@value+yml文件,使用java配置类生成客户端。

@SpringBootTest(classes = StartSpringBootMain.class)
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class TestRedis {
	@Resource
	private RedisTemplate<String, String> redisTemplate;
	@Test
	public void testSet() {
		this.redisTemplate.opsForValue().set("hello", "world");
		System.out.println(this.redisTemplate.opsForValue().get("hello"));
	}
}

如果配置序列话的话,可以创建特定的bean

@Bean
public RedisTemplate<String,Object> redisTemplate(RedisConnectionFactory cf) {
		RedisTemplate<String, Object> redis = new RedisTemplate<String, Object>();
		redis.setConnectionFactory(cf);
		redis.setKeySerializer(new StringRedisSerializer());
		redis.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class));
		return redis;
	}

如果需要链接多个redis数据库,则使用@value

spring:
  redis:
    host: 192.168.68.166
    port: 6379
    password: mldnjava
    timeout: 1000
    database: 0
    pool:
      max-active: 10
      max-idle: 8
      min-idle: 2
      max-wait: 100
  redis-two:
    host: 192.168.68.166
    port: 6379
    password: mldnjava
    timeout: 1000
    database: 0
    pool:
      max-active: 10
      max-idle: 8
      min-idle: 2
      max-wait: 100
@Configuration
public class RedisTwoConfig {
	public RedisConnectionFactory getRedisConnectionFactory(String hostName,
			String password, int port, int maxActive, int maxIdle, int minIdle,
			long maxWait, int database) { // 是负责建立Factory的连接工厂类
		JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
		jedisFactory.setHostName(hostName);
		jedisFactory.setPort(port);
		jedisFactory.setPassword(password);
		jedisFactory.setDatabase(database);
		JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
		poolConfig.setMaxTotal(maxActive);
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMinIdle(minIdle);
		poolConfig.setMaxWaitMillis(maxWait);
		jedisFactory.setPoolConfig(poolConfig);
		jedisFactory.afterPropertiesSet(); // 初始化连接池配置
		return jedisFactory;
	}
	@Bean("redisTwo")
	public RedisTemplate<String, Object> getRedisTemplate(
			@Value("${spring.redis-two.host}") String hostName,
			@Value("${spring.redis-two.password}") String password,
			@Value("${spring.redis-two.port}") int port,
			@Value("${spring.redis-two.database}") int database,
			@Value("${spring.redis-two.pool.max-active}") int maxActive,
			@Value("${spring.redis-two.pool.max-idle}") int maxIdle,
			@Value("${spring.redis-two.pool.min-idle}") int minIdle,
			@Value("${spring.redis-two.pool.max-wait}") long maxWait) {

		RedisConnectionFactory factory = this.getRedisConnectionFactory(
				hostName, password, port, maxActive, maxIdle, minIdle, maxWait,
				database); // 建立Redis的连接
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(factory);
		redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class)); // value的序列化类型
return redisTemplate;
	}
}

 或者这样配置

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisTwoConfig {
	@Value("${spring.redis-two.host}")
	private String hostName;
	@Value("${spring.redis-two.password}")
	private String password;
	@Value("${spring.redis-two.port}")
	private int port;
	@Value("${spring.redis-two.database}")
	private int database;
	@Value("${spring.redis-two.pool.max-active}")
	private int maxActive;
	@Value("${spring.redis-two.pool.max-idle}")
	private int maxIdle;
	@Value("${spring.redis-two.pool.min-idle}")
	private int minIdle;
	@Value("${spring.redis-two.pool.max-wait}")
	private long maxWait;

	public RedisConnectionFactory getRedisConnectionFactory() { // 是负责建立Factory的连接工厂类
		JedisConnectionFactory jedisFactory = new JedisConnectionFactory();
		jedisFactory.setHostName(hostName);
		jedisFactory.setPort(port);
		jedisFactory.setPassword(password);
		jedisFactory.setDatabase(database);
		JedisPoolConfig poolConfig = new JedisPoolConfig(); // 进行连接池配置
		poolConfig.setMaxTotal(maxActive);
		poolConfig.setMaxIdle(maxIdle);
		poolConfig.setMinIdle(minIdle);
		poolConfig.setMaxWaitMillis(maxWait);
		jedisFactory.setPoolConfig(poolConfig);
		jedisFactory.afterPropertiesSet(); // 初始化连接池配置
		return jedisFactory;
	}

	@Bean("redisTwo")
	public RedisTemplate<String, Object> getRedisTemplate() {
		RedisConnectionFactory factory = this.getRedisConnectionFactory(); // 建立Redis的连接
		RedisTemplate<String, Object> redisTemplate = new RedisTemplate<String, Object>();
		redisTemplate.setConnectionFactory(factory);
		redisTemplate.setKeySerializer(new StringRedisSerializer()); // key的序列化类型
		redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Object.class)); // value的序列化类型
		return redisTemplate;
	}
}

相关推荐