作为一个人新人,我是发现了 ,单独学习一个技术好像都挺简单的,但是只要跟springboot集成就不简单。
springboot和redis的整合,笔者主要困难点在两个地方。
1、springboot的redis依赖引入之后工程就是要报错。
2、springboot的redis第一个实例配置很痛苦。
springboot集成redis–pom
pom文件添加redis的依赖:
<!--springboot整合redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
刚开始,笔者只添加spring-boot-starter-redis,所以就一直报说jedis和spring-data-redis缺少了,所以笔者再次引入jedis和spring-data-reidis依赖,这个时候,都载不下来,好吧,读到这边其实很多读者应该就懂了,并不是依赖添加少了的缘故,而是因为maven下载包有问题。
笔者这边重现了错误的过程:
下图错误代表:单纯只引入spring-boot-starter-redis时提示的错误
其中重要的是:No response received after 60000
是的,就是nexus服务器未响应。
而工程的maven依赖包报了如下的错误:
是的就是少包错误,但是这个时候读者应该要意识到,我都没有添加依赖,却自动会提示少这个包,而且所在的本地库路径都有,那么其实就是说明了,不是因为没有添加依赖的缘故,而是包的下载有问题。
但是笔者那会还不知道是因为这个问题,所以给他添加依赖了,还是报错,如下:
最终笔者,采用的是手工去nexus平台下载上述的两个包,jedis和spring-data-redis.然后丢到对应的本地库的目录下:
上面,虽然没有自动载下jar包,但是有另外几个文件,也是要保留的,因为那个是关联的关键。
2、springboot redis实例
2.1 controller层
package com.neo.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
@Component
@RestController
public class RedisClient {
@Autowired
private JedisPool jedisPool;//工程启动的时候,spring容器初始化了jedis的连接池
@RequestMapping("/set")
public void set(String key, String value) throws Exception {
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.set(key, value);
} finally {
//返还到连接池
jedis.close();
}
}
}
2.2 jedis连接池初始化工具
package com.neo.redis;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
@Configuration
public class RedisConfiguration {
/**
*
* @param config 通过注解注入的形式进行,初始化工作由下面的jedisPoolConfig()方法进行了。
* @param host
* @param port
* @return
*/
@Bean
@Autowired
public JedisPool jedisPool(@Qualifier("jedis.pool.config") JedisPoolConfig config,
@Value("${spring.redis.host}")String host, @Value("${spring.redis.port}")int port) {
return new JedisPool(config, host, port);
}
@Bean(name= "jedis.pool.config")
public JedisPoolConfig jedisPoolConfig (@Value("${spring.redis.pool.max-active}")int maxTotal,@Value("${spring.redis.pool.max-idle}")int maxIdle,
@Value("${spring.redis.timeout}")int maxWaitMillis) {
JedisPoolConfig config = new JedisPoolConfig();
config.setMaxTotal(maxTotal);
config.setMaxIdle(maxIdle);
config.setMaxWaitMillis(maxWaitMillis);
return config;
}
}
2.3 redis配置内容(配置在application.properties里头)
# REDIS (RedisProperties)
# Redis数据库索引(默认为0)
spring.redis.database=0
# Redis服务器地址
spring.redis.host=localhost
# Redis服务器连接端口
spring.redis.port=6379
# Redis服务器连接密码(默认为空)
spring.redis.password=
# 连接池最大连接数(使用负值表示没有限制)
spring.redis.pool.max-active=8
# 连接池最大阻塞等待时间(使用负值表示没有限制)
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=4
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0
搭建到此over
下面做下测试:
http://localhost:8080/set?key=wlttest&value=test111
这个会去访问set方法,同时,往redis里头插入key=wlttest value=test111。
上面做完之后,在redis的客户端执行下 get wlttest 看下返回值是不是test111,是的话,项目就跑起来了。