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;

	}

}
相關文章
相關標籤/搜索