利用Nginx进行分布式部署,并使用spring-session+redis实现session共享

利用Nginx进行应用的集群部署或分布式部署时,经常遇到session共享的问题,要么在nginx代理解决(比如ip hash),要么在tomcat的context.xml配置redis,用redis接管系统session,达到共享session的目的。spring boot也提供了session用redis解决共享的方法。

一、引入依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <!--用于连接redis-->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

    <!--用于使用redis接管session-->
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session-data-redis</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

    <!--工具类,非必需-->
    <dependency>
        <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>4.1.0</version>
    </dependency>
</dependencies>

二、配置application.properties

server.port=8080
spring.redis.host=localhost
spring.redis.port=6379

# spring session使用存储类型
spring.session.store-type=redis

三、在启动类中加入@EnableRedisHttpSession 注解

package com.chaofan.sessionshare;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.session.data.redis.config.annotation.web.http.EnableRedisHttpSession;

@EnableRedisHttpSession
@SpringBootApplication
public class SessionShareApplication {
    public static void main(String[] args) {
        SpringApplication.run(SessionShareApplication.class, args);
    }
}

四、编写Controller

package com.chaofan.sessionshare.web;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


/**
 * Created by Chaofan at 2018/6/29 10:48
 * email:chaofan2685@qq.com
 **/

@RestController
@RequestMapping(value = "/")
public class IndexController {

    @Value("${server.port}")
    private String port;
    private String username = "chaofan";
    private String password = "123456";
    private String nickname = "炒饭";


    @RequestMapping(value = "/login")
    public Map<String, Object> getSession(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String user = request.getParameter("username");
        String pwd = request.getParameter("password");
        if (username.equals(user) && password.equals(pwd)){
            request.getSession().setAttribute("user", "当前登录用户是:"+nickname);
            map.put("msg", "登录成功");
            map.put("port",port);
        }else {
            map.put("msg", "账号或密码错误");
        }
        return map;
    }

    @RequestMapping(value = "/get")
    public Map<String, Object> get(HttpServletRequest request) {
        Map<String, Object> map = new HashMap<>();
        String user = (String) request.getSession().getAttribute("user");
        map.put("msg",StrUtil.isEmpty(user)?"用户未登录":user);
        map.put("port",port);
        return map;
    }

}

五、配置Nginx并启动

#user  nobody;
worker_processes  1;

#error_log  logs/error.log;
#error_log  logs/error.log  notice;
#error_log  logs/error.log  info;
#pid        logs/nginx.pid;

events {
    worker_connections  1024;
}

http {
    include       mime.types;
    default_type  application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;

    upstream web-server { 
        server localhost:8080;  
        server localhost:18011;
    }

    server {
        listen       80;
        server_name  localhost;

        location / {
            proxy_pass http://web-server;
        }       
    }
}

六、打包,启动

application.properties中的server.port分别改为8080和18011并分别打包,再用java -jar命令启动。

七、测试(工具:postman


登录
这里写图片描述
这里写图片描述

八、源码

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值