Serverless quickly build online image processing applications

Author: lean Yin

First introduced in several important concepts under article appears:

Function calculates (Function Compute) : function computing is an event-driven service, by computing function, users need to manage the operation of the servers, just write code and upload. Function calculates ready to computing resources, and is elastically stretchable run user code, and users only need to pay according to the actual code that runs resources consumed. More information function calculation reference .

ImageMagick : ImageMagick is a view, edit bitmap files as well as open-source software suite image format conversion. It can read, edit images more than 100 formats. . See Wikipedia entry

ImageMagick is an image manipulation tool, using ImageMagick can easily crop the picture and zoom. Although many languages ​​ImageMagick library encapsulates the call, but the picture processing functions and core business functions together in the same service, often do not fit on the software architecture. There are two aspects of the following reasons:

On the one hand, image processing dependent on external bin, has compiled two classifications do not have the portability, packaged and released to trouble. On the other hand, image processing is often more time-consuming computing resources, for most business systems belong to the edge of the picture processing business, rather than a core business, so more computing resources reserved for the entire service is not cost-effective. Better option is to process the image in the form of micro-class traffic and services out segmentation, disposed over the elastic underlying services provided. For this type of technology needs, Serverless is very relevant to the.

This article focuses on how to quickly deploy a highly available elastic image processing function computing services on the platform, and on this basis, easily customized.

Quick Start

Here we calculate the aid of the function of Application Center, quickly convert the picture to deploy out of service.

  1. Open the function calculates the Image Resizer application details page . If you're not on function computing services may need to be, opened the service is free, there is another function to calculate the monthly quota free trial service will not be charged.

    1.png

  2. 滚动到Image Resizer 应用详情页的最底部,点击“立即部署”按钮。

    2.png

  3. 填写应用名称:my-image-resizer,然后点击“部署”按钮。

    3.png

  4. 拷贝 HttpTriggerEndpoint 里的网址。

    4.png

  5. 在浏览器里打开上面的网址,或者通过 curl 进行调用。注意:由于没有绑定域名,所以应用中心会默认下载而不是直接在浏览器里打开图片。
curl 'https://xxxxx.cn-shanghai.fc.aliyuncs.com/2016-08-15/proxy/my-image-resizer-ResizeService-5A40B5A8B981/my-image-resizer-ResizeFunction-3E71C57C0094/' --output resized.jpg

工作原理

这是一个单函数结合 Http Trigger 的应用。Http Trigger 以 HTTP GET 方法对外暴露服务,客户端传递三个请求参数:url、width 和 height。其中

  • url 表示需要进行处理的源图片地址
  • width 表示裁剪或缩放后的图片宽度
  • height 表示裁剪的图片宽度。该参数缺失时,表示采用缩放的方式调整图片。

该应用的架构图如下:

6.jpg

FC 函数接受到 HTTP 请求之后,执行如下三个步骤:

  1. 把 url 指向的图片下载下来
  2. 使用 imagemagick 进行图片转换
  3. 将图片通过 http 协议返回给客户端

上面我们通过了函数计算的应用中心快速上线了一个图片转换的服务。函数计算是按照调用次数收费的,所以上述服务即使保持在线也不会产生费用。而又因为函数计算每月有免费的额度,所以日常开发的调用也不会产生费用。

定制化开发

依赖工具

本项目是在 MacOS 下开发的,涉及到的工具是平台无关的,对于 Linux 和 Windows 桌面系统应该也同样适用。在开始本例之前请确保如下工具已经正确的安装,更新到最新版本,并进行正确的配置。

Fun 工具依赖于 docker 来模拟本地环境。

对于 MacOS 用户可以使用 homebrew 进行安装:

brew cask install docker
brew tap vangie/formula
brew install fun

Windows 和 Linux 用户安装请参考:

  1. https://github.com/aliyun/fun/blob/master/docs/usage/installation.md

After installation, remember to perform fun configinitialization at the configuration.

Note that if you have already installed the funcraft, ensure version funcraft in 3.1.3 above.

$ fun --version
3.1.3

initialization

git clone https://github.com/vangie/fc-image-resizer
cd fc-image-resizer

Installation depends

npm install

Running locally

$ fun local start
using template: .fun/build/artifacts/template.yml
HttpTrigger httpTrigger of ResizeService/ResizeFunction was registered
        url: http://localhost:8000/2016-08-15/proxy/ResizeService/ResizeFunction
        methods: [ 'GET' ]
        authType: ANONYMOUS


function compute app listening on port 8000!

Then use your browser or curl debug URL http: // localhost: 8000 / 2016-08-15 / proxy / ResizeService / ResizeFunction

deploy

fun deploy

In order to obtain a better development experience, we recommend installing Aliyun Serverless VSCode Extension

Reference links

  1. Funcraft
  2. Aliyun Serverless VSCode Extension

" Alibaba Cloud native technology circles concerned micro service, Serverless, container, Service Mesh and other technical fields, focusing cloud native popular technology trends, cloud native large-scale landing practice, do most understand cloud original developer of technology circles."

Guess you like

Origin www.cnblogs.com/alisystemsoftware/p/12032163.html