Spring Cloud Sidecar 简介 | baeldung


在微服务世界中,使用不同的语言和框架编写多语言服务是一种常见的做法,使用Spring  Cloud Sidecar,可以在服务注册表中注册非 JVM 服务。
此外,该服务还可以使用服务发现来查找其他服务,甚至可以通过主机查找或Zuul Proxy访问配置服务器。能够集成非 JVM 服务的唯一要求是具有可用的标准健康检查端点。

案例:
我们的示例用例包含 3 个应用程序。为了展示 Cloud Netflix Sidecar 的优点,我们将在 NodeJS 中创建一个/hello端点,然后通过一个名为 sidecar 的 Spring 应用程序将其公开给我们的生态系统。我们还将开发另一个 Spring Boot 应用程序,以使用服务发现和 Zuul来响应/hello端点响应。

在这个项目中,我们的目标是涵盖请求的两个流程:

  • 用户在 echo Spring Boot 应用程序上调用 echo 端点。echo 端点使用DiscoveryClient从 Eureka 中查找 hello 服务 URL,即指向 NodeJS 服务的 URL。然后 echo 端点调用 NodeJS 应用程序上的 hello 端点
  • 用户在 Zuul Proxy 的帮助下直接从 echo 应用程序调用 hello 端点

示例的完整代码可在 GitHub 上获得

1. 激活sidecar

@SpringBootApplication
@EnableSidecar
public class SidecarApplication {
    public static void main(String[] args) {
        SpringApplication.run(SidecarApplication.class, args);
    }
}

2. 配置
下一步,我们必须设置连接到 Eureka 的属性。此外,我们使用 NodeJS hello 应用程序的端口和健康 URI 设置 sidecar 配置:

server.port: 8084
spring:
  application:
    name: sidecar
eureka:
  instance:
    hostname: localhost
    leaseRenewalIntervalInSeconds: 1
    leaseExpirationDurationInSeconds: 2
  client:
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka
    healthcheck:
      enabled: true
sidecar:
  port: 3000
  health-uri: http://localhost:3000/health


现在我们可以启动我们的应用程序了。在我们的应用程序成功启动后,Spring 在 Eureka Server 中注册了一个名为“hello”的服务。
要检查它是否有效,我们可以访问端点:http://localhost:8084/hosts/sidecar
@EnableSidecar不仅仅是向 Eureka 注册辅助服务的标记。它还导致添加@EnableCircuitBreaker 和@EnableZuulProxy,随后,我们的 Spring Boot 应用程序受益于HystrixZuul 

详细点击标题