简体   繁体   English

使用 ASP.Net Core WebApi 在两个微服务之间进行通信

[英]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:关于如何以良好、干净的松耦合边界正确地异步执行此操作的一个示例(有很多选项)如下所示:

  • Your system leverages a message bus (I like AMQP, but there are other options)您的系统利用消息总线(我喜欢 AMQP,但还有其他选择)
  • When a customer is created, your Customers Service generates a customer_created event创建客户时,您的Customers Service会生成customer_created事件
  • Your 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更新(这是保持其本地数据存储更新 - 最终一致性
  • When your Invoicing Service creates an invoice that requires information from the Customers Service , it generates a cust_invoice_pending event当您的Invoicing Service创建需要来自Customers Service的信息的发票时,它会生成一个cust_invoice_pending事件
  • Your 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将它们绑定在一起)
  • Your Invoicing Service subscribes to and sees this data it needs which allows it to populate the rest of the invoice您的Invoicing 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:这需要更多的纪律和预先计划,但在这个简单的案例中,与您提出的分布式单体相比,这里是结果解决方案的优势:

  • You could swap out your Invoicing Service or Customers Service with something completely different without ever touching the code in the other service您可以用完全不同的东西替换您的Invoicing ServiceCustomers Service ,而无需触及其他服务中的代码
  • You've abstracted away concerns related to authz for both services - they share the Messaging Bus and so long as they can both get to/from that, neither knows about the other您已经为这两种服务抽象了与 authz 相关的问题——它们共享消息总线,只要它们都可以到达/从那里,彼此都不知道
  • You can allow future services to monitor and interact with this business process (creating and detailing an invoice) without ever changing code in these two services您可以允许未来的服务监控此业务流程并与之交互(创建和详细说明发票),而无需更改这两个服务中的代码
  • If one of the two services goes down, the other service continues to be fully responsive and any work waiting on the down service to come back online automatically gets queued - it persists outside the calling/receiving 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM