[英]Communication Between two microservices using ASP.Net Core WebApi
I have two Microservices.我有两个微服务。 First Service for Customers and Second service for Invoice Service.
客户第一服务和发票服务第二服务。
In Invoice MicroService, I will save only CustomerId.在 Invoice MicroService 中,我将只保存 CustomerId。 Using this CustomerId, I would like to retrieve all the related data for Customer Microservice.
使用此 CustomerId,我想检索客户微服务的所有相关数据。 But i don't know to communicate async.
但我不知道异步通信。
Any please get me some ideas.任何人请给我一些想法。
What you are proposing - specifically one service making a direct HTTP call to another - is called tight coupling between services and is an antipattern in Microservices.您提出的建议 - 特别是一个服务直接 HTTP 调用另一个服务 - 被称为服务之间的紧密耦合,并且是微服务中的反模式。 It essentially turns your application into a distributed monolith - it should be avoided.
它本质上将您的应用程序变成了一个分布式单体——应该避免它。
One example (there are many options) on how to properly do this asynchronously and with good, clean loose-coupled boundaries would be as follows:关于如何以良好、干净的松耦合边界正确地异步执行此操作的一个示例(有很多选项)如下所示:
Customers Service
generates a customer_created
eventCustomers Service
会生成customer_created
事件Invoicing Service
subscribes to these events and, when received, makes necessary internal customer_id
updates (this is keeping its local data store updated - eventual consistency )Invoicing Service
订阅这些事件,并在收到时进行必要的内部customer_id
更新(这是保持其本地数据存储更新 - 最终一致性)Invoicing Service
creates an invoice that requires information from the Customers Service
, it generates a cust_invoice_pending
eventInvoicing Service
创建需要来自Customers Service
的信息的发票时,它会生成一个cust_invoice_pending
事件Customers Service
subscribes to this event, and when it sees one, it generates a new message like invoice_customer_detail_push
on the bus (note, I'd use an invoice_id
in the payload of this message to tie them together)Customers Service
订阅了此事件,当它看到一个事件时,它会在总线上生成一条新消息,例如invoice_customer_detail_push
(注意,我会在此消息的有效负载中使用invoice_id
将它们绑定在一起)Invoicing Service
subscribes to and sees this data it needs which allows it to populate the rest of the invoiceInvoicing Service
订阅并查看它需要的数据,从而可以填充发票的 rest It may seem like a lot of extra work and complexity, and indeed it is.这可能看起来像很多额外的工作和复杂性,确实如此。 This is the "hard" part of successful microservices architecture.
这是成功的微服务架构的“硬”部分。 It takes more discipline and planning up front, but here are the advantages - in this simple case - of the resulting solution versus the distributed monolith you've proposed:
这需要更多的纪律和预先计划,但在这个简单的案例中,与您提出的分布式单体相比,这里是结果解决方案的优势:
Invoicing Service
or Customers Service
with something completely different without ever touching the code in the other serviceInvoicing Service
或Customers Service
,而无需触及其他服务中的代码So, if the benefits are worth the tradeoffs in your case, have at it.所以,如果在你的情况下这些好处值得权衡,那就去做吧。 But, any time you see two "microservices" communicating directly, it's likely you've lost sight a bit of coupling and need to re-examine how information flows through your system.
但是,每当您看到两个“微服务”直接通信时,您可能已经忽略了一点耦合,需要重新检查信息如何在您的系统中流动。
I think you need just to send a http request to your second service using the CustomerId
as query parameter (or post body, but probably not).我认为您只需使用
CustomerId
作为查询参数(或帖子正文,但可能不是)向您的第二个服务发送 http 请求。
You need to CreateClient
in your apps, before to do http requests.在执行
CreateClient
请求之前,您需要在您的应用程序中创建客户端。
Make HTTP requests using IHttpClientFactory in ASP.NET Core 使用 ASP.NET 核心中的 IHttpClientFactory 发出 HTTP 请求
var request = new HttpRequestMessage(HttpMethod.Get,
"domain_second_service/controller/method?customerId=X");
request.Headers.Add("Accept", "application/json");
request.Headers.Add("User-Agent", "HttpClientFactory-Sample");
var client = _clientFactory.CreateClient();
var response = await client.SendAsync(request);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.