尧图网站设计 尧图网站设计YAOTU DESIGN
ARTICLE DETAIL

资讯详情

深耕网站设计与一线实操的经验洞察。

Camel in Action部署指南:Standalone、Spring Boot、WAR、OSGi 6种启动方式全解析

Camel in Action部署指南:Standalone、Spring Boot、WAR、OSGi 6种启动方式全解析 Camel in Action部署指南Standalone、Spring Boot、WAR、OSGi 6种启动方式全解析【免费下载链接】camelinaction2:camel: This project hosts the source code for the examples of the Camel in Action 2nd ed book :closed_book: written by Claus Ibsen and Jonathan Anstey.项目地址: https://gitcode.com/gh_mirrors/ca/camelinaction2Apache Camel 是轻量级企业集成模式EIP框架而部署 Apache Camel 应用是新手最常卡住的环节。本文基于开源项目Camel in Action 2nd EditionCamel in Action 第2版官方示例代码库结合第 15 章 “Running and deploying Camel” 的实战示例完整解析Standalone 独立启动、Spring Boot、WAR 部署、WildFly-Camel、OSGi/Karaf、OSGi MSF共 6 种 Camel 部署启动方式并附路由启动顺序与优雅停机技巧帮你快速选对最合适的运行容器。一、快速上手获取示例代码所有示例都放在仓库的chapter15模块下先克隆代码git clone https://gitcode.com/gh_mirrors/ca/camelinaction2每个子模块都是独立的 Maven 工程用mvn install或mvn test即可运行。章节总说明见 chapter15/README.md路径chapter15/README.md。二、6 种 Camel 部署方式速览#部署方式适用场景示例模块1Standalone 独立启动学习、轻量脚本、无需容器chapter15/standalone2Spring Boot微服务、云原生、快速起步参考chapter7/springboot-camel3WARTomcat/Jetty传统 Web 容器部署chapter15/war4WildFly-Camel skinny WARJBoss/WildFly 全家桶chapter15/wildfly-camel-war5OSGiKaraf Bundle企业级 OSGi 容器chapter15/osgi6OSGi MSF 配置驱动同一 Bundle 多路由实例chapter15/osgi-msf三、方式 1Standalone 独立启动最简单不想启动任何容器一个main方法就够。核心思路是使用camel-spring提供的Main类加载 Spring XML 上下文Main main new Main(); main.setApplicationContextUri(META-INF/spring/camel-context.xml); main.run(); // 阻塞运行CtrlC 停止完整代码见chapter15/standalone/src/main/java/camelinaction/InventoryMain.java运行命令cd chapter15/standalone mvn compile exec:java✅ 适合快速验证路由逻辑、教学演示、嵌入式场景。四、方式 2Spring Boot 启动Spring Boot 是当前最流行的 Camel 运行方式一个SpringBootApplication类 CamelConfiguration注册 Bean内嵌 Tomcatmvn spring-boot:run即起。依赖camel-spring-boot-starter无需手写main中的启动/关闭逻辑配置项统一放application.properties仓库中的 Spring Boot 实战示例可参考chapter7/springboot-camel与chapter7/springboot-rest-camel含 REST Camel 组合✅ 适合微服务架构、Docker/K8s 部署见chapter18的 Swarm/Kubernetes 示例。五、方式 3WAR 部署到 Tomcat / Jetty把 Camel 打包成标准 WAR 扔进 Servlet 容器适合已有 Web 基础设施的团队。chapter15/war打包 WARmvn install产物在target目录部署到 Apache Tomcat / Jetty或本地直接调试cd chapter15/war mvn jetty:run关键配置文件chapter15/war/src/main/webapp/WEB-INF/下web.xmlWeb 容器入口applicationContext.xmlSpring 上下文camel-cxf.xmlCamel cxf 路由定义进阶变体chapter15/war-servlet演示用CamelServlet以编程方式引导 Camel适合不希望被web.xml硬编码的场景。六、方式 4WildFly-Camel skinny WARWildFly-Camel 子系统会管理所有 Camel 依赖因此 WAR 只需打“骨架包”skinny WAR体积小、启动快cd chapter15/wildfly-camel-war mvn install -Pdeploy # 打包并自动部署到本地 WildFly示例源码在chapter15/wildfly-camel-war/src/main/java/camelinaction/。✅ 适合全量使用 JBoss/WildFly 技术栈的企业环境。七、方式 5OSGi 部署到 Apache KarafCamel 与 OSGi 天然契合。把工程打成 OSGi Bundle装进 Karaf 即可运行打包cd chapter15/osgi mvn install启动 Karaf4.1.2加载 Camel featurefeature:repo-add camel 2.20.1 feature:install http camel camel-cxf安装并启动 Bundlebundle:install -s mvn:com.camelinaction/riderautoparts-osgi/2.0.0✅ 适合模块化企业应用、需要动态安装/卸载路由的场景。八、方式 6OSGi MSFManaged Service Factory配置驱动这是 OSGi 的进阶玩法同一个 Bundle 通过配置文件动态创建多个路由实例无需写死路由。打包cd chapter15/osgi-msf mvn install安装 Bundle 同上在 Karaf 的etc目录放置配置文件例如camelinaction.fileinventoryroutefactory-path1.cfg内容只需一行pathfile://target/inventory/updates想新增一个路由实例再加一个.cfg文件即可路由自动拉起仓库已备好示例配置chapter15/osgi-msf/etc/camelinaction.fileinventoryroutefactory-path1.cfg及 path2 版本。✅ 适合同一路由模板、多数据源/多目录批量部署。九、加分项启动顺序与优雅停机路由启动顺序chapter15/startup演示用startupOrder指定路由启动先后还能在路由中手动 stop 另一条路由优雅停机Graceful Shutdownchapter15/shutdown演示gracefulShutdown如何让在途消息处理完再退出避免丢消息。测试可直接运行cd chapter15/shutdown mvn test -DtestGracefulShutdownBigFileTest十、如何选择一张表总结你的场景推荐方式学习 / 快速验证Standalone 独立启动微服务、Docker、K8sSpring Boot已有 Tomcat/Jetty 环境WAR 部署全 WildFly 技术栈WildFly-Camel skinny WAR企业级模块化、动态装卸OSGiKaraf批量多实例路由OSGi MSF以上示例代码均出自 Camel in Action 第2版官方示例库chapter15目录配合 Maven 即可复现全部 6 种部署方式。从 Standalone 起步逐步过渡到 Spring Boot 或 OSGi就是新手掌握Camel 部署的最佳路线。【免费下载链接】camelinaction2:camel: This project hosts the source code for the examples of the Camel in Action 2nd ed book :closed_book: written by Claus Ibsen and Jonathan Anstey.项目地址: https://gitcode.com/gh_mirrors/ca/camelinaction2创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考
返回列表