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

How to use Nginx+Tomcat+Redis to achieve persistent sessions

2025-03-29 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

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

This article will explain in detail how to use Nginx+Tomcat+Redis to achieve persistent conversations. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Use the open source web application solo blog to demonstrate the project. The front end uses Nginx as the load balancer, and the back end Tomcat connects to Redis to realize session storage. The characteristic of Redis is that session can be persisted. Only in this way can we really achieve multiple Tomcat load balancing in the background, and user requests can be sent to any tomcat host.

Keeplived, zabbix and ansible are not within the scope of this article for the time being and will be explained one by one in later articles.

Experimental architecture diagram

Deployment environment

CentOS7

Nginx1.10.2

Tomcat7.0

Redis3.2.3

Mariadb 5.5.44

Required software package

Commons-pool2-2.2.jar

Jedis-2.5.2.jar

Tomcat-redis-session-manager-2.0.0.jar

Download address https://github.com/izerui/tomcat-redis-session-managercommons-pool2-2.2.jar

Solo blog

Download address http://b3log.org/

I. Nginx server configuration

Pre-installation preparation

Ntpdate 172.18.0.1

Iptables-F

Install Nginx

Yum install nginx

Modify the configuration file

Upstream tomcatservers: create a backend server group to append inside the http

Location: match URL, append inside server

Note: ellipsis is the other default parameter, if there is no special need, you can press the default.

Vim / etc/nginx/nginx.confhttp {... upstream tomcatservers {server 172.18.68.21 upstream tomcatservers 8080; server 172.18.68.22 upstream tomcatservers 8080;}. Server {. Location / {proxy_pass http://tomcatservers;}...}} systemctl start nginx

Check Port

Start the server to check whether port 80 is listening properly.

Ss-ntl

II. Tomcat server configuration

The configuration of the two Tomcat servers is exactly the same. The following steps can be repeated on two hosts.

Pre-installation preparation

Ntpdate 172.18.0.1

Iptables-F

Install Tomcat

Yum install Tomcat

Deploy solo

Restarting tomcat,tomcat after copying the .war package to the webapps directory will automatically extract its files.

Cp solo-2.4.1.war / usr/share/tomcat/webapps/systemctl restart tomcat

Configure solo

Serverhost is the domain name or IP of the frontend load balancer. If static resources cannot be loaded after deployment, the problem occurs here.

Cd / usr/share/tomcat/webapps/solo-2.4/WEB-INF/classesvim latke.properties # Server # configuration protocol serverScheme=http # configure the domain name or IP when the client accesses the site, which is the port serverPort=80 used by the domain name serverHost=www.shuaiguoxia.com # of the front-end Nginx

Configure solo connection MySQL

The part of H2 runtime is used by solo by default. Uncomment part of MySQL runtime after manually commenting it out.

Set the user name and password of MySQL, and then change 172.18.68.41 to the IP address of MySQL (master).

# H2 runtime # runtimeDatabase=H2#jdbc.username=root#jdbc.password=#jdbc.driver=org.h3.Driver#jdbc.URL=jdbc:h3:~/solo_h3/db#jdbc.pool=h3# MySQL runtime # runtimeDatabase=MYSQLjdbc.username=root # username jdbc.password=123456 # password jdbc.driver=com.mysql.jdbc.Driverjdbc.URL=jdbc:mysql://172.18.68.41:3306/solo?useUnicode=yes&characterEncoding=utf8jdbc.pool=druid

Configure Tomcat connection Reids

3 software packages are required for Tomcat to connect to Reids:

Commons-pool2-2.2.jarjedis-2.5.2.jartomcat-redis-session-manager-2.0.0.jar

Download address https://github.com/izerui/tomcat-redis-session-manager

Copy the required jar package to / usr/share/tomcat/lib/

Cp commons-pool2-2.2.jar / usr/share/tomcat/lib/cp jedis-2.5.2.jarcp tomcat-redis-session-manager-2.0.0.jar

Modify the contest.xml file and add the following two lines to the file.

IP is the RedisIP address, and the port is the port that Redis listens on.

Vim / etc/tomcat/context.xml#

Restart Tomcat

Systemctl restart tomcat

III. Redis server configuration

There are two Redis servers, one master and one slave. Only the configuration files are different, the other procedures are all the same.

Pre-installation preparation

Ntpdate 172.18.0.1iptables-F

Install Redis

Yum install Redis

Configuration of Redis master node

The primary node is only configured with IP, listening port, and connection password

Vim / etc/redis.confbind 0.0.0.0 # listen on all IPport 6379 # set listening port requirepass 123456 # set the password for the connection

Redis slave node configuration

Set from node to read-only

Bind 0.0.0.0 # listens on all IPslaveof 172.18.68.31 6379 # sets the IP+ port of the master node masterauth 123456 # the connection password of the master node slave-read-only yes # the slave node is read-only

Confirm the master-slave configuration is successful

Shell > redis-cli-h 172.18.68.32 # Connect slave server redis127.0.0.1:6379 > INFO replication # View master-slave replication status information # Replicationrole:slavemaster_host:172.18.68.31 # master node IPmaster_port:6379 # port master_link_status:up # up of master node synchronization normal master_last_io_seconds_ago:9master_sync_in_progress: 0slave_repl_offset:1420slave_priority:100slave_read_only:1connected_slaves:0master_repl_offset:0repl_backlog_active:0repl_backlog_size:1048576repl_backlog_first_byte_offset:0repl_backlog_histlen:0

IV. MySQL server configuration

There are two MySQL servers, one master and one slave. Only the configuration files are different, the other procedures are all the same.

Pre-installation preparation

Ntpdate 172.18.0.1

Iptables-F

Install Mariadb

Yum install Mariadb### runs the initialization script to cd / usr/local/mysql/binmysql_secure_installation # MySql initialization script for MySQL. The following is whether to enter the root password first and confirm whether the password is set for anonymous users to allow root remote login to delete the test database.

Configuration of MySQL master server

Master-slave synchronization must open the binary log, modify the configuration file has a total of two points.

1.server-id must not conflict

two。 Create an authorized user on the primary server to give the user permission to copy.

Shell > vim / etc/my.cnfserver-id=1 # Node IDlog-bin=mysql-bin # specify binary log prefix relay-log=mysql-relay-bin # specify relaylog log prefix replicate-wild-ignore-table=mysql.% # exclude tables to replicate-wild-ignore-table=test.%replicate-wild-ignore-table=information_schema.%

Create an authorized user on the master server line to give the slave server permission to copy master server data. Authorized users should match the minimum permission rule and allow the more accurate the IP address of the connection, the better.

Musql > grant replication slave on *. * to 'slave_user'@'10.0.0.67' identified by' 123456'

MySQL slave server configuration

The master-slave configuration of MySQL is only different from server-id.

Shell > vim / etc/my.cnfserver-id=2 # Node IDlog-bin=mysql-bin # specify binary log prefix relay-log=mysql-relay-bin # specify relaylog log prefix replicate-wild-ignore-table=mysql.% # exclude tables to replicate-wild-ignore-table=test.%replicate-wild-ignore-table=information_schema.%

In the slave server with mysql command line configuration, configure the IP, username and password of the master node. The most important are master_log_file and master_log_pos, which are

The result of the query on the primary node should be the same as that of the primary node.

# query the result in the MySQL master server MariaDB [(none)] > show master status\ Gpositional * 1. Row * * File: master-log.000003 Position: 18893845 Binlog_Do_DB: Binlog_Ignore_DB:

Configure MySQL slave server

MySQL > change master to master_host='10.0.0.66',master_user='slave_user',master_password='123456',master_log_file='mysql-bin.000001',master_log_pos=106; `

View active synchronization

View the master-slave synchronization in the slave server. The yes of the last two lines marks the success of master-slave synchronization. You can also query and verify master-slave synchronization by querying tables, libraries and even data.

MariaDB [(none)] > show slave status\ G * * 1. Row * * Slave_IO_State: Waiting for master to send event Master_Host: 172.18.68.41 Master_User: slave-user Master_Port: 3306 Connect_Retry: 60 Master_Log_File: master-log.000002 Read_Master_Log_ Pos: 245 Relay_Log_File: mysql-relay-bin.000002 Relay_Log_Pos: 530 Relay_Master_Log_File: master-log.000002 Slave_IO_Running: Yes Slave_SQL_Running: Yes

Deployment completed

The deployed solo blog can be accessed by accessing 172.18.68.11/solo-2.4 through the browser, and the first entry will allow you to register a user password, that is, the administrator account password.

At this point, no matter how Nginx is scheduled, Tomcat can save the client's session in Redis. Through the Redis management software of windwos, you can see that there is session information in both master and slave databases of Redis.

This is the end of this article on "how to use Nginx+Tomcat+Redis to achieve a lasting session". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

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