主頁(yè) > 知識(shí)庫(kù) > Nginx Session共享問(wèn)題解決方案解析

Nginx Session共享問(wèn)題解決方案解析

熱門(mén)標(biāo)簽:四川保險(xiǎn)智能外呼系統(tǒng) 高德地圖標(biāo)注公司需要錢(qián) 廈門(mén)防封電銷(xiāo)電話卡 外呼系統(tǒng)全國(guó) 云南電商智能外呼系統(tǒng)哪家好 地圖標(biāo)注員有發(fā)展前景嗎 宜賓銷(xiāo)售外呼系統(tǒng)軟件 地圖標(biāo)注能更改嗎 濰坊寒亭400電話辦理多少錢(qián)

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

Nginx解決Session共享問(wèn)題:

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

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

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

  4.Tomcat配置session共享;

  5利用session集群存放Redis;

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

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

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)前請(qǐ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、沒(méi)有Nginx的訪問(wèn)效果展示

分別訪問(wèn)8080和8081

    

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.再次訪問(wèn)

   

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

1:導(dǎo)入依賴(lài)

<!--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)境配置,需要開(kāi)啟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測(cè)試

@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:?jiǎn)?dòng)項(xiàng)目測(cè)試

  

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

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

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

標(biāo)簽:德州 回訪 巴彥淖爾 滁州 湛江 廊坊 廣安 紅河

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Nginx Session共享問(wèn)題解決方案解析》,本文關(guān)鍵詞  Nginx,Session,共享,問(wèn)題,解決方案,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《Nginx Session共享問(wèn)題解決方案解析》相關(guān)的同類(lèi)信息!
  • 本頁(yè)收集關(guān)于Nginx Session共享問(wèn)題解決方案解析的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章