![5846e928b87cc9f7477daeac90d51d81.png](https://i-blog.csdnimg.cn/blog_migrate/6749822ea7de9966d3e0e7e4a48946f3.jpeg)
之前想的不加功能了,之前已经实现了redis。因为前两天看了nginx的负载均衡,虽然也配过实例,但没有将我做过的项目进行整合,我想这也是一种成长过程,也是一个实操锻炼过程
话不多说整活!!!
不只针对我的这个项目,你如果有spring boot项目也可以试试。方法都是一样的,最后效果是通过访问一个网址或者ip达到访问不同端口的目的。负载可以负载端口也可以负载IP,在于你怎么配置。先附一张实现效果图
![d6e93340ba5995649f3d66963bce2e89.gif](https://i-blog.csdnimg.cn/blog_migrate/560abe4b824fb31527765c0e367ace42.gif)
首先就是打包
将你的项目打成jar包
下面是关于idea如何打包的,会打包的自行跳过
eclipse就不贴了,都2020年了,还在用eclipse吗......这也没有鄙视的意思,觉得谁好用就用谁吧(doge保命)
- pom文件中添加打包插件
- 增加所打的包的方式,jar就写jar,war就写war
- 利用maven自带插件打包(建议先clean)
1、pom文件中添加打包插件
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
![20135952efd1d4b11baa5b456b8aced6.png](https://i-blog.csdnimg.cn/blog_migrate/7c9dd9584e48d90e17a3917f511b9856.png)
2、增加所打的包的方式,jar就写jar,war就写war
<packaging>jar</packaging>
3、利用maven自带插件打包(建议先clean)
![bc7e5df38dbbb49f61ed075fd4f7db8f.png](https://i-blog.csdnimg.cn/blog_migrate/230c462202e545ce6bae1a8297f1352a.png)
4、jar包所在位置
![4f791d6e012d071860e74a0fb3c4b89a.png](https://i-blog.csdnimg.cn/blog_migrate/0e1d9e64f87dc0884bb63e072a61a2ed.png)
最后提一下,我用过idea自带的打包工具就是那个build,然后运行的时候报的信息一大堆看不懂的,当然项目最后启动不了,可能是我方式不对,但我还是极力的不推荐,一是麻烦,二是需要注意的地方太多,用我上面这种就可以了。
![63573488bff97401d90b8851482aef9e.png](https://i-blog.csdnimg.cn/blog_migrate/520f35180ee5edb05561b8ab6c729991.jpeg)
![948ca64ab79c596054f2e1bb14d0c779.png](https://i-blog.csdnimg.cn/blog_migrate/70ee34473d4de9f584de2b679bee54b4.jpeg)
-------------------------------打包完,下来说正事--------------------------
1、为了观察效果,可以控制器中设置一个方法返回各自的端口(就是我演示中的效果)
![46e8630304c20b391169785569c44cf9.png](https://i-blog.csdnimg.cn/blog_migrate/11dd21e5ea7a26b953348e1e56c6934d.png)
![89d4f3621d08da79d03b5f381a3b547f.png](https://i-blog.csdnimg.cn/blog_migrate/027214d6e7c052a2e396aa50d8084360.png)
2、将项目打包,分别定义不同的端口8080,8081,8082
![fd8e1608963b20dc1cf25c6efb8c17b8.png](https://i-blog.csdnimg.cn/blog_migrate/8e7c96ab3aa8b75d27bbf91bbe9bf367.png)
3、将打好的包用名字进行区分,放入Linux中
![f97de6e36dc4e8015787aa5900c3aa56.png](https://i-blog.csdnimg.cn/blog_migrate/32cb5dd85a07b8cfc86d9cab41235479.png)
4、进入nginx的conf目录下修改nginx.conf
![77df355db09914a43ed75291526fded3.png](https://i-blog.csdnimg.cn/blog_migrate/854f3f0e261d8ff15c4e589178dae4db.png)
5、启动nginx.
./nginx
6、依次启动jar包(也可以使用一键启动脚本,后面说)
nohup java -jar xxx(nohup表示后台运行)
7、访问路径验证效果,一直刷新会观察到端口的变化
http://192.168.11.130/port
下面为一键启动脚本的代码,直接cv就可以用了
#!/bin/bash
#@author dali
#统一启动3个jar包文件
#这里定义你的jar包名称
APP_NAME1=computer8080.jar
APP_NAME2=computer8081.jar
APP_NAME3=computer8082.jar
#使用说明,用来提示输入参数
usage() {
echo "Usage: sh 执行脚本.sh [start|stop|restart|status]"
exit 1
}
#检查8080是否在运行
is_exist1(){
pid1=`ps -ef|grep $APP_NAME1|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid1}" ]; then
return 1
else
return 0
fi
}
#检查8081是否在运行
is_exist2(){
pid2=`ps -ef|grep $APP_NAME2|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid2}" ]; then
return 1
else
return 0
fi
}
#检查8082是否在运行
is_exist3(){
pid3=`ps -ef|grep $APP_NAME3|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid3}" ]; then
return 1
else
return 0
fi
}
#启动方法
start(){
is_exist1
if [ $? -eq "0" ]; then
echo "${APP_NAME1} is already running. pid=${pid1} ."
else
nohup java -jar $APP_NAME1 > /dev/null 2>&1 &
fi
is_exist2
if [ $? -eq "0" ]; then
echo "${APP_NAME2} is already running. pid=${pid2} ."
else
nohup java -jar $APP_NAME2 > /dev/null 2>&1 &
fi
is_exist3
if [ $? -eq "0" ]; then
echo "${APP_NAME3} is already running. pid=${pid3} ."
else
nohup java -jar $APP_NAME3 > /dev/null 2>&1 &
fi
}
#停止方法
stop(){
is_exist1
if [ $? -eq "0" ]; then
kill -9 $pid1
else
echo "${APP_NAME1} is not running"
fi
is_exist2
if [ $? -eq "0" ]; then
kill -9 $pid2
else
echo "${APP_NAME2} is not running"
fi
is_exist3
if [ $? -eq "0" ]; then
kill -9 $pid3
else
echo "${APP_NAME3} is not running"
fi
}
#输出运行状态
status(){
is_exist1
if [ $? -eq "0" ]; then
echo "${APP_NAME1} is running. Pid is ${pid1}"
else
echo "${APP_NAME1} is NOT running."
fi
is_exist2
if [ $? -eq "0" ]; then
echo "${APP_NAME2} is running. Pid is ${pid2}"
else
echo "${APP_NAME2} is NOT running."
fi
is_exist3
if [ $? -eq "0" ]; then
echo "${APP_NAME3} is running. Pid is ${pid3}"
else
echo "${APP_NAME3} is NOT running."
fi
}
#重启
restart(){
stop
start
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac