Author: Durian
Why migrate?
Our business has many RESTful APIs that provide external services and perform many different tasks, such as synchronizing commodity information in chain ERP to platforms such as Meituan/Ele.me, and online invoicing. Due to the uncertainty of various APIs and task execution, services are often unavailable due to insufficient resources, but blind expansion is costly. The entire team is stuck in constant expansion and contraction every day . The key is that sometimes it is a little slower, which will affect the business and lead to complaints. Every day, they are urged by other business departments to make new functions. What is even more difficult is that because we have no experience in operation and maintenance, we have been repeatedly used by hackers for mining with our few machines. As a R&D team of a start-up company, we have few resources, few manpower, and little experience. It is really painful to face the pressure of stability, security and business.
After being introduced to function computation by a former colleague, I found this to be very useful! The migration process is very smooth, and the effect of the migration has greatly exceeded my expectations. Here's why I think functional computing is a good fit for us:
- The default elasticity can easily deal with a large number of API requests and tasks, and will no longer be unavailable due to resource exhaustion due to untimely expansion!
- When there is no traffic, it supports scaling down to 0, which is a money-saving artifact, and there is no need to buy virtual machines and load balancing anymore. For us, the cost reduction effect is great!
- Free operation and maintenance, eliminating the operation and maintenance costs of virtual machines!
- More secure, it can't be SSHed into, and it won't be left on like a virtual machine, waiting to be scanned and hacked!
- Zero modification, no need to modify the code, the JAR package on the previous virtual machine can directly run on the function calculation!
Migration steps
There are three ways to use it, here I will talk about how to operate on the console.
- Use the Function Compute console to migrate.
- Use the S tool provided by Function Compute to deploy by command line + YAML, see details 1 [ 1] .
- Use App Center on the Function Compute console to automatically build and deploy from source code repositories such as GitHub. CICD/GitOps is there directly, it's delicious! View the details at the end of the article 2 [ 2]
1. Enable Function Compute
Access the console [ 3 ] and click "Activate for free" to jump to the activation page.
2. Create a service
Click "Services and Functions", "Create Service", enter the "Name" and click "OK".
3. Compress the JAR package to get a ZIP file
Notice! ! ! Here we need to compress the packaged JAR package, and then upload the ZIP package! ! !
Note: In fact, you can also upload the JAR package directly, but the startup command should be written as java org.springframework.boot.loader.JarLauncher I personally don't like this way of writing. I still like the way java -jar gs-rest-service-0.1.0.jar is written.
If you don't have a JAR package available yet, please refer to the SpringBoot official quick start documentation [ 4] to build it.
4. Create a function
- On the "Function Management" page, click "Create Function",
- Select " Smooth Migration of Web Server with Custom Runtime "
- " Running Environment " select the Java version you need
- Select " Upload code via ZIP package "
- " Startup command " is the command you use to start the JAR package on the virtual machine, for example: java -jar rest-service-initial-0.0.1-SNAPSHOT.jar.zip
- " Listening port " is the port your JAVA program is listening on on the virtual machine, for example: 8080
- " Request Handler Type " select " Handle HTTP Requests "
- Click "Create"
5. Test function
Find "Public Network Access Address" in the trigger list on the function details page. Note: Due to relevant regulations, this URL cannot be opened directly in the browser, and you need to configure your own domain name to access it in the browser.
You can test it with curl command.
You can also test directly through the "Test Function" tab in the function details page.
6. Use your own domain name to access functions
- Click "Home", "Domain Management", "Create Domain", "Add Custom Domain"
- Copy the "public network CNAME" in the page, and add a CNAME record for your domain name on the DNS console [ 6]
- Select the service and function you just created in the routing configuration
- Click "Create"
- Finish! The service can now be accessed through your own domain name!
7. More advanced documentation
To configure the NAS file system:
https://help.aliyun.com/document_detail/87401.html
Configure HTTPS:
https://help.aliyun.com/document_detail/90763.html
Authorize functions to access other services:
https://help.aliyun.com/document_detail/181589.html
To access resources within a VPC:
https://help.aliyun.com/document_detail/72959.html
Access the RDS database:
https://help.aliyun.com/document_detail/84514.html
Access the Redis cache:
https://help.aliyun.com/document_detail/148798.html
More quickstart documentation:
https://fcnext.console.aliyun.com/overview
After the migration
In the face of traffic peaks, we will no longer be in a hurry, and Function Compute will automatically help us expand! It's a good solution to our API scenarios and scenarios that perform various tasks from time to time. It is a great benefit for a small team like us who do not understand Docker, do not understand Kubernetes, and have no operation and maintenance personnel. It is difficult for us to expand and shrink virtual machines. At the same time, we no longer have to buy virtual machines and load balancers! The method of scaling down to 0 and pay-as-you-go has also greatly reduced our costs! Also, we've never been hacked again and mined with our money! All in all, two words! So fragrant!
Reference link:
[1] Details 1:
https://github.com/devsapp/start-web-framework/tree/master/web-framework/java/springboot
[2] Details 2:
https://fcnext.console.aliyun.com/applications/create
[3] Console:
https://fcnext.console.aliyun.com/
[4] SpringBoot official quick start documentation:
https://spring.io/guides/gs/rest-service/
[5] Cloud DNS DNS console :
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。