# 16.5 WildFly 应用服务器

## WildFly 概述

WildFly（原 JBoss Application Server）是由 Red Hat 公司开发的开源 Java EE（Java Platform, Enterprise Edition）应用服务器，为企业级 Java 应用提供运行时环境与服务。其轻量级架构与模块化设计使其在企业应用部署中具有广泛应用。WildFly 支持两种运行模式：standalone（独立模式）适用于单服务器部署，domain（域模式）适用于多服务器集中管理。

## 安装 WildFly

本节介绍如何安装 WildFly 应用服务器。

> **技巧**
>
> 可以通过 Ports 获取 WildFly 历史版本，但存在 Bug [java/wildfly: service start fail, illegal group name](https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=285956)，该 Bug 报告了 WildFly 服务启动时出现非法组名的问题，历史版本需要自行应用补丁。

安装 WildFly 有两种主要方式：

* 使用 pkg 安装：

```sh
# pkg install wildfly
```

* 使用 ports 安装：

```sh
# cd /usr/ports/java/wildfly/ 
# make install clean
```

## 查看 WildFly 安装后说明

安装完成后，可查看安装包提供的说明信息，了解后续配置步骤。

```sh
# wildfly-35.0.1:
# 安装说明如下：

# On install:
# 安装时：

# To make WildFly bind to all interfaces add this to rc.conf:
# 若要使 WildFly 绑定到所有网络接口，在 rc.conf 中添加以下内容：

# wildfly_args="-Djboss.bind.address=0.0.0.0"

# See
# 参见

# https://community.jboss.org/wiki/JBossProperties

# for additional startup properties.
# 以获取更多启动参数说明。

# To change JVM args, edit appropriate standalone.conf.
# 若要修改 JVM 参数，请编辑相应的 standalone.conf 文件。

# To add the initial admin user:
# 若要添加初始管理员用户：

# /usr/local/wildfly/bin/add-user.sh
```

## 相关文件结构

```sh
/
├── usr
│   └── local
│       └── wildfly
│           ├── bin
│           │   └── add-user.sh                # WildFly 用户添加脚本
│           ├── standalone
│           │   └── configuration
│           │       ├── mgmt-users.properties # 独立模式管理用户配置
│           │       └── mgmt-groups.properties# 独立模式管理组配置
│           └── domain
│               └── configuration
│                   ├── mgmt-users.properties # 域模式管理用户配置
│                   └── mgmt-groups.properties# 域模式管理组配置
└── var
    └── log
        └── wildfly
            ├── error                             # WildFly 错误日志
            └── log                               # WildFly 普通日志
```

## 配置 WildFly

安装完成后，需要进行基本配置，才能正常使用。

### 服务启动配置

配置 WildFly 服务并设置绑定地址：

```sh
# service wildfly enable   # 设置 WildFly 服务在系统启动时自动启动
# sysrc wildfly_args="-Djboss.bind.address=0.0.0.0 -Djboss.bind.address.management=0.0.0.0"   # 配置 WildFly 绑定到所有网络接口
```

### 服务启动与验证

启动 WildFly 服务：

```sh
# service wildfly start
```

* 打开 `http://127.0.0.1:8080`（局域网内其他设备亦可访问）即可检验服务状态。

![WildFly 服务状态](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-03a5cb4f2e4e41b35c0c8b939e4291cfbdd05781%2Fwildfly1.png?alt=media)

* 执行 WildFly 用户添加脚本 `/usr/local/wildfly/bin/add-user.sh`，来生成管理员账户。

```sh
# /usr/local/wildfly/bin/add-user.sh

What type of user do you wish to add?
 a) Management User (mgmt-users.properties)
 b) Application User (application-users.properties)
(a): # 直接按回车键选择默认选项
# 选项 a 为管理员账户，选项 b 为应用用户

Enter the details of the new user to add.
Using realm 'ManagementRealm' as discovered from the existing property files.
Username : test # 输入要创建的用户名
Password recommendations are listed below. To modify these restrictions edit the add-user.properties configuration file.
 - The password should be different from the username 
# 密码不能与用户名相同
 - The password should not be one of the following restricted values {root, admin, administrator} 
# 密码不能是 root、admin 或 administrator
 - The password should contain at least 8 characters, 1 alphabetic character(s), 1 digit(s), 1 non-alphanumeric symbol(s)
# 密码应至少包含 8 个字符，并至少包含 1 个字母、1 个数字及 1 个非字母数字字符
Password : # 输入为新用户 test 设置的密码，要求见上文
Re-enter Password : # 重复输入密码
What groups do you want this user to belong to? (Please enter a comma separated list, or leave blank for none)[  ]: # 直接按回车键留空不加入任何用户组
# 你希望该用户属于哪些用户组？（请输入以逗号分隔的列表，或留空表示不加入任何用户组）[  ]：
About to add user 'test' for realm 'ManagementRealm'
# 即将为域 'ManagementRealm' 添加用户 'test'。
Is this correct yes/no? yes # 确认创建
Added user 'test' to file '/usr/local/wildfly/standalone/configuration/mgmt-users.properties'
Added user 'test' to file '/usr/local/wildfly/domain/configuration/mgmt-users.properties'
Added user 'test' with groups  to file '/usr/local/wildfly/standalone/configuration/mgmt-groups.properties'
Added user 'test' with groups  to file '/usr/local/wildfly/domain/configuration/mgmt-groups.properties'
```

* 打开 `http://localhost:9990`（对应的局域网也可以连接）即可登录管理界面。

![WildFly 管理界面](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-6a6ba60a27841b281d868fef0aaac12011194d5f%2Fwildfly2.png?alt=media)

![WildFly 管理界面](https://338876981-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCJR3FQGH1PkdRtOljuxb%2Fuploads%2Fgit-blob-7e3dc1a8dc78e921555ac463461e50b9c63ff7ed%2Fwildfly3.png?alt=media)

## 故障排除与未竟事宜

如果服务无法启动，可通过 `/var/log/wildfly/error` 和 `/var/log/wildfly/log` 查看错误日志。

## 课后习题

1. 在 FreeBSD 上通过 ports 安装历史版本 WildFly 并修复其服务启动 bug，部署一个简单的 Java Web 应用并验证其可正常访问。
2. 分析 WildFly 的模块化架构设计，查看其类加载机制与服务配置方式。
3. 修改 WildFly 的默认 JVM 内存配置，调整堆内存大小并设置 GC 策略，验证服务启动与应用性能变化。
