The thinkphp5 project migrated to Alibaba Cloud Function Compute

1. Why should I migrate to Alibaba Cloud functions?

My project is a holiday gift collection project. There will be a short peak of traffic during the holiday. Visits are usually very low. The previous architecture is Alibaba Cloud alb+multiple ecs+cloud msyql+cloud Redis. The biggest problem is cost. ECS costs cannot be reduced when the traffic is low.

Alibaba Cloud functional computing is serverless, that is, non-service architecture. For example, your business traffic suddenly increases in a short time. The function calculation will start multiple instances at the millisecond level (the smallest unit used by Alibaba Cloud function calculation FC to run the function). If no one accesses the instance, no instance can be run, and the cost is zero. However, when someone visits, the first cold start is a little slower. You can set at least one instance to be reserved according to the actual situation.

Deploying to Alibaba Cloud functional computing can also reduce the cost of building the operating environment. In the previous mode, you need to install nginx in ecs, then install php, and install the driver Redis for php. After reading the official Alibaba Cloud functional computing document, at present, custom runtime Debian 9 has built-in php7.4 and the extension of built-in php has been adjusted to support the Redis I need. Not only is there money left to buy the server, but also there is no need to install the php environment. Not only that, there is free computing power quota every month.

The reasons for the migration are summarized as follows: 1. The cost has been reduced a lot. 2. The environment deployment has been eliminated. 3. The automatic capacity expansion is natural to cope with high concurrency

2. Calculation of the adaptation function of the old project.

Code change:

Although the environment deployment is exempted, there are still some mismatches in my previous code. For example, the log of the previous code is stored in a directory of the server. If you migrate to function calculation, the instance will be destroyed and rebuilt at any time, resulting in log loss. The solution is to write the log to Alibaba Cloud OSS or use Alibaba Cloud's log service to write it there.

One more thing to note here is that my project is not separated from the front end and the back end. Authentication is still a penetration session and cookie mode. If the session is reserved in the server file, the above problem will also exist. It is suggested that my project is saved in Redis in the province, so this one does not need to be changed. If your project has such a problem, it needs to be improved.

When communicating with cloud msyql and cloud Redis, the principle of vpc intranet intercommunication must be adopted to reduce the cost of link transmission and the risk of link hijacking.

3. Add s.yml and start shell script

Configure s.yml to use the Serverless Devs client tool to publish to Alibaba Cloud functional computing. Serverless Devs is not an Alibaba Cloud client tool, but an open source and open 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, rapidly deploy Serverless projects, but also manage projects in the whole life cycle of Serverless applications, and combine Serverless Devs with other tools/platforms very simply and quickly to further improve the efficiency of research and development, operation and maintenance.

Its official website address: https://www.serverless-devs.com/

Then look at the configuration information in my s.yml. Specifically, what are the important items.

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 specifies the location of my project code, and copies 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 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 the starting 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 need to talk about the startup script. First, cd it to the public directory. The entry of thinkphp5 is under public. This is related to the project framework. Then there is the startup script, which is the unique writing method of thinkphp5.

I won't go into details about other items. I should be able to understand them.

4 Publish using client tools

The installation of the tool is ignored, and the official document is very detailed:

https://docs.serverless-devs.com/serverless-devs/quick_start

After the Serverless Devs tool is installed, configure the AliCloud AccessKey ID and AccessKey Secret, create s.yml in the project root directory, and create the startup script start.sh in the code directory. Then you can use the client tool's s deploy to deploy to Alibaba Cloud function computing.

Publishing succeeded

4. Bind your own domain name

https://fcnext.console.aliyun.com/cn-hangzhou/domains/create

Add your domain name cname to the "public network cname" in the figure above, and then select the correct version of the service name test function. Create it.

Deployment succeeded, haha.

5. Talk about your feelings

Function calculation serverless is the future trend, and developers can pay more attention to the business layer. It took about 3 days from the expected migration to code modification and Alibaba Cloud function calculation document review to the successful migration, and gained a deeper understanding of Alibaba Cloud function calculation. At the same time, it is also expected that the migrated project will run stably at the next peak. I will also pay attention to the dynamic of Alibaba Cloud function computing at any time later, and thank Alibaba Cloud function computing team for making such excellent products.

The Serverless function calculation and evaluation essay solicitation activity is in full swing. During the period from June 28 to July 31, participating in the product evaluation submission and release of articles, you will have the opportunity to win Beats headset, mechanical keyboard, 1000 yuan Tmall supermarket card, Youku membership season card and many other good gifts!

The direction of submission can be referred to (but not limited to):

• Your experience and suggestions on functional computing FC product capabilities help other users choose Serverless services.

• Use function computing FC to create application scenario evaluation, such as building a cloud blog based on function computing FC, building an elastic and highly available Serverless web application, and building an elastic and highly available video processing system based on Serverless architecture.

Related Articles

Explore More Special Offers

  1. Short Message Service(SMS) & Mail Service

    50,000 email package starts as low as USD 1.99, 120 short messages start at only USD 1.00

phone Contact Us