Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
452 views
in Technique[技术] by (71.8m points)

java - How to run different apps on single JBoss AS 6 instance behind different ports?

I am coming from this SO however my case is not on Tomcat, but JBoss EAP 6. So suppose I have two web apps app1 and app2 running on JBoss AS 6:

  • app1 on http://localhost:8080/app1
  • app2 on http://localhost:8080/app2

However I want to configure Tomcat so that they run in root context behind separate ports:

  • app1 on http://localhost:8081
  • app2 on http://localhost:8082

How can I make it on JBoss EAP 6? Note this answer doesn't work for me as it targets JBoss 5.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: These instructions are for JBoss AS6 as requested in the original question. AS7 has different configuration file syntax.

Your problem has two parts:

  1. Make JBoss listen on multiple ports
  2. Dispatch requests to 8081 to app1 and 8082 to app2

Getting JBoss to listen on multiple ports

This one is easy.

Add lines like these to $JBOSS_HOME/server/default/deploy/jbossweb.sar/server.xml

<!-- A HTTP/1.1 Connector on port 8081 -->
<Connector protocol="HTTP/1.1" port="8081" address="${jboss.bind.address}" 
   redirectPort="${jboss.web.https.port}" />

<!-- A HTTP/1.1 Connector on port 8082 -->
<Connector protocol="HTTP/1.1" port="8082" address="${jboss.bind.address}" 
   redirectPort="${jboss.web.https.port}" />

Observe the following messages in the log when server boots up:

11:56:23,639 INFO  [org.apache.coyote.http11.Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8081
11:56:23,640 INFO  [org.apache.coyote.http11.Http11Protocol] Starting Coyote HTTP/1.1 on http-127.0.0.1-8082

Note: If you want to do it "properly" you should use placeholders instead of hardcoded numbers and edit $JBOSS_HOME/server/default/conf/bindingservice.beans/META-INF/bindings-jboss-beans.xml to define them. But, unless you need to manage ports via the management UI, it will be an overkill.

Dispatch requests to port 8081 to app1 and port 8082 to app2

This is much harder. JBoss uses its own Tomcat engine that does not support multiple webapp roots (appBase attribute does not work). Thus it is impossible to configure two different directories for your connectors. It is possible to add virtual hosts and use jboss-web.xml in each app to configure which vhost it responds to, but that means your have to use different names in client URL-s.

You have two options here.

Option 1: JBoss RewriteValve

Add this to Host configuration element (before other valve definitions) in $JBOSS_HOME/server/default/deploy/jbossweb.sar/server.xml

 <Valve className="org.jboss.web.rewrite.RewriteValve" />

Create a file $JBOSS_HOME/server/default/conf/jboss.web/localhost/rewrite.properties with the following contents:

RewriteCond %{SERVER_PORT}  =8081
RewriteRule ^/(.*)$  /app1/$1 [L]

RewriteCond %{SERVER_PORT}  =8082
RewriteRule ^/(.*)$  /app2/$1 [L]

Note: You may need to create the $JBOSS_HOME/server/default/conf/jboss.web/localhost/ directory, it does not exist by default.

Note2: Location of the rewrite.properties depends on the placement of the Valve tag in server.xml. The most intuitive placement is with other Valve elements. However it is valid directly under Engine as well. In this case rewrite.properties file needs to be moved up one directory.

Option 2: Servlet filter in ROOT context

Deploy a servlet filter to $JBOSS_HOME/server/default/deploy/ROOT.war/ that dispatches requests based on incoming port. You can either roll out your own custom filter implementation or use UrlRewriteFilter with a configuration that looks like this:

<rule>
  <condition type="port">8081</condition>
  <from>/(.*)</from>
  <to context="app1">/$1</to>
</rule>

<rule>
  <condition type="port">8082</condition>
  <from>/(.*)</from>
  <to context="app2">/$1</to>
</rule>

See also:

EDIT: Given the complexity of JBoss configuration you may also opt for an Apache based reverse proxy that sits in front of the app server.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...