1.使用lettuce客户端
SpringBoot默认使用lettuce客户端
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
2.编写配置
application.properties
server.port=8080
#-----redis配置----------
spring.redis.host=127.0.0.1
spring.redis.port=6379
# Redis数据库索引(默认为0)
spring.redis.database=0
#没有设用户名和密码
#spring.redis.username=root
#spring.redis.password=123456
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
3.代码测试
HelloController
package com.limi.springboottest2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.annotation.Resources;
@Controller
public class HelloController {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
RedisConnectionFactory redisConnectionFactory;
@ResponseBody
@GetMapping("/test")
public String test1(){
redisTemplate.opsForValue().increment("number");
String number = (String) redisTemplate.opsForValue().get("number");
System.out.println(redisConnectionFactory.getClass());
return "number="+number;
}
}
2.使用jedis客户端
1.添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
</dependency>
2.编写配置
application.properties
server.port=8080
#-----redis配置----------
spring.redis.host=127.0.0.1
spring.redis.port=6379
# Redis数据库索引(默认为0)
spring.redis.database=0
#没有设用户名和密码
#spring.redis.username=root
#spring.redis.password=123456
#连接超时时间(毫秒)
spring.redis.timeout=1800000
#连接池最大连接数(使用负值表示没有限制)
spring.redis.lettuce.pool.max-active=20
#最大阻塞等待时间(负数表示没限制)
spring.redis.lettuce.pool.max-wait=-1
#连接池中的最大空闲连接
spring.redis.lettuce.pool.max-idle=5
#连接池中的最小空闲连接
spring.redis.lettuce.pool.min-idle=0
3.代码测试
HelloController
package com.limi.springboottest2.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
import javax.annotation.Resources;
@Controller
public class HelloController {
@Autowired
private StringRedisTemplate redisTemplate;
@Autowired
RedisConnectionFactory redisConnectionFactory;
@ResponseBody
@GetMapping("/test")
public String test1(){
redisTemplate.opsForValue().increment("number");
String number = (String) redisTemplate.opsForValue().get("number");
System.out.println(redisConnectionFactory.getClass());
return "number="+number;
}
}