Web服务器群集——部署Java项目,实现Nginx反向代理Tomcat服务器
Posted Pakho`
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Web服务器群集——部署Java项目,实现Nginx反向代理Tomcat服务器相关的知识,希望对你有一定的参考价值。
部署Java项目,实现nginx反向代理Tomcat服务器
环境准备
tomcat1:192.168.100.10
tomcat2:192.168.100.11
nginx:192.168.100.12
安装TOMCAT
两台TOMCAT服务器同时操作
[root@tomcat1 ~]# ls
apache-tomcat-8.5.9.tar.gz java-demo-master.zip jdk-8u291-linux-x64.tar.gz
[root@tomcat1 ~]# tar xf apache-tomcat-8.5.9.tar.gz -C /usr/local/
[root@tomcat1 ~]# tar xf jdk-8u291-linux-x64.tar.gz -C /usr/local/
[root@tomcat1 local]# ln -s apache-tomcat-8.5.9 tomcat
[root@tomcat1 local]# ln -s jdk1.8.0_291 java
#修改环境变量
[root@tomcat1 ~]# vim /etc/profile
#末行添加
JAVA_HOME=/usr/local/java
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
[root@tomcat1 ~]# source /etc/profile #使变量生效
[root@tomcat1 ~]# java -version
java version "1.8.0_291"
Java(TM) SE Runtime Environment (build 1.8.0_291-b10)
Java HotSpot(TM) 64-Bit Server VM (build 25.291-b10, mixed mode)
[root@tomcat1 ~]# /usr/local/tomcat/bin/startup.sh
http://192.168.100.10:8080/ #使用浏览器访问测试
[root@tomcat1 ~]# /usr/local/tomcat/bin/shutdown.sh #测试完成后关闭服务器,切记
安装mysql
[root@tomcat1 ~]# yum -y install mariadb-server
[root@tomcat1 ~]# systemctl start mariadb
部署JAVA项目
[root@tomcat1 ~]# unzip java-demo-master.zip
#执行sql语句将表导入数据库
[root@tomcat1 db]# pwd
/root/java-demo-master/db
#sql语句
[root@tomcat1 db]# cat tables_ly_tomcat.sql
/*
MySQL - 5.6.30-log : Database - test
*/
CREATE DATABASE IF NOT EXISTS `test` DEFAULT CHARACTER SET utf8 ;
USE `test`;
CREATE TABLE `user` (
`id` INT(11) NOT NULL AUTO_INCREMENT,
`name` VARCHAR(100) NOT NULL COMMENT '名字',
`age` INT(3) NOT NULL COMMENT '年龄',
`sex` CHAR(1) DEFAULT NULL COMMENT '性别',
PRIMARY KEY (`id`)
) ENGINE=INNODB DEFAULT CHARSET=utf8;
COMMIT;
#在sql语句的目录当前登录mysql
[root@tomcat1 db]# mysql -uroot -p'123456'
#数据库部分
#将sql语句导入表
MariaDB [(none)]> use test;
MariaDB [test]> source tables_ly_tomcat.sql;
Database changed
Query OK, 0 rows affected (0.00 sec)
Query OK, 0 rows affected (0.00 sec)
MariaDB [test]> show tables;
+----------------+
| Tables_in_test |
+----------------+
| user |
+----------------+
1 row in set (0.00 sec)
MariaDB [test]> desc user;
+-------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(100) | NO | | NULL | |
| age | int(3) | NO | | NULL | |
| sex | char(1) | YES | | NULL | |
+-------+--------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
打包Java项目
#安装maven
[root@tomcat1 java-demo-master]# yum -y install maven
#位于java项目目录
[root@tomcat1 java-demo-master]# pwd
/root/java-demo-master
#输入打包命令
[root@tomcat1 java-demo-master]# mvn package -DskipTests=True
#打包完成后有个target包中会有打包完成的tar包
[root@tomcat1 java-demo-master]# ls
db pom.xml src target
将war包导入tomcat网页根目录
#清除tomcat默认主页文件
[root@tomcat1 ROOT]# rm -rf ./*
#将war包copy进网页根目录
[root@tomcat1 target]# cp ly-simple-tomcat-0.0.1-SNAPSHOT.war /usr/local/tomcat/webapps/ROOT/
#将war包解压
[root@tomcat1 ROOT]# unzip ly-simple-tomcat-0.0.1-SNAPSHOT.war
[root@tomcat1 ROOT]# ls
ly-simple-tomcat-0.0.1-SNAPSHOT.war META-INF WEB-INF
#对接数据库
[root@tomcat1 ROOT]# vim WEB-INF/classes/application.yml
datasource:
url: jdbc:mysql://localhost:3306/test?characterEncoding=utf-8
username: root
password: 123456
[root@tomcat1 ~]# /usr/local/tomcat/bin/startup.sh #启动tomcat
#在数据库中查看
MariaDB [test]> select * from test.user;
+----+-------------+-----+------+
| id | name | age | sex |
+----+-------------+-----+------+
| 1 | 91何先生 | 24 | M |
| 2 | 夯先生 | 25 | M |
+----+-------------+-----+------+
2 rows in set (0.00 sec)
#tomcat2操作同1 需对接数据库
[root@tomcat2 ROOT]# vim WEB-INF/classes/application.yml
datasource:
url: jdbc:mysql://192.168.100.10:3306/test?characterEncoding=utf-8
username: root
password: 123456
#进入数据库授权
MariaDB [(none)]> grant all on *.* to 'root'@'192.168.100.11' identified by '123456';
MariaDB [(none)]> select * from test.user;
+----+-------------+-----+------+
| id | name | age | sex |
+----+-------------+-----+------+
| 1 | 91何先生 | 24 | M |
| 2 | 夯先生 | 25 | M |
| 3 | 91皮先生 | 23 | M |
+----+-------------+-----+------+
3 rows in set (0.00 sec)
实现Nginx反向代理
[root@nginx ~]# yum -y install nginx-1.18.0-2.el7.ngx.x86_64.rpm
[root@nginx ~]# systemctl start nginx
#修改主配置文件
[root@nginx ~]# vim /etc/nginx/nginx.conf
#http{}中添加
upstream java {
server 192.168.100.10:8080 weight=1;
server 192.168.100.11:8080 weight=1;
}
#修改子配置文件
[root@nginx ~]# vim /etc/nginx/conf.d/default.conf
#location中添加设置推送给tomcat服务器集群
location / {
root /usr/share/nginx/html;
index index.html index.htm;
proxy_pass http://java;
}
[root@nginx ~]# systemctl restart nginx
- 轮询是upstream的默认分配方式,即每个请求按照时间顺序轮流分配到不同的后端服务器
- proxy_pass即是代理转发,在proxy_pass后面的url写入之前添加的服务池 名字为java
访问Nginx IP地址
#再次查看数据库,读取成功
MariaDB [(none)]> select * from test.user;
+----+-------------+-----+------+
| id | name | age | sex |
+----+-------------+-----+------+
| 1 | 91何先生 | 24 | M |
| 2 | 夯先生 | 25 | M |
| 3 | 91皮先生 | 23 | M |
| 4 | 611 | 22 | M |
+----+-------------+-----+------+
4 rows in set (0.00 sec)
查看Nginx访问日志
[root@nginx ~]# tailf /var/log/nginx/access.log
以上是关于Web服务器群集——部署Java项目,实现Nginx反向代理Tomcat服务器的主要内容,如果未能解决你的问题,请参考以下文章
CentOS7上部署Haproxy+Nginx实现Web群集