【问题标题】:Tomcat: remote programmatic deploy?Tomcat:远程编程部署?
【发布时间】:2011-07-26 19:05:51
【问题描述】:

我想以编程方式远程部署到 Tomcat,我有哪些选择? 我知道 /manager/deploy。 是否可以通过 JMX?即使是 MBean 不与 Tomcat 一起使用也可以。

编辑:似乎使用 /manager/deploy 进行部署不起作用 - 如果我使用包含文件的多部分格式执行 POST 请求,则 servlet 返回 405 Method not allowed。另外,servlet 的 6.0.32 代码似乎没有实现远程部署——我错了吗?该怎么做?

谢谢。

【问题讨论】:

    标签: tomcat deployment


    【解决方案1】:

    由于我也是通过 google 找到的,所以我想分享我的 tomcat 7 部署和取消部署解决方案

    -) 正如ondra-zizka 所指出的,这就像向正确的 URL 发出 Put 请求一样简单 艰难的URL已在tomcat7下更改为/manager/text/deploy?path=&update=

    需要以正斜杠开头,例如:/deployMe

    -) 您可能需要设置访问管理器应用的权限 将此添加到 TOMCAT_HOME/conf/tomcat-users.xml

    注意:tomcat 文档警告您不要让同一用户访问多个角色

    <role rolename="manager-gui"/>
    <role rolename="manager-script"/>
    <role rolename="manager-jmx"/>
    <user username="tomcat" password="s3cret" roles="manager-gui,manager-script,manager-jmx"/>
    

    -) 将 Web 应用程序部署到 apache tomcat 的示例代码

    package deployment;
    
    import java.io.BufferedReader;
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.auth.AuthScope;
    import org.apache.http.auth.UsernamePasswordCredentials;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.CredentialsProvider;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPut;
    import org.apache.http.client.methods.HttpRequestBase;
    import org.apache.http.entity.ContentType;
    import org.apache.http.entity.mime.MultipartEntityBuilder;
    import org.apache.http.impl.client.BasicCredentialsProvider;
    import org.apache.http.impl.client.CloseableHttpClient;
    import org.apache.http.impl.client.HttpClients;
    
    public class DeployManager{
    
        static CredentialsProvider credsProvider = new BasicCredentialsProvider();;
    
        public static void main(String args[]) throws ClientProtocolException, IOException{
            /*
             * warning only ever AuthScope.ANY while debugging
             * with these settings the tomcat username and pw are added to EVERY request
             */
            credsProvider.setCredentials(AuthScope.ANY,new UsernamePasswordCredentials("tomcat", "s3cret"));
    
    //      deploy();
    //      undeploy();
        }
    
    
    
        private static void deploy() throws ClientProtocolException, IOException {
            String url = "http://localhost:8080/manager/text/deploy?path=/deployMe&update=true";
            File file = new File ("deployMe.war") ;
    
            HttpPut req = new HttpPut(url) ;
            MultipartEntityBuilder meb = MultipartEntityBuilder.create();
            meb.addTextBody("fileDescription", "war file to deploy");
            //"application/octect-stream"
            meb.addBinaryBody("attachment", file, ContentType.APPLICATION_OCTET_STREAM, file.getName());
    
            req.setEntity(meb.build()) ;
            String response = executeRequest (req, credsProvider);
    
            System.out.println("Response : "+response);
        }
    
        public static void undeploy() throws ClientProtocolException, IOException{
            String url = "http://localhost:8080/manager/text/undeploy?path=/deployMe";
            HttpGet req = new HttpGet(url) ;
            String response = executeRequest (req, credsProvider);
            System.out.println("Response : "+response);
        } 
    
        private static String executeRequest(HttpRequestBase requestBase, CredentialsProvider credsProvider) throws ClientProtocolException, IOException {
            CloseableHttpClient client = HttpClients.custom().setDefaultCredentialsProvider(credsProvider).build();
            InputStream responseStream = null;
            String res = null;
            HttpResponse response = client.execute(requestBase) ;
            HttpEntity responseEntity = response.getEntity() ;
            responseStream = responseEntity.getContent() ;
    
            BufferedReader br = new BufferedReader (new InputStreamReader (responseStream)) ;
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line);
                sb.append(System.getProperty("line.separator"));
            }
            br.close() ;
            res = sb.toString();
    
            return res;
        }
    }
    

    -) Maven 依赖项

    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.3</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpmime</artifactId>
        <version>4.3</version>
    </dependency>
    

    【讨论】:

    • 嗨@systemkem,我正在尝试将WAR 文件部署到远程Tomcat 7 服务器的解决方案。然而,当我运行我的代码时,我得到了一个SocketException: Software caused connection abort: socket write error,我还不知道为什么。当我使用 3rd 方客户端进行远程部署时,它工作正常。这告诉我我的 Tomcat 服务器的远程管理配置是正确的,并且我的问题必须在代码本身内。任何想法为什么我可能会显示异常?干杯,PM。
    • 此链接的stackoverflow.com/questions/21647337/… 接受的答案包含我的问题的答案。干杯。
    • 您好@systemkern,您知道在使用您展示的MultipartEntityBuilder 代码时,我如何还可以将context.xml 文件包含为配置文件吗?干杯,PM。
    • 嗨@PonderMuse,你的意思是弹簧配置吗?
    • 你好@systemkern,我的意思是你通常会在 META-INF 下的战争中找到的 context.xml。基本上,我们希望在远程部署战争时传递一个可配置的 context.xml 以与 WAR 一起使用,而不必打开 WAR,编辑 context.xml,保存/关闭 WAR,然后远程部署 WAR(是我们目前所做的)。我们很高兴继续这样做,但我只是想知道是否可以在远程部署 HTTP PUT 操作期间将 context.xml 作为配置文件传递。
    【解决方案2】:

    我在关注旧文档。上传是通过PUT/manager/deploy?path=&lt;context-do-deploy-to&gt;&amp;update=&lt;true|false&gt; 请求完成的

    还有一个 Ant 任务在内部使用 PUT 方法。

    Tomcat 的 JMX MBean 不允许远程部署。

    【讨论】:

    • 如果您使用的是 Maven,您可以试一试Cargo。它还附带一个 API,因此您可以根据需要以编程方式调用远程部署。
    【解决方案3】:

    由于某种原因,这是一个顶级谷歌搜索结果。假设您安装了管理器,maven tomcat6 插件非常棒。

    【讨论】:

      猜你喜欢
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2019-04-28
      • 2010-12-18
      • 1970-01-01
      • 2013-06-19
      • 2012-10-18
      相关资源
      最近更新 更多