19. Sidecar 对 Polyglot 的支持

您是否要使用非 JVM 语言来利用 Eureka,Ribbon 和 Config Server? Spring Cloud Netflix Sidecar 受到Netflix Prana的启发。它包括一个 HTTP API,用于获取给定服务的所有实例(按主机和端口)。您也可以通过嵌入式 Zuul 代理来代理服务调用,该代理从 Eureka 获取其路由条目。可以通过主机查找或通过 Zuul 代理直接访问 Spring Cloud Config Server。非 JVM 应用程序应实施运行状况检查,以便 Sidecar 可以向 Eureka 报告该应用程序是否启动。

要将 Sidecar 包含在您的项目中,请使用组 ID 为org.springframework.cloud且工件 ID 为spring-cloud-netflix-sidecar的依赖项。

要启用 Sidecar,请使用@EnableSidecar创建一个 Spring Boot 应用程序。此 Comments 包括@EnableCircuitBreaker@EnableDiscoveryClient@EnableZuulProxy。在与非 JVM 应用程序相同的主机上运行结果应用程序。

要配置侧车,请将sidecar.portsidecar.health-uri添加到application.ymlsidecar.port属性是非 JVM 应用程序侦听的端口。这样,Sidecar 可以在 Eureka 中正确注册该应用程序。 sidecar.health-uri是可在非 JVM 应用程序上访问的 URI,它模仿 Spring Boot 运行状况指示器。它应该返回类似于以下内容的 JSON 文档:

health-uri-document.

{
  "status":"UP"
}

以下 application.yml 示例显示了 Sidecar 应用程序的示例配置:

application.yml.

server:
  port: 5678
spring:
  application:
    name: sidecar

sidecar:
  port: 8000
  health-uri: http://localhost:8000/health.json

DiscoveryClient.getInstances()方法的 API 是/hosts/{serviceId}/hosts/customers的以下示例响应在不同主机上返回两个实例:

/hosts/customers.

[
    {
        "host": "myhost",
        "port": 9000,
        "uri": "http://myhost:9000",
        "serviceId": "CUSTOMERS",
        "secure": false
    },
    {
        "host": "myhost2",
        "port": 9000,
        "uri": "http://myhost2:9000",
        "serviceId": "CUSTOMERS",
        "secure": false
    }
]

非 JVM 应用程序(如果 Sidecar 在端口 5678 上)的http://localhost:5678/hosts/{serviceId}可以访问此 API。

Zuul 代理会自动将 Eureka 中已知的每个服务的路由添加到/<serviceId>,因此 Client 服务在/customers可用。非 JVM 应用程序可以在http://localhost:5678/customers处访问 Client 服务(假设 Sidecar 正在监听端口 5678)。

如果 Config Server 已向 Eureka 注册,则非 JVM 应用程序可以通过 Zuul 代理对其进行访问。如果 ConfigServer 的serviceIdconfigserver并且 Sidecar 在端口 5678 上,则可以在http://localhost:5678/configserver对其进行访问。

非 JVM 应用程序可以利用 Config Server 返回 YAML 文档的功能。例如,调用http://sidecar.local.spring.io:5678/configserver/default-master.yml可能会导致 YAML 文档类似于以下内容:

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/
  password: password
info:
  description: Spring Cloud Samples
  url: https://github.com/spring-cloud-samples

要在使用 HTTP 时使运行状况检查请求接受所有证书,请将sidecar.accept-all-ssl-certificates设置为`true。