第 2 章 启动和停止 Apache Karaf

摘要

Apache Karaf 为启动和停止服务器提供了简单的命令行工具。

2.1. 启动 Apache Karaf

部署 Apache Karaf 运行时的默认方法是使用活动控制台将它部署为单机服务器。您还可以将运行时部署为没有控制台的后台进程。

2.1.1. 设置您的环境

您可以直接从安装的 bin 子目录启动 Karaf 运行时,而无需修改您的环境。但是,如果您要在其他文件夹中启动它,您需要将 Karaf 安装的 bin 目录添加到 PATH 环境变量中,如下所示:

Windows

set PATH=%PATH%;InstallDir\bin

Linux/UNIX

export PATH=$PATH,InstallDir/bin`

2.1.2. 在控制台模式中启动运行时

如果您要从安装目录启动 Karaf 运行时,请使用以下命令:

Windows

bin\fuse.bat

Linux/UNIX

./bin/fuse

如果 Karaf 正确启动,您应该在控制台中看到以下内容:

Red Hat Fuse starting up. Press Enter to open the shell now...
100% [========================================================================]

Karaf started in 8s. Bundle stats: 220 active, 220 total

 ____          _   _   _       _     _____
|  _ \ ___  __| | | | | | __ _| |_  |  ___|   _ ___  ___
| |_) / _ \/ _` | | |_| |/ _` | __| | |_ | | | / __|/ _ \
|  _ <  __/ (_| | |  _  | (_| | |_  |  _|| |_| \__ \  __/
|_| \_\___|\__,_| |_| |_|\__,_|\__| |_|   \__,_|___/___|

  Fuse (7.x.x.fuse-xxxxxx-redhat-xxxxx)
  http://www.redhat.com/products/jbossenterprisemiddleware/fuse/

Hit '<tab>' for a list of available commands
and '[cmd] --help' for help on a specific command.

Open a browser to http://localhost:8181/hawtio to access the management console

Hit '<ctrl-d>' or 'shutdown' to shutdown Red Hat Fuse.

karaf@root()>
注意

自版本 Fuse 6.2.1 起,在控制台模式中启动会创建两个进程:父进程 ./bin/karaf,该进程正在执行 Karaf 控制台;而子进程(在 java JVM 中执行 Karaf 服务器)。但是,关闭过程与之前相同。也就是说,您可以使用 Ctrl-D 或 osgi:shutdown 从控制台关闭服务器,这会终止这两个进程。

2.1.3. 在服务器模式中启动运行时

在服务器模式中启动 会在后台运行 Apache Karaf,无需本地控制台。然后,您将使用远程控制台连接到正在运行的实例。详情请查看 第 16.2 节 “远程连接和断开连接”

若要在服务器模式中启动 Karaf,请运行以下命令

Windows

bin\start.bat

Linux/UNIX

./bin/start

2.1.4. 在客户端模式中启动运行时

在生产环境中,您可能想让运行时实例只能通过本地控制台访问。换句话说,您无法通过 SSH 控制台端口远程连接到运行时。您可以通过在客户端模式中启动运行时来完成此操作:

Windows

bin\fuse.bat client

Linux/UNIX

./bin/fuse client
注意

在客户端模式中启动只阻止 SSH 控制台端口(通常是端口 8101)。其他 Karaf 服务器端口(例如,JMX 管理 RMI 端口)正常打开。

2.1.5. 在调试模式下运行 Fuse

在调试模式下运行 Fuse 有助于更有效地识别和解决错误。默认禁用这个选项。启用后,Fuse 在端口 5005 上启动 JDWP 套接字。

您有三种 在调试模式下运行 Fuse 的方法。

2.1.5.1. 使用 Karaf 环境变量

这个方法启用了 KARAF_DEBUG 环境变量(=1),然后启动容器。

$ export KARAF_DEBUG=1
$ bin/start

2.1.5.2. 运行 Fuse debug

这个方法运行 debug,其中 suspend 选项被设置为 n (no)。

$ bin/fuse debug

2.1.5.3. 运行 Fuse debug

这个方法运行 debug,其中 suspend 选项被设置为 y (是)。

注意

suspend 设为 yes 会导致 JVM 仅在运行 main () 之前暂停,直到调试器附加为止,然后它恢复执行。

$ bin/fuse debugs