整体三大块
1.pom.xml的配置
2.SpringBootApplication的配置
3.application.yml的配置
一 pom.xml的配置
在dependencies中加一下配置。开始一直出问题,总是怀疑版本问题,看网上帖子,版本上确实有区别,但事后来看没有区别。不加版本号,如果Spring Boot是1.5.9.RELEASE,它会默认使用1.8.9的版本;如果是2.0.4,它会默认使用2.0的版本
<!-- redis依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- redis依赖 -->
第三布配置的yml文件如何能被识别,是因为在resuoures里配置位置的。有文档说,SpringBootApplication类会默认在其同级config目录下找xx.yml文件。如果想自己指定目录需要加一下配置
<resources>
<resource>
<directory>src/main/resources/config</directory>
</resource>
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
</resources>
二SpringBootApplication的配置
回头来看必须有的是@SpringBootApplication。建议加上@EnableCaching。
Application的写法,我测过两种都行
写法一,简单版
@EnableCaching
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
写法二,产品版
@ComponentScan
@EnableSpringConfigured
@EnableTransactionManagement
@EnableAutoConfiguration(exclude = { FreeMarkerAutoConfiguration.class })
public @SpringBootApplication class App extends SpringBootServletInitializer {
/** 日志记录器 */
private Logger logger = LoggerFactory.getLogger(App.class);
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
protected @Override SpringApplicationBuilder configure(SpringApplicationBuilder app) {
logger.info("Application on configure");
return app.sources(App.class);
}
}三.application.yml的配置。
spring:
profiles:
active: development
data:
mongodb:
host: 10.184.161.67
port: 27017
username: cloudtest
password: cloudtest
database: cloudtest
redis:
host: 10.171.252.106
port: 6379
timeout: 5000
pool:
max-idle: 8
min-idle: 0
max-active: 1000
这个地方被坑了好久,大家注意redis的配置位置,不是跟mongodb同层级,而是跟data同层级。
因为这个导致一直在报下面的错。如果想调试是不是这个问题很简单,在后面提供的测试用例部分,在ValueOperations<String, String> operations = redisTemplate.opsForValue();行打断点,查看下redisTemplate这个对象里connectionFactory对象是否有获取到yml里配置的值。如果没有,请再好好检查下配置问题。。。
springboot 1.5.9.RELEASE的该问题报错
Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
org.springframework.data.redis.RedisConnectionFailureException: Cannot get Jedis connection; nested exception is redis.clients.jedis.exceptions.JedisConnectionException: Could not get a resource from the pool
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.fetchJedisConnector(JedisConnectionFactory.java:204)
at org.springframework.data.redis.connection.jedis.JedisConnectionFactory.getConnection(JedisConnectionFactory.java:348)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:129)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:92)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:79)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:194)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:169)
at org.springframework.data.redis.core.RedisTemplate.hasKey(RedisTemplate.java:658)
at com.xncoding.controller.HelloWorldController.sayHello(HelloWorldController.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:97)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
springboot 2.0.4.RELEASE的该问题报错
Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
org.springframework.data.redis.RedisConnectionFailureException: Unable to connect to Redis; nested exception is io.lettuce.core.RedisConnectionException: Unable to connect to localhost:6379
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getNativeConnection(LettuceConnectionFactory.java:966)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory$SharedConnection.getConnection(LettuceConnectionFactory.java:934)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getSharedConnection(LettuceConnectionFactory.java:786)
at org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory.getConnection(LettuceConnectionFactory.java:300)
at org.springframework.data.redis.core.RedisConnectionUtils.doGetConnection(RedisConnectionUtils.java:132)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:95)
at org.springframework.data.redis.core.RedisConnectionUtils.getConnection(RedisConnectionUtils.java:82)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:211)
at org.springframework.data.redis.core.RedisTemplate.execute(RedisTemplate.java:184)
at org.springframework.data.redis.core.RedisTemplate.hasKey(RedisTemplate.java:739)
at com.xncoding.controller.HelloWorldController.sayHello(HelloWorldController.java:21)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209)
四测试demo。写个helloword的controller,正常启动后 http://localhost:8080/即可触发
@RestController
public class HelloWorldController {
@Autowired
private StringRedisTemplate redisTemplate;
@RequestMapping("/")
public String sayHello() {
String str = "";
try{
ValueOperations<String, String> operations = redisTemplate.opsForValue();
// 缓存存在
boolean hasKey = redisTemplate.hasKey("testenv");
if (hasKey) {
str = operations.get("testenv");
}
}catch(Exception e){
str=e.getMessage();
System.out.println(str);
e.printStackTrace();
}
return str;
}