NginxSession共享問題解決方案解析

這篇文章主要介紹了Nginx Session共享問題解決方案解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

成都創(chuàng)新互聯(lián)的客戶來自各行各業(yè),為了共同目標(biāo),我們在工作上密切配合,從創(chuàng)業(yè)型小企業(yè)到企事業(yè)單位,感謝他們對我們的要求,感謝他們從不同領(lǐng)域給我們帶來的挑戰(zhàn),讓我們激情的團(tuán)隊(duì)有機(jī)會(huì)用頭腦與智慧不斷的給客戶帶來驚喜。專業(yè)領(lǐng)域包括網(wǎng)站制作、成都做網(wǎng)站、電商網(wǎng)站開發(fā)、微信營銷、系統(tǒng)平臺(tái)開發(fā)。

Nginx解決Session共享問題:

1.nginx或者h(yuǎn)aproxy做的負(fù)載均衡,用nginx做的負(fù)載均衡可以添加ip_hash這個(gè)配置;用haproxy做的負(fù)載均衡可以用balance source這個(gè)配置,從而使用一個(gè)IP的請求發(fā)到同一個(gè)服務(wù)器;

2.利用數(shù)據(jù)庫同步session;

3.利用cookie同步session數(shù)據(jù),但是安全性差,http請求都需要帶參增加了帶寬消耗;

4.Tomcat配置session共享;

5利用session集群存放redis;

1:創(chuàng)建一個(gè)工程,啟動(dòng)兩個(gè)Tomcat

Nginx Session共享問題解決方案解析Nginx Session共享問題解決方案解析

2:編寫一個(gè)servlet測試

package com.zn.servlet;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/nginxSessionServlet")
public class SessionIPServlet extends HttpServlet {
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    System.out.println("當(dāng)前請求端口:"+request.getLocalPort());
    String action=request.getParameter("action");
    //向Session中存放一個(gè)數(shù)據(jù)
    if(action.equals("setSession")){
      request.getSession().setAttribute("username","zhangsan");
    }else if(action.equals("getSession")){
      response.getWriter().write((String)request.getSession().getAttribute("username"));
    }
  }

  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
  }
}

3、沒有Nginx的訪問效果展示

分別訪問8080和8081

Nginx Session共享問題解決方案解析 Nginx Session共享問題解決方案解析 Nginx Session共享問題解決方案解析

4.配置nginx.conf文件

upstream myserver{
     ip_hash;
     server 127.0.0.1:8080;
     server 127.0.0.1:8081;
  }
  server{
    listen    81;
    server_name www.bproject.com;
    location / {
      root  html;
      proxy_pass http://myserver;
      index index.html index.htm;
    }
  }

5.再次訪問

Nginx Session共享問題解決方案解析Nginx Session共享問題解決方案解析 Nginx Session共享問題解決方案解析

方法二、利用spring-session+Redis實(shí)現(xiàn)session共享

1:導(dǎo)入依賴

<!--spring boot 與redis應(yīng)用基本環(huán)境配置 -->
    <!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-redis -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-redis</artifactId>
    </dependency>

    <!--spring session 與redis應(yīng)用基本環(huán)境配置,需要開啟redis后才可以使用,不然啟動(dòng)Spring boot會(huì)報(bào)錯(cuò) -->
    <dependency>
      <groupId>org.springframework.session</groupId>
      <artifactId>spring-session-data-redis</artifactId>
    </dependency>

2:創(chuàng)建controller測試

@RestController
public class SessionController {

  @RequestMapping("/setSession")
  public String setSession(HttpServletResponse response, HttpServletRequest request) throws IOException {
    request.getSession().setAttribute("username","wang");
    return "success";
  }

  @RequestMapping("/getSession")
  public String getSession(HttpServletRequest request,HttpServletResponse response){
    String username = (String) request.getSession().getAttribute("username");
    return username;
  }
}

3:application.properties文件

server.port=8082
#server.port=8083

#redis配置
spring.redis.password: wang2003

4:啟動(dòng)項(xiàng)目測試

Nginx Session共享問題解決方案解析 Nginx Session共享問題解決方案解析Nginx Session共享問題解決方案解析

結(jié)論:該方案配置簡單,數(shù)據(jù)安全且穩(wěn)定,效率高,被普遍使用;

注意:在Redis中刪除這個(gè)數(shù)據(jù)包,8082和8083端口都get不到session了,說明了session沒有存在在JVM中,而是轉(zhuǎn)存在Redis中;

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持創(chuàng)新互聯(lián)。

網(wǎng)站名稱:NginxSession共享問題解決方案解析
URL地址:http://www.muchs.cn/article48/gcejhp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)站收錄、搜索引擎優(yōu)化、面包屑導(dǎo)航、關(guān)鍵詞優(yōu)化網(wǎng)站內(nèi)鏈

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

商城網(wǎng)站建設(shè)