Python Microservices Full Course - Event-Driven Architecture with RabbitMQ

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

This is fast-paced, but very interesting, even though I haven't the first clue about microservices, RabbitMQ or Django. But I like that it shows pretty clearly and simply how they pieces tie together.

👍︎︎ 1 👤︎︎ u/Endemoniada 📅︎︎ Apr 01 2021 🗫︎ replies

This sounds good. Have you posted code somewhere?

👍︎︎ 1 👤︎︎ u/mcstafford 📅︎︎ Apr 02 2021 🗫︎ replies
Captions
hey guys welcome to the channel in this example we will build a simple macro services example using python the front end of this app will be built with angular react view and swelt you can pick your favorite there the backend is composed into two apps not bin up and the main up the admin app will be built using django and the main app will be built using flask both will run in docker and will connect with their own database in mysql they will communicate with each other using rabbitmq events and in one case the main app will internally call the admin app now let's see what this app looks like before showing you the app i would like you to check scalable where i have all my premium courses here the most popular ones are laravel microservices and i will do the same for python also at the time of this recording i don't have the django microservices course ready yet but you can check scalablescripts.com if i released it on the time that you are viewing this video i will release two more courses regarding django this is the basic one i will release an advanced course using unit testing redis and stripe and the third iteration will be microservices you can also get access to all my courses for 15 dollars per month and a three-day free trial or 50 dollars every six months so you can choose whatever you want now let's go back to the app so this is the app that we will build it's a simple app where this is the admin and this is the main app in the main app we can like the products and in the admin app we can create the products it is very simple but don't get deceived because it it has two backend apps with different database communicating with rabbitmq which i open it on my right side so let's see an example for example i will like this product here and when i like the product you will see an event happening in ruby mq right now so the event has been triggered here and we have one ready event and now is zero so it means it was consumed so if we see here let's refresh the admin we can see that we got another like here so in essence it's really simple but this like has more functionality to it since it will internally call also the admin to get a random user and if we happen to like with the same user it will throw an error we can also create a new product so let's create a product and i will add a sample image we will see that rabbitmq will trigger another event so this is a product that we created another head event has been fired and when it's consumed here we will see the product here so it seems that the product is on the admin app on the main app so we got the product so we will see how this app will work by building them so let's start creating the apps so let's start building the admin app using django rest framework to create the project go to tutorial quick start and these are the commands to create a simple django project you have to install django and django rest framework from peep i already did it so i can directly go to the terminal and write django admin start project admin and the project is created i can go to the folder now and run python 3 manage that pi run server so the project is running now and i can copy this url and paste it to the browser and we can see that we successfully installed the django project now let's open the project with our ide and this is the app that we just generated now let's run docker so let's create two files one is docker file and the other one is docker compose dot yaml file so let's open the docker file first and what we will write here is first from what type of project is this so we want to get it from python and we have to specify the version of python i will pick the latest one which is 3.9 next we will add an environment variable python and buffered1 because this is useful to get logs so we can see whatever is happening and now we have to specify a working directory i will call it just up so before going further i will create a requirements txt file so in this file we can put all the dependencies that we will use in our app so i will paste them here we will use django jungle rest framework mysql client and java mysql so we will connect with mysql jungle course headers this is needed for the front end to access the backend and pica this is useful when we will connect with a rabbitmq now that we added our requirements let's go to the docker file and we will copy requirements.txt to up let's copy the name here requirements.txt so we are copying this file to the work to the working directory requirements so we are copying inside the docker container and now we have to install it so we have to run a command pip install minus r requirements so paste it so this will install all these requirements and in the end we will copy all the files so dot slash up dot in this case means all the files here so we are copying all the files to the app directory the last command is running the project so we have to add here python manage that pi run server and we also need to specify the host so the host will be 0.0.0 with port 8000 so we have to use it this way otherwise we cannot connect the port with our localhost now let's go to our docker compose file here we have to start by adding the version the latest version of docker compose is 3.8 and then we have to specify the services so for the moment i will just add the backend service which is our app here first we have to specify the docker file so build we have to put here context which which is this folder and the docker file which is docker file so basically this means that this docker compose file should use this docker file so that's it next we need to specify a port here so this port that is running here is inside the docker container is not our local host so we have to tell our localhost to listen to a specific port so in my case this is the dockerfile port and this is our localhost port so if we don't use this we won't be able to access the server and the last change is the volumes so add here volumes and inside we will specify dot column slash up this means that uh dots means all the files here are connected with the docker container every time we make a change in the app it will pass it to the slash to the docker file up and the opposite if change happens in the docker file it will reflect it here so that's it this is our app now we have to start it first we have to stop the server here and open the terminal and write docker compose up now he's building the backend is downloading all the required libraries for python and it will also install all the requirements here let's wait till it's completed so now everything is running correctly we can see that the development server is running and also we stopped the server from this terminal and if we refresh now we'll see we'll get the same page now this is running from the docker container if we stop for example we won't be able to see it so now we don't see it if i start again now it will be much more faster so we can see it so this is how you connect django with docker now we have to connect with the database we already have an sqlite database here but we will remove it because we will be using mysql so the first thing we have to add the mysql service i will call it db and here i will add an image which is mysql version 5.7.22 so we added mysql next we have to add restart always so we have to add this command because uh sometimes mysql stops and this will stop also our docker container and our app is no will not run so this will restart it again next we have to add some environment variables so these are some environment variables that we will connect with our backend so we have to add mysql database which is admin mysql user root mysql password i will also put root here and mysql root password root so these values you can pick whatever values you want so you can specify them here next we have to add volumes so this is really important so mysql when we start when it stores data it needs to map them to a folder so i will create a folder here db data which will map var lib mysql so all the files here will be reflected to a folder here called db data so we will see that once we start this container so we are finished now we have to add here ports this is not required at all i will just add it so we can see the data inside the database so the port will be the default port for mysql is 3306 and this is inside the docker container outside we will use 33066 or whatever port we want i didn't put the same port because it will conflict with my mysql installation that's it the last change is we should add here depends on db this means that this container should run first and then this container should run now let's close it and start it again now it's pulling mysql so we can see now the database is running so we can also see a db data folder here so this are the files that when we store anything it will be reflected here we don't have to touch this folder so this is just there to stay now let's connect with our ide so we need a new data source mysql and our port was 33066 root for the user root for the password and the database was admin let's test if we connect so we connected successfully now we have only one schema with no tables so now let's add some tables to the database first we have to create a new app so we have to go inside the docker container now so to go inside the docker container open a new terminal and write docker compose exec now we need the name of the service which in our case is backend backend sh we are inside now and now let's run a juggle command to create a new app so python manage that pi startup products so we created the app we can see here the products is created and we run the command inside the docker container now let's go to admin and we need to connect uh we need to add these products to the installed apps first we have to add here rest framework next we have to add here course headers and the products that we just created our course headers needs also a middleware so this is a middleware that we need to add and also it needs a new constant i will add it here in the end course origin allow all so we added everything we need now we need to connect with mysql here we have to change this sqlite to mysql the name of the database would be admin next we have to add the user which is root the password which is also root the host those is no in our case will be this service here so just db db and the port and in this case we won't be using this port but we will use the port inside this service so is 3306 so i 3306 so this is our connection with mysql we can remove again this sql light here and let's restart again the docker containers now we have to run migrations to create tables in mysql so let's go to the products and models so we will create two models here the first one is product this will extend from models model and we will add the fields for the product that we need title which is model's chair field with a max length of 200 let's do the same for the image and we need another field which is likes this will be a positive in the integer field and the default value will be zero so this is our product and we need another table now i'll call it user models model so we won't be really using the user table we will just use the user id so i'll just pass so with this this table will have only the id field and the product will have the id and these other three fields so now we have the models we need to generate migrations for these models and we have to migrate so open the terminal let's go inside the docker container again and here we have to write python manage that pi make migrations so we can see we generated migrations these are the migrations that we generated and to run them run python make manage that by migrate so we run all the migrations if we open the database now and refresh we'll see a bunch of tables we won't be using all these tables the only two tables that we are interested is the products which we have title image and likes and the user which has only the id field so this is how we connect django with mysql and we run migrations so we created our models and now django when we return the objects in an api we need to use serializers so now i'll create a folder a file here sorry serializers and inside i will create a class product serializer this will extend from serializers so let's import it first from rest framework import serializers and this will extend from serializers dot model serializer so inside we'll add another class which is meta and we have to tell which is the model in our case is a product let's import the product here so i will remove this so we said that the product is a model and now we have to specify the fields and i will use all of them so this is our serializer is very simple we won't be using a serializer for the user since we will return a random id there now let's go to the views and now we will use the serializers so here we will create all the five methods for rest api crowd so there are multiple ways to do it in my case i will use the view set so from so i will remove the render from django so rest framework sorry import view set and what we'll do with this view set is we will create a class product view set will extend from view sets view set so this will have a five methods that we want so i will add here list this will be the list of uh the products so i will list them one by one so this needs a request so i will add here create self request also so these are for slash products route for slash product slash so i will write it here this is r4 i will add a comment slash api slash products route i'll let pass here so it won't throw an error so this will have a get request this will have a post request now there are other endpoints that we will use which is def retrieve in itself request and the primary key which is known by default and this will be like this i will add it string id and i will pass it here so there are three methods for a single product one is retrieve the other one is update and the last one is delete so these are all the five methods now i will remove the this part so to use these methods we have to add here a new file urls i'll copy the same code from this admin urls here i'll paste it here and here we will use products i'll remove the slash and what is this next parameter it will be product view set let's import it from views we have to edit here as a view but we still need extra parameters we have to tell the get method will point to the list function so as we can see here we have a list and the get method will point to this list function the post method will point to the create function and we have to add another router right now for product slash the it needs a string which is a primary key and this get method now we will call the retrieve function we don't have a post we have a put here this will point to update and delete will point to destroy so i made a mistake this is destroyer so now all the methods are here now let's use them first let's add the list of the products so first let's get the products we have to get the product here objects all of them let's import this product from models so we got all the products now we need to use the serializer we just created so serializer is equal to product serializer let's also import imported so imported product serializer here the first parameter is the products and we need another parameter here we have to tell that many is equal to true so we have this is a list of the products and it needs to return an array now that we got the serializer we need to return response with serializer that data let's import also the response here from rest framework response so this is the import so this is our list function let's see how it looks like so i open postman here you have to install it to test the apis here i will use http localhost port 8000 slash i forgot also one other thing so we added the routes here i also need to add them here first let's import from django url 0 we will import include and let's add the new path which will start with api we'll add a slash here and we will include here our project which is products so this project dot urls which is this file now that we included now we can use it here is api slash products and normally we don't have any product so it will return an empty array now let's add the next function which is create here we'll get the serializer which is equal to product serializer and we have to pass the data from the request so data is equal to request the data so we will use those data and we have to first call is valid so if we send valid data to this serializer if not we will erase an exception and if it's valid it means we will go to this line it means we'll save it after we save it we return the response so response serializer.data and we have to put the status here so we'll import from status let's import status so i don't remember the exact import here but we'll use status is equal to status dot http 201 created let's import this status so it's from rest framework we'll import status here now let's use it so we will change this from get to post and we will send some json here we'll send some request we need the title title and an image which is image let's send the request and we return the title notice that we get also the likes here by default is zero we got also an id so our first product is created if we go to the get method now we'll see an array with a one product so this is the create function the retrieve is really simple we have to get the product which is equal product objects get the id is equal to this primary key now that we got the product we have to get the serializer is equal to product serializer with the product and we have to return in response serializer.data let's use it so this has id 1 so if we put a get request with id 1 we return a single object products returns an array of objects now let's go to the update let's copy this part i'll put it to the update and also destroy because we will use the object there so we will get the serializer here is equal to product serializer we have to pass the instance which is this product so the instance is a product with uh the data that it has and we have to update it with the new data which we will get it from the request the data so this is a project that the product that we have and these are the data that we want to update it now we have to do the same thing we have to ask if the serializer is valid then we have to save in the end we'll return a response with the serializer.data and we have to change the status which is 202 accepted so let's change product with id1 we'll send a boost reboot request here and this is new title and the new image send requests and we can see that these values are changed now also the status is 202 accepted last change is destroying the object this is really simple we have to call product delete and that's it so we have to return here response with the status status http 204 no content so let's delete this product send the request and we have 200 for no content we don't have a product and if we go to the products uh not delete method so we have still an empty array now here now the last call that we need is the user's call so we'll create another class here user api view so i will not use a view set now i will use an api view api view here so let's import it from res firm framework views we'll import api view and this will be really simple we need to define the get function we need the parameter here which is a request which we won't use it i will put an underscore here we have to get the users users user sorry objects all so let's import also user from the models so we imported it here now we got the user we have to get a random user so here i will import random to get a random user is equal to random choice users and this will be a random user now let's return the id response id is user.id so this is a user so let's add it also to the urls the path will be user and we have to import it here user api view user api view here as view so let's test this route so this will be a get request to the user this returns an error because we don't have any users in the database so i will manually add here some values so i'll add here one two three four and five so i'll add five users or five numbers now let's send the request again and i get five if i send it again it's four so the result is randomized so this was our main app now we have to create another app so we can communicate it so that the other app will be a flask app now it's time to create the main app and i decided to make it with flask so i created a main folder and inside i created an empty main.pi file so let's start first i'll paste here the requirements that we need so we need to install flask flask sql alchemy sql alchemy flask migrate flat scripts flash course request mysql client and pica so there are a lot of requirements that we need and now we will go to the other project and i will copy the docker files here and paste them here so the docker file will be the same here except for the command which will be simpler just python main dot pi so this will be the docker file a docker compose file for mysql it will be the same except this will be a new port i'll put 7 here and i will change the database name to main there is no need to change the database name because this is another container but we changed it so the port the default port for flask will be 5000 and 8000 is occupied with the jungle up so i will use 8001 here and that's it so the docker files are completed now let's create a flask up here so from flask import flask let's create the app is equal to flask name here so let's start a basic app so here i will add new route up route so just slash i'll create a function index and here i will return just hello so let's see if this works so i need to add a condition if name is equal to main then up around with debug is equal to true we also need the host so the host will be 0.0.0.0 so that's it now let's try to run it without the docker container python 3 main.play so we got this port let's copy it and open a new tab in postman so we have an error so is hello not sure why throw the narrow now let's do the same but this time with docker so we have all the requirements we don't need to change anything so let's start docker compose up and let's wait till it's completed so it's finished we can see a db data folder here and uh let's test it now the port will not be 5000 because this will be inside the docker container the port will be 8001 so here i'll change it with localhost port 8001 and that's it so we can see hello now so docker is working now let's add mysql to this main.pi file so let's import sql alchemy so from flask sql alchemy import sql alchemy and we need to use it here so up config and we need to set a constant so is sql alchemy underscore database underscore uri so with this we will add the db connection and what we need is to add mysql here and here we need to add the user and the password so root root in our case at now we need the host the host will be the service here which is db so the host will be db and in the end will be the table which is main so with this we can connect to this database i will add also course here since the front end will need it so let's import it from flask course i'll add it to the app and now the database is equal to sql alchemy and here we'll pass up now let's create two models that we need so class product this will extend from db model and let's add all the required properties for the product we need the id which is a db column db integer and also is a primary key now we have to make another change for the id here we will add here auto increment to false why did i add this field because the product will not be created in this app the product will be created in the jungle app and this app will just catch the event from rabbitmq and it will create the product and when we create the product we don't want the id to be auto increment because the id will be different than the the jungle up so if we want the same id we have to put it on to incremental false and to insert directly the id as it is from the django app so we'll see by example a little bit later now let's add the title db column db string and this will be a 200 character let's do the same for the image so this is a product notice that uh this product will be different than the product from the other app because we don't have likes here and the id is different so we can you we can think this as a different type of uh the same thing let's add also another class that we need each which is product user from db model so this will have an id which is a column db integer primary key to true so this will increment normally we need here the user id which is a db column db integer and the same for the product id we need also a unique constraint for user id product id and we need to set the name which is a user product unique so this unique constraint so let's import it from sql alchemy from here so this unique constraint will make sure that the combination of user id and product id should be unique we should not repeat it so we created the models that we will use now we need to migrate so to migrate i will create a new file here i'll call it manager and now here we need some imports so first from the main app we need to import up and db we need to import from flask migrate we need to import migrate and migrate command and from flask script we need to import manager now migrate will be equal to migrate with our app and our db here we have to get to the manager so manager is equal to manager we need to pass the app inside and the manager will add the command db we'll add the migrate command here so we will have a db command to run our migrations so we will see it now that we will add the if name is equal to main so we need to add manager run and that's it so if we open the terminal now and open a new tab we'll run here docker compose exec backend sh and now we are inside the docker container now if we run python manager dot pi and we will run the command db so we added the command here with help we can see now that we have a command that we can run now let's migrate so to migrate we have to run python manager dot py db init so we created some migration files we can see them here now we need to migrate them so now around python manage dot pi db migrate so it should be python manager that pi db migrate so we created a new table if we open the database now we'll connect with the database so this will be 7 here the user is root password is also root and the database will be main so let's connect we'll see a database now with the emblemic version but we won't see our tables because we need to run another command for it which is python manage that manager sorry that by db upgrade and now we run the migration if we refresh we'll see the table so this is a product table and this is a product user table so this is how we connect flask with mysql using docker now it's time to use rabbitmq to our python apps so i will use this service cloud ampq and you have to create a free account this is the free account i already have created so i can log in so this is my free account and what i will do here is this ampq url i will just copy it and now i can move to the app so i will go to the jungle app and in the products here i'll create a python file producer and for the moment i will just copy this url now let's send an event to rabbitmq so first we have to import pica this is a package that will help us send events and let's create a variable params is equal to pica url parameters and here we can put the url that we just pasted here as a string so we created the params this is annoying the connection now is equal to pica blocking connection and we will pass the params here so we created the connection with rabbitmq now we have to create the channel which is equal to connection channel so with this we created the channel now we can publish so to publish i will create a function publish inside i will put a method and a body so for a moment i will just keep it empty so we can see an example of the publish method so channel here basic publish we need to put an exchange i will put an empty string routing key this is a queue that we want to send the event so for the moment i will put it admin so i will publish it in the same queue as this one we dehit we need the body which is for the moment hello so that's it we can use this publish function now i will use it in the views when we list the products i'll just publish here so i imported the function and when we retrieve all the products we will publish hello now we created the producer now we need to create the consumer so here we need to create the consumer so the beginning is the same let's copy everything from the con the producer paste it here and now we have to declare queue so channel q declare and the queue will be admin so now we need the callback so call back this accepts a channel as a parameter method properties and body let's pass for the moment after the callback we have to call channel basic consume here we have to pass the queue which is admin and we have to pass on message callback which is this callback that we just created so we are basically consuming now we can print started consuming since we want to have a message to see if we started consuming and the channel start consuming so with this in the end we'll close it so channel close so we basically have finished now let's uh print a message also to know if we received a message so print here re sieve in admin so we received a message and i will print the body here if we go to hello there so that's it now let's try to open the terminal go to the docker file so docker compose exec backend sh here we have to call consumer.pi so python consumer.pine so we received this messages because i already sent some test events now let's test it let's go to the route for the products and we can expect when we get the products here to send an event hello there send we can we can see now we received an event hello now let's consume to the flask app so what i will do is copy this consumer file go to the flask app and here i'll paste the consumer so everything is the same except the queue here will be main so we are done now i can so i can start it with pub python consumer dot pi so we started consuming and in the producer here i will change the routing key to main so now i will not publish here in the admin app but i will publish here in the main app so let's see if this makes any difference let's send the event we can see hello here was from before but this received the hello now so to see the difference hello main i will say hello main so let's send it again we don't see a hello main here but we will see it here so this is how we send events to different apps using pica and rabbit mq now we successfully connect us with rabbitmq and we started consuming uh with the both apps now we don't want to have all the time to tabs here to start the consumer so we'll change the docker compose file now first let's change the docker file we will cut this command here and we will add it directly here so when we build we'll execute the command that we just cut so this is basically the same as before we run the command here but now we are running it here we did that because we will create another container now so is q i will call it which will have the same docker file and the the difference now is we will start a command now and the command will be python consumer dot pi so we will run the command directly here instead of manually running it every time this also depends on the database because we will insert data later so let's do the same for the flask app so i'll paste the queue here so this file is the same so the difference now is i will cut this command here i will put it here command python main api so now let's restart all the containers so i will run docker compose up build now we don't need the other tab let's do the same for this also let's remove this tab and we will build all the containers now we are building the queue so as we can see we are getting the q so the q is here so one other thing that we can make is by running it like this a docker compose up minus d for the database so d means we won't see the logs let's do the same for the backend or for the backend and the queue we need to see the logs so now we are seeing the logs from the queue and from the back end let's do the same here so minus d here db and docker compose up now let's see if this works so we are the products here let's send the request again we won't see it here but we should see it here also another change that we have to make if we close our containers and run them again we'll notice so if i do it with a database like this and the others double compose up you will notice that i will still get the cues but they they are not consumed yet so there is one last change that we have to add in our consumer and uh here we have to add auto ack true so with this we will consume the calls and we will not get them again so let's do the same for the other consumer and let's restart the containers again with build also this one will build so they are rebuilt now if we do the same with the database and we start the other apps we will see that the queue started consuming but we cannot see anything here so that's it now with these changes consuming becomes easier now we want to send events every time a product is created updated or deleted so we have to change this publish method we will add here a method and a body so for the method we have to create variable properties which is equal to pica basic properties and here we will pass a method and here we'll pass the properties is equal to properties also for the body so we need to import json here and the body will be json dumps body so we will send objects or anything but we have to convert it to json before we send it so this is our publish function now let's use it to our views let's remove this publish here and now when we create we will publish product created and we will send the serial laser the data let's do the same for product updated product updated and for the product deleted now instead of the object we'll send directly the primary key so these are the main events that we want to send and now let's make changes to the flask app here we have to get the data which is equal to json so i will import json here json loads body so in the django app we will convert it to json so now we will con convert it back let's print it so we can see what data are we sending and now we'll make the condition if properties content type is equal to product created will create the project so the product sorry so the product is equal to product with an id data id here title so let's import also this product here is equal to data title and image is equal to data image in the end let's import also the db db session add product ndb session commit so this is how you create a an object with sql alchemy now let's do the same for the other conditions so properties content type product updated so now we will get the product is equal to product query get data with an id and we have to change this product so product title is equal to data with the title and the image so image here in the end we will call session commit the last change is deleting so properties content type product deleted and in this case we'll get the product like this but we don't send an object we send directly the id so this data is the id we have to do it like this now db session delete product and tb session commit so these are all the three cases that we will receive from the other app now let's see if they work but before we should restart our containers just to be sure so build so there is no need to restart this one but just let's do it to be sure so this is started and now we have the products here which we don't have any let's create a product here i will send some data title is a title here and the image an image let's send the request so we successfully created pro the product in the django app but let's see if we created it in the flask app so we received the queue we probably forgot we should add here some print statements like product created product updated and product deleted but doesn't matter let's see the database and we can see this is a product now so we successfully created the product from an event from rabbitmq now let's try to update it so this product will put send a put request with a new title and a new image so i will not save it let's send the request this is updated and if we see the event we got the new title and the new image we don't see the print that we just added because we have to rebuild the containers now we're just checking the database which is correct and now let's try to delete it and this is received and if we refresh here we can see we have no product so now the databases for the product are in sync from the django app and for the flask app so we completed the jungle app now let's finish the flask up since we have only one route here so let's change this route to just api slash products and here we'll return a list of products so let's import jsonify from flask here and we will return jsonify product query all so let's see if this works so let's see if this works so the endpoint is api slash products and we got one product let's create one product here so we can see it if it works so we got this product that we just created so we should see it in the database notice that the id is three so when we created this product we don't start with one but the idea that we have it on our jungle app now let's send the request and we get an error because object of type product is not json serializable to fix this error we have to add here it data class and let's import it from data classes so with this now the class will be json serializable let's do the same for the product user send the request again and we got an md object but we want to see the data which we got id title and image here to do it we have to declare it here id should be an int title should be a string str so and image should be a string with these changes now we can see the id image and the title so with this we completed the main route now let's add another route so i'll call it like and this will have app route api products we need the idea of the product which is an integer the id and we have to like it so this is the end point the method is a post request and when we like we will send an id here so how do we like the product so now it's a little bit complicated we have to internally call the django up so we have to internally call this user api view here to get a random id and we have to assign it to the product user so first we have to create a variable i will call it rec and this will call requests dot get let's import requests from so we import directly requests here and now we have to get the endpoint so the endpoint is http localhost port 8000 slash api slash user and for the moment we will just return json if i request json not sure if i need json if i here but let's try it so products with an id1 we need to like it let's send a post request send and we get an error connection refused so what is the error here there is localhost here because right now we are inside the docker container and the localhost inside the docker container is 0.0.0 how to connect with another token container localhost we have to specify here that this is docker for mac localhost so this is the way the docker knows that we are referring not the container local host but another local also our local local so with this change let's try to send it again it seems we the server stops so we probably need to restart it send it again and now we got id2 so this is id from the user so this will be user here so not post get so this endpoint 8001 will call this other endpoint now that we got the response let's use it and create a product user so here we will get the json is request.json in the end if it is successful we will return a message success and we will make a try accept here so what we will try is we will create a product user which is equal to product user with user id is equal to json id and the product id is equal to the id that we just set this one from the url and now that we set this product user we have to add it db session add product user and we have to commit and here we'll send an event which i will send it later and if an error happens there is when the user tries to like again because this is an unique constraint if that happens we will abort let's import a board from flask board we'll put 400 here and you already liked this product so a board we imported from from flask here so let's try it let's send the request again send success if we see the database product user we created one and it will happen that we will fail it seems that our unique constraint didn't work so i probably made a mistake here but i won't focus on that let's finish this up by sending an event now we need to get the producer i'll copy the same producer here so everything now is the same and we have to use it here now if this is completed we have to add here producer so publish let's import it product liked and we have to pass id of the product so let's we need to import it here so this is our flask up only one thing that i think is easily fixable now let's make the last change here on the consumer we will get the data here which is equal so let's import json json loads body let's print the data and we here will like the product so we'll get a product first which is equal to product let's import it so from products models will import product that objects get id is equal to data so i will directly make this an id so we got the id now product likes will equal to product likes plus one so we will increment it by one in the end we'll save it and we will print that product is product likes increased just to be sure let's rebuild the containers and we will rebuild this too so this is completed now we have the products here so we have two products and they have zero likes now for the product with an id2 let's see if we have that product in the main so three i guess so for the product with id3 i'll post here to like it and this should increase the likes so i guess we have an error received in main so my bed when i made the producer i should change here the routing key should be admin so let's rebuild and this should receive the event now let's like again not here here so this should not receive the event it didn't this did receive the event actually we got an error here so uh requested setting installed ups but settings are not configured this error happens because this file is outside of django and we are using the product here and uh to use a product regularly we have to load so we have to set up jungle before getting the product so we have to import here os and django we have to set uh an environment variable django sentence sending settings module is equal to admin settings so this settings here and we have to add django setup here with this we won't have that error so let's rebuild the containers we can see now that we started consuming so let's try it again now success let's send it again and we see the likes are increased here so we received the event so now we are communicating back and forth with both apps so this was the app that i wanted to show i hope you liked it if you have anything let me know in the comments thank you and also don't forget to like and subscribe
Info
Channel: Scalable Scripts
Views: 16,623
Rating: 4.9358287 out of 5
Keywords:
Id: ddrucr_aAzA
Channel Id: undefined
Length: 84min 38sec (5078 seconds)
Published: Mon Dec 28 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.