本文章最後更新時間:2017-11-27
個人部落格地址:blog.sqdyy.cn
介紹
之前在郵政實習時,leader讓我閱讀tomcat的原始碼,嘗試自己實現遠端部署專案的功能,於是便有了這此實踐。
在Tomact中有一個Manager應用程式,它是用來管理已經部署的web應用程式,在這個應用程式中,ManagerServlet是他的主servlet,通過它我們可以獲取tomcat的部分指標,遠端管理web應用程式,不過這個功能會受到web應用程式部署中安全約束的保護。
當你請求ManagerServlet時,它會檢查getPathInfo()返回的值以及相關的查詢引數,以確定被請求的操作。它支援以下操作和引數(從servlet路徑開始):
請求路徑 | 描述 |
---|---|
/deploy?config={config-url} | 根據指定的path部署並啟動一個新的web應用程式(詳見原始碼) |
/deploy?config={config-url}&war={war-url}/ | 根據指定的pat部署並啟動一個新的web應用程式(詳見原始碼) |
/deploy?path=/xxx&war={war-url} | 根據指定的path部署並啟動一個新的web應用程式(詳見原始碼) |
/list | 列出所有web應用程式的上下文路徑。格式為path:status:sessions(活動會話數) |
/reload?path=/xxx | 根據指定path重新載入web應用 |
/resources?type=xxxx | 列舉可用的全域性JNDI資源,可以限制指定的java類名 |
/serverinfo | 顯示系統資訊和JVM資訊 |
/sessions | |
/expire?path=/xxx | 列出path路徑下的web應用的session空閒時間資訊 |
/expire?path=/xxx&idle=mm | Expire sessions for the context path /xxx which were idle for at least mm minutes. |
/sslConnectorCiphers | 顯示當前connector配置的SSL/TLS密碼的診斷資訊 |
/start?path=/xx | 根據指定path啟動web應用程式 |
/stop?path=/xxx | 根據指定path關閉web應用程式 |
/threaddump | Write a JVM thread dump |
/undeploy?path=/xxx | 關閉並刪除指定path的Web應用程式,然後刪除底層WAR檔案或文件基目錄。 |
我們可以通過ManagerServlet
中getPathInfo()提供的操作,將自己的專案遠端部署到伺服器上,下面將貼出我的實踐程式碼,在實踐它之前你只需要引入httpclient包和commons包。
封裝統一的遠端請求管理類
封裝此類用於方便client請求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;
// 建立網路請求相關的配置
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);
}
}
/** 根據指定的path部署並啟動一個新的應用程式 */
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應用程式的上下文路徑。格式為path:status:sessions(活動會話數) */
public TomcatManagerResponse list() throws Exception {
StringBuilder buffer = new StringBuilder("/list");
return invoke(buffer.toString());
}
/** 獲取系統資訊和JVM資訊 */
public TomcatManagerResponse serverinfo() throws Exception {
StringBuilder buffer = new StringBuilder("/serverinfo");
return invoke(buffer.toString());
}
/** 真正傳送請求的方法 */
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;
}
}
複製程式碼
封裝響應結果集
@Data
public class TomcatManagerResponse {
private int statusCode;
private String reasonPhrase;
private String httpResponseBody;
}
複製程式碼
測試遠端部署
在測試之前請先在配置檔案放通下面使用者許可權:
<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"/>
複製程式碼
下面是測試成功遠端部署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
}
}
複製程式碼