19. 支持 Sidecar 的 Polyglot 支持
你有 non-JVM 语言,你想利用 Eureka,Ribbon 和 Config Server? Spring Cloud Netflix Sidecar 的灵感来自Netflix Prana。它包含一个 HTTP API,用于获取给定服务的所有实例(通过 host 和 port)。您还可以通过从 Eureka 获取其 route 条目的嵌入式 Zuul 代理来代理服务 calls。可以通过 host 查找或通过 Zuul 代理直接访问 Spring Cloud Config Server。 non-JVM application 应该执行运行状况检查,以便 Sidecar 可以向 Eureka 报告应用程序是启动还是关闭。
要在项目中包含 Sidecar,请使用 group ID 为org.springframework.cloud
和 artifact ID 或spring-cloud-netflix-sidecar
的依赖项。
要启用 Sidecar,请使用@EnableSidecar
创建 Spring Boot application。这个 annotation 包括@EnableCircuitBreaker
,@EnableDiscoveryClient
和@EnableZuulProxy
。 在与 non-JVM application 相同的 host 上运行生成的 application。
要配置侧车,请将sidecar.port
和sidecar.health-uri
添加到application.yml
。 sidecar.port
property 是 non-JVM application 侦听的 port。这样 Sidecar 可以正确地使用 Eureka 注册 application。 sidecar.health-uri
是 non-JVM application 上可访问的 URI,它模仿 Spring Boot 运行状况指示器。它应该 return 一个类似于以下内容的 JSON 文档:
health-uri-document.
{
"status":"UP"
}
以下 application.yml example 显示了 Sidecar application 的 sample configuration:
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
的 example 响应在不同的主机上返回两个实例:
/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
}
]
在http://localhost:5678/hosts/{serviceId}
,non-JVM application(如果 sidecar 在 port 5678 上)可以访问此 API。
Zuul 代理自动将 Eureka 中已知的每个服务的 routes 添加到/<serviceId>
,因此客户服务可在/customers
处获得。 non-JVM application 可以在http://localhost:5678/customers
访问客户服务(假设边车正在监听 port 5678)。
如果 Config Server 已注册 Eureka,则 non-JVM application 可以通过 Zuul 代理访问它。如果 ConfigServer 的serviceId
是configserver
且 Sidecar 在 port 5678 上,则可以在http://localhost:5678/configserver处访问它。
Non-JVM applications 可以利用 Config Server 的 return YAML 文档的能力。对于 example,对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 时,使运行状况检查请求能够接受所有证书。