Refactoring - Restful API Microservices
Start course
Difficulty
Advanced
Duration
2h 10m
Students
1894
Ratings
4.5/5
starstarstarstarstar-half
Description

In this advanced course, we take a legacy monolithic .Net application and re-architect it to use a combination of cloud services to increase scalability, performance, and manageability. 

Learning Objectives 

This course will enable you to:

  • Learn the principles and patterns associated with microservices
  • Understand the principles and patterns associated with Restful APIs
  • Learn important requirements to consider when migrating a monolithic application into a microservices architecture
  • Master the benefits of using microservices and associated software patterns and tools to build microservice-based applications at speed and scale
  • Understand tradeoffs between different architectural approaches
  • Become comfortable with modern open source technologies such as Dotnet Core, Docker, Docker Compose, Linux, Terraform, Swagger, React
  • Become familiar with Docker and Container orchestration runtimes to host and run containers, such as Docker Compose, Amazon ECS using Fargate, and Amazon EKS

Prerequisites

  • A basic understanding of software development
  • A basic understanding of the software development life cycle
  • A basic understanding of DevOps and CICD practices
  • Familiarity with Dotnet and C#
  • Familiarity with AWS

Intended Audience

  • Software Developers and Architects
  • DevOps Practitioners interested in CI/CD implementation
  • Anyone interested in understanding and adopting Microservices and Restful APIs within their own organization
  • Anyone interested in modernizing an existing application
  • Anyone interested in Docker, and Containers in general
  • Anyone interested in container orchestration runtimes such as Kubernetes

Source Code

Transcript

- [Instructor] Welcome back. In this lecture, we're gonna begin to build out each of the service components. Recalling that in the previous lecture, each of these projects was created as a ASP.NET web API project. So let's begin. We'll start with the AccountService. So expanding it out, the first thing we'll do is add in a Models directory. The Models directory will contain our models that we'll use and they will be a direct copy as they are already in the monolithic application. 

So here, we'll add a Consumer model. So in this case, under General, we'll go with Empty Class and we'll name it Consumer.cs. Click on New. And then we'll paste in the code from the monolithic application equivalent model. Okay, so the consumer represents the shopper who logs on into the system and they have an ID property, Firstname property, Surname property, and Age property. And with equivalent getters and setters. So save that and that's our model done for the AccountService. 

The next thing we'll do is under Controllers, we'll delete the default controller. And we'll add a new one. Under ASP.NET, if we scroll down to the bottom and we select Web API Controller Class. And we'll call it ConsumersController. Click New. Okay, so this is our controller class. And now, we'll need to update each of the methods, so that we have the right functions and the right behavior exposed through our RESTful API. So let's work on the Get method. So we'll change the signature of this. It will be public. 

It will return an ActionResult of type IEnumerable of type Consumer. And it will be a Get taking no parameters. Okay, so you notice the Consumer model type is not available, so we'll need to add it in using and... The namespace was AccountService.Models. So that's what we need to import. Okay. We now need to update the return. So here, we're simply gonna do var consumers is equal to a new list of Consumer. And we'll return consumers. Okay, so within a list, we need to instantiate a number of consumer objects.

 So new Consumer. And then we specify the properties for the Consumer. So in this case, it has an ID property, we'll set it to 111. There's a Firstname property, we'll set it to Jeremy. It has a surname. Gonna do Cook. And an age. 40. Okay, we'll repeat that. So set up another one. And another one. And one more. Okay, we'll change the IDs. And... Bob Smith. He can be 48. 

Now, remember, we're starting out the backend, so in a production system, this will probably be a database call. So we're just doing... We're hardcoding the data value just to get through the demonstration quicker. Okay, so that completes the Get method. And I'll just update the documentation, so this will be consumers. Okay, work on the next one. And we'll update the signature of the method. So again, it's public. It will be an ActionResult of type Consumer and it is Get and it takes a parameter of ID, of type int. And then that's simply going to return a consumer whose ID is the ID that we passed in. 

So updating this. It will be consumers. So we now do the Post. And I'm just gonna copy some code that I've already got, just to be a bit quicker. So we're posting to the api/consumers endpoints. And we post under a consumer. And from that, we'll save the consumer and return back the saved_consumer. For the Put, we, again, got some pre-formatted code that we'll just use. And we'll, again, update the comment. So for a Put, we send the Put view to this path and we pass in two parameters, an ID and a consumer. And then we're doing an update on that consumer. And for Delete, we'll just leave as empty. 

Okay, we'll save that and we'll actually compile. Check for any errors. Okay, so that is good. So at this stage, we should be able to fire up this web API. So let's now change this project and we'll set it as the startup project. And then we'll also take a look at the run options. So we're going to run on this server off https://localhost and this port. We'll change the open URL parameter. We'll update this to be consumers. Oop, nope, that's incorrect. It should be api/consumers. And we'll click OK.

 So if all has gone well, when we start this up, we should get some JSON that comes back because we've specified that we are gonna call this path on launch. So it's good to go. And... Okay, what happened? Okay, so we've already got some inbound port 5001, which are my docker containers. So let me jump into my terminal. And we're gonna get to Docker later on in the course, but for now, I need to shut down some existing containers that I've got running. So let's stop 9f, d5, and 26. 

Okay, that's good, so that should've released the port. And we can stop and try again. Excellent, so it's worked. So I've made a call to this path and we've got back this data. So the other thing we can do is we can take a copy of this and jump to our terminal. And we can use curl. So we do curl

 

- i, and then the path. Okay, so we're getting a warning and this is our warning, so we need to exclude that. So we do -k. And there we go. So we can see that we've got a HTTP 200 response. And here is our JSON from our call to our web API. So the next thing we can do is we can see the structure of that, of that JSON that's being returned by... We'll remove the -i. And then we'll pipe out to jq. And there we go. 

So that is indeed the same data that we set up in the AccountService web API. So we now need to do the same thing for the remaining two services. So we've completed the AccountService and we need to do the same setup for InventoryService and ShoppingService. So I'll do that in the background and skip ahead to keep the demo moving along. In the meantime, go ahead and close this lecture and I'll see you shortly in the next one, where we'll work on the Store2018 project.

About the Author
Students
126297
Labs
66
Courses
113
Learning Paths
180

Jeremy is a Content Lead Architect and DevOps SME here at Cloud Academy where he specializes in developing DevOps technical training documentation.

He has a strong background in software engineering, and has been coding with various languages, frameworks, and systems for the past 25+ years. In recent times, Jeremy has been focused on DevOps, Cloud (AWS, Azure, GCP), Security, Kubernetes, and Machine Learning.

Jeremy holds professional certifications for AWS, Azure, GCP, Terraform, Kubernetes (CKA, CKAD, CKS).