Camel K Platform 使用 Demo | 在 K8S 上的 Serverless 架构

本文介绍了如何在Apache Camel K平台上部署和运行包含多个文件的实际场景路由逻辑。通过示例展示了如何使用`kamel run`命令指定多个Java源文件,并分析了`kamel run`命令的源码,揭示了其支持多个文件和HTTP资源的加载能力。此外,讨论了运行整个项目的方法,虽然官方并不推荐大量文件的单次运行,但提出了打包依赖的解决方案。
摘要由CSDN通过智能技术生成

之前已经完成了 Camel K Platform 的部署与 Demo 运行,现在需要一些更符合实际场景的路由逻辑

本文的平台基于 camel-k master 分支构建
Camel K Platform 搭建参考文章:部署 Apache Camel K | 从 master 分支源码构建并部署 Camel K 平台

基于 Maven 引入包含外部依赖的 Demo

有个 demo 文件:myexamples/NettyHttpDemoRoute.java

package vip.wuweijie.demo.camel.routes;

import com.zhaofujun.automapper.AutoMapper;
import io.netty.handler.codec.http.HttpHeaderNames;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.netty.http.NettyHttpMessage;
import java.util.Date;

public class NettyHttpDemoRoute extends RouteBuilder {
    @Override
    public void configure() throws Exception {
        from("netty-http:http://0.0.0.0:8765")
                .to("mock:root")
                .process(exchange -> {
                    NettyHttpMessage in = exchange.getIn(NettyHttpMessage.class);
                    String ua = in.getHeader(HttpHeaderNames.USER_AGENT.toString(), String.class);
                    System.out.println("UA => " + ua);
                    HR hr = new HR(ua);
                    System.out.println(hr);
                    AutoMapper autoMapper = new AutoMapper();
                    autoMapper.mapping(HR.class, UA.class, false)
                            .field("userAgent", "value");
                    UA uaObject = autoMapper.map(hr, UA.class);
                    System.out.println(uaObject);
                })
                .to("log:netty-http-demo?showAll=true")
                .transform()
                .constant(new Date())
        ;
    }

    public static class HR {
        private final String userAgent;
        public HR(String userAgent) {
            this.userAgent = userAgent;
        }
        @Override
        public String toString() {
            return "HR{" +
                    "userAgent='" + userAgent + '\'' +
                    '}';
        }
        public String getUserAgent() {
            return userAgent;
        }
    }

    public static class UA {
        private String value;
        @Override
        public String toString() {
            return "UA{" +
                    "value='" + value + '\'' +
                    '}';
        }
        public String getValue() {
            return value;
        }
        public void setValue(String value) {
            this.value = value;
        }
    }
}

参数说明:

  • -d mvn:com.zhaofujun.automapper:automapper:1.0.release:表示通过 maven 引入依赖 com.zhaofujun.automapper:automapper:1.0.release
  • --dev:调试模式
  • -n camel-k:在 namespace camel-k 执行
    执行 demo:
kamel run myexamples/NettyHttpDemoRoute.java -d mvn:com.zhaofujun.automapper:automapper:1.0.release --dev -n camel-k

输出结果:

integration "netty-http-demo-route" created
Progress: integration "netty-http-demo-route" in phase Initialization
Integration netty-http-demo-route in phase Initialization
IntegrationPlatformAvailable for Integration netty-http-demo-route: camel-k
Progress: integration "netty-http-demo-route" in phase Building Kit
No IntegrationKitAvailable for Integration netty-http-demo-route: creating a new integration kit
Integration netty-http-demo-route in phase Building Kit
Progress: integration "netty-http-demo-route" in phase Deploying
IntegrationKitAvailable for Integration netty-http-demo-route: kit-bptcaf2o1songb4m7pag
Integration netty-http-demo-route in phase Deploying
Progress: integration "netty-http-demo-route" in phase Running
No CronJobAvailable for Integration netty-http-demo-route: different controller strategy used (deployment)
DeploymentAvailable for Integration netty-http-demo-route: deployment name is netty-http-demo-route
No ExposureAvailable for Integration netty-http-demo-route: no host or service defined
ServiceAvailable for Integration netty-http-demo-route: netty-http-demo-route(http/80) -> integration(http/8080)
Integration netty-http-demo-route in phase Running
[1] Monitoring pod netty-http-demo-route-8455456854-gsbpl[1] 2020-03-25 02:46:38.910 INFO  [main] LRUCacheFactory - Detected and using LURCacheFactory: camel-caffeine-lrucache
[1] 2020-03-25 02:46:38.964 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RuntimeConfigurer@692f203f
[1] 2020-03-25 02:46:38.965 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.ContextConfigurer@a1153bc
[1] 2020-03-25 02:46:38.965 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesConfigurer@3270d194
[1] 2020-03-25 02:46:38.965 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.RoutesDumper@4218d6a3
[1] 2020-03-25 02:46:38.966 INFO  [main] ApplicationRuntime - Add listener: org.apache.camel.k.listener.PropertiesFunctionsConfigurer@71d44a3
[1] 2020-03-25 02:46:38.971 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RuntimeConfigurer@692f203f executed in phase Starting
[1] 2020-03-25 02:46:38.974 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.PropertiesFunctionsConfigurer@71d44a3 executed in phase Starting
[1] 2020-03-25 02:46:38.975 INFO  [main] BaseMainSupport - Using properties from: 
[1] 2020-03-25 02:46:38.980 INFO  [main] RuntimeSupport - Looking up loader for language: java
[1] 2020-03-25 02:46:38.981 INFO  [main] RuntimeSupport - Found loader org.apache.camel.k.loader.java.JavaSourceLoader@489115ef for language java from service definition
[1] 2020-03-25 02:46:39.294 INFO  [main] RoutesConfigurer - Loading routes from: file:/etc/camel/sources/i-source-000/NettyHttpDemoRoute.java?language=java
[1] 2020-03-25 02:46:39.294 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesConfigurer@3270d194 executed in phase ConfigureRoutes
[1] 2020-03-25 02:46:39.345 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.ContextConfigurer@a1153bc executed in phase ConfigureContext
[1] 2020-03-25 02:46:39.346 INFO  [main] AbstractCamelContext - Apache Camel 3.1.0 (CamelContext: camel-k) is starting
[1] 2020-03-25 02:46:39.347 INFO  [main] DefaultManagementStrategy - JMX is disabled
[1] 2020-03-25 02:46:39.348 INFO  [main] HeadersMapFactoryResolver - Detected and using HeadersMapFactory: camel-headersmap
[1] 2020-03-25 02:46:39.431 INFO  [main] NettyComponent - Creating shared NettyConsumerExecutorGroup with 3 threads
[1] 2020-03-25 02:46:39.460 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
[1] 2020-03-25 02:46:39.474 INFO  [main] HttpServerBootstrapFactory - BootstrapFactory on port 8765 is using bootstrap configuration: [NettyServerBootstrapConfiguration{protocol='http', host='0.0.0.0', port=8765, broadcast=false, sendBufferSize=65536, receiveBufferSize=65536, receiveBufferSizePredictor=0, workerCount=0, bossCount=1, keepAlive=true, tcpNoDelay=true, reuseAddress=true, connectTimeout=10000, backlog=0, serverInitializerFactory=org.apache.camel.component.netty.http.HttpServerInitializerFactory@52500920, nettyServerBootstrapFactory=null, options=null, ssl=false, sslHandler=null, sslContextParameters='null', needClientAuth=false, enabledProtocols='TLSv1,TLSv1.1,TLSv1.2, keyStoreFile=null, trustStoreFile=null, keyStoreResource='null', trustStoreResource='null', keyStoreFormat='JKS', securityProvider='SunX509', passphrase='null', bossGroup=null, workerGroup=null, networkInterface='null', reconnect='true', reconnectInterval='10000'}]
[1] 2020-03-25 02:46:39.489 INFO  [main] SingleTCPNettyServerBootstrapFactory - ServerBootstrap binding to 0.0.0.0:8765
[1] 2020-03-25 02:46:39.521 INFO  [main] NettyConsumer - Netty consumer bound to: 0.0.0.0:8765
[1] 2020-03-25 02:46:39.522 INFO  [main] AbstractCamelContext - Route: route1 started and consuming from: http://0.0.0.0:8765
[1] 2020-03-25 02:46:39.525 INFO  [main] AbstractCamelContext - Total 1 routes, of which 1 are started
[1] 2020-03-25 02:46:39.526 INFO  [main] AbstractCamelContext - Apache Camel 3.1.0 (CamelContext: camel-k) started in 0.179 seconds
[1] 2020-03-25 02:46:39.526 INFO  [main] ApplicationRuntime - Listener org.apache.camel.k.listener.RoutesDumper@4218d6a3 executed in phase Started
[1] UA => curl/7.58.0
[1] HR{userAgent='curl/7.58.0'}
[1] 2020-03-25 02:46:49.893 INFO  [Camel (camel-k) thread #4 - NettyConsumerExecutorGroup] FieldMapping - 在源类型vip.wuweijie.demo.camel.routes.NettyHttpDemoRoute$HR中没有找到同名字段value
[1] UA{value='curl/7.58.0'}
[1] 2020-03-25 02:46:49.900 INFO  [Camel (camel-k) thread #4 - NettyConsumerExecutorGroup] netty-http-demo - Exchange[Id: ID-netty-http-demo-route-8455456854-gsbpl-1585104409889-0-1, ExchangePattern: InOut, Properties: {CamelToEndpoint=log://netty-http-demo?showAll=true}, Headers: {Accept=*/*, CamelHttpHost=null, CamelHttpMethod=GET, CamelHttpPath=/, CamelHttpPort=80, CamelHttpQuery=null, CamelHttpRawQuery=null, CamelHttpScheme=null, CamelHttpUri=/, CamelHttpUrl=http://0.0.0.0:8765/, CamelNettyChannelHandlerContext=ChannelHandlerContext(handler, [id: 0x78ef6f64, L:/10.240.0.64:8765 - R:/10.240.0.1:56354]), CamelNettyLocalAddress=/10.240.0.64:8765, CamelNettyRemoteAddress=/10.240.0.1:56354, content-length=0, Host=10.240.0.64:8765, User-Agent=curl/7.58.0}, BodyType: org.apache.camel.component.netty.http.NettyChannelBufferStreamCache, Body: [Body is instance of org.apache.camel.StreamCache]]

通过输出结果可以看到,路由能够正常工作。

运行一个项目的路由

一般一个集成项目不会只有一个文件,所以需要一种运行多个文件或者一个项目的方式。官方文档在这方面并没有提及什么,而且在 kamel run --help 中,参数说明用的是 [file to run],可能会让人误认为只能够支持单个文件。

Initialize empty Camel K integrations and other resources.

Usage:
  kamel init [flags]

Flags:
  -h, --help   help for init

Global Flags:
      --config string      Path to the config file to use for CLI requests
  -n, --namespace string   Namespace to use for all operations
sia@server-sia:~/GolandProjects/camel-k$ ./kamel run --help
Deploys and execute a integration pod on Kubernetes.

Usage:
  kamel run [file to run] [flags]

Flags:
      --compression                    Enable store source as a compressed binary blob
      --configmap stringArray          Add a ConfigMap
  -d, --dependency stringArray         The integration dependency
      --dev                            Enable Dev mode (equivalent to "-w --logs --sync")
  -e, --env stringArray                Set an environment variable in the integration container. E.g "-e MY_VAR=my-value"
  -h, --help                           help for run
  -k, --kit string                     The kit used to run the integration
      --label stringArray              Add a label to the integration. E.g. "--label my.company=hello"
      --logging-level stringArray      Configure the logging level. e.g. "--logging-level org.apache.camel=DEBUG"
      --logs                           Print integration logs
      --maven-repository stringArray   Add a maven repository
      --name string                    The integration name
      --open-api stringArray           Add an OpenAPI v2 spec
  -o, --output string                  Output format. One of: json|yaml
      --profile string                 Trait profile used for deployment
  -p, --property stringArray           Add a camel property
      --property-file stringArray      Bind a property file to the integration. E.g. "--property-file integration.properties"
      --resource stringArray           Add a resource
      --save                           Save the run parameters into the default kamel configuration file (kamel-config.yaml)
      --secret stringArray             Add a Secret
      --sync                           Synchronize the local source file with the cluster, republishing at each change
  -t, --trait stringArray              Configure a trait. E.g. "-t service.enabled=false"
  -v, --volume stringArray             Mount a volume into the integration container. E.g "-v pvcname:/container/path"
  -w, --wait                           Waits for the integration to be running

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

kamel run 指定多个文件

kamel run 命令可以指定多个文件,看了源码还支持 http 协议获取文件

kamel run examples/Sample0.java examples/Sample1.java examples/Sample2.java -ncamel-k -k kit-bpu59tjdk85h2k4pl6i0

运行一整个项目

相关 Issue:How can I run a whole project with many integration files in a comfortable way?

根据回复,单次运行包含大量路由文件的项目不在 Camel K 的设计理念中,考虑通过打包作为依赖的方式运行项目。

附:kamel run 源码解读

pkg/cmd/run.go 源码节选:

func (o *runCmdOptions) validateArgs(_ *cobra.Command, args []string) error {
	if len(args) < 1 {
		return errors.New("run expects at least 1 argument, received 0")
	}

	for _, fileName := range args {
		if !isRemoteHTTPFile(fileName) {
			if _, err := os.Stat(fileName); err != nil && os.IsNotExist(err) {
				return errors.Wrap(err, "file "+fileName+" does not exist")
			} else if err != nil {
				return errors.Wrap(err, "error while accessing file "+fileName)
			}
		} else {
			// nolint: gosec
			resp, err := http.Get(fileName)
			if resp != nil && resp.Body != nil {
				resp.Body.Close()
			}

			if err != nil {
				return errors.Wrap(err, "The URL provided is not reachable")
			} else if resp.StatusCode != 200 {
				return errors.New("The URL provided is not reachable " + fileName + " The error code returned is " + strconv.Itoa(resp.StatusCode))
			}
		}
	}

	return nil
}

可见,Camel K 支持:

  • 单次执行可以挂载多个文件
  • 支持通过 http 协议运行文件
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

wuweijie@apache.org

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

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

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

打赏作者

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

抵扣说明:

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

余额充值