Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Implementing session server with Nginx+Tomcat+memcached

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

Write at the front

The previous article explained the type of conversation, today's article will lead you step by step to achieve a simple session server, mainly to let you understand the working process of session server, of course, for small and medium-sized sites, this structure is also enough. The main structure of this section is:

Memcached-session-manager introduction

Network topology

Nginx reverse proxy configuration

Tomcat configuration

Memcached configuration

test

Error analysis

Memcached-session-manager introduction

Memcached-session-manager is a tomcat session manager that saves sessions in memcached or Redis for use in highly available, scalable, and fault-tolerant Web service scenarios. It supports both sticky and non-viscous configurations and is currently using tomcat 6.x journal 7.x and 8.x. Session failover (tomcat crash) is supported for sticky sessions, which is the default for non-sticky sessions (sessions are provided by different tomcats by default for different requests). In addition, memcached failover (memcached crash) is achieved through session migration. There should also be no single point of failure, so when memcached fails, the session is not lost and can be used in tomcat or other memcached.

In configuration, you must put spymemcached jar and memcached-session-manager jar in the lib directory corresponding to tomcat. If you want to use Redis instead of memcached, you need to use the .jar package corresponding to the Redis client "jedis". You also need to add some configuration properties to the Manager class so that you can store the session in memcached. Memcached itself does not have configuration functionality and must rely on the front-end Tomcat or other class servers.

For a simple implementation, you only need to install a tomcat (6, 7 or 8) and a memcached or Redis (or s.th.) that supports the memcached protocol. In your production environment, you may have multiple tomcats, and there should be multiple memcached nodes available on different hardware. Alternatively, you can store session data in Redis instead of memcached. You can use sticky sessions or non-sticky sessions, and memcached-session-manager (msm) supports two modes of operation.

Memcached-session-manager project address, http://code.google.com/p/memcached-session-manager/

Network topology

This is mainly used for a Nginx to implement front-end scheduling, then connect two Tomcat application servers, and add two memcached storage to the back end of the tomcat to implement a small architecture. Of course, the nearest DNS server to the client, a load balancer cluster close to users, and the cache server cluster after load balancing are omitted. Its simple architecture and network address are planned as follows:

All hosts in this article are centos7.3 versions

Nginx reverse proxy configuration

For nginx, this experiment only plays the role of scheduling, that is, the role of reverse proxy, and does not achieve static and dynamic separation, path rewriting and other operations, here is mainly to schedule client requests to the back-end application server Tomcat. So it's much easier to configure, just define a upstream group. Then schedule it according to the corresponding algorithm. The collective configuration is as follows:

[root@vin ~] # vim / etc/nginx/nginx.conf...http {upstream vinsent {# defines server cluster server 192.168.14.66 server 8080; server 192.168.14.77 server 8080;} server {listen 80; listen [:]: 80 server _ Root / usr/share/nginx/html; # Load configuration files for the default server block. Include / etc/nginx/default.d/*.conf; location / {proxy_pass # proxies the request to the backend Tomcat server}.}

Tomcat configuration

Tomcat as an application server, the main role is to deal with jsp files, here, we need to provide a file for testing index.jsp and the corresponding version of the .jar package. Mainly memcached-session-manager-related jar packages, and tools for serializing the cookie information of front-end users into a "key-value" format

1) install tomcat and related service packs

[root@vin tools] # cat / etc/redhat-release # version Detection CentOS Linux release 7.3.1611 (Core) [root@vin tools] # iptables-F # turn off the firewall and selinux [root@vin tools] # setenforce 0 [root@vin tools] # yum install java-1.7.0-openjdk-devel.x86_64 # install JDK [root @ vin tools] # yum installtomcat tomcat-lib tomcat-admin-webapps tomcat-webapps tomcat-docs-webapp

2) provide context container

Define a context container for testing on a host on two tomcat, and create a session manager in it.

[root@vin webapps] # vim / etc/tomcat/server.xml......

3) provide test pages for two context respectively

The structure of the test directory is as follows

[root@vin webapps] # cat/ usr/share/tomcat/webapps/test/index.jsp TomcatA TomcatA.vinsent.cn Session ID Created on # description: in order to see this process You should change it to TomcatB.vinsent.cn on the TomCatB host.

4) download the jar package

The jar packages here are mainly divided into two categories, one is memcached-session-manager-related packages, and the other is serialization-related packages: there are four main categories of serialization packages:

Kryo-serializer: msm-kryo-serializer, kryo-serializers-0.34+, kryo-3.x, minlog, reflectasm, asm-5.x, objenesis-2.x

Javolution-serializer: msm-javolution-serializer, javolution-5.4.3.1

Xstream-serializer: msm-xstream-serializer, xstream, xmlpull, xpp3_min

Flexjson-serializer: msm-flexjson-serializer, flexjson

Download address: https://github.com/magro/memcached-session-manager/wiki/SetupAndConfiguration

Here we choose to put them separately, of course, you can also put them all in the lib directory:

[root@vin webapps] # tree / usr/share/tomcat/webapps/test//usr/share/tomcat/webapps/test/ ├── index.jsp └── WEB-INF └── lib ├── javolution-5.4.3.1.jar └── msm-javolution-serializer-2.1.1.jar2 directories 3 files [root@vin webapps] # ls / usr/share/tomcat/lib/annotations-api.jar extras tomcat7-websocket.jar tomcat-jsp-2.2-api.jarcatalina-ant.jar jasper-el.jar tomcat-api.jar tomcat-juli.jarcatalina-ha.jar jasper.jar Tomcat-coyote.jar tomcat-servlet-3.0-api.jarcatalina.jar jasper-jdt.jar tomcat-el-2.2-api.jar tomcat-util.jarcatalina-tribes.jar log4j.jar tomcat-i18n-es.jar websocket-api.jarcommons-collections.jar memcached-session-manager-2.1.1. Jar tomcat-i18n-fr.jarcommons-dbcp.jar memcached-session-manager-tc7-2.1.1.jar tomcat-i18n-ja.jarcommons-pool.jar spymemcached-2.12.3.jar tomcat-jdbc.jar

Memcached configuration

The configuration of the memcached server is actually very simple, because memcached itself can not actively store cookie information, you only need to install the memcached service on the memcached server, and the serialization of the data is realized by the previous server.

[root@vin ~] # yum install memcached # install memcached I have installed Loaded plugins: fastestmirrorLoading mirror speeds from cached hostfilePackage memcached-1.4.15-10.el7.x86_64 already installed and latest versionNothing to do [root@vin ~] # systemctl start memcached # Startup Service [root@vin ~] # ss-tnl | grep 1121 # check whether the service starts normally LISTEN 0128 *: 11211 *: * LISTEN 0 128:: 11211: *

test

We test it on the client side. Enter the address of nginx in the browser: refresh and you will see that the content of the visit has changed, but the cookie value has not changed.

Error analysis

1 Serialization failed

The common phenomenon is as follows, the cookie value will change with each visit, indicating that it is stored in the memcached database.

2 prompt 404 error page when accessing

In most cases, the configuration of context is correct, or the directory structure of the provided index.jsp file is incorrect.

Version 3 does not correspond

If you can rule out the above errors and still cannot access it, the version of the jar package you provided is not consistent with your tomcat version.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report