Hello World 服务
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
创建一个简单的 Hello World 应用,将该应用打包到容器映像中,将容器映像上传到 Artifact Registry,然后将容器映像部署到 Cloud Run。
深入探索
如需查看包含此代码示例的详细文档,请参阅以下内容:
代码示例
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["很难理解","hardToUnderstand","thumb-down"],["信息或示例代码不正确","incorrectInformationOrSampleCode","thumb-down"],["没有我需要的信息/示例","missingTheInformationSamplesINeed","thumb-down"],["翻译问题","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],[],[],[],null,["# Hello World service\n\nCreate a simple Hello World application, package it into a container image, upload the container image to Artifact Registry, and then deploy the container image to Cloud Run.\n\nExplore further\n---------------\n\n\nFor detailed documentation that includes this code sample, see the following:\n\n- [Quickstart: Build and deploy a .NET web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-dotnet-service)\n- [Quickstart: Build and deploy a C++ web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-c-plus-plus-service)\n- [Quickstart: Build and deploy a Go web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-go-service)\n- [Quickstart: Build and deploy a Java Spring Boot web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-java-service)\n- [Quickstart: Build and deploy a Node.js web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-nodejs-service)\n- [Quickstart: Build and deploy a PHP web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-php-service)\n- [Quickstart: Build and deploy a Python (Flask) web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-python-service)\n- [Quickstart: Build and deploy a Ruby web app to Cloud Run](/run/docs/quickstarts/build-and-deploy/deploy-ruby-service)\n\nCode sample\n-----------\n\n### C++\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n #include \u003cgoogle/cloud/functions/framework.h\u003e\n #include \u003ccstdlib\u003e\n\n namespace gcf = ::google::cloud::functions;\n\n auto hello_world_http() {\n return gcf::MakeFunction([](gcf::HttpRequest const& /*request*/) {\n std::string greeting = \"Hello \";\n auto const* target = std::getenv(\"TARGET\");\n greeting += target == nullptr ? \"World\" : target;\n greeting += \"\\n\";\n\n return gcf::HttpResponse{}\n .set_header(\"Content-Type\", \"text/plain\")\n .set_payload(greeting);\n });\n }\n\n int main(int argc, char* argv[]) {\n return gcf::Run(argc, argv, hello_world_http());\n }\n\n### C#\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n var builder = WebApplication.CreateBuilder(args);\n\n var port = Environment.GetEnvironmentVariable(\"PORT\") ?? \"8080\";\n var url = $\"http://0.0.0.0:{port}\";\n var target = Environment.GetEnvironmentVariable(\"TARGET\") ?? \"World\";\n\n var app = builder.Build();\n\n app.MapGet(\"/\", () =\u003e $\"Hello {target}!\");\n\n app.Run(url);\n\n### Go\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n // Sample run-helloworld is a minimal Cloud Run service.\n package main\n\n import (\n \t\"fmt\"\n \t\"log\"\n \t\"net/http\"\n \t\"os\"\n )\n\n func main() {\n \tlog.Print(\"starting server...\")\n \thttp.HandleFunc(\"/\", handler)\n\n \t// Determine port for HTTP service.\n \tport := os.Getenv(\"PORT\")\n \tif port == \"\" {\n \t\tport = \"8080\"\n \t\tlog.Printf(\"defaulting to port %s\", port)\n \t}\n\n \t// Start HTTP server.\n \tlog.Printf(\"listening on port %s\", port)\n \tif err := http.ListenAndServe(\":\"+port, nil); err != nil {\n \t\tlog.Fatal(err)\n \t}\n }\n\n func handler(w http.ResponseWriter, r *http.Request) {\n \tname := os.Getenv(\"NAME\")\n \tif name == \"\" {\n \t\tname = \"World\"\n \t}\n \tfmt.Fprintf(w, \"Hello %s!\\n\", name)\n }\n\n### Java\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n\n package com.example.helloworld;\n\n import org.springframework.beans.factory.annotation.Value;\n import org.springframework.boot.SpringApplication;\n import org.springframework.boot.autoconfigure.SpringBootApplication;\n import org.springframework.web.bind.annotation.GetMapping;\n import org.springframework.web.bind.annotation.RestController;\n\n @SpringBootApplication\n public class HelloworldApplication {\n\n @Value(\"${NAME:World}\")\n String name;\n\n @RestController\n class HelloworldController {\n @GetMapping(\"/\")\n String hello() {\n return \"Hello \" + name + \"!\";\n }\n }\n\n public static void main(String[] args) {\n SpringApplication.run(HelloworldApplication.class, args);\n }\n }\n\n### Node.js\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import express from 'express';\n const app = express();\n\n app.get('/', (req, res) =\u003e {\n const name = process.env.NAME || 'World';\n res.send(`Hello ${name}!`);\n });\n\n const port = parseInt(process.env.PORT) || 8080;\n app.listen(port, () =\u003e {\n console.log(`helloworld: listening on port ${port}`);\n });\n\n### PHP\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n \u003c?php\n\n $name = getenv('NAME', true) ?: 'World';\n echo sprintf('Hello %s!', $name);\n\n### Python\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n import os\n\n from flask import Flask\n\n app = Flask(__name__)\n\n\n @app.route(\"/\")\n def hello_world():\n \"\"\"Example Hello World route.\"\"\"\n name = os.environ.get(\"NAME\", \"World\")\n return f\"Hello {name}!\"\n\n\n if __name__ == \"__main__\":\n app.run(debug=True, host=\"0.0.0.0\", port=int(os.environ.get(\"PORT\", 8080)))\n\n### Ruby\n\n\nTo authenticate to Cloud Run, set up Application Default Credentials.\nFor more information, see\n\n[Set up authentication for a local development environment](/docs/authentication/set-up-adc-local-dev-environment).\n\n require \"sinatra\"\n\n set :bind, \"0.0.0.0\"\n port = ENV[\"PORT\"] || \"8080\"\n set :port, port\n\n get \"/\" do\n name = ENV[\"NAME\"] || \"World\"\n \"Hello #{name}!\"\n end\n\nWhat's next\n-----------\n\n\nTo search and filter code samples for other Google Cloud products, see the\n[Google Cloud sample browser](/docs/samples?product=cloudrun)."]]