当前位置:网站首页>Function calculation advanced IP query tool development

Function calculation advanced IP query tool development

2020-11-09 16:06:26 Level

Scenario introduction

The scenario describes how to develop a IP Query tools .

Background knowledge

What is? Serverless

since 2006 year 8 month 9 Japan ,Google Eric, CEO · schmidt (Eric Schmidt) At the search engine Conference (SESSanJose2006) First put forward “ Cloud computing ”(Cloud Computing) After the concept of , The development of cloud computing can be described by the word "changing with each passing day" . So what exactly is Serverless Well ?

Simply speaking ,Serverless It's an architecture , A product of the development of cloud computing , As for what is Serverless, Maybe no one can give him a clear concept , If you have to say a concept that is a little easier to understand , Maybe you can refer to Martin Fowler stay 《Serverless Architectures》 Chinese vs Serverless Such definition :Serverless=BaaS + FaaS
 Insert picture description here
Serverless The difference between architecture and traditional projects

First , We take a common Web Service as an example :

 Insert picture description here
In this picture , Routing rules may be involved in the server 、 Authentication logic and other complex business codes . meanwhile , The development team has to pay a lot of attention to the operation and maintenance of this server , For example, always pay attention to the following questions :

Whether the server needs to be expanded when the number of customers increases suddenly .
Whether scripts and business code on the server are still running healthily .
Are hackers constantly attacking the server .
When we switch this thinking to Serverless After the logic of , It goes like this :
 Insert picture description here
It can be said that , When the client and database have not changed , The server has changed a lot .

The routing module and authentication module that need to be maintained by the development team before will be accessed by the service provider API Gateway system and authentication system , The development team no longer needs to maintain these two parts of the business code , Just keep maintaining the rules .
Under this structure , Business code is also broken down into function granularity , Different functions represent different functions .
We can't see the server anymore , Because Serverless The goal is to let users only focus on their own business logic , So part of the security problem 、 Resource scheduling problem ( For example, the number of users has increased dramatically 、 How to realize automatic capacity expansion, etc ) All to the cloud manufacturer .
Compared to traditional projects , Traditional projects whether or not there is user access , Services are running , There are costs , and Serverless for , Only when used to initiate a request , Function will be activated and executed , And will charge by volume , Relatively speaking, it can only be supported when there is traffic , There's no spending when there's no traffic , relatively speaking , The cost will be further reduced .
Through the above analysis and description , It's not hard to see. Serverless The difference between architecture and traditional development model , And gradually found its advantages . But the problem is , A lot of work is left to cloud vendors , So what do we do ?
 Insert picture description here
Use Serverless After the architecture :

Development teams no longer need to maintain their own servers , You don't need to worry about various performance indicators and resource utilization of the server , It allows the development team to devote more time and energy to the state and logic of the application itself .
Serverless Application deployment will be very easy . We just upload the basic code , for example Python The program only needs to upload its logic and dependency package ,C/C++、Go And other languages just need to upload their binary files ,Java Just upload it Jar Just wait , At the same time, there is no need to use Puppet、Chef、Ansible or Docker For configuration management , Greatly reduced the operation and maintenance cost .
Serverless The architecture no longer needs to monitor the underlying data , For example, you no longer need to monitor disk usage 、CPU Utilization rate, etc , You can focus more on monitoring the metrics of the application itself . At the same time Serverless Architecturally , The role of operation and maintenance personnel will be changed , Deployment will become more automated , Monitoring will be more application oriented .
Use Serverless Advantages of Architecture

From the above we can see that , Compared to traditional projects ,Serverless Have the following advantages :

You don't have to purchase and manage infrastructure like servers , Low operation and maintenance cost .
You just need to focus on the development of business logic , Design a development language supported by functional computing 、 Optimize 、 test 、 Review and upload your own application code .
Trigger the application in an event driven way to respond to user requests . And Alibaba cloud object storage OSS、API gateway 、 Log service and form storage services are seamlessly connected , Help you build applications quickly . for example , adopt OSS Solve the storage problem of pictures and videos , When new data is written into your OSS Resource time , Automatic trigger function processing data .
Provide log query 、 Performance monitoring and alarm and other functions quickly troubleshooting .
Millisecond level elastic scaling , Quickly expand the bottom layer to cope with peak pressure .
Pay as you go , Support 100 millisecond level charge . Just pay for the computing resources you actually use , It is suitable for user access scenarios with obvious peaks and troughs .
To make a long story short ,Serverless It is developed on the traditional container technology and service grid , It refers to the combination of back-end services and function services . For developers , Be able to focus more on function services , More attention should be paid to the user's business logic .

meanwhile ,Serverless It is also the inevitable product of the development of cloud computing to a certain stage . As Pratt & Whitney , The goal of cloud computing development must be the products of green technology and mass technology —— and Serverless It's a good interpretation of these : Make the most of resources 、 Reduce waste of idle resources ; At the same time, reduce the cost of learning and use .

Serverless Architecture is called “ It really achieved the original goal of Cloud Computing ”, It's a little exaggerated , But on the other hand, it shows that everyone is right about Serverless Expectations and confidence in Architecture . since 2012 Since it was proposed in ,Serverless Architecture also went through 7 Year time , Is gradually moving towards maturity .

Step one : Connect ECS The server

Alibaba cloud product resource experience address :https://developer.aliyun.com/adc/scenario/d419eecaaf184013a45db0a5d613ad39

The scene will provide a configuration for CentOS 7.7 Of ECS example ( Cloud server ). Through the operation of this tutorial , You can develop a functional computing based IP Query tools .

Step two : Open function computing service

Before you use a function to calculate , Function calculation service needs to be opened .

explain : The alicloud sub account provided in this scenario has no function computing service operation permissions , Please use your own alicloud account to operate . You don't have to worry about the deduction , Because the function computing service has a certain amount of free , See billing method .

1. Log in to the alicloud console with your own alicloud account , Then enter Function to calculate product details page .

2. single click 【 Free to open 】.
 Insert picture description here
3. read 《 Function computing service protocol 》 Check agree to service agreement , Finally, click 【 Open now 】 .
 Insert picture description here
4. single click 【 Administrative console 】 Enter the function calculation console .
 Insert picture description here

Step three : install Funcraft Tools

Fun Is a support for Serverless Application deployment tools , Can help you easily manage function calculations 、API Gateway and log services and other resources . It uses a resource profile
(template.yml), Help you develop 、 Build and deploy operations .

This step will be in ECS Installation on server Funcraft Tools .

1. Perform the following installation NodeJS.

curl -sL https://rpm.nodesource.com/setup_10.x | bash - && yum install -y nodejs

2. Perform the following installation Funcraft.

npm install request @alicloud/fun -g

3. perform fun config Command to configure locally .

fun config

Please refer to the following information to enter your alicloud account ID、AccessKeyID and AccessKey Key and other information .
Aliyun Account ID: Please check your account on the account security settings page ID.

 Insert picture description here
Aliyun Access Key ID and Aliyun Access Key Secret: Please be there. Security information management Page to see your account AK ID and AK Secret.

 If you are using the main account of alicloud , Please click continue to use in the security prompt box on the security information management page AccessKey.

 Insert picture description here
Default region name: Please select the region where the function calculation service is opened .

 Insert picture description here
The correct settings are as follows :

 Insert picture description here

Step four : Run template example

1. Execute the following command to create a new workspace .

 mkdir search_ip && cd search_ip

2. perform fun init Order a new HTTP Function triggers .

fun init http-trigger-nodejs10

The results of the command execution are as follows :

 Insert picture description here
among index.js The file is generated HelloWorld Function templates ,template.yml The file contains HelloWorld Functions and HTTP Default configuration information for triggers .

3. perform fun deploy -y Command deployment applies to function computing services .

fun deploy -y

The results of the command execution are as follows :
 Insert picture description here
4. Use curl Command access HTTP trigger . Please send the following order to 11905929798***** Replace with your alicloud account ID.

curl https://11905929********.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/search_ip/search_ip/

The results of the command execution are as follows :
 Insert picture description here
You can see in the return result that , In the use of HTTP When you trigger , You can pass some parameters by default , Include Path,Queries as well as Headers,Method etc. , One of the parameters is clientIP, The value of this field is the client's IP Address , We can use this address to determine the client's IP Address .
Next we're going to use clientIP Field development IP Query tools .

Step five : Development IP Query tools

1. Refer to the following steps to modify index.js The contents of the document .
a. take index.js Empty file contents .

cat /dev/null > index.js

b.   Use vim open  index.js  file .
vim index.js

c.   Press down  i  Key in vim The editing mode of , Add the following to the document .
var getRawBody = require('raw-body')
module.exports.handler = function (request, response, context) {
    getRawBody(request, function (err, data) {
        var respBody = new Buffer.from(" Your IP yes :" + request.clientIP);
        response.setStatusCode(200)
        response.setHeader('content-type', 'text/html')
        response.send(respBody)
    })
};
d.   Press down  esc  Key to enter command mode , Input  :wq  Save and exit vim.

2. perform fun deploy -y Command to redeploy to function computing service .

fun deploy -y

The results of the command execution are as follows :

 Insert picture description here
3. Use curl Command access HTTP trigger . Please send the following order to 11905929798***** Replace with your alicloud account ID.

curl https://11905929798*****.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/search_ip/search_ip/

The results of the command execution are as follows :

 Insert picture description here

版权声明
本文为[Level]所创,转载请带上原文链接,感谢