Spring Boot2 集成Redis

一、引入Maven依赖java

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

在学习过程当中发现有两种引入redis的依赖artifactId,分别是node

spring-boot-starter-redisredis

spring-boot-starter-data-redisspring

查询资料,有说是spring-boot-starter-redis 更名成了 spring-boot-starter-data-redis,建议使用数据库

spring-boot-starter-data-redis。bootstrap

二、在 application.properties 应用配置文件,增长 Redis 相关配置,注意下面几个参数跟以前不同了服务器

# Redis 配置
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=127.0.0.1
# Redis服务器链接端口
spring.redis.port=6379
# Redis服务器链接密码(默认为空)
spring.redis.password=
# 链接超时时间(毫秒)
spring.redis.timeout=2000

# 链接池中的最大空闲链接(使用负值表示无限数量的空闲链接)
spring.redis.jedis.pool.max-idle=8
# 链接池中的最小空闲链接(使用正数时起做用)
spring.redis.jedis.pool.min-idle=0
# 链接池最大链接数(使用负值表示没有限制)
spring.redis.jedis.pool.max-active=8
# 链接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.jedis.pool.max-wait=-1

详细解释能够参考注释。对应的配置类:app

org.springframework.boot.autoconfigure.data.redis.RedisPropertieside

Spring Boot2 中的 RedisProperties.java类spring-boot

package org.springframework.boot.autoconfigure.data.redis;

import java.time.Duration;
import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties(prefix = "spring.redis")
public class RedisProperties {

	/**
	 * Database index used by the connection factory.
	 */
	private int database = 0;

	/**
	 * Connection URL. Overrides host, port, and password. User is ignored. Example:
	 * redis://user:password@example.com:6379
	 */
	private String url;

	/**
	 * Redis server host.
	 */
	private String host = "localhost";

	/**
	 * Login password of the redis server.
	 */
	private String password;

	/**
	 * Redis server port.
	 */
	private int port = 6379;

	/**
	 * Whether to enable SSL support.
	 */
	private boolean ssl;

	/**
	 * Connection timeout.
	 */
	private Duration timeout;

	private Sentinel sentinel;

	private Cluster cluster;

	private final Jedis jedis = new Jedis();

	private final Lettuce lettuce = new Lettuce();

	

	/**
	 * Pool properties.
	 */
	public static class Pool {

		/**
		 * Maximum number of "idle" connections in the pool. Use a negative value to
		 * indicate an unlimited number of idle connections.
		 * 池中“空闲”链接的最大数目。使用负值表示无限数量的空闲链接。
		 */
		private int maxIdle = 8;

		/**
		 * Target for the minimum number of idle connections to maintain in the pool. This
		 * setting only has an effect if it is positive.
		 * 链接池中的最小空闲链接。使用正数时起做用。
		 */
		private int minIdle = 0;

		/**
		 * Maximum number of connections that can be allocated by the pool at a given
		 * time. Use a negative value for no limit.
		 * 在给定时间内池能够分配的最大链接数。负数表示无限制。
		 */
		private int maxActive = 8;

		/**
		 * Maximum amount of time a connection allocation should block before throwing an
		 * exception when the pool is exhausted. Use a negative value to block
		 * indefinitely.
		 * 当池耗尽时,在分配异常以前,链接分配应该阻塞的最大时间。使用负值阻止无限期。
		 */
		private Duration maxWait = Duration.ofMillis(-1);

	}

	/**
	 * Cluster properties.
	 */
	public static class Cluster {

		/**
		 * Comma-separated list of "host:port" pairs to bootstrap from. This represents an
		 * "initial" list of cluster nodes and is required to have at least one entry.
		 */
		private List<String> nodes;

		/**
		 * Maximum number of redirects to follow when executing commands across the
		 * cluster.
		 */
		private Integer maxRedirects;



	}

	/**
	 * Redis sentinel properties.
	 */
	public static class Sentinel {

		/**
		 * Name of the Redis server.
		 */
		private String master;

		/**
		 * Comma-separated list of "host:port" pairs.
		 */
		private List<String> nodes;


	}

	/**
	 * Jedis client properties.
	 */
	public static class Jedis {

		/**
		 * Jedis pool configuration.
		 */
		private Pool pool;

	}

	/**
	 * Lettuce client properties.
	 */
	public static class Lettuce {

		/**
		 * Shutdown timeout.
		 */
		private Duration shutdownTimeout = Duration.ofMillis(100);

		/**
		 * Lettuce pool configuration.
		 */
		private Pool pool;

	}

}
相关文章
相关标签/搜索