这是用户在 2024-3-28 21:58 为 https://camel.apache.org/manual/camel-console.html 保存的双语快照页面,由 沉浸式翻译 提供双语支持。了解如何保存?

 小骆驼控制台


camel-console 在 Camel 3.15 及更新版本中可用。


Camel 开发者控制台旨在帮助开发者,可以显示运行中的 Camel 应用程序的各种信息。这在开发和测试过程中非常方便。不过,不建议将 Camel 开发者控制台用于生产。


骆驼从 camel-consolecamel-catalog-console 中获得了一套控制台。JAR。这些控制台可以显示运行中的 JVM 和操作系统环境的一般信息,当然还有与 Camel 相关的信息,如 Camel 路由的运行时指标等。

 使用骆驼控制台


必须将 camel-console 添加到 classpath 中,并通过

CamelContext context = ...
context.setDevConsole(true);


如果使用 Camel Main / Spring Boot / Quarkus 等,则可通过配置启用控制台:

camel.main.dev-console-enabled = true


开发控制台和骆驼弹簧引导


在 Spring Boot 中,Camel 开发者控制台作为执行器可用。要启用控制台,需要添加依赖关系:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-console-starter</artifactId>
</dependency>


要包含路由指标等更多细节,就需要包含 JMX 管理:

<dependency>
    <groupId>org.apache.camel.springboot</groupId>
    <artifactId>camel-management-starter</artifactId>
</dependency>


最后,您必须在 application.properties 中的 HTTP 执行器端点暴露列表中启用 camel ,如图所示:

management.endpoints.web.exposure.include=info,health,camel


然后,控制台可通过 HTTP(使用默认端口)访问:

http://localhost:8080/actuator/camel


这将列出可用的控制台,然后您就可以通过 ID 调用控制台,例如 routes

http://localhost:8080/actuator/camel/routes


开发控制台和骆驼 Jbang


使用 Camel JBang 时,可以通过运行 Camel JBang 时的 --console 参数轻松获得开发者控制面板。


例如,要从 foo.yaml 运行 Camel 路由,并从 myapp.properties 运行附加配置,可以按如下方式运行,并从 http://localhost:8080/q/dev 启动和访问控制台

$ camel run foo.yaml myapp.properties --console


编写定制开发控制台


要编写自定义控制台,需要将 camel-console 添加为依赖关系,因为它自带基类 AbstractDevConsole ,我们可以扩展该基类来编写控制台。

@DevConsole("foo")
public class FooConsole extends AbstractDevConsole {

    public FooConsole() {
        super("acme", "foo", "Foolish", "A foolish console that outputs something");
    }

    @Override
    protected String doCallText(Map<String, Object> options) {
        StringBuilder sb = new StringBuilder();
        sb.append("Hello from my custom console");

        // more stuff here

        return sb.toString();
    }

    @Override
    protected JsonObject doCallJson(Map<String, Object> options) {
        JsonObject root = new JsonObject();
        root.put("message", "Hello from my custom console");

        // more stuff here

        return root;
    }

}


该类必须使用 DevConsole 和控制台的唯一 ID(在所有控制台中必须唯一)进行注解。在构造函数中,控制台指定要使用的组、ID、显示标题和描述。


doCallTextdoCallJson 方法负责收集控制台应输出的信息。


如果控制台不支持文本或 json 输出,则这些方法可以返回 null 。并覆盖 supportMediaType 方法,返回 true 支持的媒体类型。

 支持的媒体类型


控制台可支持以下任何一种或所有类型:

  •  文本

  • JSON


TEXT 的目的是基于纯文本,可在 CLI 和其他底层工具中输出。


对于 JSON ,控制台的意图是输出一个包含键/值对的 json 数据集,该数据集可以自定义方式显示,例如在网络浏览器或 VSCode 等集成开发环境工具中显示。

 maven 配置


要使 Camel 能够发现自定义开发控制台,就必须使用 came-component-maven-plugin 插件,例如

<build>
    <plugins>
        <plugin>
            <groupId>org.apache.camel</groupId>
            <artifactId>camel-component-maven-plugin</artifactId>
            <version>${camel-version}</version>
            <executions>
                <execution>
                    <id>generate</id>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <phase>process-classes</phase>
                </execution>
                <execution>
                    <id>generate-postcompile</id>
                    <goals>
                        <goal>generate-postcompile</goal>
                    </goals>
                    <phase>prepare-package</phase>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>build-helper-maven-plugin</artifactId>
            <executions>
                <execution>
                    <phase>generate-sources</phase>
                    <goals>
                        <goal>add-source</goal>
                        <goal>add-resource</goal>
                    </goals>
                    <configuration>
                        <sources>
                            <source>src/generated/java</source>
                        </sources>
                        <resources>
                            <resource>
                                <directory>src/generated/resources</directory>
                            </resource>
                        </resources>
                    </configuration>
                </execution>
            </executions>
        </plugin>
    </plugins>
</build>