Creating the first microservice
To start the first microservice we will use the “dp new” command of the DevPrime CLI. By confirming a new application will be created in seconds with a complete software architecture project and ready for production accelerating a job you would do in weeks.
We will use the Stream service as “Rabbitmq” for issuing events enabling “Event-Driven architecture” and state “MongoDB” for data persistence.
dp new order --stream rabbitmq --state mongodb
At the end it is possible to follow the result of the “dp” command with the creation of a new microservice application project. The new microservices created receive a complete and functional foundation accelerating the development of cloud native software.
The new microservices are ready for production !!!
Enter the new folder created to gain access to your new project open the folder through Visual Studio Code to view features enabled by accelerators.
code .
The resource settings offered by Stack are available in the appsettings file (src\App\appsettings.json) and can also use environment variables in the container and a vault in the production environment.
The project follows a Hexagonal architecture approach by secing the technology layers according to the details below. Each layer of technology has intelligive components offered by DevPrime Stack.
Hexagonal architecture | |
---|---|
I’m going to go | The main design of your application and the pattern in Visual Studio |
Web | API’s exhibition |
Stream | Implementation to receive multi-stream events (RabbitMQ, Kafka…) |
State | Integration with persistence |
Domain | Business rules are based on Domain Driven Design (DDD) and are isolated in this project. All domino classes must inherit from DevPrime Foundation. |
I don’t need to | Integration with external API’s services either using HTTP/gRPC/GraphQL |
Security | Integration with identity providers using (authentication / authorization) to protect API’s |
Healthcheck | Exposure to check availability of services in the Cluster (Liveness, Readiness). |
I’m observability | Using observational strategy (Log, Trace, Metrics) with opentelemetry support for use in |
AppCenter | Exposure of information for monitoring the services |
Extensions | Add third-party external nuget components |
The new microservice application “Order” can already run even without the business rules. In the context of microservice the log is the screen of your application and DevPrime offers an amazing viewing experience by automatically managing the log in the ‘Observability’ adapter. This in addition to automatic logging is distributed allowing integration with cross microservices processes.
Starting the microservice “Order”
You can run the application using “dotnet run” or using the “.\run.ps1” script files if you have powershell (Windows, Linux, macOS) installed or “./run.sh” in bash (Linux and macOS). From using bash on Linux it is necessary to run the permission presented below making the script executable.
chmod +x run.sh
Before starting the application remember to climb and configure containers in docker creating the network devprime and adding the MongoDB and RabbitMQ containers. Then create the queue in the RabbitMQ portal.
Run one of the files below according to your operating system.
.\run.ps1 ou ./run.sh (Linux, macOS)
When starting the application it is possible to track in the log below all the behavior of the new microservice.
Introduction to resilience and circuit breaker
When booting the microservice or at any time of its operation will be common failures occur. If you do not have a RabbitMQ container service running locally or due to be configured in the “src/App/appsettings.json” file the adapter will automatically start a “Retry” and if unsuccessful it will trigger a “Circuit Breaker” due to the failure to access the “RabbitMQ” Stream service.
On the return of service it will turn off the “Circuit Breaker” automatically. We’ll talk more about this topic in another topic.
Viewing API’s (OpenApi)
After running the first microservice you can already access the url https://localhost:5000 or https://localhost:5001 and view your API. Since we have not yet included business rules the only endpoint presented is the /healthcheck used by the Kubernetes cluster for monitoring the service.
Adding a business rule
The created project is already ready to receive the business rules in the Domain Driven Design (DDD) standard. The code must be written within the Domain folder. After including it is possible to use the “dp init” accelerator to analyze the domain and create the code needed to display the API’s further simplifying the development journey and maintaining the best development practices.
To facilitate our example we will use the domain “Order” and get the code example of the marketplace service of the Devprime platform. Use the command below and check the result in the Domain src/Core/Domain/Aggregates folder.
dp marketplace order
Next, we will use the “dp init” command that allows you to analyze business rules in the “Domain” and using the best development practices to build the necessary code to allow you to propagate a business interaction with other layers of the application while maintaining uncoupling, standardization, and stability.
The accelerator automatically builds the classes of “Domain Events” in Domain, Event Handlers in Application, “Domain Services” in Application, “Application Services” in Application, Persistence in State, “API’s/Controllers” in Web, unit tests in Tests. To use just have a domain implemented and run the command as example below.
dp init
Now when running again your microervice will already have a set of API’s as shown below.
Last modified November 18, 2022 (9ddc897)WOW!! You already have a production-ready microservice based on DevPrime