![](/img/trans.png)
[英]Unable to store session in Redis using Java Spring Boot Session Data Redis
[英]Spring-Boot: Exception when using Redis as a Session Store
我正在尝试使用spring-boot创建一个最小的Web应用程序,该应用程序将redis用作会话存储。 目标是在Web应用程序的多个实例之间共享会话状态。 该应用程序在没有Redis的情况下可以正常工作(使用“正常”会话),但是在启用Redis的配置后引发异常。 我遵循了本教程 。 该应用程序通过“ java -jar path_to.jar”启动。
要使该应用程序再次运行,只需注释掉配置类即可(请参见下文)。
第一次尝试包括部署到Heroku,但是在使用本地Redis服务器的计算机上,这也是相同的问题。 连接字符串的环境变量已正确设置。
整个应用程序仅包含以下片段。 我想念什么吗? 谢谢。
组态:
@Profile("production")
@Configuration
@EnableRedisHttpSession
public class ProductionRedisConfiguration {
@Bean
public JedisConnectionFactory connectionFactory() throws URISyntaxException {
JedisConnectionFactory redis = new JedisConnectionFactory();
String redisUrl = System.getenv("REDISCLOUD_URL");
URI redisUri = new URI(redisUrl);
redis.setHostName(redisUri.getHost());
redis.setPort(redisUri.getPort());
redis.setPassword(redisUri.getUserInfo().split(":",2)[1]);
return redis;
}
static class Initializer extends AbstractHttpSessionApplicationInitializer {
public Initializer() {
super(ProductionRedisConfiguration.class);
}
}
}
“应用”:
@EnableAutoConfiguration
@ComponentScan
public class Application {
private static final Logger LOGGER = getLogger(Application.class);
public static void main(String[] args) throws Throwable {
SpringApplication.run(Application.class, args);
}
}
。
@Controller
public class GreetingController {
@RequestMapping("/")
public @ResponseBody String session(HttpServletRequest request) {
String name = (String) request.getSession().getAttribute("name");
if(name != null) {
return "Hello, " + name;
} else {
String newName = "User " + new Random().nextInt(1000000);
request.getSession().setAttribute("name", newName);
return "No session found, you are now called " + newName;
}
}
}
依存关系:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session</artifactId>
<version>1.0.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-redis</artifactId>
</dependency>
</dependencies>
跟踪:
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:53)
at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.boot.c
ontext.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:140)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:476)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:124)
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:609)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:806)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:795)
at com.backbase.progfun.Application.main(Application.java:17)
... 6 more
Caused by: org.springframework.boot.context.embedded.EmbeddedServletContainerException: Unable to start embedded Tomcat
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:104)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.<init>(TomcatEmbeddedServletContainer.java:67)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getTomcatEmbeddedServletContainer(TomcatEmbeddedServ
letContainerFactory.java:258)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainerFactory.getEmbeddedServletContainer(TomcatEmbeddedServletCon
tainerFactory.java:138)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:1
60)
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:137)
... 13 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardServer[-1]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.startup.Tomcat.start(Tomcat.java:341)
at org.springframework.boot.context.embedded.tomcat.TomcatEmbeddedServletContainer.initialize(TomcatEmbeddedServletContainer.java:77)
... 18 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardService[Tomcat]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardServer.startInternal(StandardServer.java:732)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 20 more
Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Tomcat]]
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:154)
at org.apache.catalina.core.StandardService.startInternal(StandardService.java:443)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 22 more
Caused by: org.apache.catalina.LifecycleException: A child container failed during start
at org.apache.catalina.core.ContainerBase.startInternal(ContainerBase.java:1131)
at org.apache.catalina.core.StandardEngine.startInternal(StandardEngine.java:302)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
... 24 more
看来最近您还需要为部署在Heroku上的应用程序设置REDIS_PROVIDER
配置REDIS_PROVIDER
。
如果可以解决问题,请尝试为您的应用和heroku config:set REDIS_PROVIDER=REDISCLOUD_URL
运行以下命令: heroku config:set REDIS_PROVIDER=REDISCLOUD_URL
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.