Convert .NET Monolithic app to Microservices using AWS Microservice Extractor [Part 2]


Part 1?: Setup Microservice Extractor?, ASP.Net sample app review, ASP.NET application Onboarding
Part 2: Launch visualization, AWS Microservice Extractor for?.NET AI-Powered Recommendations, Microservice extraction, Application refactoring
Part 3: Testing, Deploy the new micro-services app on EKS & ECS (Under processing )

Agenda

1. Introduction
2. Launch visualization
3. AWS Microservice Extractor for?.NET AI-Powered Recommendations
4. Microservice extraction
5. Application refactoring
6. Conclusion

1. Introduction

we are already discussed the following points in Part 1

  1. Setup Microservice Extractor
  2. ASP.Net sample app review
  3. ASP.NET application Onboarding

Now in this Part 2 we are going to discuss the ASP.NET Class visualization, AI-Powered Recommendations, Microservice extraction and Application refactoring.

2. Launch visualization

If the onboarding status changes to Success, you can open up the visualization either by pointing your mouse at View dependency graph in top green banner or from Applications list selecting Launch Visualization.

Microservice Extractor isolates and identifies the logically grouped components to be extracted as independent services, which are visible in the tab Visualization (Nodes dependencies). A link between nodes indicates a connection between them. Normally, nodes in the visualization are represented on Project level. Filters allow you to view certain project level or namespace level nodes. In addition, you may also double-click on specific nodes to navigate the namespace level or class level aggregations.

3. AWS Microservice Extractor for?.NET AI-Powered Recommendations

What are AI-powered recommendations?

The new AI-powered recommendations system relies on an ML model that inspects the source code of your own project. When the analysis by Microservice Extractor is done, your classes are organized within this tool into prospective candidates for microservices creation.

This improved feature is especially useful for those customers that do not have the required skills to update their applications. This is typically the situation for many companies that have applications with a long ‘shelf’ life in the market, where the original developers are no longer accessible or at times developed by third parties and it becomes challenging to upgrade.

Choosing the right recommendation option

The Microservice Extractor provides three extraction options: manual classification, heuristic assessment, and AI-based recommendations. These are a few of the methods that you can use to grouping your classes for microservices hence enabling you as the developer to select an approach which is flexible and effective at least depending on how comfortable one should get with special building blocks without relying 100 percent on classical ones. Crucially, this first choice of extraction does not restrict your ability to select a different approach through the user interface later on as you contemplate the emerging needs for these new microservices.

If you have a well-developed domain understanding of some application planned for refactoring, the manual classification suits best with the purpose to build such microservice that will serve it. This requires a deep understanding of the application class structure, and even how these classes are related to each other.

If you are familiar with the application at a lower level, but if know enough about how to strip out what is needed then it should be apparent as well from the source code using heuristic analysis where your logical starting points would lie. In this case of analysis, the points selected are classified according to the group that features classes by their name. For instance, in an MVC application a class of controller may act as the starting point at which one would start to pull out an order-related microservice.

On the other hand, in case you have limited or no knowledge about applications that are being modernized, then an AI-powered recommendations engine is very priceless. These recommendations move from the heuristic analysis since they clearly distinguish the entrance and exit points for the services. The microservice extractor application uses AI recommendation algorithms to scan every source file in an application and produce recommendations that one can use as the actual list of the candidates.

to use AI- powered recommendations you can click on Generate automated groups as below:

4. Microservice extraction

In this example, you will extract Inventory as a standalone service.

  • Right-click the Inventory node and select Add node to group

  • Select create new group. Set the Group name to InventoryGroup. then click Ad

  • Right-click on the inventory class then Go

  • to group

  • Click View group details

On the right section, you can find the Group details
Note that the Nodes list refers to the InventoryGroup class. This class will be released as a standalone service and exposed as a REST API after release. Check the list of Dependencies. All dependencies in this list will be listed or referenced in the extracted standalone service.

  • Choose Extract group from the Extract and Port menu to begin the extraction process.

  • Set name for your standalone service “InventoryService1

  • After complete the extraction process you can check-out the extraction p

  • th as below:

Extraction path

  • Once extraction is passed, you can open the Output path of the Modified application code & Extracted service

  • Open Extracted service codes to identify any changes made

Extracted service codes

  • You’ll discover that it created an ApiController called InventoryController. This controller is closely linked to the methods of the Inventory class exposing them as REST APIs.
  • It also copied all the Entity Framework data model types and DBContext referenced by the extracted Inventory services under the Models folder.
  • In addition it has relocated the Inventory class to the Services folder.

Please note that although our AWS Microservice Extractor, for.NET will make its efforts to ensure that the created service compiles there is no guarantee. You may still need to fix references or update NuGet packages in order to successfully compile the Web API project.

Change the port of Extracted service to be https://localhost:8081/ as below:

  • After you finished this step click f5 to test the Extracted service

  • Open Modified application code to identify any changes made on

  • monolith app

Please take a look, at the HomeController.cs, ShoppingCartController.cs and StoreController.cs files, in the Controllers folder. The local Inventory class calls have been modified to make remote API calls using the EndpointAdapter.

  • The AWS Microservice Extractor for?.NET efficiently converts local class calls to remote API calls, but there may be times where manual adjustments or refactoring are necessary.
  • Take a look at the EndpointAdapter folder, which was generated during the extraction process, where you’ll find the InventoryEndpointFactory class. This handy class allows for a seamless switch between making remote REST API calls or local Inventory class calls, depending on the setting of the RemoteRoutingToMicroservice flag in the local web.config file.

Modified application Code / web.config

In the line 11 please change the url and link it to the new extracted service [ https://localhost:8081 ] as below:

Modified application Code / web.config

5. Application refactoring

In the preceding sections, you were introduced to the capabilities of AWS Microservice Extractor for?.NET and how it can aid in extracting microservices from your existing?.NET monolithic applications. Although the subsequent section of this tutorial is not mandatory, it offers valuable insights as most of the tasks involved do not require the use of AWS Microservice Extractor. It is highly recommended to follow these steps if your objective is to successfully refactor the extracted service and integrate it with the remaining monolith through REST APIs.

Refactor the extracted service

  • Go to Tools >> NuGet Package Manager >> Package Manager Console

To update the Microsoft.AspNet.WebApi.Client NuGet package, enter the following command in the Package Manager Console. This package will provide support for formatting and content negotiation for System.Net.Http.
Update-Package -reinstall Microsoft.AspNet.WebApi.Client        
To optimize communication efficiency, it’s beneficial to transform the Entity Framework data entities from the Inventory class into simplified “plain old CLR objects” (POCOs), also referred to as Data Transfer Objects (DTOs), before transmitting them over the network.

  • Create a new class by right-clicking on the GadgetsOnline project in the Visual Studio solution explorer and Name it DTO.cs (copy from step 2–1a from this link)
  • Create a new class by right-clicking on the GadgetsOnline project in the Visual Studio solution explorer and Name it DTOHelper.cs (copy from step 2–1b from this link)
  • Navigate to the directory “ Models/GadgetsOnlineEntities.cs” and disable the code highlighted in the following section. This particular code is responsible for seeding the data and is unnecessary for the extracted service.

  • Open Controllers/InventoryController.cs and do the below code changes.

  1. In line 39

//Remove
                //return Ok(myInstance.GetBestSellers(count));        
//ADD 
                return Ok(DTOHelper.GetDTOProductList(myInstance.GetBestSellers(count)));        

2. in line 99

//Remove
      // return Ok(myInstance.GetAllProductsInCategory(category));        
//Add
      return Ok(DTOHelper.GetDTOProductList(myInstance.GetAllProductsInCategory(category)));        

3. In line 130

//Remove
          //return Ok(myInstance.GetProductById(id));        
//Add
          return Ok(DTOHelper.GetDTOProduct(myInstance.GetProductById(id)));        

  • To initiate the service in debug mode, simply press F5. Once activated, a browser window will automatically launch as below:

Launch the Extracted service

Refactor the modified application code.

  • Go to Tools >> NuGet Package Manager >> Package Manager Console

To update the Microsoft.AspNet.WebApi.Client NuGet package, enter the following command in the Package Manager Console. This package will provide support for formatting and content negotiation for System.Net.Http.
Update-Package -reinstall Microsoft.AspNet.WebApi.Client        

  • Open Controllers/ShoppingCartController.cs and update line 15 as indicated below

// remove
    //Inventory inventory;        
//Add
    IInventoryEndpoint inventory;        

  • Open Controllers/StoreController.cs and update line 14 as indicated below.

//Remove
   //Inventory inventory;        
//Add
   IInventoryEndpoint inventory;
   // GET: Store        

Simply hit the F5 button to activate the application’s debug mode. Keep in mind that it may take a few minutes for the web app to fully load, and you may see a message saying “This site can’t be reached” in the meantime. If that occurs, just give it some time to start up completely. Once the application is up and running, you’ll be directed to the same home page you started with. However, take note that the categories displayed on the left side and the list of top-selling products at the bottom are now being fetched from the Inventory Web API through REST API requests.

The ASP.NET Application after refactoring

6. Conclusion

To sum it up, implementing the AWS Microservices Extractor for?.NET has splitted the monolithic application into two separate services that seamlessly communicate through REST APIs. This structural change not only amplifies scalability and adaptability, but also maintains a consistent end-user experience. Adopting microservices has been a game-changing move, enhancing efficiency and flexibility in the development and operation of the application.


要查看或添加评论,请登录

Mohammad Jomaa的更多文章

社区洞察

其他会员也浏览了