詳解如何通過(guò)tomcat的ManagerServlet遠(yuǎn)程部署項(xiàng)目
介紹
之前在郵政實(shí)習(xí)時(shí),leader讓我閱讀tomcat的源代碼,嘗試自己實(shí)現(xiàn)遠(yuǎn)程部署項(xiàng)目的功能,于是便有了這此實(shí)踐。
在Tomact中有一個(gè)Manager應(yīng)用程序,它是用來(lái)管理已經(jīng)部署的web應(yīng)用程序,在這個(gè)應(yīng)用程序中,ManagerServlet是他的主servlet,通過(guò)它我們可以獲取tomcat的部分指標(biāo),遠(yuǎn)程管理web應(yīng)用程序,不過(guò)這個(gè)功能會(huì)受到web應(yīng)用程序部署中安全約束的保護(hù)。
當(dāng)你請(qǐng)求ManagerServlet時(shí),它會(huì)檢查getPathInfo()返回的值以及相關(guān)的查詢參數(shù),以確定被請(qǐng)求的操作。它支持以下操作和參數(shù)(從servlet路徑開始):
請(qǐng)求路徑 | 描述 |
---|---|
/deploy?config={config-url} | 根據(jù)指定的path部署并啟動(dòng)一個(gè)新的web應(yīng)用程序(詳見源碼) |
/deploy?config={config-url}&war={war-url}/ | 根據(jù)指定的pat部署并啟動(dòng)一個(gè)新的web應(yīng)用程序(詳見源碼) |
/deploy?path=/xxx&war={war-url} | 根據(jù)指定的path部署并啟動(dòng)一個(gè)新的web應(yīng)用程序(詳見源碼) |
/list | 列出所有web應(yīng)用程序的上下文路徑。格式為path:status:sessions(活動(dòng)會(huì)話數(shù)) |
/reload?path=/xxx | 根據(jù)指定path重新加載web應(yīng)用 |
/resources?type=xxxx | 枚舉可用的全局JNDI資源,可以限制指定的java類名 |
/serverinfo | 顯示系統(tǒng)信息和JVM信息 |
/sessions | |
/expire?path=/xxx | 列出path路徑下的web應(yīng)用的session空閑時(shí)間信息 |
/expire?path=/xxx&idle=mm | Expire sessions for the context path /xxx which were idle for at least mm minutes. |
/sslConnectorCiphers | 顯示當(dāng)前connector配置的SSL/TLS密碼的診斷信息 |
/start?path=/xx | 根據(jù)指定path啟動(dòng)web應(yīng)用程序 |
/stop?path=/xxx | 根據(jù)指定path關(guān)閉web應(yīng)用程序 |
/threaddump | Write a JVM thread dump |
/undeploy?path=/xxx | 關(guān)閉并刪除指定path的Web應(yīng)用程序,然后刪除底層WAR文件或文檔基目錄。 |
我們可以通過(guò)ManagerServlet
中g(shù)etPathInfo()提供的操作,將自己的項(xiàng)目遠(yuǎn)程部署到服務(wù)器上,下面將貼出我的實(shí)踐代碼,在實(shí)踐它之前你只需要引入httpclient包和commons包。
封裝統(tǒng)一的遠(yuǎn)程請(qǐng)求管理類
封裝此類用于方便client請(qǐng)求ManagerServlet:
import java.io.File; import java.net.URL; import java.net.URLEncoder; import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.http.Header; import org.apache.http.HttpHost; import org.apache.http.HttpResponse; import org.apache.http.HttpStatus; import org.apache.http.auth.AuthScope; import org.apache.http.auth.Credentials; import org.apache.http.auth.UsernamePasswordCredentials; import org.apache.http.client.AuthCache; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpRequestBase; import org.apache.http.client.protocol.ClientContext; import org.apache.http.impl.auth.BasicScheme; import org.apache.http.impl.client.BasicAuthCache; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.impl.conn.PoolingClientConnectionManager; import org.apache.http.protocol.BasicHttpContext; public class TomcatManager { private static final String MANAGER_CHARSET = "UTF-8"; private String username; private URL url; private String password; private String charset; private boolean verbose; private DefaultHttpClient httpClient; private BasicHttpContext localContext; /** constructor */ public TomcatManager(URL url, String username) { this(url, username, ""); } public TomcatManager(URL url, String username, String password) { this(url, username, password, "ISO-8859-1"); } public TomcatManager(URL url, String username, String password, String charset) { this(url, username, password, charset, true); } public TomcatManager(URL url, String username, String password, String charset, boolean verbose) { this.url = url; this.username = username; this.password = password; this.charset = charset; this.verbose = verbose; // 創(chuàng)建網(wǎng)絡(luò)請(qǐng)求相關(guān)的配置 PoolingClientConnectionManager poolingClientConnectionManager = new PoolingClientConnectionManager(); poolingClientConnectionManager.setMaxTotal(5); this.httpClient = new DefaultHttpClient(poolingClientConnectionManager); if (StringUtils.isNotEmpty(username)) { Credentials creds = new UsernamePasswordCredentials(username, password); String host = url.getHost(); int port = url.getPort() > -1 ? url.getPort() : AuthScope.ANY_PORT; httpClient.getCredentialsProvider().setCredentials(new AuthScope(host, port), creds); AuthCache authCache = new BasicAuthCache(); BasicScheme basicAuth = new BasicScheme(); HttpHost targetHost = new HttpHost(url.getHost(), url.getPort(), url.getProtocol()); authCache.put(targetHost, basicAuth); localContext = new BasicHttpContext(); localContext.setAttribute(ClientContext.AUTH_CACHE, authCache); } } /** 根據(jù)指定的path部署并啟動(dòng)一個(gè)新的應(yīng)用程序 */ public TomcatManagerResponse deploy(String path, File war, boolean update) throws Exception { StringBuilder buffer = new StringBuilder("/deploy"); buffer.append("?path=").append(URLEncoder.encode(path, charset)); if (war != null) { buffer.append("&war=").append(URLEncoder.encode(war.toString(), charset)); } if (update) { buffer.append("&update=true"); } return invoke(buffer.toString()); } /** 獲取所有已部署的web應(yīng)用程序的上下文路徑。格式為path:status:sessions(活動(dòng)會(huì)話數(shù)) */ public TomcatManagerResponse list() throws Exception { StringBuilder buffer = new StringBuilder("/list"); return invoke(buffer.toString()); } /** 獲取系統(tǒng)信息和JVM信息 */ public TomcatManagerResponse serverinfo() throws Exception { StringBuilder buffer = new StringBuilder("/serverinfo"); return invoke(buffer.toString()); } /** 真正發(fā)送請(qǐng)求的方法 */ private TomcatManagerResponse invoke(String path) throws Exception { HttpRequestBase httpRequestBase = new HttpGet(url + path); HttpResponse response = httpClient.execute(httpRequestBase, localContext); int statusCode = response.getStatusLine().getStatusCode(); switch (statusCode) { case HttpStatus.SC_OK: // 200 case HttpStatus.SC_CREATED: // 201 case HttpStatus.SC_ACCEPTED: // 202 break; case HttpStatus.SC_MOVED_PERMANENTLY: // 301 case HttpStatus.SC_MOVED_TEMPORARILY: // 302 case HttpStatus.SC_SEE_OTHER: // 303 String redirectUrl = getRedirectUrl(response); this.url = new URL(redirectUrl); return invoke(path); } return new TomcatManagerResponse().setStatusCode(response.getStatusLine().getStatusCode()) .setReasonPhrase(response.getStatusLine().getReasonPhrase()) .setHttpResponseBody(IOUtils.toString(response.getEntity().getContent())); } /** 提取重定向URL */ protected String getRedirectUrl(HttpResponse response) { Header locationHeader = response.getFirstHeader("Location"); String locationField = locationHeader.getValue(); // is it a relative Location or a full ? return locationField.startsWith("http") ? locationField : url.toString() + '/' + locationField; } }
封裝響應(yīng)結(jié)果集
@Data public class TomcatManagerResponse { private int statusCode; private String reasonPhrase; private String httpResponseBody; }
測(cè)試遠(yuǎn)程部署
在測(cè)試之前請(qǐng)先在配置文件放通下面用戶權(quán)限:
<role rolename="admin-gui"/> <role rolename="admin-script"/> <role rolename="manager-gui"/> <role rolename="manager-script"/> <role rolename="manager-jmx"/> <role rolename="manager-status"/> <user username="sqdyy" password="123456" roles="manager-gui,manager-script,manager-jmx,manager-status,admin-script,admin-gui"/>
下面是測(cè)試成功遠(yuǎn)程部署war包的代碼:
import static org.testng.AssertJUnit.assertEquals; import java.io.File; import java.net.URL; import org.testng.annotations.Test; public class TestTomcatManager { @Test public void testDeploy() throws Exception { TomcatManager tm = new TomcatManager(new URL("http://localhost:8080/manager/text"), "sqdyy", "123456"); File war = new File("E:\\tomcat\\simple-war-project-1.0-SNAPSHOT.war"); TomcatManagerResponse response = tm.deploy("/simple-war-project-1.0-SNAPSHOT", war, true); System.out.println(response.getHttpResponseBody()); assertEquals(200, response.getStatusCode()); // output: // OK - Deployed application at context path /simple-war-project-1.0-SNAPSHOT } @Test public void testList() throws Exception { TomcatManager tm = new TomcatManager(new URL("http://localhost:8080/manager/text"), "sqdyy", "123456"); TomcatManagerResponse response = tm.list(); System.out.println(response.getHttpResponseBody()); assertEquals(200, response.getStatusCode()); // output: // OK - Listed applications for virtual host localhost // /:running:0:ROOT // /simple-war-project-1.0-SNAPSHOT:running:0:simple-war-project-1.0-SNAPSHOT // /examples:running:0:examples // /host-manager:running:0:host-manager // /manager:running:0:manager // /docs:running:0:docs } @Test public void testServerinfo() throws Exception { TomcatManager tm = new TomcatManager(new URL("http://localhost:8080/manager/text"), "sqdyy", "123456"); TomcatManagerResponse response = tm.serverinfo(); System.out.println(response.getHttpResponseBody()); assertEquals(200, response.getStatusCode()); // output: // OK - Server info // Tomcat Version: Apache Tomcat/7.0.82 // OS Name: Windows 10 // OS Version: 10.0 // OS Architecture: amd64 // JVM Version: 1.8.0_144-b01 // JVM Vendor: Oracle Corporation } }
參考資料
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
tomcat的webapps和ROOT目錄的區(qū)別及說(shuō)明
這篇文章主要介紹了tomcat的webapps和ROOT目錄的區(qū)別及說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06Tomcat啟動(dòng)報(bào)錯(cuò)子容器啟動(dòng)失敗問(wèn)題及解決
這篇文章主要介紹了Tomcat啟動(dòng)報(bào)錯(cuò)子容器啟動(dòng)失敗問(wèn)題及解決,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-06-06nginx+tomcat單個(gè)域名及多個(gè)域名配置教程
這篇文章主要介紹了nginx+tomcat單個(gè)域名及多個(gè)域名配置教程,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2017-12-12新版Eclipse集成Tomcat時(shí)找不到server選項(xiàng)的解決方法
這篇文章主要給大家分享了新版Eclipse集成Tomcat時(shí)找不到server選項(xiàng)的解決方案,文章通過(guò)圖文介紹講解的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-10-10Tomcat打破雙親委派機(jī)制實(shí)現(xiàn)隔離Web應(yīng)用的方法
本文主要介紹了Tomcat打破雙親委派機(jī)制實(shí)現(xiàn)隔離Web應(yīng)用的方法,文中通過(guò)示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Tomcat服務(wù)器響應(yīng)過(guò)慢解決方案
這篇文章主要介紹了Tomcat服務(wù)器響應(yīng)過(guò)慢解決方案,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09idea中沒有tomcat選項(xiàng)如何配置添加tomcat
多的工程用IDEA打開調(diào)試,如果用到 tomcat服務(wù),都要配置一下,本文主要介紹了idea中沒有tomcat選項(xiàng)如何配置添加tomcat,下面就來(lái)具體介紹一下,感興趣的可以了解一下2024-05-05同一臺(tái)服務(wù)器(電腦)運(yùn)行多個(gè)Tomcat的設(shè)置方法步驟
這篇文章主要介紹了同一臺(tái)服務(wù)器(電腦)運(yùn)行多個(gè)Tomcat的設(shè)置方法步驟,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04