简体   繁体   English

微服务调用同一个 kubernetes 集群中的另一个微服务

[英]Microservice calling another microservice in same kubernetes cluster

If java/spring boot microservice A (deployed in its own container & with its own Kubernetes Service of type ClusterIP) needs to send a REST request to Java/spring boot microservice B (has its own Kubernetes Service of type ClusterIP) in the same Kubernetes cluster, what's the best way for A to determine B's kubernetes service IP (especially if B is redeployed)? If java/spring boot microservice A (deployed in its own container & with its own Kubernetes Service of type ClusterIP) needs to send a REST request to Java/spring boot microservice B (has its own Kubernetes Service of type ClusterIP) in the same Kubernetes集群,A 确定 B 的 kubernetes 服务 IP 的最佳方法是什么(特别是如果 B 被重新部署)? Note: internal call where B doesn't have a NodePort or LoadBalancer nor an Ingress.注意:B 没有 NodePort 或 LoadBalancer 也没有 Ingress 的内部调用。

The right way to do this is to have a Service for B , and have A use the name of that service to access B .正确的方法是为B提供Service ,并让A使用该服务的名称来访问B This way, B will be accessible to A regardless of its address.这样A无论其地址如何, B都可以访问 B。

Use service name instead of the service IP.使用服务名称而不是服务 IP。 service-A can invoke service-B using a name service-B.服务 A 可以使用名称服务 B 调用服务 B。 Kubernetes will take care of routing service-B calls to appropriate POD and also load balance incase of multiple PODs. Kubernetes 将负责将服务 B 调用路由到适当的 POD,并在多个 POD 的情况下进行负载平衡。 You dont worry about restarts or which POD actually services the request.您不必担心重新启动或哪个 POD 实际为请求提供服务。

Keep a URL of Service B in the environment variables of service A's kubernetes deployment file, When tasks in your service A are done, call that URL so that it takes the request to service B.在服务A的kubernetes部署文件的环境变量中保留一个服务B的URL,当你的服务A的任务完成后,调用那个URL,以便它接受服务请求

The is sync communication.是同步通信。

For Async communication you can use kafka, where you can just publish an event over kubernetes network and make the other microservices subscribe to that event on the need basis.对于异步通信,您可以使用 kafka,您可以通过 kubernetes 网络发布事件,并让其他微服务根据需要订阅该事件。

In the first case, you can do something like在第一种情况下,您可以执行类似的操作

- env
  name: service-B-url 
  value: http://service-name:port-number.cluster.svc.local/{uri-path}

Its better to use nslookup service name to get FQDN of the service to be called最好使用 nslookup 服务名称来获取要调用的服务的 FQDN

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 向另一个微服务请求微服务 - Request microservice to another microservice 如果调用另一个微服务,如何为微服务编写集成测试 - How to write Integration test for a microservice if it calling another microservice 使用 spring 微服务从 kubernetes 集群中的服务中提取配置映射 - Extracting configmaps from services in kubernetes cluster using a spring microservice Spring Boot 微服务依赖 Jenkins 中的另一个微服务 - Spring Boot Microservice depends on another Microservice in Jenkins 将 WebClient 请求从微服务发送到另一个微服务时出现错误请求 - Bad Request when sending a WebClient request from a microservice to another microservice 如何使用docker镜像从另一个微服务调用一个微服务 - How to call one microservice from another microservice using docker images 微服务如何使用另一个微服务的安全性配置 - How can a microservice use the security config of another microservice 如何使用来自另一个微服务 Java 的 JWT 安全微服务 - How to consume a JWT secured microservice from another microservice Java Spring Boot微服务框架如何从一个微服务调用另一个微服务 - spring boot microservice framework how to call another microservice from one microservice Axon - 从另一个微服务消费事件 - Axon - Consume Event from another Microservice
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM