Sina Sae-application porting Guide

Source: Internet
Author: User
Tags wrappers create domain
Application porting Guide I. Why transplant the application?       SAE prohibits I/O write operations, and the code directory cannot be written. This means that the image uploading and cache generation operations of common programs cannot run normally on SAES. At this time, you need to modify the code before running your program on SAES.      Why does SAE prohibit I/O write operations?       SAE adopts a distributed architecture design. application code will be deployed on Multiple front-end servers, and each access request may reach different servers. (For example ):      Assume that there are now four servers: A, B, C, and D. The user uploads an image to server a. The second access request may arrive at server B. At this time, the image stored on server a cannot be obtained.        SAE uses storage services such as memcachex and storage to replace traditional I/O operations, which is more efficient than traditional I/O read/write operations. This effectively solves the problem of low program performance caused by Io bottlenecks.        In addition, many websites are attacked because the server has the write permission, program code can be modified by hackers, and SAE prohibits write operations, which also improves the server security.    Therefore, to improve performance and security, Sae prohibits local I/O write operations. Developers can use storage, memcache, kvdb, and other services to store data to be written.    2. Quickly port your program   Use wrappers   Although SAE prohibits I/O write operations, it does not prohibit fwrite, file_put_contents and Other write operation functions. At the same time, Sae also provides the wrappers service, which makes the porting program relatively simple. Suppose we want to port the following code to Sae.
  file_put_contents('test.php','');   include 'test.php';?>

 You only need to add a prefix to the file address.

  file_put_contents('saemc://test.php','');   include 'saemc://test.php';?>

  If the address prefix is saemc :// Read/write operations in memcache. If the prefix is saestor: //, read/write operations are performed in storage. Prefix: saekv: // indicates the operation on kvdb. This is the function of wrappers.  Note:   1. Before using wappers, you must initialize the corresponding service.    Initialize memcache: Enter the memcache Service Management Background Http://sae.sina.com.cn /? M = mcmng In the upper-right corner of the page, select my applications. Click "Click here to initialize MC" to initialize memcache.    Initialize storage: Enter the storage service management background http://sae.sina.com.cn /? M = Storage , After selecting the specified application
Click "create domain" to create a domain. Multiple domains can be created here. The developer uses wrappers to upload the file to which domain, depending on the first directory name of the file address. For example The address "saestor: // upload/image/1.jpg" is saved in the domain named upload.    In addition, MySQL and kvdb must be initialized before they can be used. If the program is useful to these services, initialize them first.    2. cache with frequent operations is not suitable for storage.      Storage read/write efficiency is much lower than kvdb and memcache. The cache with frequent operations is not suitable for storage. Therefore, do not use saestor: // unless you are operating on files uploaded by users ://.    Especially the cache of some program code snippets. In the preceding example, if you use Code such as include 'saestor: // XXX', the program performance will be low.      After understanding the usage of wrappers, you can start to transplant the program.  Quick locating with errors   Developers can quickly locate the code to be modified by using the SAE error prompt.    First, make sure that the error message is enabled for your application. Go to the application's code management background http://sae.sina.com.cn /? M = vermng. Check whether the "error prompt" of the corresponding version is enabled (it is enabled by default ). Check whether the program has any code similar to error_reporting (0). The error prompt is blocked. If yes, comment them out first.    Then upload the program to Sae. If there is any incompatibility, the error prompt will tell you the specific line of the specific file.    If a message like this appears:  Sae_warning: file_put_contents (./xxx. php) [function. File-put-Contents]: failed to open stream: Permission denied in XXX. php on line 2   It indicates that the program is performing Io write operations. You only need to add the wrappers prefix to the address to achieve compatibility.     If your program is well encapsulated, it is often very easy to change the code for Io write operations. For example, thinkphp defines the runtime_path constant. All files generated by Io write operations will be stored in the folder specified by the runtime_path constant. to transplant the thinkphp program, you only need to change the runtime_path constant and add the saemc: // prefix. In fact, this is also true for other frameworks or programs with better encapsulation. You only need to change a line of code to be compatible with IO write operations. If you re-use thinkphp to develop a program, we recommend using thinkphp of SAE.    Note: After wrappers is used, some operations are invalid. For example, file_exists, you can comment out such a function directly.      If the following message is displayed:  Sae_warning: mysql_connect () [function. mysql-connect]: This app is not authorised in XXX. php on line 2   The program is using the MySQL database. At this time, you need to initialize MySQL and import the database. After initialization, you can click "manage MySQL" and use phpMyAdmin to import data.
You can use the deferredjob service to import large data volumes.    After importing the database, you must change the database username and password in the original program to Sae. The database username, password, and database name of SAE are represented by constants. You can see it in the MySQL service management office. The MySQL database of SAE supports distributed connection between two databases. If your original program only supports single database operations, connect to the domain name of the master database.    When no error message is prompted for your program on SAES, your program can run on SAES. Check whether some detailed functions are implemented. For example, the image upload function. We recommend that you store uploaded images or other attachments in storage. After the image is stored in storage,
The image access address is also different from the previous one. You need to use the geturl method of storage to obtain the image access address, such:
$ S = new saestorage (); $ url = $ S-> geturl ('domain ', 'filepath'); // obtain the image address, is filepath the path of the image in storage?>

  Next, developers can further optimize the program to achieve better performance.  Iii. Performance Optimization   We provide some optimization suggestions.  1 Io operations generate files for differentiated Storage    (1) It is recommended to put the template compilation cache or the cache of other program code segments in memcache, and resolutely oppose putting the template compilation cache in storage,
Because storage has lower read/write efficiency than memcache, saving them to storage will slow your program running.    (2) store uploaded images or attachments in storage. If your images require anti-leech protection and other functions, you can also easily implement anti-leech protection in the SAE service management background by setting domain attributes.    (3) Save some fixed caches to kvdb. Some caches are not suitable for storage in memcache,
Because the cache in memcache may disappear, for example, when the memcache space is insufficient, the cache first stored in memcache will be deleted. Therefore, when you read the cache stored in memcache, you must determine whether the cache exists. If the cache does not exist, regenerate it. However, some caches generally do not change after they are generated once. When reading the cache, you do not need to judge whether the cache exists.
Kvdb is recommended for storing this type of cache.    2 Database read/write splitting    SAE The master database of the MySQL database can be read and written, and the slave database is read-only. Queries consume less beans from the slave database and respond faster. Therefore, we recommend that you implement master-slave separation as much as possible. Currently, many frameworks and programs support read/write splitting, which requires simple configuration. If the program only supports a single database, you can try to execute the SQL statement to determine that the slave database is connected if the SELECT statement is used.  3 Use the services provided by SAE    SAE provides many efficient services. We recommend that you use the SAE service wherever possible. For example, the program has the ranking function, which can be implemented by the rank service; the verification code function can use the saevcode service; the mail service can be used to send emails. Please refer to the documents related to each service for learning.  4 Consider using native Interfaces      Wrappers is convenient and simple, but it is certainly not as efficient as the native memcache or storage interface. If you care about efficiency,
You can use the original interface for porting. The Native Interface to achieve the porting method can refer to: http://qing.weibo.com/1631767865/6142cd3933000cj9.html  Iv. FAQs 1 Implement pseudo-static    SAE does not support. htaccess files, but it can implement pseudo-static state through the appconfig service.  2 Generate static pages    Some programs have a mechanism to generate pure static html. You can use kvdb to store HTML static page data. Because Sae prohibits I/O write operations and cannot implement pure static data, we can achieve the same effect using pseudo static data, the following is a simple example.  Assume that the program generates an HTML file using the following code before transplantation:
file_put_contents('html/index.html', 'htmlcontent');?>
    In this way, the user enters the address http: // your domain name/html/index.html in the browser. A pure static page is accessed.  Now we need to port the program to Sae.  First, save the HTML data to kvdb.
file_put_contents('saekv://html/index.html', 'htmlcontent');?>

  Use config. yaml to write a pseudo-static statement.
- rewrite: if (path ~ "/html/(.*)") goto "/readhtml.php?path=html/$1"

  If you access the HTML directory, the static data is read from the readhtml. php file.  The readhtml. PHP code is:
echo file_get_contents('saekv://'.$_GET['path']);?>

  In this way, users can access data by entering the address http: // your domain name/html/index.html in the browser.  3 Log records.Some programs have the logging function. If logs are read and written too frequently and are not suitable for storing log files in storage, we recommend that you use sae_debug for logging. However, sae_debug logs are also output to the browser. Many programs want to secretly record logs, which can be implemented by encapsulating functions. For example:
Function sae_log ($ MSG) {sae_set_display_errors (false); // disable the output sae_debug ($ MSG); // record the log sae_set_display_errors (true ); // enable information output after logging. Otherwise, normal error messages will be blocked.}?>
Logs recorded by the sae_debug function can be viewed in the "log Center" in the application management background. logs of the debug type are displayed. You need to select the debug type from the drop-down menu in the search box. 4 Cache sharing    Multiple versions can be created for each application of SAE, but these versions share the same memcache, kvdb, and so on.
Services, sometimes prone to cache sharing issues. For example, if an application has created two versions with the same program and the program has a template cache mechanism, only the template of version 1 is modified, it is found that the content of version 2 has been modified. We need to avoid the same cache name between different versions. Developers can use the $ _ server ['HTTP _ appversion'] variable to add the application version number to the cache name.  5 Memcache cache requires an update mechanism      Because the memcache cache may disappear, you should judge whether the cache exists when reading the memcache cache. If the cache does not exist, otherwise, once the memcache cache is deleted and the program does not update the cache, the website may not be accessible. After the program is transplanted, the developer can manually clear the memcache cache in the application's memcahe service management background, and then test whether the website can run normally.
We recommend that you use kvdb for caching without an update mechanism.  6. How to obtain the storage Domain Name   Sometimes the image access path of the program can define the prefix. We only need to replace the prefix with the storage domain name. How to obtain the storage Domain Name:
$ S = new saestorage (); $ domain = rtrim ($ S-> geturl ('domain ',''),'/'); // note that the second parameter of geturl is null?>

7. wrappers cannot be used in some places Not all file addresses with the corresponding wrappers prefix can be compatible. For example, the following code:
$ IMG = imagecreatefrompng ("http://sae.sina.com.cn/static/image/logo.beta.new.png ");//... After some column image processing functions... Imagepng ($ IMG, 'saestor: // upload/logo.png '); // do you want to save the processed image through wrappers?>

  The above Code cannot save images to storage.     Developers can use temporary files to solve the above problems.
$ IMG = imagecreatefrompng ("http://sae.sina.com.cn/static/image/logo.beta.new.png ");//... After some column image processing functions... Optional '); // save as a temporary file file_put_contents ('saestor: // upload/logo.png', file_get_contents(sae_tmp_path.'logo.png ');?>

  Therefore, although you add the wrappers prefix to the file address in the program, you also need to check whether the file address is used in a function that does not support wrappers.    Generally, file operation functions support wrappers, such as file_put_contents, fwrite, file_get_contents, include, file_exists, filemtime, move_uploaded_file, and so on. Only some functions with special functions do not support wrappers.  8. Some functions and classes are disabled in SAE.   For platform security considerations, Sae disables some function classes. For details, seeHttp://sae.sina.com.cn /? M = devcenter & catid = 220.     Use other functions with the same functions to replace Disabled functions. For example, use simplexml to replace domdocument for XML parsing.  5. Submit to application Repository   Developers can submit the transplanted programs to the application repository of SAE, so that others can quickly obtain your programs through one-click installation of the application repository.    We recommend that you write a "Transplant record" When porting a program ". Submit the transplant records to us at the time of application submission. So that we can easily review your application.    The port records need to be clearly written about where the port program was modified and What Sae service was used.    For more details, see Http://sae.sina.com.cn /? M = devcenter & catid = 230.    Or directly access the following address: http://sae.sina.com.cn /? M = apps & A = step_sae_app

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.