In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/02 Report--
Preface
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. Function to calculate more information.
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.
Version 2.0 of Fun has made a lot of efforts in deployment, and provides relatively complete features to facilitate and smooth deployment of cloud resources to the cloud. But in this version, the local development experience, there is still a lot of work to be done. So we decided to launch Fun Local to make up for this deficiency.
Fun Local: Fun Local exists as a subcommand of Fun, and can be used directly through the fun local command as long as the version of Fun is 2.6.0 or greater. The Fun Local tool can completely simulate and run the functions in function calculation locally, and provide the function of single-step debugging, which aims to make up for the deficiency of function calculation compared with the traditional application development experience, and provides a new way for users to solve the problem of function calculation.
Except for this article, which introduces users how to use fun local, several other articles in the "correct posture of Development function Computing" series will show users the great efficiency improvement that Fun Local brings to the development of function computing.
Fun Local command format
Use fun local invoke-h to view help information for fun local invoke:
Fun local invoke-h Usage: invoke [options] Run your serverless application locally for quick development & testing. Options:-d,-- debug-port used for local debugging-c,-- config print out ide debug configuration. Options are VSCode-e,-- event event file containing event data passed to the function-h,-- help output usage information local run function
The command format for running the function is:
Fun local invoke [options]
Options and service can be omitted.
From the way of calling, it can be understood that fun local invoke supports calling through function name, or service name / function name, that is,
Fun local invoke functionfun local invoke service/function
For example, if you want to run a function named php72, you can do it directly with the following command:
Fun local invoke php72
The call result is:
For example, to run a function named nodejs8, you can use:
Fun local invoke nodejs8
The results are as follows:
If template.yml contains multiple services and multiple services contain functions with the same name, calling fun by function name will only run the first function with a matching name.
If you want an exact match, you can use the service name / function name.
For example, to call php72 under localdemo, you can use:
Fun local invoke localdemo/php72
In this case, you will get the same result as fun local invoke php72.
The following is a demonstration of running the nodejs8 function:
Run functions of type java locally
Unlike interpretive languages, java needs to be compiled before it can be run as a function. In our example, you can go to the java8 directory in demo and execute:
Mvn package
You can see log:
[INFO] skip non existing resourceDirectory / Users/tan/code/fun/examples/local/java8/src/test/resources [INFO] [INFO]-maven-compiler-plugin:3.1:testCompile (default-testCompile) @ demo-[INFO] No sources to compile [INFO] [INFO]-maven-surefire-plugin:2.12.4:test (default-test) @ demo-[INFO] No tests to run. [INFO] [INFO]-maven- Dependency-plugin:2.8:copy-dependencies (copy-dependencies) @ demo-[INFO] fc-java-core-1.0.0.jar already exists in destination. [INFO] [INFO]-maven-jar-plugin:2.4:jar (default-jar) @ demo-[INFO]-- -[INFO] BUILD SUCCESS [INFO]-[INFO] Total time: 1. 223 s [INFO] Finished at: 2018-11-22T10:45:14+08:00 [INFO] Final Memory: 15M/309M [INFO]-
This command generates the demo-1.0-SNAPSHOT.jar file in the java8/target directory.
Since the CodeUri we configured in template.yml is java8/target/demo-1.0-SNAPSHOT.jar, no changes are needed and can be run directly through the following command:
Fun local invoke java8
The running results are as follows:
The following is a demonstration of running the java8 function:
Local debugging
Fun local invoke supports the-d,-- debug-port option, which allows you to step into the function locally. This document only describes how to configure debugging and does not involve debugging skills. For more articles, please refer to.
Note: all the debugging technologies involved in Fun Local are based on the common debugging protocols of various languages, so developers of any language can debug using the remote debugging method of the corresponding language, even if they do not like to use VSCode.
Debug functions of nodejs and python types locally
The debugging methods are basically the same for functions of nodejs6, nodejs8, python2.7, python3, and java8 types. Let's take nodejs8 as an example.
We demonstrated above that you can run a function named nodejs8 through fun local invoke nodejs8. If you want to debug this function, you just need to use the-d argument and configure the appropriate port number.
For example, if we run the function in debug mode and set the debug port to 3000, you can use the following command:
Fun local invoke-d 3000 nodejs8
In addition, it is recommended to add the parameter-- config. When debugging, you can output the configuration information of the IDE used for debugging:
Fun local invoke-d 3000-- config VSCode nodejs8
The result of the command execution is as follows:
Skip pulling images... you can paste these config to .vscode / launch.json, and then attach to your running function/ config begin / {"version": "0.2.0", "configurations": [{"name": "fc/localdemo/nodejs8", "type": "node" "request": "attach", "address": "localhost", "port": 3000, "localRoot": "/ Users/tan/code/fun/examples/local/nodejs8", "remoteRoot": "/ code", "protocol": "inspect" "stopOnEntry": false}]} / config end / Debugger listening on ws://0.0.0.0:3000/b65c288b-bd6a-4791-849b-b03e0d16b0ceFor help see https://nodejs.org/en/docs/inspector
The program will be blocked here and will not continue to execute. The program will not continue to execute until the connection to the IDE is up. Next, we will explain VSCode configuration and VSCode debugging respectively.
The VSCode configuration is needed only when the function is debugged for the first time, and if it has been configured, it does not need to be configured again.
VSCode configuration
Create a vscode launch.json file
Copy the configuration between config begin and config end in the log to launch.json.
After completing the above configuration, you can see the list of configured functions in the Debug view.
At this point, the VSCode configuration is complete. For more information on VSCode configuration, please refer to the official documentation.
VSCode debugging
After the VSCode configuration is successful, just click the set breakpoint in the VSCode editor sidebar, and then click the "start debugging" button to start debugging.
The following is an example of a local single-step debugging process for nodejs8 functions:
Debug functions of type java locally
The process of debugging java functions is similar to nodejs and python. But because java programmers usually like to use IDE like IDEA and Eclipse, let's talk about it separately.
Use VSCode to debug java
When using VSCode to debug java, you need to install two plug-ins: Language Support for Java (TM) by Red Hat and Debugger for Java. Using VSCode's plug-in market to install plug-ins is easy, you can refer to.
Here is an example of using VSCode to debug a function of type java:
Use IDEA to debug javaIDEA configuration
It is relatively simple for IDEA to configure remote debugging. First, click Run-> Edit Configurations... in the menu bar. :
Then create a new Remote Debugging:
Then we output a random name and configure the port number to 3000.
The following is a demonstration of the complete process of configuring IDEA remote debugging:
Start debugging using IDEA
First, run the java function as debug:
Fun local invoke-d 3000 java8
You can see that the function is stuck here, and then we use IDEA to connect and start debugging. You can use Run-> Debug... on the menu bar Or the toolbar directly clicks the Debug button to start debugging.
The following is a complete demonstration of the process of remote debugging with IDEA:
Debug functions of type php locally
Php debugging is somewhat different from other types of function debugging in terms of process.
First, php runs through the fun local invoke php72 command, which is consistent with other types of functions. When debugging, like other types of functions, start the function in debug mode with the-d argument:
Fun local invoke-d 3000-- config VSCode php72
But the difference is that after running the php function as debug, the php function does not block the connection waiting for the vscode debugger, but simply ends the run.
Skip pulling images... you can paste these config to .vscode / launch.json, and then attach to your running function/ config begin / {"version": "0.2.0", "configurations": [{"name": "fc/localdemo/php72", "type": "php" "request": "launch", "port": 3000, "stopOnEntry": false, "pathMappings": {"/ code": "/ Users/tan/code/fun/examples/local/php7.2"} "ignore": ["/ var/fc/runtime/**"]}} / config end / FunctionCompute php7.2 runtime inited.FC Invoke Start RequestId: 6e8f7ed7-653d-4a6a-94cc-1ef0d028e4b4FC Invoke End RequestId: 6e8f7ed7-653d-4a6a-94cc-1ef0d028e4b4hello worldRequestId: 6e8f7ed7 -653d-4a6a-94cc-1ef0d028e4b4 Billed Duration: 48 ms Memory Size: 1998 MB Max Memory Used: 58 MB
This is because, for php programs, you need to start the debugger for vscode first.
The process for functions of type php to start the VSCode debugger is the same as for other types of functions: copy the vscode configuration from the log above to launch.json and click "start debugging".
Then restart the php function in debug mode on the terminal to start debugging:
Fun local invoke-d 3000 php72
Event event source
Function computing provides a wealth of triggers, including but not limited to object storage triggers, log service triggers, CDN event triggers and so on. In order to fully simulate the online environment, you usually need to construct trigger events in order to fully simulate the online environment, whether running or debugging functions locally.
The trigger event can be a readable json configuration or a piece of non-readable binary data. Here we take json as an example, assuming that the content of the trigger event is:
{"testKey": "testValue"}
You can pass the contents of this event to the function in three ways:
Pipe: echo'{"testKey": "testValue"}'| fun local invoke nodejs8
File: write the json contents to a file with an arbitrary file name, such as event.json. Then specify the file name with-e: fun local invoke-e event.json nodejs8
Redirect: fun local invoke nodejs8 < event.json or fun local invoke nodejs8
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.