如何从服务总线调用API

时间:2018-07-04 11:52:21

标签: asp.net-mvc microservices publish-subscribe azureservicebus servicebus

我正在尝试学习构建基于微服务的应用,其中微服务将通过某些服务总线进行通信。就我而言,我正在使用Azure Service总线。

通过参考以下链接,可以设置初始系统。邮件已到达队列。

https://docs.microsoft.com/en-us/azure/service-bus-messaging/service-bus-dotnet-multi-tier-app-using-service-bus-queues

但是作为下一步或模仿真实应用程序,我有OrderAPI来处理订单。

这是我的WorkerRole类的样子

namespace OrderProcessingRole
{
 public class WorkerRole : RoleEntryPoint
 {
    // The name of your queue
    const string QueueName = "ProcessingQueue";

    // QueueClient is thread-safe. Recommended that you cache 
    // rather than recreating it on every request
    QueueClient Client;
    ManualResetEvent CompletedEvent = new ManualResetEvent(false);

    public override void Run()
    {
        Trace.WriteLine("Starting processing of messages");

        // Initiates the message pump and callback is invoked for each message that is received, calling close on the client will stop the pump.
        Client.OnMessage((receivedMessage) =>
            {
                try
                {
                    // Process the message
                    Trace.WriteLine("Processing Service Bus message: " + receivedMessage.SequenceNumber.ToString());
                }
                catch
                {
                    // Handle any message processing specific exceptions here
                }
            });

        CompletedEvent.WaitOne();
     }

     public override bool OnStart()
     {
        // Set the maximum number of concurrent connections 
        ServicePointManager.DefaultConnectionLimit = 12;

        // Create the queue if it does not exist already
        string connectionString = CloudConfigurationManager.GetSetting("Microsoft.ServiceBus.ConnectionString");
        var namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
        if (!namespaceManager.QueueExists(QueueName))
        {
            namespaceManager.CreateQueue(QueueName);
        }

        // Initialize the connection to Service Bus Queue
        Client = QueueClient.CreateFromConnectionString(connectionString, QueueName);
        return base.OnStart();
    }

    public override void OnStop()
    {
        // Close the connection to Service Bus Queue
        Client.Close();
        CompletedEvent.Set();
        base.OnStop();
    }}}

我不确定,当服务总线到位时,如何以及在何处调用OrdersAPI。

我猜应该在

OnStart() -> 
Client.OnMessage((receivedMessage) =>
            {
                try
                {
                   //Order API call here
                }
                catch
                {

                }
            });

如果我的猜测正确,那么我如何调用托管在其上的OrderAPI

http://localhost:8090/api/order

谢谢。

2 个答案:

答案 0 :(得分:0)

假设OrdersAPI是您的Web API终结点,它从浏览器接收请求,它将构造Azure Service Bus消息并发送到队列。然后,您的工作人员角色将收到这些消息并进行处理。无法在Web API中执行处理。

答案 1 :(得分:0)

Orders API应该在内部调用

print (df)
   bill_no       item_name
0      201      Vegetables
1      202         Noodles
2      203  Dairy Products
3      204      Vegetables
4      205       Ice Cream

使用HttpClient触发API。这样,只要从队列接收到消息,就会调用API。

由于工作角色是在Azure环境中托管的,因此只能从此处调用公共位置托管的API。它不标识本地计算机中托管的API。尝试在mapAzure App Service中托管API。