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