springboot+websocket集群session共享

1.安装redis(省略)

外网访问权限更改redis.conf 文件

bind 127.0.0.1

protected-mode yes

更改为

# bind 127.0.0.1

protected-mode no

启动redis命令:    ./redis-server ../redis.conf

2.安装nginx(省略)

更改nginx.conf 文件

    upstream springboots{
            ip_hash;	#同一个ip负载到同一个服务器	
	    server 192.168.1.204:8001;
	    server 192.168.1.204:8002;
    }
    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://springboots;             # 下面处理websocket            # Origin不能少,否则会爆403
	    proxy_http_version 1.1;
            proxy_set_header Upgrade $http_upgrade;
    	    proxy_set_header Connection "upgrade";
	    proxy_set_header Origin "";
        }

3.stringboot 代码

Maven配置

        <!-- springboot - Redis -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>
        <!--spring session 与redis应用基本环境配置,需要开启redis后才可以使用,不然启动Spring boot会报错 -->
        <dependency>
            <groupId>org.springframework.session</groupId>
            <artifactId>spring-session-data-redis</artifactId>
        </dependency>

Application.properties

application.properties 
特别注意此处需要spring.session.store-type

########################  Redis ###################################
spring.redis.database=0
spring.redis.host=127.0.0.1
spring.redis.port=6379
spring.redis.password=
# 连接池最大连接数
spring.redis.pool.max-active=8
spring.redis.pool.max-wait=-1
# 连接池中的最大空闲连接
spring.redis.pool.max-idle=8
# 连接池中的最小空闲连接
spring.redis.pool.min-idle=0
# 连接超时时间(毫秒)
spring.redis.timeout=0

spring.session.store-type=redis

Config

/**
 * @description 分布式Session,使用spring.session.store-type=redis自动配置
 */
@EnableRedisHttpSession(maxInactiveIntervalInSeconds= 1800)
public class SessionConfig {

}
  • 1
  • 2








  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值