目录
article
Spring Redis
Spring Redis
Messaging with Redis
- spring boot 配置和使用 redis(包含 spring data redis 1.x 和 2.x 配置)
- spring-data-redis2.0 以上配置 redis 连接
- Spring Data Redis
实例代码
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>liujiajia.me.sample</groupId>
<artifactId>redis-sample</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>redis-sample</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
<swagger.version>2.9.2</swagger.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>${swagger.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>${swagger.version}</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>aliyun</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<pluginRepository>
<id>aliyun-plugin</id>
<url>https://maven.aliyun.com/repository/public</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
</project>
application.properties
## redis config
spring.redis.host=localhost
spring.redis.port=6379
#spring.redis.password=
# 最大空闲连接数
spring.redis.jedis.pool.max-active=8
# 最小空闲连接数
spring.redis.jedis.pool.max-idle=8
# 等待可用连接的最大时间,负数为不限制
spring.redis.jedis.pool.max-wait=-1
# 最大活跃连接数,负数为不限制
spring.redis.jedis.pool.min-idle=1
# 数据库连接超时时间,2.0 中该参数的类型为 Duration,这里在配置的时候需要指明单位 1.x 可以将此参数配置 10000 单位是 ms
# 连接池配置,2.0 中直接使用 jedis 或者 lettuce 配置连接池
spring.redis.timeout=60s
spring.redis.database=0
RedisSampleApplication.java
package liujiajia.me.sample.redissample;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@EnableSwagger2
@SpringBootApplication
public class RedisSampleApplication {
public static void main(String[] args) {
SpringApplication.run(RedisSampleApplication.class, args);
}
}
RedisController.java
package liujiajia.me.sample.redissample.controller;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource;
@RestController
@RequestMapping("api/redis")
public class RedisController {
@Autowired
StringRedisTemplate redisTemplate;
@RequestMapping(value = "hash/{key}/{value}", method = RequestMethod.POST)
public Boolean sendMessage(@PathVariable String key, @PathVariable String value) {
// redisTemplate.convertAndSend("chat", value);
redisTemplate.opsForHash().put("hk", key, value);
return true;
}
}
附 1. spring properties - redis
摘自 Spring Application properties
| Key | Default Value | Description |
|---|---|---|
spring.redis.client-name | Client name to be set on connections with CLIENT SETNAME. | |
spring.redis.cluster.max-redirects | Maximum number of redirects to follow when executing commands across the cluster. | |
spring.redis.cluster.nodes | 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. | |
spring.redis.database | 0 | Database index used by the connection factory. |
spring.redis.host | localhost | Redis server host. |
spring.redis.jedis.pool.max-active | 8 | Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. |
spring.redis.jedis.pool.max-idle | 8 | Maximum number of “idle” connections in the pool. Use a negative value to indicate an unlimited number of idle connections. |
spring.redis.jedis.pool.max-wait | -1ms | 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. |
spring.redis.jedis.pool.min-idle | 0 | Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if both it and time between eviction runs are positive. |
spring.redis.jedis.pool.time-between-eviction-runs | Time between runs of the idle object evictor thread. When positive, the idle object evictor thread starts, otherwise no idle object eviction is performed. | |
spring.redis.lettuce.cluster.refresh.adaptive | FALSE | Whether adaptive topology refreshing using all available refresh triggers should be used. |
spring.redis.lettuce.cluster.refresh.period | Cluster topology refresh period. | |
spring.redis.lettuce.pool.max-active | 8 | Maximum number of connections that can be allocated by the pool at a given time. Use a negative value for no limit. |
spring.redis.lettuce.pool.max-idle | 8 | Maximum number of “idle” connections in the pool. Use a negative value to indicate an unlimited number of idle connections. |
spring.redis.lettuce.pool.max-wait | -1ms | 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. |
spring.redis.lettuce.pool.min-idle | 0 | Target for the minimum number of idle connections to maintain in the pool. This setting only has an effect if both it and time between eviction runs are positive. |
spring.redis.lettuce.pool.time-between-eviction-runs | Time between runs of the idle object evictor thread. When positive, the idle object evictor thread starts, otherwise no idle object eviction is performed. | |
spring.redis.lettuce.shutdown-timeout | 100ms | Shutdown timeout. |
spring.redis.password | Login password of the redis server. | |
spring.redis.port | 6379 | Redis server port. |
spring.redis.sentinel.master | Name of the Redis server. | |
spring.redis.sentinel.nodes | Comma-separated list of “host:port” pairs. | |
spring.redis.sentinel.password | Password for authenticating with sentinel(s). | |
spring.redis.ssl | FALSE | Whether to enable SSL support. |
spring.redis.timeout | Connection timeout. | |
spring.redis.url | Connection URL. Overrides host, port, and password. User is ignored. Example: redis://user:password@example.com:6379 |