标签: spring-session

Spring Cloud,Spring Data Redis和Eureka的生产注意事项

我有一个跨越4种服务器类型的Spring Cloud微服务应用程序:一个安全网关,两个UI服务器和一个REST API服务器。其中的每一个都将在生产环境中的自己的VM上运行:REST服务器的4个服务器实例,彼此之间的2个实例。

该系统预计将为大约30,000个用户提供服务。

服务发现由Eureka提供。我有两个用于故障转移的Eureka服务器。

共享的HTTP会话由Spring Session和Spring Data Redis使用参与服务器上的@EnableRedisHttpSession注释提供。

我决定为Redis设置3个VM(在此URL:http://redis.io/topics/sentinel上的“示例2:三个框的基本设置” )。

每个虚拟机将运行Redis服务器和Redis标记进程(其中Redis服务器之一将为主服务器,两个实例为从属服务器)

这一切在开发机器和系统测试机器上都非常有效,它们大多数都在同一服务器上运行所有进程。

我现在正在尝试在具有多个VM的类似于生产的环境中运行性能测试。我想从已经在生产中使用类似Spring Cloud设置的开发人员那里获得一些反馈和建议:

  • 我应该寻找什么边缘情况?
  • 有建议的配置设置吗?我的设置如下所示。
  • 是否有一些配置设置在测试环境中可能会很好运行,但在生产环境中会成为严重的问题?
  • 在我的特定情况下,我还希望有一种从Redis清除旧数据的解决方案,因为它仅用于保存会话信息。如果由于某种原因spring无法在会话期满时清除会话数据(例如,服务器突然被杀死),我想对真正的旧数据进行一些清除。我了解了有关Redis的LRU /缓存机制的信息,但是似乎只有在达到某些数据大小时,它才能在时间方面没有任何保证。

这是我的主Redis服务器的配置。从站几乎是相同的,只是端口不同,表明它们是主站的从站:

daemonize no

port 6379
dbfilename "dump6379.rdb"
dir "/Users/odedia/Work/Redis/6379"
pidfile "/Users/odedia/Work/Redis/redis6379.pid"
#logfile "/Users/odedia/Work/Redis/redis6379.log"

tcp-backlog 511
timeout 0
tcp-keepalive 60
loglevel notice
databases 16
save 900 1
save 300 10
save 60 10000
stop-writes-on-bgsave-error yes
rdbcompression yes
rdbchecksum yes
slave-serve-stale-data yes
slave-read-only no
repl-diskless-sync no
repl-diskless-sync-delay 5
repl-disable-tcp-nodelay no
slave-priority 100
appendonly no
appendfilename "appendonly.aof"
appendfsync everysec
no-appendfsync-on-rewrite no …
Run Code Online (Sandbox Code Playgroud)

production redis spring-data-redis spring-session spring-cloud

5
推荐指数
1
解决办法
1317
查看次数

Spring Security maxSession不起作用

当用户超过maxSession计数时,我想防止登录。例如,每个用户只能登录一次。然后,如果登录的用户尝试使用其他登录系统,则应禁用该用户的登录。

.sessionManagement()
.maximumSessions(1).expiredUrl("/login?expire").maxSessionsPreventsLogin(true)
.sessionRegistry(sessionRegistry());


@Bean
public static ServletListenerRegistrationBean httpSessionEventPublisher() {
    return new ServletListenerRegistrationBean(new HttpSessionEventPublisher());
}
Run Code Online (Sandbox Code Playgroud)

spring spring-security spring-session

5
推荐指数
2
解决办法
2107
查看次数

Spring Session Redis 序列化器 SerializationException

为了尝试为现有应用程序外部化 tomcat 会话,我正在尝试 Spring Session Redis 解决方案。按照以下步骤在 pom.xml 中包含必要的依赖项后,如下所示:

<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>${org.springframework-version}</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
            <version>1.2.1.RELEASE</version>
        </dependency>
Run Code Online (Sandbox Code Playgroud)

在 web.xml 中添加 springSessionRepositoryFilter,如下所示:

<filter>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSessionRepositoryFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
Run Code Online (Sandbox Code Playgroud)

并在 Spring XML 配置中添加以下内容

<bean class="org.springframework.session.data.redis.config.annotation.web.http.RedisHttpSessionConfiguration"/>

<context:property-placeholder location="classpath:application.properties"/>

<bean class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory" p:port="${spring.redis.port}"/>
Run Code Online (Sandbox Code Playgroud)

并构建并部署到 tomcat,这是我收到的错误:

org.springframework.data.redis.serializer.SerializationException: Cannot serialize; nested exception is org.springframework.core.serializer.support.SerializationFailedException: Failed to serialize object using DefaultSerializer; nested exception is java.io.NotSerializableException: com.sun.jersey.client.apache.ApacheHttpClient
Run Code Online (Sandbox Code Playgroud)

任何建议或帮助将不胜感激。谢谢 !!还附上我的 pom.xml 条目: pom.xml 条目

spring tomcat redis spring-session

5
推荐指数
1
解决办法
2万
查看次数

在 Spring 微服务之间共享 Websocket 会话

我有一个用于 Web 套接字的 Spring boot 应用程序。我没有使用 Stomp Web 套接字。有没有办法可以在微服务的多个实例之间共享 Web 套接字会话。

有没有办法在 Redis 或 cassandra 中保存 websocket 会话?

我的用例是,我的微服务的多个实例正在运行,它正在侦听 kafka 队列,因此当收到消息时,我需要使用 Web 套接字会话将其发送到客户端。我将会话保存在将微服务作为 MAP。我的问题是我的任何一个微服务正在获取消息,如果该微服务的会话不可用,则消息不会发送到客户端。

如果我能够将 websocket 会话保存在 REDIS 或 Cassandra 中,我就可以查询会话并将其发送到客户端。

我无法按照要求使用 Stomp Web 套接字,它必须是普通的 Web 套接字。

websocket spring-boot spring-websocket spring-session

5
推荐指数
1
解决办法
1777
查看次数

在集成测试中禁用带有 Redis 的 Spring 会话

我正在将 spring 会话与 redis 一起使用,但我想在进行测试时禁用它。我的班级有注释:

@ActiveProfiles("integrationtests")
Run Code Online (Sandbox Code Playgroud)

我的 application-integrationtests.tml 文件包含:

spring.autoconfigure.exclude: org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration
Run Code Online (Sandbox Code Playgroud)

但它仍然失败:

Caused by: 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
Run Code Online (Sandbox Code Playgroud)

如果我打开 redis-server ,测试就会工作,但我当然不想保持这种状态;)

//更新

我一直在尝试

@SpringBootTest(classes = {Application.class})
@ActiveProfiles("integrationtests")
Run Code Online (Sandbox Code Playgroud)

和排除 Redis 的 Application.class:

@SpringBootApplication(exclude={SessionAutoConfiguration.class, RedisAutoConfiguration.class, RedisHttpSessionConfiguration.class})
public class Application {

public static void main(String[] args) throws Exception {
    SpringApplication.run(Application.class, args);
}
}
Run Code Online (Sandbox Code Playgroud)

但它失败了:

Error creating bean with name 'redisMessageListenerContainer' defined in class path resource [org/springframework/session/data/redis/config/annotation/web/http/RedisHttpSessionConfiguration.class]
Run Code Online (Sandbox Code Playgroud)

spring autoconfigure debug看到我已经排除了这个类,但是没有效果: …

java spring redis spring-data-redis spring-session

5
推荐指数
1
解决办法
6025
查看次数

如何防止Redis写入匿名用户会话

我有这个示例应用程序:

package com.example.session;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class DemoRedisDataSessionApplication {

    @Configuration
    @EnableWebSecurity
    @EnableRedisHttpSession(redisNamespace = "demo-redis-data-session")
    public static class AppConfiguration extends WebSecurityConfigurerAdapter {

        @Override
        protected void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.inMemoryAuthentication().withUser("user").password("0000").roles("USER");
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http.formLogin().and()
                    .authorizeRequests().antMatchers("/ping").permitAll().and()
                    .authorizeRequests().anyRequest().fullyAuthenticated();
        }

    }

    @RestController
    public static class AppController {
        @GetMapping("/ping")
        public String ping() {
            return "pong";
        }

        @GetMapping("/secured")
        public String …
Run Code Online (Sandbox Code Playgroud)

spring-security spring-data-redis spring-boot spring-session

5
推荐指数
1
解决办法
218
查看次数

Spring Security 如何通过各种会话创建和会话固定组合来处理 JSESSIONID?

我有一个使用 Spring Security 4.0.1.RELEASE 的基于 J2EE REST 的应用程序。不用说,除了 StackOverflow 上有针对性的问题之外,关于 和 的 Spring 文档很少sessionCreationPolicysessionFixation

我正在为 Spring Security 使用基于 Java 的配置,如下所示:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(secureEnabled=true, prePostEnabled=true, jsr250Enabled=true, order=1)
public class DefaultSecurityBeansConfig extends WebSecurityConfigurerAdapter {
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.sessionManagement()
            .sessionCreationPolicy(SessionCreationPolicy.IF_REQUIRED)
            .sessionFixation().migrateSession()
            .and()...; // additional config omitted for brevity
    }
}
Run Code Online (Sandbox Code Playgroud)

我真的只是想知道 Spring 会发生什么行为,因为它与 相关,考虑到和JSESSIONID的所有可能组合。sessionCreationPolicysessionFixation

SessionCreationPolicy枚举中可能的值为ALWAYSNEVERIF_REQUIREDSTATELESS

会话固定的可能值为newSessionmigrateSessionchangeSessionId和 …

java spring stateless jsessionid spring-session

5
推荐指数
1
解决办法
2万
查看次数

Spring引导Websocket扩展

我有一个使用 web-sockets 和 stomp 的 Spring Boot 应用程序,由于我们的 ISAM 设置的限制,我必须使用该xhr-polling协议,并且该应用程序将托管在Pivotal Cloud Foundry (PCF).

当我运行单个实例时,使用以下代码(如下)一切正常。

服务器

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig extends AbstractWebSocketMessageBrokerConfigurer {

    @Override
    public void configureMessageBroker(MessageBrokerRegistry registry) {
        registry.enableSimpleBroker("/dummy");
        registry.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry stompEndpointRegistry) {
        stompEndpointRegistry
            .addEndpoint("/dummyendpoint")
            .setAllowedOrigins("*")
            .withSockJS();
    }
}
Run Code Online (Sandbox Code Playgroud)

客户

var socket,client;
socket = new SockJS('http://localhost:8080/dummyendpoint');
client = Stomp.over(socket);

client.connect({}, function () {
  client.subscribe('/dummy/message', function (message) {
    console.log('subscribed');
  }
});
Run Code Online (Sandbox Code Playgroud)

但是,如果我扩展到 2 个实例,web-socket连接就会开始失败:

GET localhost:8080/dummyendpoint/info -> Status 200
POST …
Run Code Online (Sandbox Code Playgroud)

long-polling cloud-foundry spring-websocket spring-session

5
推荐指数
1
解决办法
2479
查看次数

Spring 会话启动错误 - 无法自动配置会话存储库,请检查您的配置(会话存储类型为“jdbc”)

我正在尝试创建一个简单的 Spring Boot Session 项目。这是 Spring Initializr 的基本设置。我收到以下错误:

2018-06-20 12:59:24.318  INFO 8108 --- [           main] c.j.s.SpringSessionExampleApplication    : Starting SpringSessionExampleApplication on pankaj with PID 8108 (/Users/pankaj/Documents/workspace-sts-3.9.4.RELEASE/Spring-Session-Example/target/classes started by pankaj in /Users/pankaj/Documents/workspace-sts-3.9.4.RELEASE/Spring-Session-Example)
2018-06-20 12:59:24.329  INFO 8108 --- [           main] c.j.s.SpringSessionExampleApplication    : No active profile set, falling back to default profiles: default
2018-06-20 12:59:24.372  INFO 8108 --- [           main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@39de3d36: startup date [Wed Jun 20 12:59:24 IST 2018]; root of context hierarchy
WARNING: An illegal reflective access operation has occurred …
Run Code Online (Sandbox Code Playgroud)

spring spring-boot spring-session

4
推荐指数
1
解决办法
5690
查看次数

`server.servlet.session.persistent` 的默认值

我从 Spring Boot 文档中看到server.servlet.session.persistentoption 的默认值是,false但在我的应用程序中这true很奇怪,因为我没有显式修改它。我猜 Spring Security、Spring Session 或 Spring Redis 集成会将其转换为true. 也许这里有人知道这种行为的根源?

PS我使用Spring Boot 2.1.5。

我的 pom XML。

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>ru.geekmarket</groupId>
    <artifactId>geek-market-ui</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>geek-market-ui</name>
    <description>Geek market e-commerce study project</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf.extras</groupId>
            <artifactId>thymeleaf-extras-springsecurity5</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId> …
Run Code Online (Sandbox Code Playgroud)

java spring tomcat spring-boot spring-session

4
推荐指数
1
解决办法
2598
查看次数