Developing microservices with. NET Core+docker

Source: Internet
Author: User
Tags dotnet docker swarm

. NET core has been released for a long time, because in recent years the main use of Java, so has not used. NET core, today there is a C # written demo, need to make a service, do not want to go to Java to implement, consider using. NET core to taste fresh, the goal is to develop a micro-service, It is then deployed to a Docker swarm cluster for other applications to invoke.

Environment preparation

Download the latest version of. NET Core 2.1.3, open command-line validation after installation:

The following output appears, which means the installation is successful.

dotnetUsage: dotnet [options]Usage: dotnet [path-to-application]Options:  -h|--help         Display help.  --info            Display .NET Core information.  --list-sdks       Display the installed SDKs.  --list-runtimes   Display the installed runtimes.path-to-application:  The path to an application .dll file to execute.

Then the VS code installs the C # plug-in,

and installing the NuGet plugin for easy management of extensions.

Create a project

Use the dotnet new name Create project, where name is the project type, as follows:

C:\users\jqpeng>dotnet New--HELP usage: new [options] option:-H,--help displays help on this command. -L,--list lists the template that contains the specified name.  If no name is specified, list all templates. -N,--name is creating the name of the output.  If no name is specified, the name of the current directory is used.  -O,--output the location where the generated output is to be placed.  -I,--install install the source or template package.  -U,--uninstall uninstalls a source or template package.  --NUGET-SOURCE Specifies the NuGet source to use during installation. --type filter templates based on the available types.  The predefined values are "project", "Item", or "other".  --force forces the content to be generated, even if the content changes existing files. -lang,--language filters the template by language and specifies the language of the template to be created. Template short name language tag---------------------------------                               -------------------------------------------------------------------------------------------Console Application console [C #], F #, VB Common/consoleclass Library cl Asslib [C #], F #, VB common/libraryunit Test Project MSTest [C #] , F #, VB Test/mstestnunit3 Test Project NUnit [C #], F #, VB test/nunitnunit 3 test Item              Nunit-test [C #], F #, VB test/nunitxunit test Project Xunit              [C #], F #, VB Test/xunitrazor page page [C #] Web/asp. Netmvc viewimports Viewimports [C #] web/asp. Netmvc Viewstart Viewstart [C #] web/asp. Netasp.net Core Empty Web [C #], F # web/emptyasp.net core Web App (M Odel-view-controller) MVC [C #], F # web/mvcasp.net Core Web App R            Azor [c] Web/mvc/razor pagesasp.net Core with Angular Angular              [C #] Web/mvc/spaasp.net Core with React.js                       react [C #] Web/mvc/spaasp.net Core with React.js and Redux R              Eactredux [C #] Web/mvc/sparazor Class Library razorclasslib [C #] Web/razor/library/razor Class libraryasp.net Core Web API Webapi [C #]  , F # web/webapiglobal.json file Globaljson confignuget                                        Config Nugetconfig configweb config                                  Webconfig configsolution File sln Solutionexamples:dotnet new MVC--auth individual dotnet new Classlib--framework netcoreap p2.1 dotnet New--help

To develop microservices, you can dotnet new mvc create an MVC project, and then the VS code opens.

Engineering implementation

Implementation is simple, step by step coding can, and a few years ago. NET MVC no Essential difference:

Create a new Apicontroller and add a method:

 public JsonResult Search(string tfsid)    {      try      {        return Json(new { success = true, data = PailitaoApi.SearchByTFSId(tfsid) });      }      catch (Exception ex)      {        Console.WriteLine(ex.Message);        return Json(new { success = false, data = "Search failed!" }); ;      }    }

Then delete the default template file, clean up the code in Homecontroler, keep index, reduce the package volume

public string Index()    {      return "api";    }

Main code ok!

Docker Image Compilation

In accordance with the official guidelines, we write dockefile, mainly through the implementation of the microsoft/dotnet:2.1-sdk AS build compilation, through microsoft/dotnet:2.1-aspnetcore-runtime to run, specifically see below, note pailitao.dll need to modify the name of your project:

FROM microsoft/dotnet:2.1-sdk AS buildWORKDIR /app# copy csproj and restore as distinct layersCOPY *.csproj ./RUN dotnet restore# copy everything else and build appCOPY . ./RUN dotnet publish -c Release -o outFROM microsoft/dotnet:2.1-aspnetcore-runtime AS runtimeWORKDIR /appCOPY --from=build /app/out ./EXPOSE 80ENTRYPOINT ["dotnet", "appname.dll"]

Simple analysis:

    • Build, first dotnet restore
    • Compile release version dll via dotnet publish-c release-o out
    • Then build the runtime and copy out the out directory from the build environment
    • Then expose 80 port, set entrypoint to ' dotnet Appname.dll '
Micro-Service Cicd

The first is to build, first put the code into SVN, a new free-mode project in Jenkins, build using Build/publish Docker Image:

Then deploy, build the image, and execute the deployment through the shell:

docker service rm  pailitaoservicedocker service create --name pailitaoservice  --replicas 1 --publish 15001:80 192.168.86.8:5000/pailitaoservice

Finally, the test under the service:

Get it done!

Jadepeng
Source: Jqpeng's technical notepad--http://www.cnblogs.com/xiaoqi
Your support is the greatest encouragement to bloggers, thank you for your serious reading.
This article is copyrighted by the author, welcome, but without the consent of the author must retain the statement, and in the article page obvious location to the original link, otherwise reserves the right to pursue legal responsibility.

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.