Linux

JBoss EAP 6.x como serviço no RHEL7: Systemd

Para aqueles que já estão utilizando o RHEL 7 (ou suas derivações: Centos, OEL, Scientific Linux) perceberam que os serviços não são mais controlados pelo mecanismo SysV (adicionados através do comando chkconfig). O RHEL 7 utiliza um novo mecanismo de gerência de serviços e inicialização do sistema chamado Systemd. Para mais detalhes ou um overview do Systemd veja as referências [1] e [2].

Abaixo descrevo um passo a passo de como adicionar o JBoss EAP 6 como serviço através do Systemd.

Crie um usuário chamado jboss.

[root@rhel7-server-1 bin]# adduser jboss
[root@rhel7-server-1 bin]# passwd jboss

Instale o JBoss EAP em um diretório de sua preferência. O script original sugere o diretório: /usr/share/jboss-as.

Eu particularmente costumo instalar em /opt.

mkdir /opt/redhat
unzip /tmp/jboss-eap-6.x.zip -d /opt/redhat
chown -R jboss.jboss /opt/redhat

Crie os seguintes diretórios que serão utilizados pelo script de inicialização.

[root@rhel7-server-1 bin]# mkdir /etc/jboss-as
[root@rhel7-server-1 bin]# mkdir /var/log/jboss-as
[root@rhel7-server-1 bin]# mkdir /var/run/jboss-as
[root@rhel7-server-1 bin]# chown jboss.jboss /etc/jboss-as
[root@rhel7-server-1 bin]# chown jboss.jboss /var/log/jboss-as
[root@rhel7-server-1 bin]# chown jboss.jboss /var/run/jboss-as

Copie e altere o arquivo de configuração auxiliar do script de init

[root@rhel7-server-1 bin]# cp init.d/jboss-as.conf /etc/jboss-as/
[root@rhel7-server-1 bin]# vim /etc/jboss-as/jboss-as.conf

Remova o comentário das variáveis e adiciene novas conforme destacado no trecho abaixo.

# General configuration for the init.d scripts,
# not necessarily for JBoss AS itself.

# Path to JBoss EAP Installation
JBOSS_HOME=/opt/redhat/jboss-eap-6.3

# The username who should own the process.
#
JBOSS_USER=jboss

# The amount of time to wait for startup
#
STARTUP_WAIT=30

# The amount of time to wait for shutdown
#
SHUTDOWN_WAIT=30

# Location to keep the console log
#
JBOSS_CONSOLE_LOG=/var/log/jboss-as/console.log

# JBoss configuration file
JBOSS_CONFIG=standalone.xml

# Public IP Address where JBoss will listen
JBOSS_PUB_BIND=192.168.122.65

# Management IP Address where JBoss will listen
JBOSS_MGMT_BIND=192.168.122.65

Faça a seguinte alteração no script de init original do JBoss EAP

vim $JBOSS_HOME/bin/init.d/jboss-as-standalone.sh
if [ -z "$JBOSS_PUB_BIND" ]; then
JBOSS_PUB_BIND=127.0.0.1
fi

if [ -z "$JBOSS_MGMT_BIND" ]; then
JBOSS_MGMT_BIND=172.0.0.1
fi

JBOSS_SCRIPT="$JBOSS_HOME/bin/standalone.sh -b $JBOSS_PUB_BIND -bmanagement=$JBOSS_MGMT_BIND"

Nota: por padrão o JBoss sobe na interface de loopback (localhost – 127.0.0.1). Essa alteração é necessária para informar/parametrizar os endereços onde as interfaces pública e de gerência devem ouvir conexões externas.

Caso tenha instalado a JVM OpenJDK (Default no REHL 7), edite o arquivo abaixo.
Nota: Caso o arquivo não exista, crie!

vim /etc/java/java.conf
# System-wide Java configuration file -*- sh -*-

# Location of jar files on the system
JAVA_LIBDIR=/usr/share/java

# Location of arch-specific jar files on the system
JNI_LIBDIR=/usr/lib/java

# Root of all JVM installations
JVM_ROOT=/usr/lib/jvm

# You can define a system-wide JVM root here if you're not using the
# default one.
#
# If you have a base JRE package installed
# (e.g. java-1.6.0-openjdk):
JAVA_HOME=$JVM_ROOT/jre
#
# If you have a devel JDK package installed
# (e.g. java-1.6.0-openjdk-devel):
#JAVA_HOME=$JVM_ROOT/java

# Options to pass to the java interpreter
#JAVACMD_OPTS=

Crie o script que será adicionado ao systemd com o seguinte conteúdo:

[jboss@rhel7-server-1 init.d]$ sudo vim /etc/systemd/system/jboss-as.service
[Unit]
Description=JBoss EAP Service
After=syslog.target network.target

[Service]
ExecStart=/opt/redhat/jboss-eap-6.3/bin/init.d/jboss-as-standalone.sh start
ExecStop=/opt/redhat/jboss-eap-6.3/bin/init.d/jboss-as-standalone.sh stop
Type=forking
PIDFile=/var/run/jboss-as/jboss-as-standalone.pid

[Install]
WantedBy=multi-user.target

Execute os comandos abaixo para habilitar o serviço no systemd:

[jboss@rhel7-server-1 init.d]$ sudo chmod 644 /etc/systemd/system/jboss-as.service

[jboss@rhel7-server-1 init.d]$ sudo systemctl enable jboss-as.service
ln -s '/etc/systemd/system/jboss-as.service' '/etc/systemd/system/multi-user.target.wants/jboss-as.service'

Inicie o serviço.

[jboss@rhel7-server-1 init.d]$ sudo systemctl start jboss-as.service

[jboss@rhel7-server-1 bin]$ sudo systemctl status jboss-as.service
jboss-as.service - JBoss EAP Service
Loaded: loaded (/etc/systemd/system/jboss-as.service; enabled)
Active: active (running) since Wed 2014-11-12 22:40:58 BRST; 8s ago
Main PID: 6969 (java)
CGroup: /system.slice/jboss-as.service
├─6877 /bin/sh /opt/redhat/jboss-eap-6.3/bin/init.d/jboss-as-standalone.sh start
├─6879 runuser -s /bin/bash jboss -c ulimit -S -c 0 >/dev/null 2>&1 ; LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=/var/run/jboss-as/jboss-as-standalone.pid /opt/redha...
├─6881 bash -c ulimit -S -c 0 >/dev/null 2>&1 ; LAUNCH_JBOSS_IN_BACKGROUND=1 JBOSS_PIDFILE=/var/run/jboss-as/jboss-as-standalone.pid /opt/redhat/jboss-eap-6.3/bin/st...
├─6882 /bin/sh /opt/redhat/jboss-eap-6.3/bin/standalone.sh -b 192.168.122.65 -bmanagement=192.168.122.65 -c standalone.xml
└─6969 java -D[Standalone] -server -XX:+UseCompressedOops -verbose:gc -Xloggc:/opt/redhat/jboss-eap-6.3/standalone/log/gc.log -XX:+PrintGCDetails -XX:+PrintGCDateSta...

Nov 12 22:40:58 rhel7-server-1 systemd[1]: Starting JBoss EAP Service...
Nov 12 22:40:58 rhel7-server-1 systemd[1]: Started JBoss EAP Service.
Nov 12 22:40:58 rhel7-server-1 runuser[6879]: pam_unix(runuser:session): session opened for user jboss by (uid=0)
Nov 12 22:41:02 rhel7-server-1 jboss-as-standalone.sh[6869]: Starting jboss-as: [ OK ]

Verifique o log do serviço:

[jboss@rhel7-server-1 init.d]$ tail -f /var/log/jboss-as/console.log

22:13:13,010 INFO [org.jboss.ws.common.management] (MSC service thread 1-2) JBWS022052: Starting JBoss Web Services - Stack CXF Server 4.3.0.Final-redhat-3
22:13:13,033 INFO [org.apache.coyote.http11.Http11Protocol] (MSC service thread 1-1) JBWEB003000: Coyote HTTP/1.1 starting on: http-/127.0.0.1:8080
22:13:13,081 INFO [org.jboss.as.server.deployment.scanner] (MSC service thread 1-4) JBAS015012: Started FileSystemDeploymentService for directory /opt/redhat/jboss-eap-6.3/standalone/deployments
22:13:13,084 INFO [org.jboss.as.remoting] (MSC service thread 1-4) JBAS017100: Listening on 127.0.0.1:4447
22:13:13,084 INFO [org.jboss.as.remoting] (MSC service thread 1-2) JBAS017100: Listening on 127.0.0.1:9999
22:13:13,338 INFO [org.jboss.as.connector.subsystems.datasources] (MSC service thread 1-4) JBAS010400: Bound data source [java:jboss/datasources/ExampleDS]
22:13:13,465 INFO [org.jboss.as] (Controller Boot Thread) JBAS015961: Http management interface listening on http://127.0.0.1:9990/management
22:13:13,465 INFO [org.jboss.as] (Controller Boot Thread) JBAS015951: Admin console listening on http://127.0.0.1:9990
22:13:13,466 INFO [org.jboss.as] (Controller Boot Thread) JBAS015874: JBoss EAP 6.3.0.GA (AS 7.4.0.Final-redhat-19) started in 3564ms - Started 151 of 189 services (56 services are lazy, passive or on-demand)

Veja o arquivo de PID do processo do serviço

[jboss@rhel7-server-1 init.d]$ cat /var/run/jboss-as/jboss-as-standalone.pid
5615

[jboss@rhel7-server-1 init.d]$ pstree -U -h -l -s 5615
systemd───jboss-as-standa───runuser───bash───standalone.sh───java───34*[{java}]
[jboss@rhel7-server-1 init.d]$

[1] https://access.redhat.com/articles/754933
[2] https://access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/7/html/System_Administrators_Guide/chap-Managing_Services_with_systemd.html