第 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 管理门户指南中的 服务发现部分。

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 项目,请按如下所示配置项目:

  1. 编辑 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 插件将运行服务发现Enricher。您不需要将 Service Discovery Enricher 指定为 pom.xml 文件中的依赖项,除非您要自定义增强的行为(如 自定义 API 服务注解值 中所述)。

  2. 在 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/schemediscovery.3scale.net/pathdiscovery.3scale.net/description-path 注解创建值,如 Red Hat 3scale API 管理门户指南的 服务发现部分所述。

增强程序添加以下标签和注解,以使服务可以被 3scale 发现:

  • discovery.3scale.net 标签:默认情况下,增强器将此值设置为 "true"。3scale 执行选择器定义以查找需要发现的所有服务时,它会使用此标签。
  • 以下注解:

    • discovery.3scale.net/discovery-version:(可选)3scale 发现过程的版本。增强器将此值默认设置为 "v1"。
    • discovery.3scale.net/scheme :托管该服务的 URL 的方案部分。增强器使用默认的 "http",除非您在 restConfiguration 元素的 scheme 属性中覆盖它。其他可能的值是 "https"。
    • discovery.3scale.net/path :托管该服务的 URL 的路径部分。当路径位于 root, "/" 时,会省略此注解。增强器从 restConfiguration 元素的 path 属性中获取这个值。
    • discovery.3scale.net/port :服务的端口。增强器从 Kubernetes 服务定义中获取这个值,该服务包含它公开的服务的端口号。如果 Kubernetes 服务定义公开多个服务,则增强器将使用列出的第一个端口。
    • discovery.3scale.net/description-path: (可选) OpenAPI 服务描述文档的路径。增强器从 restConfiguration 元素的 contextPath 属性中获取这个值。

您可以自定义服务发现Enricher 的行为,如 自定义 API 服务注解值 中所述。