1. 程式人生 > >用nginx+tomcat+memcached方式實現session共享

用nginx+tomcat+memcached方式實現session共享

參考資料:https://www.cnblogs.com/kevingrace/p/6398672.html 架構完成步驟 1.搭建基礎開發環境 2.安裝tomcat 3.安裝nginx 4.啟動tomcat,配置nginx輪詢,實現nginx反向代理的效果 5.安裝memcached,測試 前臺程式碼:

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
    <base href="<%=basePath%>">

    <title>My JSP 'index.jsp' starting page</title>
    <meta http-equiv="pragma" content="no-cache">
    <meta http-equiv="cache-control" content="no-cache">
    <meta http-equiv="expires" content="0">
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="This is my page">
    <!--
    <link rel="stylesheet" type="text/css" href="styles.css">
    -->
</head>

<body>

SessionID:<%=session.getId()%>
<BR>
SessionIP:<%=request.getServerName()%>
<BR>
SessionPort:<%=request.getServerPort()%>
<%
    out.println("This is Tomcat Server 11111");
%>
</body>

tomcat context.xml配置程式碼:

<?xml version='1.0' encoding='utf-8'?>
<!--
  Licensed to the Apache Software Foundation (ASF) under one or more
  contributor license agreements.  See the NOTICE file distributed with
  this work for additional information regarding copyright ownership.
  The ASF licenses this file to You under the Apache License, Version 2.0
  (the "License"); you may not use this file except in compliance with
  the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
-->
<!-- The contents of this file will be loaded for each web application -->
<Context>

    <!-- Default set of monitored resources. If one of these changes, the    -->
    <!-- web application will be reloaded.                                   -->
    <WatchedResource>WEB-INF/web.xml</WatchedResource>
    <WatchedResource>${catalina.base}/conf/web.xml</WatchedResource>

    <!-- Uncomment this to disable session persistence across Tomcat restarts -->
    <!--
    <Manager pathname="" />
    -->

    <!-- Uncomment this to enable Comet connection tacking (provides events
         on session expiration as well as webapp lifecycle) -->
    <!--
    <Valve className="org.apache.catalina.valves.CometConnectionManagerValve" />
    -->
    <!--memcached配置開始-->
	<Manager className="de.javakaffee.web.msm.MemcachedBackupSessionManager"
			memcachedNodes="n1:47.98.139.5:11211"
			sticky="false"
			sessionBackupAsync="false"
			lockingMode="uriPattern:/path1|/path2"
			requestUriIgnorePattern=".*\.(ico|png|gif|jpg|css|js)$"
			transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
    />
    <!--memcached配置結束-->
</Context>

注意點: 啟動memcached時 -l 引數說明網上文件寫的很不明確,此引數其實是配置訪問此memcached快取資料庫的地址,例如192.168.0.0訪問192.0.0.1服務上的memcached,此處應該寫192.168.0.0,預設是允許所有ip訪問,此處如果錯誤會導致tomcat連線不上memcached。