Spring-Cloud-参考文档使用Sidecar支持多语言

6次阅读

共计 1895 个字符,预计需要花费 5 分钟才能阅读完成。

使用 Sidecar 支持多语言

你是否有希望利用 Eureka、Ribbon 和 Config Server 的非 JVM 语言?Spring Cloud Netflix Sidecar 的灵感来自 Netflix Prana,它包含一个 HTTP API,用于获取给定服务的所有实例(按主机和端口)。你还可以通过嵌入式 Zuul 代理代理服务调用,该代理从 Eureka 获取其路由条目,可以通过主机查找或 Zuul 代理直接访问 Spring Cloud Config Server,非 JVM 应用程序应实现健康检查,以便 Sidecar 可以向 Eureka 报告应用程序是启动还是关闭。

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

要启用 Sidecar,请使用 @EnableSidecar 创建 Spring Boot 应用程序,此注解包括 @EnableCircuitBreaker@EnableDiscoveryClient@EnableZuulProxy,在与非 JVM 应用程序相同的主机上运行生成的应用程序。

要配置 Sidecar,请将 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 应用程序的示例配置:

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>,因此 customers 服务可在/customers 处获得,非 JVM 应用程序可以访问位于 http://localhost:5678/customers 的 customer 服务(假设 sidecar 正在侦听端口 5678)。

如果配置服务器已在 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

要在使用 HTTPs 时将健康检查请求设置为接受所有证书,请将 sidecar.accept-all-ssl-certificates 设置为true


正文完
 0