Apache Camel K 介绍

8 篇文章 0 订阅
2 篇文章 1 订阅

Apache Camel K

Camel 是什么

GitHub: https://github.com/apache/camel

Apache Camel 是一款开源集成框架,通过这一框架能够快速的集成各类数据格式不同的生产或消费数据的系统。

  • Camel 对大部分 EIP(Enterprise Integration Pattren,企业集成模式)提供了相关的实现
  • Camel 包含了数百种组件,能够访问各种数据库、消息中间件、API 等,几乎支持任何通信方式
  • Camel 支持大约 50 种数据格式,能够将数据转换成多种不同的格式,涵盖了金融、电信、医疗保险等行业标准格式的支持
  • Camel 能够独立运行,也能嵌入已有的系统中,或者基于微服务架构,甚至以 Serverless 的方式运行

示例:每 30 秒发送一条 Camel K RocketMQ Sample 到指定的 RocketMQ ,并输出日志。

import org.apache.camel.builder.RouteBuilder;

public class SampleRocketMQ extends RouteBuilder {
  @Override
  public void configure() throws Exception {
	  from("timer:send?period=30000")
	      .process(exchange -> {
	          exchange.getIn().setBody("Camel K RocketMQ Sample".getBytes());
	      })
          .to("rocketmq:camel_k_sample?namesrvAddr=rpi3.lo:9876&producerGroup=camel_k_producer")
          .to("log:camel_k_sample_rocketmq?showAll=true")
	  ;
  }
}

Camel K 是什么

GitHub: https://github.com/apache/camel-k

Apache Camel K 是基于 Apache Camel 构建,运行在 Kubernetes 上的 Serverless 架构的轻量级集成平台。

用户可以立即在部署好的 Camel K Platform 上运行 Camel DSL 编写的集成代码。

为什么需要 Camel K

FaaS ,让用户集中于核心业务逻辑

用户只需专注于集成逻辑的开发,无需再考虑 Gradle 脚本编写、SpringBoot 版本选择等琐碎的事情。

充分利用 Kubernetes 的特性

故障迁移、资源调度、资源隔离……

自动选择合适的资源

Camel K 能够根据运行的集成代码选择合适的 K8S 资源,例如:

  • 当集成代码中的 timer 周期不小于 60s 的时候,Camel K 会部署一个 Cron schedule,而不是一直运行一个 Pod
  • 当检测到集成代码包含 HTTP 消费者时,会自动暴露端口并创建对应的 Service
  • ……

Camel K 与 Camel (SpringBoot) 对比

Camel KCamel (SpringBoot)
项目结构主要包含集成代码文件、集成代码单元测试的 Java 工程完整的 (SpringBoot) Java 工程
部署流程上传集成代码文件 或 指定 URL编译、jar、(构建镜像)部署
部署时间秒级分钟级
启动时间秒级1 分钟左右
资源占用相对较低,可根据集成类型分配资源相对较高,持续占用

Camel K 相关概念

Platform

Camel K 平台实例,本质上是一个 Operator。

camel-k-operator 的职责:

  • Kit 管理,包括构建、查询、删除等
  • Integration 管理,包括创建、删除、查询、更新

camel-k CRD:

NAME                                       SHORTNAMES   APIGROUP                       NAMESPACED   KIND
builds                                                  camel.apache.org               true         Build
camelcatalogs                              cc           camel.apache.org               true         CamelCatalog
integrationkits                            ik           camel.apache.org               true         IntegrationKit
integrationplatforms                       ip           camel.apache.org               true         IntegrationPlatform
integrations                               it           camel.apache.org               true         Integration

Kit

Kit 对应 Docker image,是运行 Camel 的基础镜像。
包含一个 Camel K Runtime 运行环境及第三方依赖,不包含集成代码。

默认的 Kit 仅包含了 Camel 的核心文件,只能使用一些基本的组件,所以需要根据集成代码的依赖构建各种 Kit。

如果有集成代码依赖了 RocketMQ,则可以构建一个名为 kit-rocketmq 的 Kit,
提供给使用了 RocketMQ 的集成代码在 Camel K 平台上运行。

Integration

Integration 对应 Deployment(或 Cron Schedule 等其他资源),所用的 image 就是上一节提到的 Kit,
一组集成代码文件被挂载到 Pod 中,经过 Camel K Runtime 动态编译运行。

kamel

Camel K 的 CLI 客户端。

主要功能:

  • Camel K Platform 管理(安装、卸载、重置)
  • 运行集成代码
  • Kit 管理
  • Integration 管理
Apache Camel K is a lightweight integration platform, born on Kubernetes, with serverless
superpowers.

Usage:
  kamel [command]

Available Commands:
  completion  Generates completion scripts
  delete      Delete integrations deployed on Kubernetes
  describe    Describe a resource
  get         Get integrations deployed on Kubernetes
  help        Help about any command
  init        Initialize empty Camel K files
  install     Installs Camel K on a Kubernetes cluster
  kit         Configure an Integration Kit
  log         Print the logs of an integration
  rebuild     Clear the state of integrations to rebuild them
  reset       Reset the Camel K installation
  run         Run a integration on Kubernetes
  uninstall   Uninstall Camel K from a Kubernetes cluster
  version     Display client version

Flags:
      --config string      Path to the config file to use for CLI requests
  -h, --help               help for kamel
  -n, --namespace string   Namespace to use for all operations

Use "kamel [command] --help" for more information about a command.

Camel K 运行机制

执行 kamel run Sample.java 运行集成代码后
Camel K 运行机制

Camel K Platform 部署

部署环境需求

Kubernetes

运行 Camel K 的基础

Registry

Camel K Operator 在构建 Kit 后,会将 Kit 对应的 Docker image 推到 Registry 上备用

源码构建环境需求

由于上一版本 camel-k:1.0.0-RC2 存在一些问题,新的 Release 尚未发布,
开发者在 Issue 中建议从 master 分支构建 camel-k。

构建源码需要以下环境:

  • Golang 1.13+
  • Operator SDK v0.9.0+ 用于构建 Operator 和 Docker 镜像,GitHub地址:https://github.com/operator-framework/operator-sdk ,Release 页面有直接可用的二进制可执行文件下载
  • GNU Make
  • JDK 1.8+ 用于构建 Camel 项目本身,目前 master 分支可以使用 Java 11
  • Docker 17.03+
  • kubectl v1.12.0+ 除了客户端,当然还要有可用的 Server

CI/CD 需求

目前 Camel K 运行集成代码的方式是通过 CLI 工具 kamel,但将可以操作 K8S 的服务器权限开放给开发人员不太合适,
因此需要寻求一种合适 Camel K 的部署方式。

可以考虑通过 Jenkins 配置 Camel K 集成代码部署脚本,封装 kamel 命令。

脚本大致思路

  1. git clone /path/to/repository
  2. kamel run $(cat enable_routes)

Camel 监控

Camel K 官方文档:https://camel.apache.org/camel-k/latest/traits/jolokia.html

Camel K 特性包括了 Jolokia,结合 Hawtio 可以实现 Camel 可视化。

hawtio

Camel K 使用相关

个人建议项目结构

camel-k-integration/
├── build.gradle
├── gradle
│   └── wrapper
│       ├── gradle-wrapper.jar
│       └── gradle-wrapper.properties
├── gradlew
├── gradlew.bat
├── enable_routes
├── settings.gradle
└── src
    ├── main
    │   ├── java
    │   │   ├── HttpRoute.java
    │   │   └── org
    │   │       └── example
    │   │           └── camel
    │   │               └── SampleRoute.java
    │   └── resources
    └── test
        ├── java
        │   └── org
        │       └── example
        │           └── camel
        │               └── SampleRouteTest.java
        └── resources

enable_routes 文件中存放需要运行的集成代码文件列表:

src/main/java/org/example/camel/SampleRoute.java
src/main/java/HttpRoute.java

示例:通过 kamel 部署集成代码

examples/SampleRocketMQ.java

import org.apache.camel.builder.RouteBuilder;

public class SampleRocketMQ extends RouteBuilder {
  @Override
  public void configure() throws Exception {
	  from("timer:send?period=30000")
	      .process(exchange -> {
	          exchange.getIn().setBody("Camel K RocketMQ Sample".getBytes());
	      })
          .to("rocketmq:camel_k_sample?namesrvAddr=rpi3.lo:9876&producerGroup=camel_k_producer")
          .to("log:camel_k_sample_rocketmq?showAll=true")
	  ;
  }
}

执行命令

kamel run examples/SampleRocketMQ.java -d mvn:vip.wuweijie.camel:camel-rocketmq:3.2.0-0.0.2 -n camel-k --name camel-k-rocketmq

Rancher

Pod 日志:

2020-04-21 15:46:38.566 INFO  [main] LRUCacheFactory - Detected and using LURCacheFactory: camel-caffeine-lrucache
2020-04-21 15:46:38.631 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RuntimeConfigurer@25bfcafd
2020-04-21 15:46:38.632 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.ContextConfigurer@71454b9d
2020-04-21 15:46:38.632 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesConfigurer@2fc6f97f
2020-04-21 15:46:38.633 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesDumper@2d6764b2
2020-04-21 15:46:38.633 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.PropertiesFunctionsConfigurer@6399551e
2020-04-21 15:46:38.636 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RuntimeConfigurer@25bfcafd executed in phase Starting
2020-04-21 15:46:38.640 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesFunctionsConfigurer@6399551e executed in phase Starting
2020-04-21 15:46:38.641 INFO  [main] BaseMainSupport - Using properties from:
2020-04-21 15:46:38.645 INFO  [main] RuntimeSupport - Looking up loader for language: java
2020-04-21 15:46:38.646 INFO  [main] RuntimeSupport - Found loader org.apache.camel.k.loader.java.JavaSourceLoader@fade1fc for language java from service definition
2020-04-21 15:46:39.153 INFO  [main] RoutesConfigurer - Loading routes from: file:/etc/camel/sources/i-source-000/SampleRocketMQ.java?language=java
2020-04-21 15:46:39.153 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesConfigurer@2fc6f97f executed in phase ConfigureRoutes
2020-04-21 15:46:39.187 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.ContextConfigurer@71454b9d executed in phase ConfigureContext
2020-04-21 15:46:39.187 INFO  [main] AbstractCamelContext - Apache Camel 3.1.0 (CamelContext: camel-k) is starting
2020-04-21 15:46:39.189 INFO  [main] DefaultManagementStrategy - JMX is disabled
2020-04-21 15:46:39.190 INFO  [main] HeadersMapFactoryResolver - Detected and using HeadersMapFactory: camel-headersmap
2020-04-21 15:46:39.277 INFO  [main] AbstractCamelContext - StreamCaching is not in use. If using streams then its recommended to enable stream caching. See more details at http://camel.apache.org/stream-caching.html
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by io.netty.util.internal.PlatformDependent0$1 (file:/deployments/dependencies/io.netty.netty-all-4.0.42.Final.jar) to field java.nio.Buffer.address
WARNING: Please consider reporting this to the maintainers of io.netty.util.internal.PlatformDependent0$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
RocketMQLog:WARN No appenders could be found for logger (io.netty.util.internal.PlatformDependent0).
RocketMQLog:WARN Please initialize the logger system properly.
2020-04-21 15:46:39.388 INFO  [main] AbstractCamelContext - Route: route1 started and consuming from: timer://send
2020-04-21 15:46:39.395 INFO  [main] AbstractCamelContext - Total 1 routes, of which 1 are started
2020-04-21 15:46:39.395 INFO  [main] AbstractCamelContext - Apache Camel 3.1.0 (CamelContext: camel-k) started in 0.208 seconds
2020-04-21 15:46:39.395 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@2d6764b2 executed in phase Started
2020-04-21 15:46:40.463 INFO  [NettyClientPublicExecutor_1] camel_k_sample_rocketmq - Exchange[Id: ID-camel-k-rocketmq-8596f966c9-95hrq-1587484000450-0-1, ExchangePattern: InOnly, Properties: {CamelTimerCounter=1, CamelTimerFiredTime=Tue Apr 21 15:46:40 GMT 2020, CamelTimerName=send, CamelTimerPeriod=30000, CamelToEndpoint=log://camel_k_sample_rocketmq?showAll=true}, Headers: {firedTime=Tue Apr 21 15:46:40 GMT 2020}, BodyType: byte[], Body: Camel K RocketMQ Sample]
2020-04-21 15:47:10.398 INFO  [NettyClientPublicExecutor_1] camel_k_sample_rocketmq - Exchange[Id: ID-camel-k-rocketmq-8596f966c9-95hrq-1587484000450-0-2, ExchangePattern: InOnly, Properties: {CamelTimerCounter=2, CamelTimerFiredTime=Tue Apr 21 15:47:10 GMT 2020, CamelTimerName=send, CamelTimerPeriod=30000, CamelToEndpoint=log://camel_k_sample_rocketmq?showAll=true}, Headers: {firedTime=Tue Apr 21 15:47:10 GMT 2020}, BodyType: byte[], Body: Camel K RocketMQ Sample]
  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

[email protected]

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值