Java 快速开始
本文档介绍从零开始,如何将一个 Java 应用容器化,并部署到 CloudBase 云托管。
代码示例:
https://github.com/TencentCloudBase/cloudbase-examples/tree/master/cloudbaserun/java
或者一键部署到云托管:
第 1 步:编写基础应用(创建 Spring Boot 项目)
请确保已安装 curl 和 unzip。在终端执行以下命令,快速生成一个 Spring Boot Web 项目:
curl https://start.spring.io/starter.zip \
-d type=maven-project \
-d dependencies=web \
-d javaVersion=1.8 \
-d bootVersion=2.3.3.RELEASE \
-d name=helloworld \
-d artifactId=helloworld \
-d baseDir=helloworld \
-o helloworld.zip
unzip helloworld.zip
cd helloworld
上述命令将创建一个 Spring Boot 项目。
提示
将 src/main/java/com/example/helloworld/HelloworldApplication.java 内容更新如下:
package com.example.helloworld;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
public class HelloworldApplication {
@RestController
class HelloworldController {
@GetMapping("/")
String hello() {
return "Hello World!";
}
}
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
在 src/main/resources/application.properties 中,将服务器端口设置成 80:
server.port=80
以上代码会创建一个基本的 Web 服务器,并监听 80 端口。