Original link: https://developer.aliyun.com/article/982746
1. Why migrate to Alibaba Cloud Functions?
My project is a holiday gift collection project, and there will be a short-term traffic peak during the festival. Visits are usually low. The previous architecture was the purchased Alibaba Cloud alb + multiple ecs + cloud msyql + cloud redis. The biggest problem is cost. When the traffic is usually low, the cost of ecs cannot be reduced.
Alibaba Cloud Function Computing is serverless, that is, a serverless architecture. For example, your business traffic suddenly increases in a short period of time. Function Compute will start multiple instances in milliseconds (the smallest unit used by Alibaba Cloud Function Compute FC to run functions). However, when someone accesses it, the first cold start is slightly slower, and at least one instance can be reserved according to the actual situation.
Deploying to Alibaba Cloud Function Compute can also reduce the cost of running the environment. In the previous mode, you need to install nginx on ecs, then install php, and install php's driver redis, etc. After reading the official documentation of Alibaba Cloud Function Compute, the custom runtime Debian 9 has built-in php7.4 and the built-in php extension is fully integrated and supports the redis I need. Not only is there money left to buy a server, but there is no need to install a php environment, not only that, but also a free computing power quota every month.
The reasons for the migration are summarized as follows: 1. The cost is reduced a lot 2. The environment deployment is eliminated 3. Automatic expansion, and it is born to deal with high concurrency
2. Transform the old project adaptation function calculation.
Code changes:
Although it is said that the environment deployment is avoided, my previous code is still inappropriate. For example, the logs of the previous code are stored in a certain directory of the server. If you migrate to Function Compute, the instance will be destroyed and rebuilt at any time, resulting in loss of logs. The solution is to write logs to Alibaba Cloud oss, or use Alibaba Cloud's log service to write to it.
Another point to note here is that my project is not separated from the front and back ends, and the authentication is still the penetrating session and cookie mode. If the session is a file kept on the server, the above problems will also exist. It is recommended to store in redis. My project is stored in redis, so this piece does not need to be changed. If there is such a problem in your project, then Need to improve.
When function computing communicates with cloud msyql and cloud redis, the principle of VPC intranet interworking must be adopted to reduce the overhead of link transmission and the risk of link hijacking.
3. Add s.yml and start the shell script
Configure s.yml to use the Serverless Devs client tool to publish to Alibaba Cloud Function Compute. Serverless Devs is not a client tool of Alibaba Cloud, but an open source serverless developer platform dedicated to providing developers with a powerful tool chain system. Through this platform, developers can not only experience multi-cloud serverless products with one click, deploy serverless projects at high speed, but also manage projects in the entire life cycle of serverless applications, and combine Serverless Devs with other tools/platforms very simply and quickly to further Improve R&D, operation and maintenance efficiency.
Its official website address: https://www.serverless-devs.com/
Then look at the configuration information in my s.yml, specifically what the important items are.
edition: 1.0.0
name: compoent-test
access: 'default'
services:
cn-hangzhou-test1002-func-3i3c0f95:
component: devsapp/fc
props:
region: cn-hangzhou
service:
logConfig:
enableRequestMetrics: true
enableInstanceMetrics: true
logBeginRule: DefaultRegex
project: aliyun-fc-cn-hangzhou-ae3ef8b8-db4a-5b7a-a040-7012789ad20f
logstore: function-log
role: acs:ram::1621341641365186:role/AliyunFcDefaultRole
internetAccess: true
name: test1002
function:
customRuntimeConfig:
command:
- bash
args:
- '-c'
- 'chmod 777 /code/start.sh && /code/start.sh'
handler: index.handler
instanceType: e1
runtime: custom
timeout: 5
instanceConcurrency: 20
memorySize: 512
caPort: 9000
environmentVariables: {}
internetAccess: true
name: func-3i3c0f95
asyncConfiguration: {}
codeUri: ./test1002/func-3i3c0f95
triggers:
- name: defaultTrigger
description: ''
type: http
qualifier: LATEST
config:
methods:
- GET
- POST
- PUT
- DELETE
authType: anonymous
codeUri: ./test1002/func-3i3c0f95 This specifies the location of my project code, and will copy the code under this directory to the /code directory of the debain system.
customRuntimeConfig:
command:
- bash
args:
- '-c'
- 'chmod 777 /code/start.sh && /code/start.sh'
This sentence refers to the project startup script. In fact, it is to execute the shell script of this start.sh, first give a 777 permission, and then execute it. Translated into a shell script is actually bash -c 'chmod 777 /code/start.sh && /code/start.sh'
caPort: 9000
The listening port 9000 must be the same as that in the startup script start.sh
#!/usr/bin/env bash
cd /code/tp5/public
php -S 0.0.0.0:9000 router.php
Here I think I still have to talk about this startup script, first cd to the public directory, the entry of thinkphp5 is under public, which is related to the project framework. Then there is this startup script, which is a unique way of writing in thinkphp5.
I won't go into detail about other items, and I should be able to understand it by looking at it.
4. Publish using client tools
The installation of the tool is ignored, and the official documentation is very detailed.
https://docs.serverless-devs.com/serverless-devs/quick_start
After the Serverless Devs tool is installed, configure the AccessKey ID and AccessKey Secret of Alibaba Cloud, create s.yml in the project root directory, and create the above startup script start.sh in the code directory, and then you can use the s of the client tool deploy is deployed to Alibaba Cloud Function Compute.
Published successfully
4. Bind your own domain name
https://fcnext.console.aliyun.com/cn-hangzhou/domains/create
Put your own domain name cname to the "public network cname" in the figure above, and then select the correct version of the service name test function. Just create it.
The deployment was successful, haha.
5. Talk about your feelings
Function computing serverless is the future trend, and developers can focus more on the business layer. It took about 3 days from the beginning of the expected migration to the modification of the code and the review of Alibaba Cloud Function Computing documents to the successful migration. I have a deeper understanding of Alibaba Cloud Function Computing, and I also hope that the migrated projects will be implemented next time. It can run stably during peak usage. I will also keep an eye on the dynamics of Alibaba Cloud Function Computing in the future, and I would like to thank the Alibaba Cloud Function Computing team for making such an excellent product.
For more content, pay attention to the Serverless WeChat official account (ID: serverlessdevs), which brings together the most comprehensive content of serverless technology, regularly holds serverless events, live broadcasts, and user best practices.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。