在公司内部,有大量的Java Web应用需要部署到测试环境,如何做到通过Shell脚本就能自动化部署呢?这里介绍一种方法,供参考。

开发人员工作电脑

通过SwitchHosts等软件设置虚假域名:

192.168.71.129 test.myapp.com

这里,假设192.168.71.129是一台测试虚拟机,硬件较好,通常能部署十余个Java Web应用。

测试虚拟机

关键软件

/export/servers/
jdk1.6.0_25
jdk1.7.0_71
jdk1.8.0_66
nginx
tomcat6.0.33
tomcat7.0.61
tomcat8.0.42

这里安装了所需的Nginx、各版本JDK和Tomcat。

Tomcat启动和shutdown脚本

/export/Domains/test.myapp.com/server1/
bin
conf
logs
temp
webapps
work

bin/start.sh

#!/bin/bash
export CATALINA_HOME=/export/servers/tomcat7.0.61
export TOMCAT_USER="root"
export CATALINA_BASE=/export/Domains/test.myapp.com/server1
###JAVA
export JAVA_HOME=/export/servers/jdk1.8.0_66
export JAVA_BIN=/export/servers/jdk1.8.0_66/bin
export PATH=/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/root/bin:/bin
export CLASSPATH=.:/lib/dt.jar:/lib/tools.jar
export  JAVA_OPTS="-Djava.library.path=/usr/local/lib -server -Xms1024m -Xmx1024m -XX:MaxPermSize=256m -Djava.awt.headless=true -Dsun.net.client.defaultConnectTimeout=60000 -Dsun.net.client.defaultReadTimeout=60000 -Djmagick.systemclassloader=no -Dnetworkaddress.cache.ttl=300 -Dsun.net.inetaddr.ttl=300"
export JAVA_HOME JAVA_BIN PATH CLASSPATH JAVA_OPTS
$CATALINA_HOME/bin/startup.sh "-config $CATALINA_BASE/conf/server.xml"

bin/stop.sh

#!/bin/bash
export CATALINA_HOME=/export/servers/tomcat7.0.61
export CATALINA_BASE=/export/Domains/test.myapp.com/server1
$CATALINA_HOME/bin/shutdown.sh -config $CATALINA_BASE/conf/server.xml
ps -aef | grep java|grep "/test.myapp.com/server1/"| grep -v grep | sed 's/ [ ]*/:/g' |cut -d: -f2|kill -9 `cat`

conf/server.xml

这里指定的HTTP端口,一定要和Nginx里的端口一样,如8081,注意:端口不要重复。

<?xml version='1.0' encoding='utf-8'?>
 <Server port="9001" shutdown="SHUTDOWN">

  <Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />
  <Listener className="org.apache.catalina.core.JreMemoryLeakPreventionListener" />
  <Listener className="org.apache.catalina.mbeans.GlobalResourcesLifecycleListener" />

  <GlobalNamingResources>
    <Resource name="UserDatabase" auth="Container"
              type="org.apache.catalina.UserDatabase"
              description="User database that can be updated and saved"
              factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
              pathname="conf/tomcat-users.xml" />
  </GlobalNamingResources>

  <Service name="Catalina">
    <Connector port="8001" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443"  URIEncoding="utf-8" useBodyEncodingForURI="true"/>
    <Engine name="Catalina" defaultHost="localhost" jvmRoute="s1">

      <Realm className="org.apache.catalina.realm.UserDatabaseRealm"
             resourceName="UserDatabase"/>
      <Host name="localhost"  appBase="webapps"
            unpackWARs="false" autoDeploy="false"
            xmlValidation="false" xmlNamespaceAware="false">

      </Host>
    </Engine>
  </Service>
</Server>
conf\Catalina\localhost\ROOT.xml
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/" docBase="/export/App/test.myapp.com" >
</Context>

应用程序文件

/export/App/test.myapp.com/
META-INF
WEB-INF

WEB-INF下是大量的jar文件和资源文件。

Nginx配置

主要思路就是通过域名分发请求

nginx\conf
++domains
++++test.myapp.com  注意:这是文件,不是目录
++++等其他应用的配置
++nginx.conf
++mime.types
++等其他

nginx.conf里的关键配置:

include domains/*;

完整配置:

user					admin  admin;
worker_processes                        8;
worker_cpu_affinity                     00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000;
error_log				/export/servers/nginx/logs/nginx_error.log  warn;
pid					/export/servers/nginx/logs/nginx.pid;
worker_rlimit_nofile    		65535;
events 
{
					use epoll;
					worker_connections 65535;
}
http 
{
	include       			mime.types;
	default_type  			application/octet-stream;
	server_tokens 			on;

    log_format main               '$scheme $http_host $remote_addr $server_addr [$time_local] "$request" $status $body_bytes_sent "$http_referer" "$http_user_agent" $upstream_addr $upstream_response_time $request_time $http_x_forwarded_for $cookie_pin';

	server_names_hash_bucket_size   128;
	client_header_buffer_size 	32k;
	large_client_header_buffers 	4 32k;
	client_max_body_size 		300m;
	sendfile 			on;
	tcp_nopush     			on;
	keepalive_timeout 		0;
	tcp_nodelay 			on;
	client_body_buffer_size  	512k;
	fastcgi_intercept_errors 	on;
	proxy_connect_timeout 		90;
	proxy_read_timeout       	180;
	proxy_send_timeout       	180;
	proxy_buffer_size        	256k;
	proxy_buffers            	4 256k;
	proxy_busy_buffers_size 	256k;
	proxy_temp_file_write_size 	256k;
	server_name_in_redirect 	off;
	proxy_hide_header       X-Powered-By;

	gzip 				on;
	gzip_min_length  		100;
	gzip_buffers     		4 16k;
	gzip_http_version 		1.0;
	gzip_comp_level 		9;
	gzip_types       		text/plain application/x-javascript text/css application/xml;
	gzip_vary 			on;
        error_page 400 401 402 403 404 405 408 410 412 413 414 415 500 501 502 503 506 = http://test.myapp.com/error2.html;

include domains/*;
###########status#########
        server
                {
                 listen                 80;
                 server_name            status.myapp.com;
        location / {
                 stub_status            on;
                 access_log             off;
                 }
        }
}

/export/servers/nginx/conf/domains/test.myapp.com这个文件(注意:不是目录)形如:

upstream tomcat_test.myapp.com {
                server 127.0.0.1:8001  weight=10 max_fails=2 fail_timeout=30s;
                }
server
                {
                listen                   80;
                server_name              test.myapp.com;
                access_log               /export/servers/nginx/logs/test.myapp.com/test.myapp.com_access.log main;
                error_log                /export/servers/nginx/logs/test.myapp.com/test.myapp.com_error.log warn;
                error_page 411 = @my_error;
                location @my_error {
                 }
                root /export/App/test.myapp.com/;
      location / {
        proxy_next_upstream     http_500 http_502 http_503 http_504 error timeout invalid_header;
        proxy_set_header        Host  $host;
        proxy_set_header        X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_pass              http://tomcat_test.myapp.com;
        expires                 0;
        }
        location /logs/ {
                autoindex       off;
                deny all;
        }
}

相比于将war包直接放到Tomcat webapp目录下,这种部署方式更有利于自动化部署,可以和Jenkins等持续集成软件结合使用。还通过Shell脚本将以下目录打包、压缩、删除。通过Git、SVN等拉取新代码,再次生成目录,完成一次代码构建。

/export/App/test.myapp.com/

参考资料:
1、https://blog.csdn.net/SeanTandol/article/details/85722147

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐