WildFly Cookbook
上QQ阅读APP看书,第一时间看更新

Configuring multiple WildFly instances to run on the same machine with different IPs

In some cases, mostly due to architectural reasons, you may need to run multiple WildFly instances on a single server. You can do this by isolating each instance and giving it different binding IPs. The IPs might be virtual or effectively provided by the physical network interfaces installed on your system. Either way is good.

Getting ready

If you decide on virtual IPs, you can do this as follows:

$ sudo ifconfig YOUR_NIC:1 10.0.1.1 netmask 255.255.255.0
$ sudo ifconfig YOUR_NIC:2 10.0.1.2 netmask 255.255.255.0

Here YOUR_NIC might be eth0. Now, if you list all the available interfaces in your server, you will also see the following new ones:

$ ifconfig
eth0: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        ether f0:de:f1:99:b2:94  txqueuelen 1000  (Ethernet)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
        device interrupt 20  memory 0xf2600000-f2620000  
eth0:1: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.0.1.1  netmask 255.255.255.0  broadcast 10.0.1.255
        ether f0:de:f1:99:b2:94  txqueuelen 1000  (Ethernet)
        device interrupt 20  memory 0xf2600000-f2620000
eth0:2: flags=4099<UP,BROADCAST,MULTICAST>  mtu 1500
        inet 10.0.1.2  netmask 255.255.255.0  broadcast 10.0.1.255
        ether f0:de:f1:99:b2:94  txqueuelen 1000  (Ethernet)
        device interrupt 20  memory 0xf2600000-f2620000  

After that, we need to create a standalone configuration for each instance that we want to set up and run. If you have already done so from the previous recipe (Configuring multiple WildFly instances to run on the same machine with different ports), you can jump to the How to do it... section. Otherwise, all we need to do is replicate the concept explained in the Binding WildFly onto a custom IP recipe. Suppose we want two running nodes/instances, the command is as follows:

$ cd $WILDFLY_HOME
$ cp -a standalone node-1
$ cp -a standalone node-2

Now we are ready to configure each instance.

How to do it…

Open a terminal window and type in the following commands:

$ cd $WILDFLY_HOME
$ ./bin/standalone.sh -Djboss.server.base.dir=$WILDFLY_HOME/node-1 -Djboss.bind.address=10.0.1.1
…
22:32:34,259 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0006: Undertow HTTP listener default listening on /10.0.1.1:8080
22:32:34,549 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-3) WFLYDS0013: Started FileSystemDeploymentService for directory /home/luigi/WFC/wildfly/node-1/deployments
22:32:34,623 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
22:32:34,729 INFO  [org.jboss.ws.common.management] (MSC service thread 1-6) JBWS022052: Starting JBoss Web Services - Stack CXF Server 5.0.0.Beta3
22:32:35,022 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://127.0.0.1:9990/management
22:32:35,023 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://127.0.0.1:9990
22:32:35,024 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 9.0.0.Beta2 (WildFly Core 1.0.0.Beta2) started in 3260ms - Started 202 of 379 services (210 services are lazy, passive or on-demand)

Type the following in a different terminal window:

$ cd $WILDFLY_HOME
./bin/standalone.sh -Djboss.server.base.dir=$WILDFLY_HOME/node-2 -Djboss.bind.address=10.0.1.2
...
22:33:02,522 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-8) WFLYUT0006: Undertow HTTP listener default listening on /10.0.1.2:8080
22:33:02,735 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-8) WFLYDS0013: Started FileSystemDeploymentService for directory /home/luigi/WFC/wildfly/node-2/deployments
22:33:02,830 ERROR [org.jboss.msc.service.fail] (MSC service thread 1-1) MSC000001: Failed to start service jboss.serverManagement.controller.management.http: org.jboss.msc.service.StartException in service jboss.serverManagement.controller.management.http: WFLYSRV0083: Failed to start the http-interface service
 at org.jboss.as.server.mgmt.UndertowHttpManagementService.start(UndertowHttpManagementService.java:269)
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.startService(ServiceControllerImpl.java:1948)
 at org.jboss.msc.service.ServiceControllerImpl$StartTask.run(ServiceControllerImpl.java:1881)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
 at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.RuntimeException: java.net.BindException: Address already in use
 at org.jboss.as.domain.http.server.ManagementHttpServer.start(ManagementHttpServer.java:160)
 at org.jboss.as.server.mgmt.UndertowHttpManagementService.start(UndertowHttpManagementService.java:235)
 ... 5 more
Caused by: java.net.BindException: Address already in use
 at sun.nio.ch.Net.bind0(Native Method)
 at sun.nio.ch.Net.bind(Net.java:436)
 at sun.nio.ch.Net.bind(Net.java:428)
 at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:214)
 at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:74)
 at sun.nio.ch.ServerSocketAdaptor.bind(ServerSocketAdaptor.java:67)
 at org.xnio.nio.NioXnioWorker.createTcpConnectionServer(NioXnioWorker.java:182)
 at org.xnio.XnioWorker.createStreamConnectionServer(XnioWorker.java:243)
 at org.jboss.as.domain.http.server.ManagementHttpServer.start(ManagementHttpServer.java:147)
 ... 6 more
  
22:33:02,858 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-7) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
22:33:02,907 INFO  [org.jboss.ws.common.management] (MSC service thread 1-3) JBWS022052: Starting JBoss Web Services - Stack CXF Server 5.0.0.Beta3
22:33:02,911 ERROR [org.jboss.as.controller.management-operation] (Controller Boot Thread) WFLYCTL0013: Operation ("add") failed - address: ([
 ("core-service" => "management"),
 ("management-interface" => "http-interface")
]) - failure description: {"WFLYCTL0080: Failed services" => {"jboss.serverManagement.controller.management.http" => "org.jboss.msc.service.StartException in service jboss.serverManagement.controller.management.http: WFLYSRV0083: Failed to start the http-interface service
 Caused by: java.lang.RuntimeException: java.net.BindException: Address already in use
 Caused by: java.net.BindException: Address already in use"}}
22:33:02,969 INFO  [org.jboss.as.controller] (Controller Boot Thread) WFLYCTL0183: Service status report
WFLYCTL0186:   Services which failed to start:      service jboss.serverManagement.controller.management.http: org.jboss.msc.service.StartException in service jboss.serverManagement.controller.management.http: WFLYSRV0083: Failed to start the http-interface service

22:33:03,253 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0063: Http management interface is not enabled
22:33:03,254 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0054: Admin console is not enabled
22:33:03,254 ERROR [org.jboss.as] (Controller Boot Thread) WFLYSRV0026: WildFly Full 9.0.0.Beta2 (WildFly Core 1.0.0.Beta2) started (with errors) in 3277ms - Started 196 of 377 services (2 services failed or missing dependencies, 210 services are lazy, passive or on-demand)

Ouch! Another fail!

What's wrong now? Pretty much the same thing we had in the previous recipe. We didn't change any binding parameter (IP address or port number) for the management interface.

As a matter of fact, as you can see from the log of the node-1 instance, the management and HTTP interface are bound to the local address, which is the default one. Thus, the node-2 instance will have the same settings, hence the error: Address already in use.

Let's fix this error:

$ ./bin/standalone.sh -Djboss.server.base.dir=$WILDFLY_HOME/node-1 -Djboss.bind.address=10.0.1.1 -Djboss.bind.address.management=10.0.1.1
...
22:38:41,054 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-6) WFLYUT0006: Undertow HTTP listener default listening on /10.0.1.1:8080
22:38:41,313 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-6) WFLYDS0013: Started FileSystemDeploymentService for directory /home/luigi/WFC/wildfly/node-1/deployments
22:38:41,372 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-2) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
22:38:41,450 INFO  [org.jboss.ws.common.management] (MSC service thread 1-8) JBWS022052: Starting JBoss Web Services - Stack CXF Server 5.0.0.Beta3
22:38:41,792 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://10.0.1.1:9990/management
22:38:41,793 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://10.0.1.1:9990
22:38:41,794 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 9.0.0.Beta2 (WildFly Core 1.0.0.Beta2) started in 3224ms - Started 202 of 379 services (210 services are lazy, passive or on-demand)

Type the following in a different terminal window:

$ ./bin/standalone.sh -Djboss.server.base.dir=$WILDFLY_HOME/node-2 -Djboss.bind.address=10.0.1.2 -Djboss.bind.address.management=10.0.1.2
…
22:39:55,815 INFO  [org.wildfly.extension.undertow] (MSC service thread 1-1) WFLYUT0006: Undertow HTTP listener default listening on /10.0.1.2:8080
22:39:56,054 INFO  [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) WFLYDS0013: Started FileSystemDeploymentService for directory /home/luigi/WFC/wildfly/node-2/deployments
22:39:56,176 INFO  [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-8) WFLYJCA0001: Bound data source [java:jboss/datasources/ExampleDS]
22:39:56,241 INFO  [org.jboss.ws.common.management] (MSC service thread 1-7) JBWS022052: Starting JBoss Web Services - Stack CXF Server 5.0.0.Beta3
22:39:56,567 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0060: Http management interface listening on http://10.0.1.2:9990/management
22:39:56,567 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0051: Admin console listening on http://10.0.1.2:9990
22:39:56,568 INFO  [org.jboss.as] (Controller Boot Thread) WFLYSRV0025: WildFly Full 9.0.0.Beta2 (WildFly Core 1.0.0.Beta2) started in 3271ms - Started 202 of 379 services (210 services are lazy, passive or on-demand)

There you go! We have two WildFly instances running at the same time, each using a dedicated IP, even if they are both using the same PORT. Let's check it at OS level:

There's more…

As you have noticed in this recipe and in the previous one, we can customize the WildFly binding relative to the IP address and port number, both for the service components (which is your application) and the management components (which are the Admin Console and the CLI).

Obviously, we can even mix customization and thus change both the IP and the port, and change the port number for the service components and the IP address for the management interface. You can do whatever you want as long as you don't make equals binding.

A typical configuration has the services components bound to a public interface (that is, an IP address that's visible externally) and the management interface bound to a private interface (that is, an IP address that's only visible locally or within your network).