Easily build applications based Serverless's Go (Gin, Beego example)

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 .
Fun: Fun is a tool used to support Serverless application deployment, can help you easily manage function computing, API Gateway, logging services and other resources. It is through a resource configuration file (template.yml), help you develop, build, deploy operation. More Fun document reference .

Note: This tip requires Fun version 3.6.4 or greater.

Results preview

Two application examples in this article involves the deployment of a link, click on the preview:

  1. Gin simple example: http://gentest.functioncompute.com/ping
  2. Beego simple example: http://beegotest.functioncompute.com
  3. Gin blog site Example: http://mdblog.functioncompute.com

Preparing the Environment

First, in accordance with the Fun of installation documentation method described in the Fun to install the machine.
PS: The method described in this article, does not require installation Docker, you can only install Fun, the easiest way is to directly download an executable binary file.

After installation is complete, you can perform Fun fun --version check whether the installation was successful.

Example 1: Example migrate to function calculation gin

First, we follow the official example of the steps, the installation gin (version requires golang above 1.11+):

go get -u github.com/gin-gonic/gin

Create a example.go, fill in the following contents:

package main

import "github.com/gin-gonic/gin"

func main() {
    r := gin.Default()
    r.GET("/ping", func(c *gin.Context) {
        c.JSON(200, gin.H{
            "message": "pong",
        })
    })
  r.Run() // listen and serve on 0.0.0.0:8080 (for windows "localhost:8080")
}

Use the following command to start the application:

go run example.go

Then visit localhost: 8080 / the ping , you can see the return the following results:

Finally, the direct use fun deploy to deploy.

fun deploy

We detail here the first time under deployment process:

  1. First detected Fun Fun This is not a project will be prompted to automatically create (you can press the Enter or enter y)
  2. Fun will then try to detect the application startup port (function computing requirements must be started 0.0.0.0:9000), if you do not match, it will help modify:
  3. Press Enter, Fun will automatically detect build the executable program generated, if not detected, the user is prompted to compile the specified command
  4. After pressing Enter, it will automatically compile, and after completion of the translation, it will automatically generate the required bootstrap file Fun and template.yml file, and then will be automatically deployed operations, prior to deployment, this deployment will be listed first brought changes:
  5. If confirmed no problem with the above changes, then press Enter to start the deployment process resources.

After the deployment is complete, you can see automatically generates a temporary test domain name can be accessed 14118335-1911504709953557.test.functioncompute.com , through this temporary domain name, can be used to develop, test preview application.

Use the temporary domain name, access application API interface 14118335-1911504709953557.test.functioncompute.com/ping test, the effect is as follows:

Note: The temporary domain name just as a demonstration and development, there is a statute of limitations, if used to produce, bind the domain name has been filed.

Example Two: migrate to the exemplary function calculation beego

First, we follow the official example of steps to build beego application:

go get github.com/astaxie/beego

Create a hello.go, fill in the following contents:

package main

import "github.com/astaxie/beego"

func main(){
    beego.Run()
}

Use the following command to start the application:

go run hello.go

Then visit localhost: 8080 , you can see the results are returned.

Finally deployment with fun deploy applications after experiencing temporary domain name to preview an example of a similar process, you can return through the Fun.

Example Three: Migration gin build a blog application to function calculation

Example Three we choose to apply a slightly more complex, we follow the official help documentation step, build a blog-based application gin of:

1. Cloning of the local item (Download zip package may be):

git clone https://github.com/tanhe123/mdblog.git

2. modify the configuration file

In the configdirectory, there is a configuration file config.example.toml, copy it, and modify the name config.toml, and then modify several configuration inside:

  • Port = 8091 to modify the port = 9000, represents the application startup, start the 9000 port.
  • The debug = true revised to debug = false, expressed using the production version
  • The dir = "logs" amended as dir = "/ tmp", indicates that the log is written to the / tmp directory (without mount NAS, the function calculates only the directory can be written)

3. Compile and run the application

go build # 会生成一个 mdblog 的二进制可执行程序
./mdblog # 直接运行该可执行程序

If you experience network problems, you can use https://goproxy.cn/ accelerated.

Visit http: // localhost: 9000 preview

4. Deploy

Finally, after local tests no problem, we need to publish to the function calculation, although this example than the above two aspects of the code is much more complicated, but the deployment process is the same, immediate execution fun deployand then all the way to enter.

This example was generated temporary domain: https://14118335-1911504709953557.test.functioncompute.com

Open the can see the following effect:

Open a specific blog post, the effect is as follows:

to sum up

We introduced through three examples of how to deploy the application to go on a computing function, we can see from these three examples, application migration go to a function calculation is very easy, the main focus was on how to configure and start locally application, and then deploy simply by fun deploycommand. After the deployment is complete, you can enjoy the function of elastic computing brings scalable, pay by volume, operation and maintenance-free characteristics.

More Reference

  1. Easily build applications based Serverless of ThinkPHP
  2. Funcraft

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

Guess you like

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