• Linux中部署JAVA程序


    JAVA程序在开发完成后,需要部署到服务器,如果是WEB项目,需要部署到WEB服务器,否则部署到应用服务器。

    JAVA是跨平台的编程语言,服务器的操作系统可以是Windows、Linux或者其它,下面将在Redhat6操作系统下,

    详细说明JAVA程序在WEB服务器和应用服务器上的部署情况。

    1、JAVA程序部署在应用服务器

    (1) JAVA程序HelloWorld 在Redhat6上部署的目录结构

    bin : 存放shell脚本run.sh

    conf :存放配置文件log4j.properties

    lib :存放JAR包HelloWorld.jar、log4j-1.2.16.jar

    logs:存放程序运行日志文件log.log

    (2)编写测试类HelloWorld.java 并打成JAR包HelloWorld.jar

    package com.test;
    
    import org.apache.log4j.Logger;
    import org.apache.log4j.PropertyConfigurator;
    
    public class HelloWorld {
    	private static Logger log = Logger.getLogger(HelloWorld.class);
    	
    	public static void main(String[] args) {
    		try{
    			//log4j.properties变量的值在脚本bin/run.sh 中读取
    			String config = System.getProperty("log4j.properties");
    			if (config != null) {
    				PropertyConfigurator.configure(config);
    			}
    			log.info("HelloWorld");
    			
    			Thread thread = new Thread(){
    				public void run(){
    					while(true){
    						try {
    							Thread.sleep(5*1000);
    							log.info("每隔5秒打印一下日志");
    						} catch (InterruptedException e) {
    							e.printStackTrace();
    							log.error(e.getMessage());
    						}
    					}
    				}
    			};
    			thread.run();
    		} catch (Exception e) {
    			log.error("[X]启动失败:"+e.getMessage());
    			System.exit(1);
    		}
    	}
    
    }
    

    (2)编写shell启动脚本run.sh

    #! /bin/sh
    
    #-------------------------------------------------------------------
    # 定义变量
    #-------------------------------------------------------------------
    APP_NAME=HelloWorld
    GREP_KEY="Diname="${APP_NAME}
    
    # -Xms512m 设置JVM堆的初始内存
    # -Xmx1024m 设置JVM堆的最大内存
    # -Dlog4j.properties 设置log4j日志文件参数,可给JAVA程序调用,调用格式是System.getProperty("log4j.properties")
    APP_OPTS="-Xrs -Xms512m -Xmx1024m -Dlog4j.properties=../conf/log4j.properties"
    
    # 程序主类
    APP_CLASS="com.test.HelloWorld"
    
    # 日志文件
    APP_LOG="../logs/log.log"
    
    # 模块运行需要的lib
    APP_LIBS=./:`ls ../lib/*.jar | paste -s -d":" -`
    
    # 当前的类路径=当前模块的类路径+JDK的类路径
    APP_CLASSPATH=${APP_LIBS}:.:${CLASSPATH}
    
    # 检查HelloWorld进程是否已经在运行,如果在运行则返回1,否则返回0
    is_exist(){
    	# ps -ef : 查询所有进程
    	# grep -w "${GREP_KEY}" : 从所有进程中查出名称为HelloWorld的进程,-w为精确查找
    	# grep -v "grep" : 排除名称为grep的进程
    	# awk '{print $2}' : 输出第二个参数,也就是进程号
    	pid=`ps -ef | grep -w "${GREP_KEY}" | grep -v "grep" | awk '{print $2}'`
    
    	# 判断进程号是否为空
    	if [ -z "${pid}" ]
    		then return 1
    	else 
    		return 0
    	fi
    }
    
    # 打印HelloWorld进程的状态信息
    status(){
    	is_exist 
    	if [ $? -eq "0" ]
    		then echo "${APP_NAME} is running. pid=${pid} ."
    	else
    		echo "${APP_NAME} is not running"
    	fi
    }
    
    # 启动HelloWorld进程
    start(){
    	is_exist 
    	if [ $? -eq "0" ]
    		then echo "${APP_NAME} is already running. pid=${pid} ."
    		return 0
    	else
    		echo "try to start ${APP_NAME} ... "
    
    		# 调用nohup命令启动HelloWorld
    		# 1>&- : 表示关闭标准输出日志到nohup.out  
    		# 2>${APP_LOG} : 表示输出日志到../logs/log.log
    		# 最后的& : 表示退出帐户/关闭终端时程序不退出
                    nohup $JAVA_HOME/bin/java -${GREP_KEY} ${APP_OPTS} -classpath ${APP_CLASSPATH} ${APP_CLASS} 1>&- 2>${APP_LOG} &
    
    		# 程序的启动需要一定的时间,这里设置暂停时间(3秒),单位是秒
                    sleep 3
    
                    is_exist
                    if [ $? -eq "0" ]
                    then
                            echo "${APP_NAME} is running now. pid=${pid}."
                            return 0
                    else
                            echo "failed to start ${APP_NAME}! see ${APP_LOG} for more details."
                            return 1
                    fi
    	fi
    }
    
    # 停止HelloWorld进程
    stop()
    {
    	is_exist
    
    	if [ $? -eq 0 ]
    		then 	echo "try to stop ${APP_NAME} ..."
    
    			# 调用kill命令杀掉进程
    			/usr/bin/kill -9  ${pid}
    
    			if [ $? -ne 0 ]
    				then echo "failed to stop ${APP_NAME}!"
    				return 1
    			else
    				echo "${APP_NAME} stopped."
    				return 0
    			fi
    	else
    		echo "${APP_NAME} is not running!"
    		return 1
    	fi
    }
    
    # 重启HelloWorld进程
    restart(){
    	stop
    	start
    }
    
    # 显示帮助信息
    help()
    {
    echo "status                    show the status of ${APP_NAME} server."
    echo "start                     start the ${APP_NAME} server."
    echo "stop                      stop the ${APP_NAME} server."
    echo "restart                   restart the ${APP_NAME} server."
    }
    
    # 主函数
    main()
    {
    	case "$1" in 
    	status)   status;;
    	start)    start;;
    	stop)     stop;;
    	restart)  restart;;
    	*)        echo "command param error ! see follow help "; help;;
    	esac
    }
    
    # 执行主函数 $1表示选择第一个字符串为参数,比如终端命令是:./run.sh start status,则选择start为输入参数
    main $1
    
    


    (3)启动程序

    在终端目录/opt/HelloWorld/bin下,输入命令:./run.sh start 

     查看日志文件logs/log.log中的内容

    至此,JAVA程序HelloWorld已经在LINUX上部署完成。

    2、JAVA WEB程序部署在TOMCAT服务器

          待续...

  • 相关阅读:
    【摘】DB2程序性能
    动态html标签textarea的readOnly属性(JavaScript)
    Html和Xml 文件中的特殊字符 需要转义【转】
    ping的通telnet不正常 服务器之间连接不稳定
    AJAX 和 JSP 10.5(转)实现进度条【转】
    RedHat上部署was7.0
    JPPF 在Windows Server 2008R2上的配置
    银联贷记卡账务账务计算说明
    mybatis源码分析
    使用Neo4j的apoc插件,实现数据从MySQL抽取到Neo4j
  • 原文地址:https://www.cnblogs.com/riskyer/p/3343311.html
Copyright © 2020-2023  润新知