第 5 章 为 3scale 发现准备 API 服务
Red Hat 3scale API Management 是红帽的产品,可让您监管对公共互联网上的 API 服务的访问。3scale 的功能包括实施服务级别协议(SLA)的功能,管理 API 版本,提供安全性和身份验证服务等。Fuse 支持 3scale 的 服务发现功能,它可以轻松地从 3scale 管理门户 UI 发现 Fuse 服务。通过使用服务发现,您可以扫描在同一 OpenShift 集群中运行的 Fuse 应用程序,并将关联的 API 定义自动导入到 3scale。
先决条件
- 在 OpenShift 中部署并运行了提供 API 服务的 Fuse 应用。
Fuse 应用标注了必要的注释,使它可以被 3scale 发现。
注意由 API Designer 生成的 Fuse 项目预先配置为自动提供必要的注解。
对于不是由 API Designer 生成的 Fuse 项目,您必须配置项目,如 API Designer 生成的 Fuse 项目添加注解 中所述。
- 3scale API 管理系统被部署到与要发现的 API 服务 相同的 OpenShift 集群上。
有关在 3scale 中发现 API 服务的步骤详情,请参阅 Red Hat 3scale API Management Portal 指南中的 服务发现部分。
5.1. 为 API Designer 生成的 Fuse 项目添加注解
为了使 3scale 能够发现 API 服务,提供 API 服务的 Fuse 应用程序必须包含 Kubernetes Service Annotations,使其可被发现。这些注解由 Service Discovery Enricher 提供,它作为 OpenShift Maven 插件的一部分。
对于 Apache Camel Rest DSL 项目,OpenShift Maven 插件默认运行服务发现增强器。
由 API Designer 生成的 Fuse 项目预先配置为自动提供所需的注解。
流程
对于不是由 API Designer 生成的 Fuse Rest DSL 项目,请按如下所示配置项目:
编辑 Fuse 项目的
pom.xml
文件,使其包含openshift-maven-plugin
依赖项,如下例所示:<plugin> <groupId>org.jboss.redhat-fuse</groupId> <artifactId>openshift-maven-plugin</artifactId> <version>${fuse.version}</version> <executions> <execution> <goals> <goal>resource</goal> <goal>build</goal> </goals> </execution> </executions> </plugin>
如果满足某些项目级别条件(例如,项目必须是 Camel Rest DSL 项目),OpenShift Maven 插件将运行服务发现功能。您不需要将 Service Discovery Enricher 指定为
pom.xml
文件中的依赖项,除非您要自定义 ener 的行为(如 自定义 API 服务注解值 所述)。在 Fuse Rest DSL 项目的
camel-context.xml
文件中,在restConfiguration
元素中指定以下属性:-
方案
:托管服务的 URL 的方案部分。您可以指定 "http" 或 "https"。 -
contextPath
:API 服务托管的 URL 的路径部分。 apiContextPath
:API 服务描述文档托管的位置的路径。如果文档是自托管,或者完整 URL 是外部的,您可以指定相对路径。以下来自
camel-context.xml
文件示例摘录显示了restConfiguration
元素中的注解属性值:<?xml version="1.0" encoding="UTF-8"?> <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 xmlns="http://camel.apache.org/schema/spring"> <restConfiguration component="servlet" scheme="https" contextPath="myapi" apiContextPath="myapi/openapi.json"/> ...
-
增强器使用这些 restConfiguration
元素值提供的信息,为 discovery.3scale.net/scheme
、discovery.3scale.net/path
以及 discovery.3scale.net/description-path
注解创建 discovery.3scale.net/description-path 注解,因此,该项目由 3scale 部署 OpenShift 服务被 3scale 发现,如红帽 3scale API 管理门户 中的服务发现部分所述。
enricher 添加以下标签和注解,以使服务可以被 3scale 发现:
-
discovery.3scale.net
标签:默认情况下,enricher 将此值设置为 "true"。3scale 执行选择器定义以查找需要发现的所有服务时,它会使用此标签。 以下注解:
-
discovery.3scale.net/discovery-version
:(可选)3scale 发现过程的版本。enricher 默认将此值设置为 "v1"。 -
discovery.3scale.net/scheme
: 托管该服务的 URL 的方案部分。增强器会使用默认的 "http",除非您在restConfiguration
元素的scheme
属性中覆盖它。另一个可能的值是 "https"。 -
discovery.3scale.net/path
:托管该服务的 URL 的路径部分。如果路径为 "/",则省略此注解。enricher 从restConfiguration
元素的path
属性中获取这个值。 -
discovery.3scale.net/port
:服务的端口。增强器从 Kubernetes 服务定义中获取这个值,其中包含其公开的服务端口号。如果 Kubernetes 服务定义公开多个服务,则增强器会使用列出的第一个端口。 -
discovery.3scale.net/description-path
:(可选)OpenAPI 服务描述文档的路径。enricher 从restConfiguration
元素的contextPath
属性中获取这个值。
-
您可以自定义 Service Discovery Enricher 的行为,如 自定义 API 服务注解值 中所述。