358.8. HTTP/2 示例


在这个示例中,我们将 camel-undertow 组件配置为支持 HTTP/2 协议,并在 http://localhost:7766/foo/bar 公开 HTTP 服务。

这个示例的目录结构如下:

├── pom.xml 1
├── README.md
└── src
    └── main
        └── resources
            └── META-INF
                └── spring
                    └── camel-context.xml 2

以下文件对于将 camel-undertow 组件配置为支持 HTTP/2 协议非常重要:

1
pom.xml :包含以下属性和依赖项:
<properties>
    <camel.version>2.23.1</camel.version>
</properties>

<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-core</artifactId>
    <version>${camel.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-spring</artifactId>
    <version>${camel.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-undertow</artifactId>
    <version>${camel.version}</version>
    </dependency>
<dependency>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-http-common</artifactId>
    <version>${camel.version}</version>
</dependency>

<plugin>
    <groupId>org.apache.camel</groupId>
    <artifactId>camel-maven-plugin</artifactId>
    <version>${camel.version}</version>
    <configuration>
        <fileApplicationContextUri>src/main/resources/META-INF/spring/camel-context.xml</fileApplicationContextUri>
    </configuration>
</plugin>
2
camel-context.xml :配置 camel-undertow 组件,如下所示:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd">
    <camelContext id="cbr-example-context" xmlns="http://camel.apache.org/schema/spring">
        <route id="cbr-route" trace="true">
            <from id="_from1" uri="undertow:http://localhost:7766/foo/bar"/>
            <setBody id="_setBody1">
                <constant>Sending Response</constant>
            </setBody>
            <log id="_log5" message="Headers ${in.headers}"/>
            <log id="_log5" message="Done processing ${body}"/>
        </route>
    </camelContext>
    <bean class="org.apache.camel.component.undertow.UndertowComponent" id="undertow">
        <property name="hostOptions" ref="undertowHostOptions"/>
    </bean>
    <bean     class="org.apache.camel.component.undertow.UndertowHostOptions" id="undertowHostOptions">
        <property name="http2Enabled" value="true"/>
    </bean>
</beans>
注意

默认情况下,对于 UndertowHostOptions 类,http2Enabled 属性设置为 false然后,此类被称为 UndertowComponent,在 camel 路由中使用。

在本例中,通过在 pom.xml 文件中使用 camel-maven-plugin,我们在执行 Maven 命令 mvn camel:run 时,可以在 http://localhost:7766/foo/bar 中公开 HTTP 服务。

我们可以使用 curl 命令测试这个示例,如下所示:

$ curl -v --http2 http://localhost:7766/foo/bar
* Trying ::1...
* TCP_NODELAY set
* connect to ::1 port 7766 failed: Connection refused
* Trying 127.0.0.1...
* TCP_NODELAY set
* Connected to localhost (127.0.0.1) port 7766 (#0)
> GET /foo/bar HTTP/1.1
> Host: localhost:7766
> User-Agent: curl/7.53.1
> Accept: */*
> Connection: Upgrade, HTTP2-Settings
> Upgrade: h2c
> HTTP2-Settings: AAMAAABkAARAAAAAAAIAAAAA
>
< HTTP/1.1 101 Switching Protocols
< Connection: Upgrade
< Upgrade: h2c
< Date: Sun, 10 Dec 2017 08:43:58 GMT
* Received 101
* Using HTTP2, server supports multi-use
* Connection state changed (HTTP/2 confirmed)
* Copying HTTP/2 data in stream buffer to connection buffer after upgrade: len=0
* Connection state changed (MAX_CONCURRENT_STREAMS updated)!
< HTTP/2 200
< accept: */*
< http2-settings: AAMAAABkAARAAAAAAAIAAAAA
< breadcrumbid: ID-dhcppc1-1512886066149-0-25
< content-length: 16
< user-agent: curl/7.53.1
< date: Sun, 10 Dec 2017 08:43:58 GMT
<
* Connection #0 to host localhost left intact
Sending Response
Red Hat logoGithubRedditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

© 2024 Red Hat, Inc.