Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”.
Serverless, what is Serverless, what is the relationship with the server, why Serverless is needed, and how to use Serverless, these questions will be explained clearly in this article. In this article, we will briefly introduce the basic concepts of Serverless and use Serverless to quickly build Spring Boot applications.
1. What is Serverless?
1.1 Basic Concepts
The normal development and release process now is the first code, and then put the code into the mirror, mirror server pull the latest resume update, we need not only coding, also need to consider the deployment of the server, application log, capacity, load balance, etc., so what the new architecture can help us save operations, development is only concerned about coding, testing, This is Serverless.
Serverless refers to a scenario where the server-side logic is implemented by the developer, runs in a stateless computing container, is event-driven, the server is completely managed by a third party, and the business-level state is recorded in a database or storage resource.
In a word, developers only need to focus on business implementation and do not need to care about things unrelated to business, such as how to deploy to the cloud, do not need to care about expansion, do not need to care about operation and maintenance, all these things are handed over to the cloud service provider providing Serverless.
The difference with IaaS is that in IaaS, the user needs to purchase a good server in advance and increase resources when requests are high, decrease resources when requests are low, and the container is still ready to wait for requests when there are no requests.
Serverless does not need to purchase service resources in advance, starts only when required, allocates resources when an event is triggered, and closes the container when no event is triggered and waits for the next event. Then all the developers need to do is to implement the business with the code, and then package the code and upload it to the cloud service provider. The rest of the operation and maintenance are handed over to the provider.
1.2 the advantages and disadvantages
First, let’s look at the disadvantages of Serverless:
- Cold start. Serverless also runs applications in containers. When a request comes in, it checks to see if there are any running containers. If there are no running containers, a new container is started to run the application. This is called a cold start. Since you need to start a new container, the response time will not be as fast.
- High switching costs. Since we are using services provided by cloud providers, when we need to change providers, the cost is very high because each one has a different specification.
- Is not flexible. The opposite of convenience is inflexibility, because all our operation and maintenance are handed over to the provider, so we can’t control the server or optimize the server to achieve our purpose.
Let’s look at the pros:
- Improve development efficiency, this has been said many times before, will not repeat.
- Reduced operation and maintenance costs, no need to manage the server, just pay for the uptime.
1.3 Application Scenarios
Given these advantages and disadvantages, which scenarios are appropriate to use Serverless?
- Transient, stateless applications can accept cold start scenarios.
- Scenarios that are infrequent but likely to surge in requests.
Such as real-time file processing, data ETL, serverless mobile back-end, audio and video analysis and other scenarios.
1.4 Serverless products
- AWS Lanmbda: Supports file processing, stream processing, Web applications, IoT backends, mobile backends.
- Cloud Functions: Support integration with third party services and apis, serverless mobile back end, server IoT back end, file processing, video and image analysis, sentiment analysis.
- Ali cloud function computing: with rich trigger types, support a variety of programming languages, there are very convenient development tools for debugging deployment, as well as rich computing types
- Tencent Cloud Serverless: has simplified configuration, convenient operation and maintenance, one-stop development and other features, support command line development tools based on cloud functions, rapid migration of traditional application frameworks and other scenarios.
Next we will use ali Cloud functions to calculate the actual deployment of the Spring Boot application.
2. Quickly set up the Spring Boot application
Ali Cloud function computing is event-driven fully managed computing services. With functional computing, you don’t need to purchase and manage infrastructure such as servers; you just write and upload code. Functional computing prepares you for computing resources, runs tasks flexibly and reliably, and provides log query, performance monitoring, and alarm capabilities.
2.1 Development Process
The workflow of function calculation is shown in the figure below:
- Developers use programming languages to code the code, currently function calculation supported programming languages: Java, PHP, Go, Python, Node.js and other languages.
- Developers upload code to function calculations via the function computation console, Funcraft, Serverless Devs, API or SDK.
- Trigger function execution, because Serverless is event-driven, it needs to be triggered by HTTP request, OSS, API gateway, etc.
2.2 Rapid Deployment
Here we manage functions through Serverless Devs.
FC components and ROS components are provided by the functional computing team based on Serverless Devs. FC components and ROS components are tools used to support the full life cycle of Aliyun Serverless applications. This component is an advanced version of Funcraft, similar to the behavior description of Funcraft. Funcraft helps you to develop, build, and deploy through the resource configuration file template.yml, while FC and ROS components are developed through the resource configuration file, or YAML file. Helps you quickly develop, build, test, and deploy applications to functional computing.
2.2.1 Installing Serverless Devs
It can be installed through package manager (NPM, YARN) and script installation. Here we install via NPM:
npm install @serverless-devs/s -g
Copy the code
After the command is executed, run the s-v command to check whether the installation is successful:
~ s -v
@serverless-devs/s: 2.0.92, darwin-x64, node-v12.18.3
Copy the code
2.2.2 Configuring Serverless Devs
Run s config add on the terminal and enter Account ID, Access Key ID, Secret Access Key, and aliasName as prompted.
~ s config add
? Please select a template: Alibaba Cloud (alibaba)
🧭 Refer to the document for alibaba key: http://config.devsapp.net/account/alibaba
? AccountID 1831277402*****
? AccessKeyID LTAIkC5X*****
? AccessKeySecret ZMbINFFtAUlWPykcI51U******
? Please create alias for key pair. If not, please enter to skip default
Alias: default
AccountID: 1831277402******
AccessKeyID: LTAIkC5X******
AccessKeySecret: ZMbINFFtAUlWPykcI51U*******
Configuration successful
Copy the code
2.2.3 Creating a Spring Boot Project
Run the s init devsapp/start-springboot -d projectName command in the target directory to initialize the project:
➜ Project s init devsapp/start-springboot -d serverlessSpringboot 🚀 Serverless Awesome: https://github.com/Serverless-Devs/package-awesome ✔ file decompression completed? Both please select the credentialalias default
_____ _ ______ _
/ ___| (_) | ___ \ | |
\ `--. _ __ _ __ _ _ __ __ _| |_/ / ___ ___ | |_
`--. \ '_ \| '__ | |'_ \ / _ ` | ___ \ / _ \ _ \ | | / \ (/ / | _) | | | | | | | (_ | | | _ / / | (_) (_) | | _ \ ____ / | | / __ _ | | _ _ - | | | _ | \ __, \____/ \___/ \___/ \__| | | __/ | |_| |___/ Welcome to the start-springboot application This application requires to open these services: FC : https://fc.console.aliyun.com/ ACR: https://cr.console.aliyun.com/ This application can help you quickly deploy the SpringBoot project: Full yaml configuration : https://github.com/devsapp/sprintboot#%E5%AE%8C%E6%95%B4yaml SpringBoot development docs: https://spring.io/projects/spring-boot/ This application homepage: https://github.com/devsapp/start-springboot 🏄 Thanks for using Serverless - Devs 👉 You could [CD /Users/dcs/Project/serverlessSpringboot] and enjoy your serverless journey! 🧭️ If you need help for this example, you can use [s-h] after you enter folder. 💞 Document ❤ Star: https://github.com/Serverless-Devs/Serverless-DevsCopy the code
Switch to the project directory and run the DEPLOY command to deploy the project to function calculation. You need to enable the NAS and create a common NAS file system.
Aliyun function computing supports seamless integration with NAS. This allows your functions to access files stored on one of the NAS file systems as if they were local. All you need to do is configure the NAS on the service, including the NAS region, mount point, grouping, and so on. After the configuration is successful, functions under the service can access the specified NAS file system as if they were accessing the local file system.
➜ serverlessSpringboot s deploy [2021-10-30T21:18:13.963] [INFO] [s-cli] -start... 📎 Using web frameworktype: nas, If you want to deploy with container, you can [s cli fc-default setWeb-framework Container] to switch. [2021-10-30T21:18:14.222] [INFO] [Web-framework] - The configuration of The domain name is not detected, And a temporary domain name is generated. [2021-10-30T21:18:16.653] [INFO] [fC-deploy] - Using region: Cn-hangzhou [2021-10-30T21:18:16.654] [INFO] [fC-deploy] - Using accessalias: default [2021-10-30T21:18:16.654] [INFO] [fC-deploy] - Using accessKeyID: * * * * * * * * * * * Udxi [the 2021-10-30 T21: thou. 654] [INFO] [FC - DEPLOY] - Using accessKeySecret: * * * * * * * * * * * 1 BWC 📎 Usingfc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set[2021-10-30T21:18:16.827] [INFO] [fc-deploy] - Checking Service web-springboot exists [2021-10-30T21:18:17.105] [INFO] [fC-deploy] - Setting role: AliyunFCDefaultRole [2021-10-30T21:18:17.152] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists [2021-10-30T21:18:17.447] [INFO] [RAM] -updating role: AliyunFCDefaultRole [the 2021-10-30 T21: that is, 561] [INFO] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists [2021-10-30T21:18:17.721] [INFO] [fC-deploy] - Using logConfig: auto:fcWill try to generate default SLS project [2021-10-30T21:18:17.892] [INFO] [SLS] - Checking project 1831277402525713-CN-HANG-LogProject exists [2021-10-30T21:18:18.122] [INFO] [SLS] - Checking Logstore 1831277402525713-CN-Hang-LogProject/FC-service-web-springboot-LogStore exists [2021-10-30T21:18:20.170] [INFO] [SLS] -checking Logstore index 1831277402525713- CN-hang-logproject /fc-service-web-springboot-logstore exists 📎 Usingfc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set[2021-10-30T21:18:20.233] [INFO] [fC-deploy] -generated logConfig: enableInstanceMetrics:true
enableRequestMetrics: truelogBeginRule: ~ logstore: fc-service-web-springboot-logstore project: 1831277402525713-CN-Hang-LogProject [2021-10-30T21:18:20.234] [INFO] [fC-deploy] - Using vpcConfig: auto:fcAutomatically [2021-10-30T21:18:20.976] [INFO] [VPC] - Getting VPC: Fc-deploy-component-generated - vpc-CN-hangzhou [2021-10-30T21:18:21.157] [INFO] [VPC] - Getting Vswitch: Fc-deploy-component-generated - vswitch-CN-hangzhou [2021-10-30T21:18:21.272] [INFO] [VPC] - Getting securityGroup: Fc-deploy-component-generated - securitygroup-cn-hangzhou [2021-10-30T21:18:21.439] [INFO] [fc-deploy] -generated vpcConfig: securityGroupId: sg-bp16mj0j92jm506ind4g vSwitchId: vsw-bp12p47aa34uzgsu7wic2 vpcId: Vpc-bp1o9ew2j2mkrr8v5l3jz [2021-10-30T21:18:21.440] [INFO] [fC-deploy] - Using nasConfig: auto:fcWill try to generate related NAS file system automatically [2021-10-30T21:18:22.259] [INFO] [FC-NAS] -creating NasFileSystem: Alibaba-fcdeploycomponent-defaultnas-cn-hangzhou [2021-10-30T21:18:22.566] [INFO] [fC-NAS] - Creating MountTarget: 0cb1C4b180 [2021-10-30T21:18:25.241] [INFO] [fC-NAS] -NAS mount Target domain already created, waitingfor status to be 'Active', now is Pending [2021-10-30T21:18:27.505] [INFO] [FC-NAS] -NAS mount target domain already created, waitingfor status to be 'Active', now is Pending [2021-10-30T21:18:29.736] [INFO] [fC-NAS] -NAS mount target domain already created, waitingfor status to be 'Active', now is Pending [2021-10-30T21:18:31.853] [INFO] [fC-NAS] -NAS mount target domain already created, waitingfor status to be 'Active', now is Active [2021-10-30T21:18:32.091] [INFO] [fC-deploy] - Using region: Cn-hangzhou [2021-10-30T21:18:32.091] [INFO] [fC-deploy] - Using accessalias: default [2021-10-30T21:18:32.091] [INFO] [fC-deploy] - Using accessKeyID: * * * * * * * * * * * Udxi [the 2021-10-30 T21: oh. 091] [INFO] [FC - DEPLOY] - Using accessKeySecret: * * * * * * * * * * * 1 BWC 📎 Usingfc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set[2021-10-30T21:18:32.106] [INFO] [fc-deploy] - Checking Service _fc_nas_web-springboot-ensure -nas-dir-exist-service exists [2021-10-30T21:18:32.178] [INFO] [fc-deploy] -setting role: AliyunFCDefaultRole [2021-10-30T21:18:32.184] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists [2021-10-30T21:18:32.367] [INFO] [RAM] -updating role: AliyunFCDefaultRole [the 2021-10-30 T21: oh. 468] [INFO] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists [2021-10-30T21:18:32.602] [INFO] [fC-deploy] - Checking Function nas_dir_checker exists ✔ Make service _FC_NAS_web springboot - ensure - nas - dir - exist - service success. ✔ the Makefunction_fc_nas_web-springboot-ensure -nas-dir-exist-service/nas_dir_checker success. [2021-10-30T21:18:35.575] [INFO] _fc_nas_web-springboot-ensure -nas-dir-exist-service/nas_dir_checker success. [2021-10-30T21:18:35.660] [fc-deploy] - Checking Service _fc_nas_web-springboot-ensure -nas-dir-exist- Service exists [2021-10-30T21:18:35.660] [INFO ] [FC-DEPLOY] - Checking Function nas_dir_checker exists There is auto configinthe service: _fc_nas_web-springboot-ensure -nas-dir-exist-service [2021-10-30T21:18:43.438] [INFO] [fc-deploy] -using region: Cn-hangzhou [2021-10-30T21:18:43.439] [INFO] [fC-deploy] - Using accessalias: default [2021-10-30T21:18:43.439] [INFO] [fC-deploy] - Using accessKeyID: * * * * * * * * * * * Udxi [the 2021-10-30 T21: regained. 439] [INFO] [FC - DEPLOY] - Using accessKeySecret: * * * * * * * * * * * 1 BWC 📎 Usingfc deploy type: sdk, If you want to deploy with pulumi, you can [s cli fc-default set[2021-10-30T21:18:43.454] [INFO] [fc-deploy] -checking Service _fc_nas_web-springboot Exists [2021-10-30T21:18:43.527] [INFO] [fC-deploy] - Setting role: AliyunFCDefaultRole [2021-10-30T21:18:43.533] [INFO] [RAM] - Checking Role AliyunFCDefaultRole exists [2021-10-30T21:18:43.774] [INFO] [RAM] -updating role: AliyunFCDefaultRole [the 2021-10-30 T21: regained. 881] [INFO] [RAM] - Checking Plicy AliyunFCDefaultRolePolicy exists [2021-10-30T21:18:44.044] [INFO] [fC-deploy] - Checking Function nas_dir_checker exists [2021-10-30T21:18:44.120] [INFO [fc-deploy] - Checking Trigger httpTrigger exists [2021-10-30T21:18:44.192] [INFO] [fc-deploy] - Checking Trigger HttpTrigger exists ✔ come Make service _fc_nas_web-springboot successfunctionClick ok. Click ok. _fc_nas_web-springboot /nas_dir_checker success. configure Make trigger _fc_nas_web-springboot /nas_dir_checker/httpTrigger Success. [2021-10-30T21:18:47.007] [INFO] [fc-deploy] - Checking Service _fc_nas_web-springboot exists [2021-10-30T21:18:47.078] [INFO] [fC-deploy] - Checking Function nas_dir_checker exists [2021-10-30T21:18:47.206] [INFO] [DEPLOY] - Checking Function NAS_dir_checker exists [2021-10-30T21:18:47.206] [INFO ] [FC-DEPLOY] - Checking Trigger httpTrigger exists There is auto configinThe service: _fc_nas_web-springboot [2021-10-30T21:18:47.389] [INFO] [fc-deploy] -generated nasConfig: groupId: Generated The service: _fc_nas_web-springboot [2021-10-30T21:18:47.389] 10003 mountPoints: - fcDir: /mnt/auto nasDir: /web-springboot serverAddr: 0cb1c4b180-ulm3.cn-hangzhou.nas.aliyuncs.com userId: 10003 [2021-10-30T21:18:47.391] [INFO] [fC-deploy] - Checking Function Springboot exists [2021-10-30T21:18:47.504] [WARN] [FC - DEPLOY] - Image registry.cn-hangzhou.aliyuncs.com/web-framework/java11:0.0.1 dosed the not exist locally. Maybe you need to run's build' first ifIt dose not exist REMOTELY. [2021-10-30T21:18:47.506] [INFO] [fC-deploy] - Checking Trigger web-springboot exists [2021-10-30T21:18:47.576] [INFO] [fC-deploy] - Checking Trigger web-springboot exists in ✔ Make service web-springboot Success. ✔ the MakefunctionWeb - springboot/springboot success. ✔ Make the trigger web - springboot/springboot/web - springboot success. [2021-10-30T21:18:49.288] [INFO] [fC-deploy] - Checking Service Web-springboot exists [2021-10-30T21:18:49.359] [INFO] [fC-deploy] - Checking Function Springboot exists [2021-10-30T21:18:49.485] [INFO] [fc-deploy] - Checking Trigger Web-springboot exists [2021-10-30T21:18:49.669] [INFO] [fC-deploy] - Using customDomain: auto:fcWill try to generate related custom domain resources automatically foreground devsapp_domain. Zip file decompression completed foreground ✔ End of request [2021-10-30T21:18:57.618] [INFO] [fC-deploy] -generated auto custom domain: [the 2021-10-30 T21:18:57. 619] springboot.web-springboot.1831277402525713.cn-hangzhou.fc.devsapp.net [INFO] [FC - DEPLOY] - Creating custom domain: Springboot.web-springboot.1831277402525713.cn-hangzhou.fc.devsapp.net ✔ devsapp_fc - domain.zip file decompression Completed [2021-10-30T21:18:58.878] [INFO] [fc-domain] - Creating Custom DOMAIN: springboot.web-springboot.1831277402525713.cn-hangzhou.fc.devsapp.net There is auto configin the service: web-springboot
Tips for next step
======================
* Display information of the deployed resource: s info
* Display metrics: s metrics
* Display logs: s logs
* Invoke remote function: s invoke
* Remove Service: s remove service
* Remove Function: s remove function* Remove Trigger: s remove trigger * Remove CustomDomain: s remove domain Reminder nas upload instruction: The upload. / code/target/demo - 0.0.1 - the SNAPSHOT. Jar/MNT/auto/springboot/demo - 0.0.1 - the SNAPSHOT. Jar Tipsfor next step
======================
* Invoke remote function: S blue stocking Uploaddone
Tips for next step
======================
* Invoke remote function: s invoke springboot Try container acceleration Springboot: region: CN-hangzhou serviceName: web-springboot functionName: springboot customDomains: - http://springboot.web-springboot.1831277402525713.cn-hangzhou.fc.devsapp.netCopy the code
In the browser to access the temporary domain name http://springboot.web-springboot.1831277402525713.cn-hangzhou.fc.devsapp.net, you can see has been deployed successfully.
2.2.4 Customize the domain name
First of all, click “Domain name Management” in Ali Cloud function computing console to add a custom domain name.
Enter the domain name, select the service name, function name, version, and click OK. Notice The domain name must be recorded and resolved to the CNAME on the page.
After the configuration is successful, you can enter the customized domain name in the browser to verify the configuration.
3. Summary
This article introduces what Serverless is, what problems it solves and how to quickly deploy SpringBoot application on Ali Cloud function computing platform through Serverless Devs.