脉冲星客户端

当你使用脉冲星Spring靴Starters时,你会得到脉冲星客户端自动配置。spring-doc.cadn.net.cn

默认情况下,应用程序尝试连接到本地的 Pulsar 实例pulsar://localhost:6650. 这可以通过设置Spring.pulsar.client.service-url财产价值变为不同。spring-doc.cadn.net.cn

该值必须是有效的脉冲星协议URL。

你可以通过指定任意Spring.Pulsar.client.*应用属性。spring-doc.cadn.net.cn

如果你没有使用Starters,你需要配置并注册脉冲星客户端你自己。 有一个DefaultPulsarClientFactory那个可以帮忙的建造者定制工具。

1. TLS加密(SSL)

默认情况下,Pulsar客户端以明文与Pulsar服务通信。 以下部分将介绍如何配置Pulsar客户端以使用TLS加密(SSL)。 前提是经纪人也配置了使用 TLS 加密。spring-doc.cadn.net.cn

Spring Boot 自动配置目前不支持任何 TLS/SSL 配置属性。 你可以提供PulsarClientBuilderCustomizer该系统在 Pulsar 客户端构建器上设置必要的属性。 Pulsar 支持隐私增强邮件(PEM)和 Java KeyStore(JKS)证书格式。spring-doc.cadn.net.cn

按照以下步骤配置TLS:spring-doc.cadn.net.cn

  1. 调整Pulsar客户端服务网址以使用以下内容脉冲星+SSL键scheme 和 TLS 端口(通常如此)6651).spring-doc.cadn.net.cn

  2. 调整管理员客户端服务的网址以使用https://scheme 和 TLS 网页端口(通常)8443).spring-doc.cadn.net.cn

  3. 为客户提供架构商定制服务,将相关属性设定在架构商身上。spring-doc.cadn.net.cn

你可以在官方Pulsar TLS加密文档中找到更多上述信息。spring-doc.cadn.net.cn

2. 认证

要连接需要认证的Pulsar集群,你需要指定使用哪个认证插件以及该插件所需的参数。 使用Spring Boot自动配置时,你可以通过配置属性设置插件和插件参数(大多数情况下)。spring-doc.cadn.net.cn

你需要确保定义在以下spring.pulsar.client.authentication.param.*完全符合你验证插件的预期(通常是骆驼壳的)。 Spring Boot不会尝试为这些条目做任何放松装订。spring-doc.cadn.net.cn

例如,如果你想配置发行者的URL,AuthenticationOAuth2你必须使用的认证插件spring.pulsar.client.authentication.param.issuerUrl. 如果你使用其他形式,比如发行人url发行者网址,该设置不会应用到插件上。spring-doc.cadn.net.cn

使用环境变量作为认证参数通常存在问题,因为在翻译过程中会丢失大小写敏感性。 例如,考虑以下内容发行者网址通过环境变量设置的认证参数:spring-doc.cadn.net.cn

SPRING_PULSAR_CLIENT_AUTHENTICATION_PARAM_ISSUERURL=https://some.server.com

当 Spring Boot 加载该属性时,它会使用发行人url(小写)而非预期发行者网址(卡尔卡壳)。 你可以通过用环境变量的值来绕过这个限制,就像你application.yml中相关授权属性的值一样。 继续上述例子:spring-doc.cadn.net.cn

spring:
  pulsar:
    client:
      authentication:
        param:
          issuerUrl: ${SPRING_PULSAR_CLIENT_AUTHENTICATION_PARAM_ISSUERURL}

当不使用Spring Boot自动配置时,你可以使用org.apache.pulsar.client.api.AuthenticationFactory创建认证后,直接在你提供给客户端工厂的客户端定制器中设置。spring-doc.cadn.net.cn

以下列表展示了如何配置每种支持的认证机制。spring-doc.cadn.net.cn

点击这里查看Athenz
spring:
  pulsar:
    client:
      authentication:
        plugin-class-name: org.apache.pulsar.client.impl.auth.AuthenticationAthenz
        param:
          tenantDomain: ...
          tenantService: ...
          providerDomain: ...
          privateKey: ...
          keyId: ...
这也需要TLS加密
点击这里获取Tokens
spring:
  pulsar:
    client:
      authentication:
        plugin-class-name: org.apache.pulsar.client.impl.auth.AuthenticationToken
        param:
          token: some-token-goes-here
点击这里了解基础内容
spring:
  pulsar:
    client:
      authentication:
        plugin-class-name: org.apache.pulsar.client.impl.auth.AuthenticationBasic
        param:
          userId: ...
          password: ...
点击这里查看OAuth2
spring:
  pulsar:
    client:
      authentication:
        plugin-class-name: org.apache.pulsar.client.impl.auth.oauth2.AuthenticationOAuth2
        param:
          issuerUrl: ...
          privateKey: ...
          audience: ...
          scope: ...
点击这里查看Sasl
spring:
  pulsar:
    client:
      authentication:
        plugin-class-name: org.apache.pulsar.client.impl.auth.AuthenticationSasl
        param:
          saslJaasClientSectionName: ...
          serverType: ...
点击此处查看mTLS(PEM)
由于该选项需要TLS加密,而TLS本身就需要你提供客户端构建器定制器,建议直接在提供的TLS定制器中添加认证。 你可以使用org.apache.pulsar.client.api.AuthenticationFactory帮助创建认证对象如下:
Authentication auth = AuthenticationFactory.TLS("/path/to/my-role.cert.pem", "/path/to/my-role.key-pk8.pem");

请参阅官方的 Pulsar 文档,内容包括 mTLS(PEM)。spring-doc.cadn.net.cn

点击这里查看mTLS(JKS)
由于该选项需要TLS加密,而TLS本身就需要你提供客户端构建器定制器,建议直接在提供的TLS定制器中添加认证。 你可以使用org.apache.pulsar.client.api.AuthenticationFactory帮助创建认证对象如下:
Authentication auth = AuthenticationFactory.create(
        "org.apache.pulsar.client.impl.auth.AuthenticationKeyStoreTls",
        Map.of("keyStoreType", "JKS", "keyStorePath", "/path/to/my/keystore.jks", "keyStorePassword", "clientpw"));

请参见官方的 Pulsar 文档,内容是 mTLS(JKS)。spring-doc.cadn.net.cn

你可以在官方 Pulsar 安全文档中找到关于每个支持插件及其所需属性的更多信息。spring-doc.cadn.net.cn

3. 自动集群级故障切换

PulsarSpring Boot器还能自动配置脉冲星客户端用于自动集群级故障切换spring-doc.cadn.net.cn

你可以使用spring.pulsar.client.failover.*应用程序属性用于配置集群级故障转移。spring-doc.cadn.net.cn

以下示例配置客户端为主集群和两个备份集群。spring-doc.cadn.net.cn

application.yml
spring:
  pulsar:
    client:
      service-url: "pulsar://my.primary.server:6650"
      failover:
        delay: 30s
        switch-back-delay: 15s
        check-interval: 1s
        backup-clusters:
          - service-url: "pulsar://my.second.server:6650"
            authentication:
              plugin-class-name: org.apache.pulsar.client.impl.auth.AuthenticationToken
              param:
                token: "my-token"
          - service-url: "pulsar://my.third.server:6650"
除了客户端配置外,经纪商还必须满足一些前提条件才能使用此功能。

当不使用 Spring Boot 自动配置时,你可以提供客户端自定义器,配置客户端进行集群级故障切换。spring-doc.cadn.net.cn