简体   繁体   English

Spring-Boot:将Redis用作会话存储时发生异常

[英]Spring-Boot: Exception when using Redis as a Session Store

I'm trying to create a minimal web application using spring-boot which makes use of redis as a session store. 我正在尝试使用spring-boot创建一个最小的Web应用程序,该应用程序将redis用作会话存储。 The goal is to share session state between multiple instances of the web application. 目标是在Web应用程序的多个实例之间共享会话状态。 The application works fine without Redis (using "normal" sessions) but throws an exception after I enabled the configuration for Redis. 该应用程序在没有Redis的情况下可以正常工作(使用“正常”会话),但是在启用Redis的配置后引发异常。 I followed this tutorial . 我遵循了本教程 The application is started via "java -jar path_to.jar". 该应用程序通过“ java -jar path_to.jar”启动。

To make the app work again, it is sufficient to comment out the configuration class (see below). 要使该应用程序再次运行,只需注释掉配置类即可(请参见下文)。

The first attempt included the deployment to Heroku, but it's the same problem on my machine using a local Redis server. 第一次尝试包括部署到Heroku,但是在使用本地Redis服务器的计算机上,这也是相同的问题。 The environment variable for the connection string is set up properly. 连接字符串的环境变量已正确设置。

The whole application consists only of the snippets shown below. 整个应用程序仅包含以下片段。 Am I missing something? 我想念什么吗? Thank you. 谢谢。

Configuration: 组态:

@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);
        }
    }
}

"App": “应用”:

@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: 依存关系:

<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>

Trace: 跟踪:

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

It appears that as of late you also need to set the REDIS_PROVIDER config var for apps deployed at Heroku. 看来最近您还需要为部署在Heroku上的应用程序设置REDIS_PROVIDER配置REDIS_PROVIDER

Try running the following and for your app and lmk if it resolves the problem: heroku config:set REDIS_PROVIDER=REDISCLOUD_URL 如果可以解决问题,请尝试为您的应用和heroku config:set REDIS_PROVIDER=REDISCLOUD_URL运行以下命令: heroku config:set REDIS_PROVIDER=REDISCLOUD_URL

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM