In windows, nginx + tomcat group + redis realize session sharing, nginxredis

Source: Internet
Author: User

In windows, nginx + tomcat group + redis realize session sharing, nginxredis

 

 

In this example

Nginx 1.9.13

Tomcat 7.0.64

Apsaradb hybriddb for redis 2.8

 

Procedure:

① Prepare two tomcat servers, modify the port, and modify the index. jsp content in tomcat/webapps/ROOT:

<% @ Page language = "java" contentType = "text/html; charset = UTF-8" pageEncoding = "UTF-8" %> <% String username = request. getParameter ("username"); System. out. println ("username:" + username); if (username! = Null) {session. setAttribute ("userSession", username); System. out. println ("value in session is:" + session. getAttribute ("userSession") ;}%> <! DOCTYPE html PUBLIC "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd"> 

Make sure that both tomcat servers can be started and accessed properly.

 

② Download ngix and modify the conf file as follows

# Users and groups used by Nginx # user niumd; # Number of working sub-processes (usually equal to the number of CPUs or twice the number of CPU) worker_processes 1; # error log storage path # error_log logs/error. log; # error_log logs/error. log notice; error_log logs/error. log info; # specify the pid to store the file pid logs/nginx. pid; events {# epoll is recommended for linux using the network I/O model. For FreeBSD, kqueue # use epoll; # worker_connections 1024;} http {include mime. types; default_type application/octet-stream; # define log format # log_format main '$ remote_addr-$ remote_user [$ time_local] $ request' #' "$ status" $ body_bytes_sent "$ http_referer" '#' "$ http_user_agent "" $ http_x_forwarded_for "'; # access_log off; access_log logs/access. log; client_header_timeout 3 m; latency 3 m; send_timeout 3 m; latency 1 k; latency 4 4 4 k; sendfile on; tcp_nopush on; tcp_nodelay on; # keepalive_timeout 75 20; include gzip. conf; upstream localhost {# ip_hash; # do not enable ip_hash during testing. Otherwise, it will not jump between different servers, which server will be accessed for the first visit? # The configured server list is shown below. weight indicates the weight. The larger the value, the more requests are allocated. The default value is 1. server localhost: 8898 weight = 1; server localhost: 8899 weight = 1;} server {listen 8888; #8888 is the listening port, so ip address is used for access: 8888 to access server_name localhost; location/{proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http: // localhost; # The name here must be the same as the name following upstream above }}}View Code

 

③ Start nginx In the cmd window and stop using the nginx-s stop command

 

Download redisand start server.exe in the slave. You can modify port 6379 in redis. conf as needed. The default port is 6379. Stop using redis-cli shutdown.

 

⑤ Create a maven project and select jdk of the same version as the local java_home, add the https://github.com/jcoleman/tomcat-redis-session-manager code to the dependency and Package

 

⑥ Modify context. xml in tomcat/conf

<? 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 License t in compliance with the License. you may obtain a copy of the License 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 --> <WatchedResource> WEB-INF/web. xml </WatchedResource> <! -- Uncomment this to disable session persistence resume SS 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 "/> --> <Valve className =" com. orangefunction. tomcat. redissessions. redisSessionHandlerValve "/> <Manager className =" com. orangefunction. tomcat. redissessions. redisSessionManager "host =" localhost "port =" 6379 "database =" 0 "maxInactiveInterval =" 60 "/> </Context>View Code

 

7. Add the jvmRoute flag to the engine information in tomcat/conf/server. xml.

<Engine name="Catalina" defaultHost="localhost" jvmRoute="jvm8899">

 

Copy the jar package prepared in step 5 and the dependent jedis-2.7.2.jar commons-pool2-2.4.1.jar to the lib directory of each tomcat

 

⑨ Start redis first, then start all tomcat, and then start nginx to input the nginx listening port for access. The results are as follows:

When no session information is entered at the beginning, multiple refreshes will show that two different tomcat servers are accessed.

 

 

Enter and save the information in the input box, and refresh it continuously.

 

 

 

During the execution process, you will also see the redis console continuously providing relevant information.

 

In the future, I will put all the instance code and configuration files on my online storage. You are welcome to download and verify and communicate with each other.

: Http://pan.baidu.com/s/1pK7GIbp

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.