本教程介绍如何为 Java 开发准备本地机器,包括开发在Google Cloud上运行的 Java 应用。请按照以下步骤安装 Java 及相关工具。
目标
- 安装 JDK(Java 开发工具包)。
- 安装构建自动化工具。
- 安装 gcloud CLI。
- (可选)安装 IDE 或编辑器。
- (可选)安装 IDE Google Cloud 插件。
- 安装 Java 版 Cloud 客户端库。
- 设置身份验证。
安装 JDK(Java 开发工具包)
您可以选择任意 Java 发行版,但必须确保设置以下环境变量:
- JAVA_HOME:指向 JDK 安装的基础。
- PATH:包含
$JAVA_HOME/bin。
Eclipse Temurin 是建议与Google Cloud搭配使用的 OpenJDK(Java 开发工具包)发行版。Temurin 采用开源许可,通过了 Java SE TCK 认证,并经过测试,可确保生产质量的性能和安全性。
(推荐)安装 Temurin
Temurin 的安装说明因操作系统而异。
- 二进制文件可供下载。
- 对于 Docker 容器,请使用官方“eclipse-temurin”映像。
如果您使用的是 Compute Engine 启动映像,则可以使用以下安装脚本。
CentOS/RHEL/Rocky
- 确定 CentOS/RHEL/Rocky Linux 的主要版本:
eval "$(grep VERSION_ID /etc/os-release)" eval "$(grep ^ID= /etc/os-release)" OLD_IFS=$IFS IFS='.' read -ra split_version <<< "$VERSION_ID" IFS=$OLD_IFS MAJOR_VERSION=$split_version
- 创建 Adoptium 源代码库文件 `/etc/yum.repos.d/adoptium.repo`:
sudo tee /etc/yum.repos.d/adoptium.repo << EOM [Adoptium] name=Adoptium baseurl=https://packages.adoptium.net/artifactory/rpm/$ID/$MAJOR_VERSION/\$basearch enabled=1 gpgcheck=1 gpgkey=https://packages.adoptium.net/artifactory/api/gpg/key/public EOM
- 更新软件包列表:
sudo yum update -y
- 安装 Temurin:
sudo yum install -y temurin-17-jdk
- 验证安装:
java -version
Debian/Ubuntu
- 安装公共代码库 GPG 密钥。如果您使用的是 Ubuntu 16.4,请在保存到文件之前通过
gpg --dearmor传递密钥。(例如:sudo wget ... | gpg --dearmor | sudo tee ...)sudo mkdir -p /etc/apt/keyrings sudo wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/apt/keyrings/adoptium.asc
- 确定 Linux 发行版的名称,并创建源列表文件
/etc/apt/sources.list.d/adoptium.list:eval "$(grep VERSION_CODENAME /etc/os-release)" sudo tee /etc/apt/sources.list.d/adoptium.list << EOM deb [signed-by=/etc/apt/keyrings/adoptium.asc] https://packages.adoptium.net/artifactory/deb $VERSION_CODENAME main EOM
- 更新软件包列表:
sudo apt update -y
- 安装 Temurin:
sudo apt install -y temurin-17-jdk
- 验证安装:
java -version
SLES
- 确定 SLES 的主要版本:
eval "$(grep VERSION_ID /etc/os-release)" OLD_IFS=$IFS IFS='.' read -ra split_version <<< "$VERSION_ID" IFS=$OLD_IFS MAJOR_VERSION=$split_version
- 安装公共代码库 GPG 密钥:
sudo mkdir -p /etc/zypp/keyrings sudo wget -O - https://packages.adoptium.net/artifactory/api/gpg/key/public | sudo tee /etc/zypp/keyrings/adoptium.asc sudo rpm --import /etc/zypp/keyrings/adoptium.asc
- 确定 SLES 的版本,并注册 Adoptium 代码库:
sudo zypper ar -f "https://packages.adoptium.net/artifactory/rpm/sles/$MAJOR_VERSION/$(uname -m)" adoptium
- 更新软件包列表:
sudo zypper update -y
- 安装 Temurin:
sudo zypper install -y temurin-17-jdk
- 验证安装:
java -version
Windows
如需安装 Temurin,请以管理员身份运行 PowerShell 3.0 版或更高版本,并执行以下命令。
- 下载 Temurin。以下说明中的
Invoke-WebRequest命令要求使用 PowerShell 3.0 或更高版本。$JdkVersion = 17 $JdkUrl = "https://api.adoptium.net/v3/binary/latest/$JdkVersion/ga/windows/x64/jdk/hotspot/normal/eclipse?project=jdk" $JdkExtractionPath = "C:\temurin-$JdkVersion-jdk" $JdkDownload = "$JdkExtractionPath.zip" [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]'Tls12' Invoke-WebRequest -Uri $JdkUrl -OutFile $JdkDownload Expand-Archive $JdkDownload -DestinationPath $JdkExtractionPath -Force
- 设置
JAVA_HOME和Path变量:pushd $JdkExtractionPath $JdkPath = (Get-ChildItem).FullName popd [System.Environment]::SetEnvironmentVariable('JAVA_HOME', $JdkPath, 'Machine') - (Optional) Refresh your session's `$env:Path` value. Otherwise, start a
new session:
$MachinePath = [System.Environment]::GetEnvironmentVariable('Path', 'Machine') $UserPath = [System.Environment]::GetEnvironmentVariable('Path', 'User') $env:Path = "$MachinePath;$UserPath" - 验证安装:
java -version
安装构建自动化工具
Apache Maven、Gradle 和 SBT 是软件包管理选项,可帮助快速且一致地跨平台构建 Java 应用依赖项。
安装 gcloud CLI
gcloud CLI 是一套适用于 Google Cloud的工具。它包含 gcloud 和 bq,可用于通过命令行访问 Compute Engine、Cloud Storage、BigQuery 以及其他产品和服务。这些工具既能以交互方式使用,也可以在自动化脚本中运行。
(可选)安装 IDE 或编辑器
用于开发 Java 应用的热门编辑器(排名不分先后)包括但不限于:
- Visual Studio Code
- JetBrains 的 IntelliJ IDEA 和/或 Webstorm
- Eclipse 基金会的 Eclipse
- GitHub 的 Atom
这些编辑器(有时借助于插件)可为您提供从语法突出显示、智能感知、代码补全到完全集成式调试功能的所有能力。
(可选)安装 IDE 插件
如需访问编辑器中的有用功能,请查看以下插件:
安装 Java 版 Cloud 客户端库
使用 Java 版 Cloud 客户端库与 Google Cloud服务(例如 Datastore 和 Cloud Storage)集成。您可以为个别 API 安装软件包(如 BigQuery),如以下示例所示。
If you are using Maven, add
the following to your pom.xml file. For more information about
BOMs, see The Google Cloud Platform Libraries BOM.
If you are using Gradle, add the following to your dependencies:
If you are using sbt, add the following to your dependencies:
If you're using Visual Studio Code or IntelliJ, you can add client libraries to your project using the following IDE plugins:
The plugins provide additional functionality, such as key management for service accounts. Refer to each plugin's documentation for details.
设置身份验证
要运行客户端库,您必须先设置身份验证。
If you're using a local shell, then create local authentication credentials for your user account:
gcloud auth application-default login
You don't need to do this if you're using Cloud Shell.
If an authentication error is returned, and you are using an external identity provider (IdP), confirm that you have signed in to the gcloud CLI with your federated identity.
如需了解详情,请参阅使用客户端库时进行身份验证。
使用客户端库
为客户端库配置端点
如果您使用的 API 支持区域端点,请使用端点来配置要向哪个服务器发送请求。例如,使用 Google.Cloud.Dataproc.V1 API 时,您可以配置客户端端点。如需详细了解 Dataproc 的区域级端点,请点击此处。请务必将以下示例中的 MY-PROJECT 替换为您的项目名称,并将 us-central1 替换为适合您配置的区域:
ClusterControllerSettings settings =
ClusterControllerSettings.newBuilder()
.setEndpoint("us-central1-dataproc.googleapis.com:443")
.build();
try (ClusterControllerClient clusterControllerClient = ClusterControllerClient.create(settings)) {
String projectId = "MY-PROJECT";
String region = "us-central1";
Cluster cluster = Cluster.newBuilder().build();
}后续步骤
(可选)为 App Engine 使用 Maven 或 Gradle 插件
如果您在 App Engine 标准或柔性环境中开发,可以使用 Apache Maven 和 Gradle 构建工具的插件,这些插件为直接开发、测试和部署应用提供了便捷的功能。
针对 App Engine 标准环境
在 App Engine 标准环境中使用 Maven App Engine 插件或 Gradle 插件。
针对 App Engine 柔性环境
在 App Engine 柔性环境中使用 Maven App Engine 插件或 Gradle 插件。
更多资源
- 浏览 Google Cloud 产品的文档。
- 从 GitHub 克隆 Java 示例代码库。