腾讯云 云函数 最简单的hello Demo
程序员文章站
2022-04-19 16:06:01
...
目录
一、总得来说,有三个步骤:
- 创建一个简单的maven项目
- 使用maven创建jar部署包
- 构建云函数
二、具体
2.1 创建一个简单的maven项目
2.1.1 maven框架
参考:https://blog.csdn.net/ichuany/article/details/84792190
备注:只是参考链接,构建maven的框架就好,里面的代码啥的 不需要参考
2.1.2 代码
参考官方文档:文档中心 > 云函数 > 开发指南 > Java > Java 说明:
https://cloud.tencent.com/document/product/583/12214
Java 开发的 SCF 云函数的代码形态一般如下所示:
package example;
public class Hello {
public Hello() {
}
public String mainHandler(KeyValueClass kv) {
System.out.println("Hello world!");
System.out.println(String.format("key1 = %s", kv.getKey1()));
System.out.println(String.format("key2 = %s", kv.getKey2()));
return String.format("Hello Yangkeke");
}
}
建立参数 KeyValueClass 类:
package example;
public class KeyValueClass {
String key1;
String key2;
public String getKey1() {
return this.key1;
}
public void setKey1(String key1) {
this.key1 = key1;
}
public String getKey2() {
return this.key2;
}
public void setKey2(String key2) {
this.key2 = key2;
}
public KeyValueClass() {
}
}
pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>examples</groupId>
<artifactId>java-example</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>java-example</name>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>2.3</version>
<configuration>
<createDependencyReducedPom>false</createDependencyReducedPom>
</configuration>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
2.2 使用maven创建jar部署包
编译打包
在项目文件夹根目录下执行命令 mvn package
,应有编译输出类似如下:
D:\workcode\Maven\spring-helloworld>mvn package
[INFO] Scanning for projects...
[INFO]
[INFO] -----------------------< examples:java-example >------------------------
[INFO] Building java-example 1.0-SNAPSHOT
[INFO] --------------------------------[ jar ]---------------------------------
[INFO]
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ java-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 0 resource
[INFO]
[INFO] --- maven-compiler-plugin:3.1:compile (default-compile) @ java-example ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 2 source files to D:\workcode\Maven\spring-helloworld\target\classes
[INFO]
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ java-example ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory D:\workcode\Maven\spring-helloworld\src\test\resources
[INFO]
[INFO] --- maven-compiler-plugin:3.1:testCompile (default-testCompile) @ java-example ---
[INFO] No sources to compile
[INFO]
[INFO] --- maven-surefire-plugin:2.12.4:test (default-test) @ java-example ---
[INFO] No tests to run.
[INFO]
[INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ java-example ---
[INFO] Building jar: D:\workcode\Maven\spring-helloworld\target\java-example-1.0-SNAPSHOT.jar
[INFO]
[INFO] --- maven-shade-plugin:2.3:shade (default) @ java-example ---
[INFO] Replacing original artifact with shaded artifact.
[INFO] Replacing D:\workcode\Maven\spring-helloworld\target\java-example-1.0-SNAPSHOT.jar with D:\workcode\Maven\spring-helloworld\target\java-example-1.0-SNAPSHOT-shaded.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 2.687 s
[INFO] Finished at: 2020-09-30T14:00:30+08:00
[INFO] ------------------------------------------------------------------------
2.3 构建云函数
地址:https://console.cloud.tencent.com/scf/list?rid=8&ns=default
选择jar包
点击“测试”