windows Tomcat同时多个实例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16110528/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 10:26:51  来源:igfitidea点击:

Tomcat multiple instances simultaneously

windowstomcatmultiple-instancescatalina

提问by Bruno Klein

I am trying to run multiple instances of Tomcat, but even after configuring different ports for listening and shutting down the second instance, it keeps trying to listen on 8080 (configured for 8081). I read that I have to set a different value for CATALINA_BASE. From all the articles there are online, none of them actually show in which file this variable can be set.

我正在尝试运行 Tomcat 的多个实例,但即使在配置不同的端口用于侦听和关闭第二个实例之后,它仍然试图侦听 8080(为 8081 配置)。我读到我必须为CATALINA_BASE. 从网上的所有文章中,没有一篇真正显示可以在哪个文件中设置此变量。

Where and how can I set CATALINA_BASE for my Tomcat instance in C:\apache-tomcat-7.0.39

我在哪里以及如何为我的 Tomcat 实例设置 CATALINA_BASE C:\apache-tomcat-7.0.39

回答by Goran Vasic

Let's say that you have only one Tomcat folder located in C:\apache-tomcat-7.0.39, and that you wish to run two instances from it.

假设您只有一个 Tomcat 文件夹位于C:\apache-tomcat-7.0.39,并且您希望从中运行两个实例。

Make sure that you have CATALINA_HOMEsystem/user variable set, and pointing to C:\apache-tomcat-7.0.39

确保您设置了CATALINA_HOME系统/用户变量,并指向C:\apache-tomcat-7.0.39

  1. Create a folder C:\instance1. Copy conf, webappsand tempfolders from C:\apache-tomcat-7.0.39 and paste them to C:\instance1. You can delete contents from webapps and temp folders located under instance1, but don't touch confcontents.

  2. Now copy>paste C:\instance1 and rename it to instance2. That way, both instance1and instance2will have the same content.

  3. Go to C:\instance2\conf, edit server.xmland change the numbers of these ports (I marked those as XXXX):

    <Server port="XXXX" shutdown="SHUTDOWN">

    <Connector port="XXXX" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

    <Connector port="XXXX" protocol="AJP/1.3" redirectPort="8443" />

  4. Deploy whatever you want into instance1\webapps and instance2\webapps

  5. Create the following 4 batch files under C:\

  1. 创建文件夹 C:\instance1。从 C:\apache-tomcat-7.0.39复制confwebappstemp文件夹并将它们粘贴到 C:\instance1。您可以从位于 instance1 下的 webapps 和 temp 文件夹中删除内容,但不要触摸conf内容。

  2. 现在复制>粘贴 C:\instance1 并将其重命名为 instance2。这样,instance1instance2将具有相同的内容。

  3. 转到 C:\instance2\conf,编辑server.xml并更改这些端口的编号(我将它们标记为 XXXX):

    <Server port="XXXX" shutdown="SHUTDOWN">

    <Connector port="XXXX" protocol="HTTP/1.1" connectionTimeout="20000" redirectPort="8443" />

    <Connector port="XXXX" protocol="AJP/1.3" redirectPort="8443" />

  4. 将您想要的任何内容部署到 instance1\webapps 和 instance2\webapps

  5. 在 C:\ 下创建以下 4 个批处理文件

instance1_startup.bat

instance1_startup.bat

@echo off

set CATALINA_BASE=C:\instance1

cd "%CATALINA_HOME%\bin"

set TITLE=My Tomcat Instance 01

call startup.bat %TITLE%

@回声关闭

设置 CATALINA_BASE=C:\instance1

cd "%CATALINA_HOME%\bin"

set TITLE=我的 Tomcat 实例 01

调用 startup.bat %TITLE%

instance1_shutdown.bat

instance1_shutdown.bat

@echo off

set CATALINA_BASE=C:\instance1

cd "%CATALINA_HOME%\bin"

call shutdown.bat

@回声关闭

设置 CATALINA_BASE=C:\instance1

cd "%CATALINA_HOME%\bin"

调用shutdown.bat

instance2_startup.bat

instance2_startup.bat

@echo off

set CATALINA_BASE=C:\instance2

cd "%CATALINA_HOME%\bin"

set TITLE=My Tomcat Instance 02

call startup.bat %TITLE%

@回声关闭

设置 CATALINA_BASE=C:\instance2

cd "%CATALINA_HOME%\bin"

set TITLE=我的 Tomcat 实例 02

调用 startup.bat %TITLE%

instance2_shutdown.bat

instance2_shutdown.bat

@echo off

set CATALINA_BASE=C:\instance2

cd "%CATALINA_HOME%\bin"

call shutdown.bat

@回声关闭

设置 CATALINA_BASE=C:\instance2

cd "%CATALINA_HOME%\bin"

调用shutdown.bat

  1. Run instance1_startup.batand instance2_startup.bat, hopefully it should work.
  1. 运行instance1_startup.batinstance2_startup.bat,希望它应该可以工作。

回答by Jason Sperske

The easiest way I have run two copies of Tomcat involved the following steps (I was trying to run two distinct versions of tomcat, 6 and 7):

我运行两个 Tomcat 副本的最简单方法涉及以下步骤(我试图运行两个不同版本的 tomcat,6 和 7):

  • Establish 2 copies of tomcat in different folders (if they are different versions then this is easy, if they are the same version then you will need be distinguished in some other way. There are a lot of files that Tomcat creates to manage it so running two instances with the same work directory likely isn't possible)

  • Change the following ports that tomcat is listening to in server.xml

    • <Connector port="8080"><- This is the port that tomcat uses to respond to HTTP requests
    • <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /><- this defines two ports, one for the AJP connector (used if you are using tomcat behind an Apache or IIS server) and the port used for HTTPS traffic
    • <Server port="8005" shutdown="SHUTDOWN"><- this is the port that Tomcat uses to respond to SHUTDOWN events
  • 在不同的文件夹中建立2个tomcat的副本(如果是不同版本那么这很容易,如果它们是相同版本那么你需要通过其他方式区分。Tomcat创建了很多文件来管理它所以运行两个具有相同工作目录的实例可能是不可能的)

  • 更改 tomcat 正在侦听的以下端口 server.xml

    • <Connector port="8080"><- 这是tomcat用来响应HTTP请求的端口
    • <Connector port="8009" protocol="AJP/1.3" redirectPort="8443" /><- 这定义了两个端口,一个用于 AJP 连接器(如果您在 Apache 或 IIS 服务器后面使用 tomcat 时使用)和用于 HTTPS 流量的端口
    • <Server port="8005" shutdown="SHUTDOWN"><- 这是 Tomcat 用来响应 SHUTDOWN 事件的端口

Finally, if you are running this as a Windows service you will need to establish different service names for each instance (you can do this during setup, the default for Tomcat 7 is tomcat7). Once Tomcat is running all of it's configuration fields use relative paths so you don't need to touch CATALINA_BASE

最后,如果您将此作为 Windows 服务运行,您将需要为每个实例建立不同的服务名称(您可以在设置期间执行此操作,Tomcat 7 的默认值是 tomcat7)。一旦 Tomcat 运行,它的所有配置字段都使用相对路径,因此您无需触摸CATALINA_BASE

回答by Alpesh Gediya

If you have not configured Tomcat for multiple instances by setting a CATALINA_BASE directory, then $CATALINA_BASE will be set to the value of $CATALINA_HOME, the directory into which you have installed Tomcat.

如果您没有通过设置 CATALINA_BASE 目录为多个实例配置 Tomcat,则 $CATALINA_BASE 将设置为 $CATALINA_HOME 的值,即您安装 Tomcat 的目录。

回答by Kesavan Maharajan

The easiest way is download another tomcat 8 or 9 and install it while installing change all the port numbers. Copying the existing instance and changing the port number hardly in server.xml can cause some errors with services.

最简单的方法是下载另一个 tomcat 8 或 9 并安装它,同时安装更改所有端口号。复制现有实例并在 server.xml 中几乎不更改端口号可能会导致服务出现一些错误。

回答by Jammulaiah Pothuraju

1. 1st create one bat file(tomcat-start1.bat) and add the below code and keep   this bat file in any location.create instance(a folder having with webapps, config, bin folders) or give directly location in "newins" tomcat folder. and change the port numbers as 8181 in server.xml.
@echo off

set javabin=C:\Program Files\Java\jdk1.8.0_101\bin
set apachehom=E:\Apache Tomcat 8.0.15
set newins=E:\my_instance_1 (or)E:\Apache Tomcat 8.0.15

start "Tomcat 01" "%javabin%\java.exe" ^
-Djava.util.logging.config.file="%apachebas%\conf\logging.properties" ^
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager ^
-Djava.util.logging.config.file="%apachebas%\conf\logging.properties" ^
-Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager ^
-Djava.endorsed.dirs="%apachehom%\endorsed" ^
-classpath "%apachehom%\bin\bootstrap.jar;%apachehom%\bin\tomcat-juli.jar" ^
-Dcatalina.base="%newins%" ^
-Dcatalina.home="%apachehom%" ^
-Djava.io.tmpdir="%apachebas%\temp" ^
org.apache.catalina.startup.Bootstrap  start

exit

2. create java class and add this code and run
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

@Controller
@RequestMapping("/hello")
public class TomcatInstance {

    @RequestMapping(method = RequestMethod.GET)
    public void printHello(ModelMap model) {

        Runtime runtime = Runtime.getRuntime();
        try {
            Process p1 = runtime.exec("cmd /c start E:\Jammulaiah\Sample\tomcat-start1.bat");
            InputStream is = p1.getInputStream();
             int i = 0;
             StringBuffer sb=new StringBuffer();
             while ((i = is.read()) != -1) {
             sb.append((char) i);
             System.out.println(sb.toString());
             }
        } catch (IOException ioException) {
            System.out.println(ioException.getMessage());
        }

    }

}