[英]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.