A detailed example of creating a microservices with Microservice4net

Source: Internet
Author: User

The term "microservices architecture (microservice Architecture)" has been widely disseminated over the past few years, and it is used to describe a particular way of designing applications as a set of independently deployable services. At present, this architecture is not exactly defined, but in the organization around the business capabilities, automatic deployment (automated deployment), end intelligence (intelligence in the endpoints), language and data decentralized control, but there are some common characteristics.

MicroServices (microservices)--just a term in the software architecture that fills the streets. Although we despise this kind of thing very much, but this thing describes the software style, more and more attracted our attention. Over the past few years, we have found that more and more projects are beginning to use this style, so that colleagues around us take it for granted as a default development form when building enterprise applications. Unfortunately, however, the micro-service style is what it should be and how it should be developed, but it is hard to find a theoretical description.

In short, the MicroServices architecture style is like developing a single application into a small set of services, each running in its own process and using lightweight mechanisms to communicate, usually the HTTP API. These services are built around business capabilities and are deployed independently through a fully automated deployment mechanism. These services are written in different programming languages, as well as with different data storage technologies, and maintain a minimum of centralized management.

In this article, I introduce the foreigner (microservice4net), which makes the class library very simple to create a micro-service in C # programs.

You can choose either a Windows service or a console application in one of the hosted ways. First step: Create a new console project named: Microservice4net.example, and then install microservice4net through the NuGet Package Manager

Step Two: Add namespaces using Microservice4net;

Add the following code in the Main method

static void Main (string[] args) {var microservice = new Microservice ();    Microservice.run (args);}

Default port: 8080. If you want to change the port, the following

static void Main (string[] args) {var microservice = new Microservice (port:8086);    Microservice.run (args);}

  

Part III: Add a new apicontroller, such as Examplecontroller

Using System.Web.Http; namespace Microservice4net.example.controllers{public class Examplecontroller:apicontroller    {        [Route (" Example ")]public string getexample ()        {return ' Example ';        }}    }

Complete the start console

In the browser input http://localhost:8086/Example:

So far, we've been so dumb to simply create a small micro-service that gives us more time to spend on business logic.

If you want to host a run on Windows services, you need to add two empty classes

12 publicclass MicroServiceInstaller : ProjectInstaller { }public classMicroServiceService : InternalService { }

After you rebuild the solution, it's a lot easier next. Open cmd as Administrator, switch to the project-compiled directory using the CD, execute Microservice4net.example.exe-install (Microservice4net.example is the project name, replace it with your own defined name) ), after the installation is successful, open the Service Management console and discover that we have successfully installed the service.

If you want to uninstall the service, execute Microservice4net.example.exe-uninstall

Done, if you need source code, click

Reprint to: Crazy ants

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.