{"id":16109,"date":"2022-01-11T17:31:02","date_gmt":"2022-01-11T17:31:02","guid":{"rendered":"https:\/\/swoopfunding.com\/za\/?post_type=swoop-engineering&p=16109"},"modified":"2023-11-29T12:04:46","modified_gmt":"2023-11-29T12:04:46","slug":"microservices-with-dapr-mini-series-pub-sub-tye","status":"publish","type":"swoop-engineering","link":"https:\/\/swoopfunding.com\/za\/swoop-engineering\/microservices-with-dapr-mini-series-pub-sub-tye\/","title":{"rendered":"Microservices with Dapr mini-series (Pub\/Sub + Tye)"},"content":{"rendered":"\n
Swoop provides instant loan offers (aka ILO as we call) to the customers powered by API to API automated journey. Since we will be connecting to a number of APIs, we needed a way to manage and abstract the common configuration in a distributed way. As our platform employs the sidecar pattern with envoy proxy and kong proxy (for API gateway), we were looking for options to separate the application core logic and common configuration as same as the sidecar pattern that can be secured and scaled at speed.<\/p>\n\n\n\n
Dapr is a Distributed Application Runtime, which may come across as Japanese… but let’s break it down.<\/p>\n\n\n\n
So, in English, the same way in which the .NET Framework uses the Common Language Runtime (CLR) that makes .NET developers life easier by providing services like thread management, garbage collection, type-safety and exception handling, Dapr provides services that simplify the development and execution of microservices.<\/p>\n\n\n\n
Dapr enables you to create microservices without all the complexities that are usually attached to microservices. It does this by abstracting away the details of using and managing state stores, secrets, pub\/sub infrastructures and enables the configuration of those dependencies outside of the microservice you\u2019re developing, in effect, reducing the complexity of the software we\u2019re developing. <\/p>\n\n\n\n
So, for instance, let\u2019s say you want to use Redis for messaging\u2026 no problem\u2026
Changed your mind and now want to use RabbitMQ instead? Again, no problem!<\/p>\n\n\n\n
You don\u2019t change a single line of code in your microservice to accommodate that change! All you do is update the relevant manifests belonging to your application and Dapr takes care of the rest!<\/p>\n\n\n\n
Dapr uses the Sidecar Architecture, which is a programming model where an application has another application that runs side by side with it, it resembles a sidecar attached to a motorbike and it’s a good analogy because the sidecar application shares the same lifecycle as its parent.<\/p>\n\n\n\n
When using Dapr, your application will be communicating with the Dapr sidecar that spins up at the same time your application does. The two will then communicate over either HTTP or gRCP.<\/p>\n\n\n\n For each microservice you have, you’ll have a Dapr sidecar attached to it where your app will be communicating with the Dapr sidecar and the Dapr sidecar will be communicating with other Dapr sidecars, which in turn will communicate with the microservice the sidecar is attached to.<\/p>\n\n\n\n A practical example would be a scenario where we are tasked with creating a system that monitors spikes of activity on a web application and sends notifications when a spike occurs. <\/p>\n\n\n\n The monitoring system, using Dapr would look something along the lines of this<\/p>\n\n\n\n The system is composed of two microservices:<\/p>\n\n\n\n Each of which has a Dapr sidecar attached<\/p>\n\n\n\n When MonitoringService finds a spike of activity, it will communicate with its own Dapr sidecar Dapr_MonitoringService, telling it that it needs to fire off a notification.<\/p>\n\n\n\n The Dapr_MonitoringService will then locate and inform the Dapr_NotificationService that a notification has to be fired passing along the details of the notification in question.<\/p>\n\n\n\n The Dapr_NotificationService will then tell the NotificationService to fire off that notification.<\/p>\n\n\n\n The communication between each service and sidecar occurs over either HTTP or gRCP, though this is abstracted away from the developer within the Dapr SDKs<\/a>, you could still manually post an HTTP request directly to the Dapr sidecar if you wanted to.<\/p>\n\n\n\n Dapr has a number of different building blocks that it exposes which allow us to do a number of different things\u2026<\/p>\n\n\n\n
of information between the two.<\/figcaption><\/figure>\n\n\n\n
\n\n\n\nBuilding Blocks<\/strong><\/h4>\n\n\n\n