In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)05/31 Report--
How to easily build Go applications based on Serverless? for this problem, this article introduces the corresponding analysis and solutions in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
First of all, we introduce several important concepts that appear in this article:
Function computing (Function Compute): function computing is an event-driven service. Through function calculation, users do not need to manage the operation of the server, but only need to write code and upload. The function calculation prepares the computing resources and runs the user code in an elastic way, while the user only needs to pay according to the resources consumed by the actual code.
Fun: Fun is a tool for supporting Serverless application deployment, which can help you easily manage resources such as function computing, API gateways, log services, and so on. It assists you in developing, building, and deploying operations through a resource configuration file (template.yml). More documentation references for Fun.
Note: the techniques described in this article require Fun version 3.6.4 or greater.
Effect preview
For the deployed links of the two application examples involved in this article, click the preview effect:
Simple example of Gin: http://gentest.functioncompute.com/ping
Simple example of Beego: http://beegotest.functioncompute.com
Gin blog website example: http://mdblog.functioncompute.com
Environmental preparation
First install Fun on this machine as described in the Fun installation documentation.
PS: the method described in this article, you don't need to install Docker, just install Fun. The easiest way is to download executable binaries directly.
After the installation is complete, you can execute fun-version to check that the Fun installation is successful.
Example 1: migrate gin samples to function calculations
First of all, let's follow the steps of the official example to install gin (golang version is required to be above 1.11 +):
Go get-u github.com/gin-gonic/gin
Create an example.go with the following content:
Package mainimport "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 message 8080 (for windows "localhost:8080")}
Start the application using the following command:
Go run example.go
After visiting localhost:8080/ping, you can see that the returned result is as follows:
Finally, you can deploy directly using fun deploy.
Fun deploy
Here is a detailed description of the first deployment process:
Fun first detects that this is not a Fun project and prompts whether to create it automatically (press enter or enter y)
Then Fun will try to detect the startup port of the application (function calculation requires that it be started at 0.0.0.0Va9000). If it does not match, it will help to modify it:
Press enter, and Fun will automatically detect the executable program generated by the building. if it cannot be detected, the user will be prompted to compile using the specified command.
After pressing enter, it will be compiled automatically. After the compilation is completed, the bootstrap file and template.yml file required for Fun will be automatically generated, and then the automatic deployment operation will be performed. Before deployment, the changes brought about by this deployment will be listed first:
If you confirm that there is no problem with the above changes, press enter to start the resource deployment process.
After the deployment is completed, you can see that a temporary domain name 14118335-1911504709953557.test.functioncompute.com that can be accessed for testing is automatically generated. Through this temporary domain name, it can be used to preview applications during development and testing.
Use the temporary domain name to access the application's API interface 14118335-1911504709953557.test.functioncompute.com/ping test, and the results are as follows:
Note: the temporary domain name is only used for demonstration and development, it is time-limited. If it is used for production, please bind the domain name that has been registered.
Example 2: migrate beego samples to function calculations
First of all, we follow the steps of the official example to build the beego application:
Go get github.com/astaxie/beego
Create a hello.go with the following content:
Package mainimport "github.com/astaxie/beego" func main () {beego.Run ()}
Start the application using the following command:
Go run hello.go
Then visit localhost:8080, and you can see the result returned.
Finally, after going through the process similar to example 1 using fun deploy directly, you can preview the application through the temporary domain name returned by Fun.
Example 3: migrate the blog built by gin to function calculation
In example 3, we choose a slightly more complex application. We follow the steps of the official help document to build a blog application based on gin:
1. Clone the project locally (or download the zip package directly): git clone https://github.com/tanhe123/mdblog.git2. Modify the configuration file
In the config directory, there is a configuration file config.example.toml, make a copy of it, change the name to config.toml, and then modify several configurations in it:
Change port = 8091 to port = 9000, which means that when the application starts, it starts at port 9000.
Change debug = true to debug = false, indicating that the production version is used
Change dir = "logs" to dir = "/ tmp", which means that the log is written to the / tmp directory (if NAS is not mounted, only this directory can be written)
3. Compiling and running the application go build # generates a binary executable program for mdblog. / mdblog # runs the executable program directly
If you encounter network problems, you can use https://goproxy.cn/ acceleration.
Access the http://localhost:9000 preview effect
4. Deployment
Finally, when there is no problem with the local test, we need to publish to the function calculation. Although this example is much more complicated than the above two code levels, the deployment process is the same. Just execute fun deploy and enter all the way.
The temporary domain name generated by this example is: https://14118335-1911504709953557.test.functioncompute.com
Open it and you can see the following effect:
Open a specific blog post, and the results are as follows:
This is the answer to the question on how to easily build Serverless-based Go applications. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.