编程知识 cdmana.com

Serverless landing practice

.Net Core Serverless First experience

What is? Serverless

  Serverless It's a relatively new topic in today's software world . It doesn't have a universally accepted definition of authority , Everyone and every enterprise may interpret it differently , and Serverless It is in this situation that the development of . But even so , Somewhat Serverless It's still widely recognized :

  • The host and process of the server are completely managed by the supplier

  • It can automatically scale according to the load

  • Charge according to the exact usage , Like water and electricity .( Utility calculation )

   Quote Nuggets , Original address :https://juejin.im/post/5c68fdbfe51d4539a569f259

 

Serverless evolution

 

 

   In this development process, there are several gradual milestones :

  • Virtualization technology is used to virtualize large physical machines into a single VM resources .

  • Move virtualization cluster to cloud computing platform , Just do simple operation and maintenance .

  • Take every one. VM According to the principle of minimizing the running space, it is divided into more detailed Docker Containers .

  • be based on Docker Container building does not manage any running environment 、 Just write the core code Serverless framework .

   Quote alicloud , Original address :https://help.aliyun.com/knowledge_detail/65565.html?spm=a2c4g.11186631.2.1.4f811bbeDYGmvp

 

Alibaba cloud Serverless -  Function calculation

   Function computing is an event driven fully managed computing service . Use functions to calculate , You don't need to purchase and manage infrastructure like servers , Just write and upload the code . Function calculation is ready for you to calculate resources , Run tasks flexibly and reliably , And provide log query 、 Performance monitoring, alarm and other functions .

With the help of function calculation , You can quickly build any type of application and service , And just pay for the resources actually consumed by the task .

   Quote alicloud , Original address :https://help.aliyun.com/document_detail/52895.html?spm=a2c4g.11186623.6.541.23dc641aB3U3K0

 

To write .Net Core Code

  1. Create a new one .net core Console program , It's called AliyunServerless.

  

 

 

  2. Reference the function calculation component provided by Alibaba cloud Aliyun.Serverless.Core

  Install-Package Aliyun.Serverless.Core

  

 

  3. Write according to the requirements of function calculation .Net Core Class and function of , Here are two classes and functions

  a. Print execution log


    /// <summary>
/// Print execution log
/// </summary>
public class LogHandler
{
public void Handle(Stream input, IFcContext context)
{
ILogger logger
= context.Logger;
logger.LogInformation($
"Handle request: {context.RequestId}");
}
}

 

  b.  Print server IP


    /// <summary>
/// Print server IP
/// </summary>
public class IpHandler
{
public void Handle(Stream input, IFcContext context)
{
ILogger logger
= context.Logger;
var ipHostInfo = Dns.GetHostEntry(Dns.GetHostName());
logger.LogInformation($
"Handle request: {context.RequestId}");

foreach (var ipAddress in ipHostInfo.AddressList)
{
logger.LogInformation($
"IP Address:{ipAddress}");
}
}
}


 

  explain :

  

    Please refer to the original address for details :https://help.aliyun.com/document_detail/112379.html?spm=a2c4g.11174283.6.567.206852120XSx37

 

  4. Release the program and package it into zip

  dotnet publish -c Release

   

 

Configure Alibaba cloud function computing

  1.  Fee description and free quota

  

 

  2. Select the region where the function executes

   Refer to the entry address :https://fc.console.aliyun.com/fc/overview/cn-shenzhen

  

 

 

  3. Create services and functions

  a. Create a function

  

 

 

  b. choice “ Event function ”, then “ next step ”

  

 

  c. Configuration function information

  

 

   among :

  “ The service ” It's grouping , Fill in as needed .

  “ The name of the function ” Is the name. , Fill in as needed .

  “ Running environment ” choice dotnetcore2.1.

  “ Function entrance ” Fill in... As required , The template is { Assembly }::{ Namespace }.{ Class name }::{ Function name }, Here is :AliyunServerless::AliyunServerless.LogHandler::Handle

  “ Function execution memory ”, Memory required for function execution .

  “ Timeout time ”, Function execution cannot exceed the set time .

  

  d. Execute function

   Click on “ perform ”, You can see the executive summary ( execution time 、 Using memory 、 Execution status, etc ) And implementation results :

  

 

  e. Create trigger

  

 

   It's used here “ Timing trigger ”, Support Cron expression , The highest scheduling frequency is 1 minute , Second level scheduling is not supported ; except “ Timing trigger ”, There are also the following trigger types :

  

    Interested friends can do their own research .

    After the trigger is configured , The function will be triggered by timing , Because here is the print log , You need to configure the log to see , No log is configured in this article , Don't show the trigger effect .

 

Extended testing

   Follow the steps for creating functions , establish “ Print server IP” function , Execute and track , give the result as follows :

  1. Multiple execution (5 Different times RequestId), The server IP It hasn't changed ( All for 21.0.3.3), Description assign to the same server

  

  

  

  

 

  

 

  2. Re upload package , The server IP Will change , But the same package server IP unchanged

  a. First time upload again :

  

 

  

 

  b. Second upload :

  

  

 

  3. My choice of execution area is “ south China 1( Shenzhen )”, But the server IP Is the U.S. IP; Because there's no server information , This conclusion is only for reference

  

  

  

 

  4. Call statistics will be non real time ( Normal phenomenon ), Most business months should be free enough to use

  

  

Source code address

  https://github.com/ErikXu/AliyunServerless


版权声明
本文为[Architect advanced Club]所创,转载请带上原文链接,感谢
https://cdmana.com/2021/10/20211002145640771q.html

Scroll to Top