Home  >  Article  >  Database  >  How to implement persistent sessions through Nginx+Tomcat+Redis

How to implement persistent sessions through Nginx+Tomcat+Redis

WBOY
WBOYforward
2023-05-28 08:37:05882browse

Deployment environment

centos7
nginx1.10.2
tomcat7.0
redis3.2.3
mariadb 5.5.44

Required packages

commons-pool2-2.2.jar
jedis-2.5.2.jar
tomcat-redis-session- manager-2.0.0.jar

solo blog

How to implement persistent sessions through Nginx+Tomcat+Redis

##1. nginx server configuration

Preparation before installation

ntpdate 172.18.0.1

iptables -f

##Install nginx

yum install nginx


Modify the configuration file


upstream tomcatservers: Create a back-end server group, append

location inside http: match url, append

inside the server. Note: The ellipses are other default parameters. If there is no special need, just press the default

vim /etc/nginx/nginx.conf
http {
...
upstream tomcatservers {
 server 172.18.68.21:8080;
 server 172.18.68.22:8080;
 }
...
server {
...
 location / {
  proxy_pass http://tomcatservers;
  }
...
}
}
systemctl start nginx

Check the port


When starting the server, check whether port 80 is listening normally

ss -ntl


2. Tomcat server configuration


Two tomcats The server configuration is exactly the same. Repeat the following steps on both hosts

Preparation before installation

ntpdate 172.18.0.1

iptables -f



Install tomcat

yum install tomcat


Deploy solo


.war package is copied to the webapps directory After restarting tomcat, tomcat will automatically decompress its files

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

Configure solo


serverhost as the domain name or IP of the front-end load balancing, if static appears after the deployment is completed When resources cannot be loaded, there is usually something wrong here.

cd /usr/share/tomcat/webapps/solo-2.4/web-inf/classes
vim latke.properties
 #### server ####
 # 配置协议
 serverscheme=http
 # 配置客户端访问站点时的域名或ip,也就是前端nginx的域名
 serverhost=www.shuaiguoxia.com
 # 使用的端口
 serverport=80

Configuring solo to connect to mysql


The h2 runtime part is used by solo by default. After manually commenting it out, uncomment the mysql runtime part.


Set the username and password of mysql, and then change 172.18.68.41 to the ip address of mysql (main).

#### h2 runtime ####
#runtimedatabase=h2
#jdbc.username=root
#jdbc.password=
#jdbc.driver=org.h2.driver
#jdbc.url=jdbc:h2:~/solo_h2/db
#jdbc.pool=h2
#
#### mysql runtime ####
runtimedatabase=mysql
jdbc.username=root      # 用户名
jdbc.password=123456     # 密码
jdbc.driver=com.mysql.jdbc.driver
jdbc.url=jdbc:mysql://172.18.68.41:3306/solo?useunicode=yes&characterencoding=utf8
jdbc.pool=druid

Configuring tomcat connection reids

Tomcat connection reids requires 3 software packages:

commons-pool2-2.2.jar
jedis-2.5.2.jar
tomcat-redis-session-manager-2.0.0.jar

Download address

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.jar
cp 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
#
 <valve classname="com.orangefunction.tomcat.redissessions.redissessionhandlervalve" />
 <manager classname="com.orangefunction.tomcat.redissessions.redissessionmanager" 
 host="172.18.68.31" 
 port="6379" 
 database="0" 
 maxinactiveinterval="60" />

Restart tomcat

systemctl restart tomcat


3. Redis server configuration


There are two redis servers, one master and one slave. Only the configuration files are different, the other processes are the same.

Preparation before installation

ntpdate 172.18.0.1
iptables -f

Install redis

yum install redis


Redis master node configuration


The master node only configures the ip, listening port, and connection password

vim /etc/redis.conf
bind 0.0.0.0       # 监听所有ip
port 6379        # 设定监听的端口
requirepass 123456      # 设定连接的密码

redis slave node configuration


Set the slave node as read-only

bind 0.0.0.0       # 监听所有ip
slaveof 172.18.68.31 6379    # 设定主节点的ip+端口
masterauth 123456      # 主节点的连接密码
slave-read-only yes      # 从节点为只读

Confirm that the master-slave configuration is successful

shell > redis-cli -h 172.18.68.32  # 连接从服务器redis
127.0.0.1:6379> info replication   # 查看主从复制状态信息
# replication
role:slave
master_host:172.18.68.31     # 主节点ip
master_port:6379       # 主节点的端口
master_link_status:up     # up为主从同步正常
master_last_io_seconds_ago:9
master_sync_in_progress:0
slave_repl_offset:1420
slave_priority:100
slave_read_only:1
connected_slaves:0
master_repl_offset:0
repl_backlog_active:0
repl_backlog_size:1048576
repl_backlog_first_byte_offset:0
repl_backlog_histlen:0

4. Mysql server configuration


There are two mysql servers, one master and one slave. Only the configuration files are different, the other processes are the same.

Preparation before installation

ntpdate 172.18.0.1

iptables -f



Install mariadb

yum install mariadb
#
#
#运行初始化脚本对mysql进行
cd /usr/local/mysql/bin
mysql_secure_installation   #mysql初始化脚本,以下为每一项的翻译
 先回车
 是否设置root密码
 输入密码
 确认密码
 是否设置匿名用户
 是否允许root远程登录
 删除test数据库
 现在是否生效

mysql master server configuration


Master-slave synchronization must enable binary logs, and there are two points in modifying the configuration file.


1.server-id must not conflict


2.Create an authorized user on the main server to give the user replication permissions.

shell > vim /etc/my.cnf
server-id=1           #节点id
log-bin=mysql-bin         #指定二进制日志前缀
relay-log=mysql-relay-bin       #指定relaylog日志前缀
replicate-wild-ignore-table=mysql.%     #排除要复制的表
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

Create an authorized user on the main server row so that the slave server has permission to copy the main server data. Authorized users should match the law of least privilege, and the more precise the IP addresses allowed to connect, the better.

musql > grant replication slave on *.* to 
 &#39;slave_user&#39;@&#39;10.0.0.67&#39; identified by &#39;123456&#39;;

Mysql slave server configuration


Mysql master-slave configuration is only different in server-id

shell > vim /etc/my.cnf
server-id=2           #节点id
log-bin=mysql-bin         #指定二进制日志前缀
relay-log=mysql-relay-bin       #指定relaylog日志前缀
replicate-wild-ignore-table=mysql.%     #排除要复制的表
replicate-wild-ignore-table=test.%
replicate-wild-ignore-table=information_schema.%

When connecting to the slave server Mysql command line configuration, configure the IP, user name and password of the master node. The most important ones are master_log_file and master_log_pos. These two items are the results of

query on the master node, and they must be the same as the query results on the master node.

# 在mysql主服务器中查询结果
mariadb [(none)]> show master status\g;
*************************** 1. row ***************************
  file: master-log.000003
  position: 18893845
  binlog_do_db: 
  binlog_ignore_db:

Configure mysql slave server

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

View the active synchronization situation


View the master-slave in the slave server Synchronization situation. The yes in the last two lines indicates that the master-slave synchronization is successful. You can also query and verify the master-slave synchronization status 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


You can access the deployed solo blog by accessing 172.18.68.11/solo-2.4 through the browser. When you enter for the first time, you will be asked to register a user password, which is the administrator. account password.

How to implement persistent sessions through Nginx+Tomcat+RedisNo matter how nginx is scheduled, tomcat can save the client's session in redis. You can use the redis management software of windwos to see that there is session information in both the redis master and slave databases.


The above is the detailed content of How to implement persistent sessions through Nginx+Tomcat+Redis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete