start of literary work
A long time ago I wanted to do a set of case studies on how to build a cloud-native application from scratch under .NET. But this is a bit of a big topic and would have to cover a lot of ground. I was going to start with a new Core Web API application, but then I felt that even if I started from scratch, I wouldn't be able to cover every step in detail, and a lot of the basic stuff doesn't really have a lot of introductory value, so I might as well skim over the development of the server-side RESTful APIs and Blazor WebAssembly, and get right to the cloud-native related content. Native content. For more information on the fundamentals and development of Core, click the[here]For more information on Blazor WebAssembly client development, please click on[here]。
So what is cloud native? If you ask ChatGPT, you can probably get the following explanation:
Cloud Native (CN) refers to an approach to building and running applications that take full advantage of the cloud computing model. Cloud Native applications are typically designed to run efficiently in modern cloud environments and have the following characteristics:
containerization: Applications and services are packaged in containers, which provides isolation, fast startup, and consistency.
microservice: Applications are broken down into small, independent services, each performing a single business function.
dynamic management: Manage container lifecycles dynamically using container orchestration tools such as Kubernetes.
Continuous Delivery: Automate deployment processes for frequent and reliable code releases.
Cloud-native applications are designed for elasticity, scalability, maintainability, and rapid iteration in cloud environments.
Well, good, this is ChatGPT said, perhaps everyone's understanding of the concept of cloud native is different, in any case, this explanation explains exactly what I do this case to introduce: based on the Core Web API and Blazor WebAssembly to achieve a microservices architecture and containerization based on the distributed application. In the process of practice, I will be the implementation of the main technologies and related details of the introduction of clear, the content is expected to be more, perhaps sometimes slower to update, I hope readers bear with me.
Description:Since .NET 5, the name .NET Core should in principle not continue to be used to refer to the cross-platform versions of .NET, .NET 5/6/7/8 and the upcoming . NET 5/6/7/8 and the upcoming .NET 9, all of which are cross-platform versions of .NET. In this series of articles, unless otherwise noted, .NET refers to the cross-platform version of .NET, not to the classic .NET Framework.
Case Introduction
This series of articles intends to use the "Stickers" (Stickers) as an example to introduce. "Stickers are small cards used to record what you plan to do today, and after writing on them, you can stick them on your computer or desk as a reminder, which is very similar to a To-Do List, roughly:
The application we want to develop is to achieve such a function: the user can add, delete, change and check their own "stickers", in order to make the requirements simple enough to discuss the technology more efficiently, we do not think about the layout of each sticker, and do not think about the color classification of the stickers, so from the user's point of view, it is probably the effect of this:
The business of the entire application is simple: add, delete, and check for the sticker object, and then maintain the information about the current login account. As mentioned at the beginning of this article, this series of articles will not cover code-level programming details from scratch, but rather all aspects of .NET cloud-native application practices, so each article will also be relatively independent of each other, and the articles may not necessarily be coherent with each other, but hopefully, through this series, we will be able to cover all of the relevant aspects of .NET cloud-native.
Required knowledge
To read this series of articles, you need a certain C# programming foundation and some object-oriented and architectural design ideas. In the process of introducing the cases, I will analyze some real-world problems and provide some ideas from the design level, and then step by step to realize this design. Through reading, you can understand the process of solving these real-world problems, and I believe it will bring you some gains. To summarize, the required knowledge structure is as follows (or will cover the following related content, in no particular order, and subsequent articles will not necessarily be introduced in this order):
- C# Programming Basics
- Object-oriented analysis and design (and certain design patterns)
- Core Web API Basics
- Blazor WebAssembly Basics
- Initial Concepts of Domain Driven Design
- SaaS and Multi-Tenancy Basic Concepts
- Layered Architecture and Microservice Architecture
- Relational Database Fundamentals
- nginx and reverse proxy
- Docker and Application Containerization
- Docker Compose based compilation and deployment
- NET Aspire Preliminary
- Kubernetes Initial
- DevOps Related Basics
- Azure DevOps and Azure Cloud Deployment
- ElasticSearch Basics (Extended)
- Applications of the Large Language Model (Extended)
. : The last two extensions will be in the realization of the basic functionality of the complete application to supplement the expansion, in the early stage does not involve too much related content. Next, we will start from building the framework structure of the project.
Project framework structure construction
Do PPT or use text editing software to write articles, the first step to do is not directly from the body of the article to start writing, but first the entire article outline structure is set, and then in the text editing software on the fonts and paragraph styles to set up for different components of the article can be easily applied to different styles, so that not only in the style of searching and dealing with the time can be done twice as fast! and the structure of the whole article will be very clear layout. Software project development is the same, in the understanding of what needs to be done, the first step is to think about how to build the entire framework structure (also known as "scaffolding"). Based on the case study above, our project will have the following topology:
Overall, the entire distributed application will contain 5 docker containers that implement different parts of the application using different technologies, specifically:
- API Gateway: API gateway implemented using nginx reverse proxy, clients (browsers) access back-end API microservices and front-end resources through the API gateway.
- Keycloak: Certification and authorization authority for Stickers applications, providing certification authorization services for applications
- Sticker Microservices: back-end API services, using the Core Web API implementation, currently provides "stickers" management functions (in short, is to add, delete, change and check)
- Sticker front-end applicationNET Blazor WebAssembly implementation, hosted by nginx container
- pgsql database: PostgreSQL database, needless to say, API microservices and Keycloak depend on it!
In the first phase, we will implement the above, and then we will discuss more things such as message queuing, caching, microservice governance, microservice communication, distributed transactions, etc., as we expand our functionality.
If you are developing an actual product project, the team can consider that on the basis of a certain design statute, different people work on different microservices at the same time, which is one of the advantages of microservices architecture, it allows team members who specialize in different technologies to work together under a heterogeneous technology system. However, I am currently working on a case study, so I will still proceed step by step, the first step is to implement Sticker microservice, which is a back-end service, temporarily only provide "sticker" management functions, and then access Keycloak to complete the authentication of the logged-in user and authorization of resource access.
Tools and IDE
Before starting the presentation and walkthrough of this case, make sure that the following tools and IDEs are installed on the development machine:
- .NET 8 SDK
- Visual Studio 2022, if you develop under Linux, you can use Visual Studio Code with Microsoft's official C# plug-in, or you can choose to use the JetBrains Rider IDE, but this is a charge!
- Docker and Docker Compose
Start with Sticker Microservices
Now start creating our code project by first creating a new folder:
$ mkdir stickers
Then, in this folder, create a newsrc
folder, which is used to hold all development code files:
$ cd stickers
$ mkdir src
go intosrc
NET solution file, then, create a Core Web API project and add this project under the newly created solution:
$ cd src
$ dotnet new sln -n stickers
$ dotnet new webapi -n --no-https --use-controllers
$ dotnet sln add /
And so, you will be insrc
directory to get asolution file, and a
subdirectory, which contains the code for the Sticker microservice that we'll be making editorial changes to next. Now, go to the
subdirectory, and then execute the
dotnet run
command, you will see output similar to the following:
daxnet@daxnet-HP-ZBook:~/Projects/stickers/src/$ dotnet run
Building...
info: [14]
Now listening on: http://localhost:5141
info: [0]
Application started. Press Ctrl+C to shut down.
info: [0]
Hosting environment: Development
info: [0]
Content root path: /home/daxnet/Projects/stickers/src/
Open a browser and visit http://localhost:5141/swagger/ (note that the port may be different, based on the output above) to see the Swagger page for the API service:
Except that there is currently only a default one that comes with the Core Web API templates.GET /WeatherForecast
API, but the project is already created successfully. Starting with the next post, we'll start developing the Stickers microservice.
summarize
I made an opening post today, intending to make a series of introduction to the development of .NET cloud-native applications based on microservices architecture, and I don't know if there will be any readers interested in learning about this part of the content, if there are any good suggestions, please feel free to leave a comment.
source code (computing)
As always, it is open-sourced under the MIT license agreement, and for the convenience of domestic readers, the code is hosted in the code cloud, with a code base of:/daxnet/stickersThe code for each chapter is placed in a branch named "chapter_XXX", so the code for this chapter is here:/daxnet/stickers/tree/chapter_1/。