title
stringlengths 3
46
| content
stringlengths 0
1.6k
|
---|---|
21:625 | syntax = `proto3`; |
21:626 | option csharp_namespace = `GrpcMicroService`; |
21:627 | import `google/protobuf/timestamp.proto`; |
21:628 | package purchase; |
21:629 | message PurchaseMessage { |
21:630 | string id = 1; |
21:631 | google.protobuf.Timestamp time = 2; |
21:632 | string location = 3; |
21:633 | int32 cost =4; |
21:634 | google.protobuf.Timestamp purchaseTime = 5; |
21:635 | } |
21:636 | |
21:637 | The automatic generation of the message classes is enabled in both projects with the same XML declaration in their project files: |
21:638 | <ItemGroup> |
21:639 | <Protobuf Include=`Protosmessages.proto` GrpcServices=`Client` /> |
21:640 | </ItemGroup> |
21:641 | |
21:642 | Both projects need to specify Client code generation since no service needs to be created. |
21:643 | To communicate with the RabbitMQ server, both projects must add the RabbitMQ.Client NuGet package. |
21:644 | Finally, FakeSource also adds the Polly NuGet package because we will use Polly to define reliable communication strategies. |
21:645 | The ExecuteAsync method of the client project is a little bit different: |
21:646 | protected override async Task ExecuteAsync(CancellationToken stoppingToken) |
21:647 | { |
21:648 | Random random = new Random(); |
21:649 | var factory = new ConnectionFactory{ HostName = `localhost` }; |
21:650 | IConnection? connection =null; |
21:651 | IModel? channel = null; |
21:652 | try |
21:653 | { |
21:654 | while (!stoppingToken.IsCancellationRequested) |
21:655 | { |
21:656 | ... |
21:657 | } |
21:658 | } |
21:659 | finally |
21:660 | { |
21:661 | if (connection != null) |
21:662 | { |
21:663 | channel.Dispose(); |
21:664 | connection.Dispose(); |
21:665 | channel = null; |
21:666 | connection = null; |
21:667 | } |
21:668 | } |
21:669 | } |
21:670 | |
21:671 | Communication requires the creation of a connection factory, then the creation factory generates a connection, and the connection generates a channel. The connection factory is created outside of the main loop since it can be reused several times, and it is not invalidated by communication errors. |
21:672 | For the connection and channel, outside of the main loop, we just define the variables and where to place them since they are invalidated in the case of communication exceptions, so we must dispose of them and recreate them from scratch after each exception. |
21:673 | The main loop is enclosed in try/finally to ensure that any channel/connection pair is disposed of before leaving the method. |
21:674 | Inside the main loop, as a first step, we create the purchase message: |
21:675 | var purchaseDay = DateTime.UtcNow.Date; |
21:676 | //randomize a little bit purchase day |
21:677 | purchaseDay = purchaseDay.AddDays(random.Next(0, 3) – 1); |
21:678 | var purchase = new PurchaseMessage |
21:679 | { |
21:680 | //message time |
21:681 | PurchaseTime = Timestamp.FromDateTime(purchaseDay), |
21:682 | Time = Timestamp.FromDateTime(DateTime.UtcNow), |
21:683 | Id = Guid.NewGuid().ToString(), |
21:684 | //add random location |
21:685 | Location = locations[random.Next(0, locations.Length)], |
21:686 | //add random cost |
21:687 | Cost = 200 * random.Next(1, 4) |
21:688 | }; |
21:689 | |
21:690 | Then, the message is serialized: |
21:691 | byte[]? body = null; |
21:692 | using (var stream = new MemoryStream()) |
21:693 | { |
21:694 | purchase.WriteTo(stream); |
21:695 | stream.Flush(); |
21:696 | body = stream.ToArray(); |
21:697 | } |
21:698 | |
21:699 | Before executing the communication, we define a Polly policy: |
21:700 | var policy = Policy |
21:701 | .Handle<Exception>() |
21:702 | .WaitAndRetry(6, |
21:703 | retryAttempt => TimeSpan.FromSeconds(Math.Pow(2, |
21:704 | retryAttempt))); |
21:705 | |
21:706 | The above policy is an exponential retry, which, in the case of an exception, waits for an exponentially growing amount of time. So, if six attempts are made, then the second attempt is made after 2 seconds, the third after 4 seconds, the fourth after 8 seconds, and so on. If all attempts fail, the exception is rethrown and causes the message to be lost. If it’s important that messages can’t be lost, we can combine this strategy with a circuit break strategy (see Resilient task execution in Chapter 11, Applying a Microservice Architecture to Your Enterprise Application). |
21:707 | Once we have defined the retry policy, we can execute all the communication steps in the context of this policy: |
21:708 | policy.Execute(() => |
21:709 | { |
21:710 | try |
21:711 | { |
21:712 | if(connection == null || channel == null) |
21:713 | { |
21:714 | connection = factory.CreateConnection(); |
21:715 | channel = connection.CreateModel(); |
21:716 | channel.ConfirmSelect(); |
21:717 | } |
21:718 | //actual communication here |
21:719 | ... |
21:720 | ... |
21:721 | } |
21:722 | catch |
21:723 | { |
21:724 | channel.Dispose(); |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.