Linux部署Java集群项目,实现负载均衡+高可用模式。

Linux部署Java集群项目,实现负载均衡+高可用模式。

0、环境

系统:centos7
Jdk:1.8
Tomcat:8
Ngxin+Keepalived

1、防火墙设置

关闭防火墙或者放行端口

1.1关闭防火墙

#停止firewall
systemctl stop firewalld.service

#禁止firewall开机启动
systemctl disable firewalld.service

#查看默认防火墙状态(关闭后显示notrunning,开启后显示running)
firewall-cmd --state

1.2加入放行端口

#添加放行端口(–permanent永久生效,没有此参数重启后失效)
firewall-cmd --zone=public --add-port=80/tcp --permanent
firewall-cmd --zone=public --add-port=8080/tcp --permanent

#刷新防火墙 使其生效
firewall-cmd --reload

#查看开发的端口
firewall-cmd --zone=public --list-ports

2、安装JDK

#查看java列表
yum -y list java*

#安装1.8.0 openjdk(或者官网下载jdk)
yum -y install java-1.8.0-openjdk

#JDK默认安装路径/usr/lib/jvm
ls /usr/lib/jvm
在这里插入图片描述
#环境变量配置
vi /etc/profile

#将如下配置添加至文件中,然后保存退出。

# set java environment  
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
PATH=$PATH:$JAVA_HOME/bin  
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar  
export JAVA_HOME  CLASSPATH  PATH 

#保存关闭profile文件,执行如下命令生效
source /etc/profile

#使用如下命令,查看JDK变量
echo $JAVA_HOME
echo $PATH
echo $CLASSPATH
在这里插入图片描述

3、装tomcat8

3.1 安装wget

yum -y install wget

3.2下载tomcat8

cd /usr/local
wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8/v8.5.47/bin/apache-tomcat-8.5.47.tar.gz
#解压
tar -zxvf apache-tomcat-8.5.47.tar.gz

#移动重命名
mv apache-tomcat-8.5.47 /usr/local/tomcat

3.3配置jdk到tomcat

#修改配置文件
vi /usr/local/tomcat/bin/catalina.sh

#在 OS specific support. 前面加入如下代码
JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.232.b09-0.el7_7.x86_64
JRE_HOME=$JAVA_HOME/jre

3.4配置tomcat开机启动

#创建tomcat.service文件
vi /usr/lib/systemd/system/tomcat.service

[Unit]
Description=Tomcat
After=syslog.target network.target remote-fs.target nss-lookup.target

[Service]
Type=oneshot
ExecStart=/usr/local/tomcat/bin/startup.sh
ExecStop=/usr/local/tomcat/bin/shutdown.sh
ExecReload=/bin/kill -s HUP $MAINPID
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target

#允许tomcat开机启动
systemctl enable tomcat

3.5 tomcat常见命令

#启动tomcat
systemctl start tomcat.service

#关闭tomcat
systemctl stop tomcat.service

#重启tomcat
systemctl restart tomcat.service

#查看状态tomcat
systemctl status tomcat.service

4、nginx负载均衡

备注:nginx 负载均衡参数ip_hash:实现同一ip地址下访问项目为同一个服务器,实现session共享机制,缺点:当服务器挂了,已在访问该服务器的用户请求会失效,需要重新访问项目。

4.1 分别在3台服务器上安装nginx

yum install -y gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel

cd /usr/local/

wget https://nginx.org/download/nginx-1.13.4.tar.gz

tar -zxvf nginx-1.13.4.tar.gz

cd nginx-1.13.4

./configure --prefix=/etc/nginx
make
make install

#创建日志目录
mkdir -p /home/bdxhdata/nginx/logs

#修改配置
whereis nginx
cd /etc/nginx/conf

> nginx.conf
vi nginx.conf

nginx.conf:(配置信息)

worker_processes  1;
error_log  /home/bdxhdata/nginx/logs/nginx-error.log;

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  /home/bdxhdata/nginx/logs/nginx-access.log  main;
    sendfile        on;
    keepalive_timeout  65;

upstream bdxh{  
ip_hash;
	server 192.168.100.11:8080;
	server 192.168.100.12:8080;
   	server 192.168.100.13:8080;
    }
   server {  
	listen       80;  
	server_name  localhost;  
	#charset koi8-r;  
	#access_log  logs/host.access.log  main;  
	location /rsv {  
	    proxy_pass   http://bdxh;  
	    index  index.html index.htm;  
	}

        error_page   500 502 503 504  /50x.html;
        location = /50x.html {
            root   html;
        }

	location = /favicon.ico {
	    log_not_found off;
	    access_log off;
	}
    }
}

#启动nginx
/etc/nginx/sbin/nginx

#重启nginx
/etc/nginx/sbin/nginx -s stop
/etc/nginx/sbin/nginx

#查看ip占用
yum -y install net-tools
netstat -tunlp

5、安装keepalived实现高可用

5.1 下载与配置

yum -y install keepalived

> /etc/keepalived/keepalived.conf
vi /etc/keepalived/keepalived.conf

#keepalived.conf 配置:(主服务器配置:state MASTER)

! Configuration File for keepalived
global_defs {
	router_id jackfang
}

vrrp_script check_nginx {
    script "/etc/keepalived/nginx_check.sh"         
    interval 2                            
    weight 2                  
}
vrrp_instance VI_1 {
    state MASTER
    interface ens33
    virtual_router_id 100
    priority 100
    advert_int 1
    authentication {
        auth_type PASS
        auth_pass 1111
    }
    track_script {
        check_nginx
    }
    virtual_ipaddress {
        192.168.100.10
    }
}

#keepalived.conf 配置:(备服务器配置)
state MASTER 改为state BACKUP
priority 100 改小 priority 99

5.2 创建检测nginx脚本

vi /etc/keepalived/nginx_check.sh

#!/bin/bash
A=`netstat -tunlp|grep 80|wc -l`
if [ $A -eq 0 ];then
/etc/nginx/sbin/nginx
sleep 2
if [ `netstat -tunlp|grep 80|wc -l` -eq 0 ];then
        killall keepalived
fi
fi

chmod +x /etc/keepalived/nginx_check.sh

service keepalived start
service keepalived restart
service keepalived stop
#查看keepalived运行状态
service keepalived status
Linux部署Java集群项目,实现负载均衡+高可用模式。_第1张图片
mkdir -p /etc/sysconfig/

cp /usr/local/keepalived/etc/rc.d/init.d/keepalived /etc/init.d/
cp /usr/local/keepalived/etc/sysconfig/keepalived /etc/sysconfig/
ln -s /usr/local/sbin/keepalived /usr/sbin/
ln -s /usr/local/keepalived/sbin/keepalived /sbin/

#启动keepalived
/usr/local/keepalived/sbin/keepalived

6、部署RSV(JavaWeb项目)项目到tomcat

6.1 将打包好的项目移动到webapps文件夹下

Linux部署Java集群项目,实现负载均衡+高可用模式。_第2张图片

#重启tomcat服务
service tomcat restart

6.2 测试访问rsv项目

Linux部署Java集群项目,实现负载均衡+高可用模式。_第3张图片

#测试当停止11、12 其中一台服务器时,仍然能访问成功表示实现了高可用配置。

你可能感兴趣的:(JAVA)