使用 Sidecar 整合非 JVM 微服务

使用 Sidecar 整合非 JVM 微服务

假设有个微服务运行在 ip:123.123.123.123 port: 8080 上,我们现在需要将其注册在 Eureka Server 上

首先我们需要在该服务上提供一个 REST 接口,让该接口返回服务的状态,即

{ "status":"UP" // status 有几种取值,分别是: UP、DOWN、OUT_OF_SERVICE、UNKNOWN 等 }

编写 Sidecar 来注册该微服务

1、创建工程,添加下列依赖

<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-netflix-sidecar</artifactId> </dependency>

2、在启动类上添加 @EnableSidecar 注解,声明这是一个 Sidecar。

@EnableSidecar 是一个组合注解,它整合了 @EnableCircuitBreaker、@EnableDiscoveryClient、@EnableZuulProxy。

3、配置 applicaiton.yml

server: port:8070 spring: application: name: microservice-sidecar-node-service eureka: instance: hostname: hostname #非 JVM 微服务的 hostname client: service-url: defaultZone: http://localhost: 8761/eureka/ instance: prefer-ip-address: true sidecar: port: 8080 health-uri: http://localhost:8080/health.json

或者

server: port:8070 spring: application: name: microservice-sidecar-node-service eureka: client: service-url: defaultZone: http://localhost: 8761/eureka/ instance: prefer-ip-address: true sidecar: port: 8080 health-uri: http://hostname:8080/health.json hostname: hostname # 非 JVM 微服务的 hostname ip-address: IP # 非 JVM 微服务的 IP 地址

4、Sidecar 的端点

  • 1)/

    该端点返回一个测试页面,展示 Sidecar 的常用端点

  • 2)/hosts/{serviceId}

    该端点返回 DiscoveryClient.getInstances(serviceId),即指定微服务在 Eureka 上的实例列表。

  • 3)/ping

    返回 OK

  • 4)/{serviceId}

    由于 Sidecar 整合了 Zuul ,因此可以使用该端点将请求转发到 serviceId。


__EOF__

本文作者ZOLMK
本文链接https://www.cnblogs.com/zolmk/p/14080942.html
关于博主:评论和私信会在第一时间回复。或者直接私信我。
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
声援博主:如果您觉得文章对您有帮助,可以点击文章右下角推荐一下。您的鼓励是博主的最大动力!
posted @   zolmk  阅读(199)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
点击右上角即可分享
微信分享提示