jacky4955 发表于 2018-11-9 10:09:55

nginx+tomcat8+memcached实现session共享

  安装nginx-1.13.8
  安装jdk1.8.0_144
  安装tomcat-8.5.24
  安装memcached-1.4.15
  下载tomcat8连接memcached所需jar包
  官方参考资料:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#decide-which-serialization-strategy-to-use
  配置nginx
  以下是我的主配置文件,请根据自己情况进行修改
  worker_processes1;
  events {
  worker_connections1024;
  }
  http {
  include       mime.types;
  default_typeapplication/octet-stream;
  sendfile      on;
  keepalive_timeout65;
  upstream tomcat_nginx {
  server 192.168.3.58:8080;
  server 192.168.3.54:8080;
  server 192.168.3.31:8080;
  }
  server {
  listen       80;
  server_namelocalhost;
  location / {
  proxy_pass http://tomcat_nginx;
  }
  error_page   500 502 503 504/50x.html;
  location = /50x.html {
  root   html;
  }
  }
  }
  配置nginx的启动脚本
  #! /bin/bash
  # Description: Startup script for webserver on CentOS. cp it in /etc/init.d and
  # chkconfig --add nginx && chkconfig nginx on
  # then you can use server command control nginx
  #
  # chkconfig: 2345 08 99
  # description: Starts, stops nginx
  set -e
  PATH=$PATH:/usr/local/nginx/sbin/
  DESC="nginx daemon"
  NAME=nginx
  DAEMON=/usr/local/nginx/sbin/$NAME
  CONFIGFILE=/usr/local/nginx/conf/nginx.conf
  #PIDFILE=/var/run/nginx.pid
  PIDFILE=/usr/local/nginx/logs/nginx.pid
  SCRIPTNAME=/etc/init.d/$NAME
  # Gracefully exit if the package has been removed.
  test -x $DAEMON || exit 0
  d_start() {
  $DAEMON -c $CONFIGFILE || echo -n " already running"
  }
  d_stop() {
  kill -QUIT `cat $PIDFILE` || echo -n " not running"
  }
  d_reload() {

  kill -HUP `cat $PIDFILE` || echo -n " can't>  }
  case "$1" in
  start)
  echo -n "Starting $DESC: $NAME"
  d_start
  echo "."
  ;;
  stop)
  echo -n "Stopping $DESC: $NAME"
  d_stop
  echo "."
  ;;
  reload)
  echo -n "Reloading $DESC configuration..."
  d_reload
  echo "reloaded."
  ;;
  restart)
  echo -n "Restarting $DESC: $NAME"
  d_stop
  sleep 1
  d_start
  echo "."
  ;;
  *)
  echo "Usage: $SCRIPTNAME {start|stop|restart|force-reload}" >&2
  exit 3
  ;;
  esac
  exit 0
  启动nginx
  编写tomcat的测试页面,一下是两种格式的,都可以用(放在webapps/ROOT/)
  
  
  
  
  192.168.3.31
  port:8080
  this is tomcat 1!
  
  
  
  SessionID:
  SessionIP:
  SessionPort:
  
  启动tomcat
  tomcat访问测试

  nginx访问测试

  tomcat8+memcached实现会话共享
  下载jar包
  不同版本的tomcat使用的jar包可能会有区别,本测试使用的是tomcat-8.5.24
  jar包详细列表及tomcat(注:jedis-2.9.0.jar是连接redis的jar包)

  修改conf/context.xml(每个tomcat)
  这里是 non-sticky sessions + kryo,建议参考官方文档:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration
  
  WEB-INF/web.xml
  ${catalina.base}/conf/web.xml

    memcachedNodes="n1:192.168.3.31:11211"
  sticky="false"
  sessionBackupAsync="false"
  lockingMode="none"
  requestUriIgnorePattern=".*\.(ico|png|gif|jpg|jpeg|css|js)$"
  sessionBackupTimeout="1000"
  transcoderFactoryClass="de.javakaffee.web.msm.serializer.kryo.KryoTranscoderFactory"
  />
  
  memcachedNodes是指定memcached,可以有多个节点,n1是标签,其他的靠猜
  启动memcached,重启tomcat
  访问测试

  使用redis代替memcached
  上面jar包里面jedis-2.9.0.jar就是连接redis的jar包,详情看官方的解释
  测试失败(⊙﹏⊙)b
  
  一堆版本不兼容的问题,尝试所有的jar包都用最新的结果不行
  报错

  缺少jar包或版本冲突
  参考:https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration#decide-which-serialization-strategy-to-use
  https://wenku.baidu.com/view/caecc5cf6037ee06eff9aef8941ea76e58fa4abd.html

页: [1]
查看完整版本: nginx+tomcat8+memcached实现session共享