Maison> base de données> Redis> le corps du texte

Comment utiliser le partage de session Redis avec nginx+tomcat

PHPz
Libérer: 2023-05-30 18:40:06
avant
1563 Les gens l'ont consulté

Préparation de l'environnement

1. Préparez un serveur nginx ip192.168.1.133 port 81

Processus d'installation :

#首先安装依赖: yum -y install gcc-c++ yum -y install pcre pcre-devel yum -y install zlib zlib-devel yum -y install openssl openssl—devel #注意 : 安装nginx必须使用 root 用户安装 #创建一个nginx目录 mkdir /usr/local/src/nginx #进入到nginx目录 cd /usr/local/src/nginx #下载或上传安装包 wget http://nginx.org/download/nginx.tar.gz 或 rz上传 #解压安装包 tar -xvf nginx.tar.gz #进入到解压后的目录 cd nginx # 下面 才开始正式安装 #把nginx安装到指定用户的目录 mkdir -p /ucenter/soft/nginx #安装配置 prefix为安装目录 user为用户 group为 组 ./configure --prefix=/ucenter/soft/nginx --user=ucenter --group=ucenter #编译 make #安装 make install #在linux系统中由于非root用户不能占用80端口,所以需要使普通用户以root身份启动nginx。 cd /ucenter/soft/nginx/sbin #把soft文件下所有的文件所属者修改为ucener -r 表示递归 chown ucenter:ucenter ./soft/ -r #修改 ./nginx 的所属为root chown root nginx #让普通用户可以使用80端口,可以使用root权限启用nginx chmod u+s nginx #修改配置文件 在修改配置文件之前 ,要备份该文件 cd conf/ # 要注意nginx 的工作进程,一般根据cpu的核数去修改 vim nginx.conf #关闭防火墙,打开80端口 service iptables stop #启动nginx ./nginx #重启nginx ./nginx -s reload #关闭nginx ./nginx -s stop
Copier après la connexion

Préparez un serveur Tomcat, préparez d'abord l'environnement Java et installez l'étape jdk est omise

Ensuite, installez respectivement 3 adresses IP de serveur Tomcat : 192.168.1.143, port tomcat1 8080, port tomcat2 8081 et port tomcat3 8082.

nginx+tomcat怎么使用redis session共享

fichier de configuration apache-tomcat-7.0.64/conf/server.xml Modifiez ces trois endroits pour que les ports n'entrent pas en conflit

  
Copier après la connexion

Modifiez index.jsp dans le répertoire racine de Tomcat et ajoutez chaque logo Tomcat séparément , et affichez l'identifiant de session sur la page

<%-- 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. --%>  <%@ page session="true" %> <% java.text.simpledateformat sdf = new java.text.simpledateformat("yyyy"); request.setattribute("year", sdf.format(new java.util.date())); request.setattribute("tomcaturl", "http://tomcat.apache.org/"); request.setattribute("tomcatdocurl", "/docs/"); request.setattribute("tomcatexamplesurl", "/examples/"); %>   <%=request.getservletcontext().getserverinfo() %>    

${pagecontext.servletcontext.serverinfo}--8080

if you're seeing this, you've successfully installed tomcat. congratulations!


managing tomcat

for security, access to the manager webapp is restricted. users are defined in:

$catalina_home/conf/tomcat-users.xml

in tomcat 7.0 access to the manager application is split between different users.read more...


release notes

changelog

migration guide

security notices

documentation

tomcat 7.0 documentation

tomcat 7.0 configuration

tomcat wiki

find additional important configuration information in:

$catalina_home/running.txt

developers may be interested in:

getting help

faqandmailing lists

the following mailing lists are available:


copyright ©1999-${year} apache software foundation. all rights reserved

Copier après la connexion

nginx+tomcat怎么使用redis session共享

A ce moment, modifiez le fichier de configuration nginx nginx.conf, ajoutez les adresses IP et les numéros de port des trois tomcats, et utilisez nginx comme proxy

#user nobody; worker_processes 1; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 1024; } http { include mime.types; default_type application/octet-stream; #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 logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; upstream localhost1 { #ip_hash; server 192.168.1.143:8080; server 192.168.1.143:8081; server 192.168.1.143:8082; } server { listen 81; server_name localhost; #charset koi8-r; #access_log logs/host.access.log main; location / { proxy_connect_timeout 3; proxy_send_timeout 30; proxy_read_timeout 30; proxy_pass http://localhost1; } #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the php scripts to apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the php scripts to fastcgi server listening on 127.0.0.1:9000 # #location ~ \.php$ { # root html; # fastcgi_pass 127.0.0.1:9000; # fastcgi_index index.php; # fastcgi_param script_filename /scripts$fastcgi_script_name; # include fastcgi_params; #} # deny access to .htaccess files, if apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } # another virtual host using mix of ip-, name-, and port-based configuration # #server { # listen 8000; # listen somename:8080; # server_name somename alias another.alias; # location / { # root html; # index index.html index.htm; # } #} # https server # #server { # listen 443; # server_name localhost; # ssl on; # ssl_certificate cert.pem; # ssl_certificate_key cert.key; # ssl_session_timeout 5m; # ssl_protocols sslv2 sslv3 tlsv1; # ssl_ciphers high:!anull:!md5; # ssl_prefer_server_ciphers on; # location / { # root html; # index index.html index.htm; # } #} }
Copier après la connexion

À ce moment, démarrez respectivement trois tomcats et nginx, visitez, chaque fois que vous actualisez la page, vous accéderez aléatoirement à 8080 ou 8081 ou 8082, et l'identifiant de session qui apparaît sur la page est également différent, comment devrions-nous faire ces trois Tomcats share Quant à la session, nous utilisons Redis.

À ce stade, installez redis sur le serveur 192.168.1.143 qui possède trois tomcats. Les étapes d'installation sont les suivantes :

$ wget http://download.redis.io/releases/redis-3.2.3.tar.gz $ tar xzf redis-3.2.3.tar.gz $ cd redis-3.2.3 $ make malloc=libc #启动redis src前面是安装的路径 $ src/redis-server & #关闭redis src/redis-cli shutdown #使用redis 放入键值对 key value $ src/redis-cli 127.0.0.1:6379> set foo bar ok 127.0.0.1:6379> get foo "bar" $
Copier après la connexion

Après avoir installé redis, téléchargez les cinq dépendances dans les trois dossiers lib tomcat. commons-pool2-2.0.jar

jedis-2.5.2.jar

tomcat-redis-session-manager1.2.jar

tomcat-juli.jar

tomcat-juli-adapters .jar

Tous les pots sont ici,

puis réparez trois fichiers tomcat context.xml et ajoutez la configuration suivante

   web-inf/web.xml    
Copier après la connexion

À ce moment, redémarrez les trois tomcats et nginx respectivement. Après avoir vérifié le journal tomcat, j'ai trouvé qu'il n'y avait aucune exception signalée. , cela signifie que nous avons réussi et nous commencerons ensuite les tests.

Nous accédons à l'adresse du serveur nginx :

Ce que nous obtenons est tomcat1 du port 8080, l'identifiant de session est 1a0625767f27ba95ef4d5f061fe0568d


nginx+tomcat怎么使用redis session共享À ce moment, appuyez sur f5 pour actualiser la page, et nous obtenons tomcat2 de port 8081, s L'identifiant de session est toujours 1a0625767f27ba95ef4d5f061fe0568d


nginx+tomcat怎么使用redis session共享Actualisez à nouveau la page et obtenez Tomcat3 du port 8082, et l'identifiant de session est toujours 1a0625767f27ba95ef4d5f061fe0568d. ,

nginx+tomcat怎么使用redis session共享Pour le moment, cela signifie que nous avons construit avec succès Tomcat + équilibrage de charge nginx + synchronisation de session Redis !

nginx aide à répartir nos requêtes uniformément sur trois tomcats --》tomcat1, tomcat2 et tomcat3

redis nous aide à synchroniser les sessions De cette façon, les performances de notre serveur seront considérablement améliorées, si un tomcat échoue Enfin, il n'y aura pas. impact sur le service global.

Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!

Étiquettes associées:
source:yisu.com
Déclaration de ce site Web
Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn
Derniers téléchargements
Plus>
effets Web
Code source du site Web
Matériel du site Web
Modèle frontal
À propos de nous Clause de non-responsabilité Sitemap
Site Web PHP chinois:Formation PHP en ligne sur le bien-être public,Aidez les apprenants PHP à grandir rapidement!