使用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

猜你喜欢

转载自blog.csdn.net/yunxing323/article/details/112339011