Microservices with Kafka: ECommerce Example (.NET 8) — Transcript
Full transcript
- 0:00hello everyone welcome to code
- 0:02overdose in this video I will show you
- 0:05how to implement a microservice
- 0:07architecture inet 8 using the message
- 0:10broker Kafka I will use an example of an
- 0:14e-commerce app if you don't know what
- 0:16microservice architecture is comment
- 0:19below to let me know and I will make
- 0:21another video for
- 0:23you now you can see this overview of our
- 0:26solution our solution will have two
- 0:29services product service and Order
- 0:31service each using different databases
- 0:35and capable of making API calls to each
- 0:37other in this sample I will use Entity
- 0:40framework code first and SQL server for
- 0:43these Services these two services will
- 0:46use Kafka to produce and consume
- 0:49messages additionally there will be one
- 0:52web app to show the interface which will
- 0:55call the apis from the product service
- 0:57and Order
- 0:58service okay before we start creating
- 1:01the solution don't forget to subscribe
- 1:03to get the latest content and now let's
- 1:15start now let's create a
- 1:19project first of all we will create a
- 1:22web frontend project using Blazer web
- 1:27app name it e-commerce do web
- 1:33and set the solution name to
- 1:36e-commerce choose the target.
- 1:42net8 next we will create a product
- 1:47service search API and create a web
- 1:54API I will name it e-commerce
- 1:57productservice
- 2:02then create another web API for the
- 2:04order
- 2:07service and name it e-commerce do
- 2:10orderservice
- 2:19add a library to store our
- 2:26models and name it e-commerce dood
- 2:34to run this I will add a project.net
- 2:37Aspire app
- 2:47host and name it e-commerce app
- 2:51host now we have the basic folder
- 2:54structure for our microservice
- 2:56architecture solution in the apphost
- 2:59project open
- 3:01program.is we will configure our project
- 3:03here first I will configure the product
- 3:15service we need to add a project
- 3:18reference to see the options
- 3:20here apphost should have references to
- 3:23the web product service and Order
- 3:25service projects
- 3:30the rest of the projects should have
- 3:31references to the model
- 3:48library now we can add the product
- 3:50service
- 3:58project then add the same for the order
- 4:01service
- 4:10project next add the web frontend
- 4:26project with references to product
- 4:29service service and Order
- 4:35service let's set startup project to
- 4:38apphost and run
- 4:47it I will open each project to check for
- 4:51any errors
- 5:00it seems to be working okay
- 5:02now next let's create some model for
- 5:06product and order I will change the name
- 5:08to product
- 5:13model and product should have integer
- 5:18ID string
- 5:23name decimal price
- 5:29integer
- 5:33quantity and another for order
- 5:44model should have integer
- 5:48ID string customer
- 5:53name integer product ID
- 6:00integer
- 6:05quantity and datetime Order
- 6:14date now let's create DB context for
- 6:17product service I will create a
- 6:20folder name it data and create a
- 6:26class name it product DB contact
- 6:33to use Entity framework I will install
- 6:36Entity framework
- 6:39package search
- 6:47Entity and install package Entity
- 6:49framework
- 6:53core and entityframework core. SQL
- 6:57Server
- 7:03now it can inherit DB
- 7:09context and create a Constructor for
- 7:23it run
- 7:26database ensure created to autocreate
- 7:29dat
- 7:31database and create a DB set for product
- 7:39model then let's init some data for
- 7:41products by override on model
- 7:48creating I will put some initial data
- 7:50for it
- 8:28then go to program and add DB context
- 8:31for product DB
- 8:42context have option use SQL server and
- 8:45put the connection string into
- 8:49it here I use SQL Express and set
- 8:53database name is e-commerce product
- 9:02then create a controller to return a
- 9:04list products create an empty API
- 9:07controller name it product
- 9:13controller let's inject product DB
- 9:16context by primary
- 9:24Constructor and create an HTTP get
- 9:27method
- 9:30return list product
- 9:36model have name get
- 9:40products and return list of
- 9:50products let's run to check that our
- 9:52product service doesn't have any errors
- 10:00open product API let's execute API get
- 10:03list
- 10:05product great it return a list of
- 10:08initial products then I will create
- 10:10another DB context for order
- 10:20service name it order DB
- 10:25context then install the package like we
- 10:28do in product service
- 10:43now it can inherit DB
- 10:47context create Constructor
- 11:05create DB
- 11:12set then go to program and add DB
- 11:22context and the option use SQL server
- 11:25with database name e-commerce order
- 11:37then create an empty API
- 11:42controller name it order
- 11:52controller let's inject order DB
- 11:57service and create an API I to return
- 12:00list of
- 12:25orders let's run app again to check it
- 12:28doesn't have any errors
- 12:38great it returns success
- 12:43200 now I will create a page to show
- 12:46list of products where the user can see
- 12:49and buy a product in Pages folder create
- 12:52a razor component name it product list
- 13:01Define the root is
- 13:06products defin a list
- 13:18products and override uninitialized
- 13:26async and get product here by inject
- 13:29HTTP
- 13:37client and get from Json
- 13:54async to get the path of product service
- 13:57let's go to properties folder
- 13:59in launch settings you can copy the
- 14:01https
- 14:05address then put it in
- 14:11here about the UI of products list I
- 14:15will use bootstrap card for it search
- 14:18bootstrap
- 14:23card and I will copy this
- 14:39I will for each list of product and each
- 14:41product will be displayed as a
- 14:52card it will show product name
- 15:05price
- 15:09quantity and button
- 15:15by and remove this style let's run to
- 15:18see
- 15:25it we have a list of products here
- 15:29when I click by I want to go to the cart
- 15:32page where we can create an
- 15:37order in Pages folder create a razor
- 15:40component name it cart.
- 15:46Riser Define the route is
- 15:52cart and have param product
- 15:57ID then Define par
- 15:59product
- 16:06ID and defined a model for
- 16:17order and another model for
- 16:27product override uninitialized async and
- 16:31set product ID for
- 16:41order then get product information by ID
- 17:21I will create a simple form here
- 17:38it will have an input for customer name
- 18:02an input for showing product
- 18:11name and an input for
- 18:17quantity now we don't have an API to get
- 18:20product info by ID let's create it in
- 18:23product
- 18:27controller create create a HTTP get
- 18:30allowed to pass
- 18:46ID and return the product map with that
- 18:48ID
- 19:07then go to product list and set the URL
- 19:09when click buy button to cart SL product
- 19:21ID okay let's run it now
- 19:34Click
- 19:35by we missing a button to create the
- 19:45order I will add button here
- 20:03and it will call an HTTP post to order
- 20:05service to create an
- 20:22order let's copy the URL of order
- 20:24service
- 20:48if the API returns success I will alert
- 20:50an success message
- 21:13let create an API to create order in
- 21:16order
- 21:20service create a hhtp post
- 21:34set order date and saving it
- 22:00it should be JJ's
- 22:04runtime let's run it
- 22:14again let's create a new
- 22:18order set customer name and
- 22:23quantity it shows success
- 22:33I will create a page to show a list of
- 22:43orders I will copy from product
- 22:52list set route is orders
- 23:03change to order and fix the
- 23:24URL I will create a table to show order
- 23:27information for
- 24:24then create another link to navigate to
- 24:26the list of orders
- 24:41we have one order
- 24:44here but the quantity of the product has
- 24:47not changed because this information
- 24:49comes from the product service so when
- 24:52order service creates a new order it
- 24:54should send a message to product service
- 24:57then product service can update the
- 24:59quantity of that product we will use
- 25:01Kafka to produce and consume the
- 25:03messages here when finish creating an
- 25:07order we can produce a message here but
- 25:10you must run Kafka Client First I will
- 25:13use Docker to run it so make sure you
- 25:15have Docker desktop on your machine in
- 25:18apphost Project create a file name it
- 25:21Kafka diml
- 25:29here I config two
- 25:35Services one is Zookeeper use image from
- 25:38confluent Inc have Port
- 25:412181 and one is Kafka use image from
- 25:45confluent Inc also have port
- 25:489092 and set some environment variable
- 25:50to connect Kafka to zookeeper then let's
- 25:53start the docker desktop
- 26:00and open the terminal from App
- 26:04host and run Docker compose file name
- 26:13up the container will be created
- 26:26now you can see the log of Kafka the
- 26:30Kafka client work okay
- 26:34now then I will create a Kafka producer
- 26:37in order
- 26:41service create a folder
- 26:47Kafka and create a class name it Kafka
- 26:55producer create an interface
- 27:07and add scope for
- 27:20it it will have a function produce a
- 27:23sync allowed to pass topic and Kafka
- 27:25message
- 27:33we need to install confluent Kafka
- 27:48package and message with type string and
- 27:51string
- 28:00Define a field eye
- 28:11producer and in
- 28:14Constructor Define a consume
- 28:24config have group ID is Order Group
- 28:32bootstrap servers is address of CFA
- 28:37client set auto offset reset to
- 28:45earliest then use producer Builder
- 29:00then just return prodor async
- 29:07function in order service let's inject
- 29:11iafa
- 29:15producer then we can use produce a sync
- 29:20function set the topic is order topic
- 29:33and message have key is order
- 29:37ID and value is Json order
- 29:53model I will set a debugger here
- 30:07create a new
- 30:11order press F10 to go to the next
- 30:15step if it passes this step it will
- 30:18successfully push the message to
- 30:23Kafka now I will create a Kafka consumer
- 30:26in product service to consume that
- 30:29message create folder
- 30:32Kafka and create a class name it kofka
- 30:41consumer it should be inherited
- 30:43background service so it will run when
- 30:45the app starts let's Implement abstract
- 30:50class then add hosted service for it
- 31:00defined a function name it consume
- 31:05async allow to pass topic and
- 31:08cancellation
- 31:14token Define a config like we do for
- 31:17Kafka producer and use consumer
- 31:23Builder consumer will subcribe the topic
- 31:29then we need a while loop to get consume
- 31:41result then we can get the message from
- 31:43that
- 31:46topic and convert it to order model
- 32:01to use product DB context to update the
- 32:04product to DB we need to inject service
- 32:07scope
- 32:10Factory then create a
- 32:18scope and get required service product
- 32:21DB context
- 32:30find the
- 32:37product and update the
- 32:56quantity then close the
- 33:01consumer I will put some debugger
- 33:12here and execute async should return
- 33:15task
- 33:20run call the function consume a
- 33:25sync set topic is order
- 33:30topic now let's run the
- 33:41app press F10 to go to the next
- 33:49step you can see it received the
- 33:51previous message now let's continue
- 33:58and you can see the quantity have been
- 34:00updated now I will create a new order to
- 34:05test clear the debugger every time order
- 34:09service vve produce a message the
- 34:11product service will receive the message
- 34:13and handle it right
- 34:17away and the quantity has been updated
- 34:20wonderful in a real project before
- 34:23updating the quantity we would have a
- 34:25lot of steps to check but in in this
- 34:28sample I just want to show you the flow
- 34:30to implement it if you have any
- 34:32questions about this please comment
- 34:35below I will reply to you as soon as
- 34:38possible thanks for watching and see you
- 34:40in the next video
- 34:43[Music]
About this transcript
This page contains the full transcript of Microservices with Kafka: ECommerce Example (.NET 8) by Code Overdose, generated from the public captions YouTube serves with the video. The transcript has 1,827 words across 336 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.
What you can do with it
Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.
Free YouTube transcript tool
YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.