从 Java 应用程序配置 Azure 应用服务部署源
07/28/2017
本文内容
此示例在单个 Azure 应用服务计划中将代码部署到四个应用程序,每个应用程序使用不同的部署源。
运行示例
创建 authentication file,并使用计算机中该文件的完整路径设置环境变量 AZURE_AUTH_LOCATION。 运行:
git clone https://github.com/Azure-Samples/app-service-java-configure-deployment-sources-for-web-apps.git
cd app-service-java-configure-deployment-sources-for-web-apps
mvn clean compile exec:java
使用 Azure 进行身份验证
创建身份验证文件,并在命令行中使用文件的完整路径导出环境变量 AZURE_AUTH_LOCATION。
export AZURE_AUTH_LOCATION=/Users/raisa/azure.auth
身份验证文件用于配置入口点 Azure 对象,该对象是管理库用来定义、创建和配置 Azure 资源的。
// pull in the location of the security file from the environment
final File credFile = new File(System.getenv("AZURE_AUTH_LOCATION"));
Azure azure = Azure
.configure()
.withLogLevel(LogLevel.NONE)
.authenticate(credFile)
.withDefaultSubscription();
详细了解在使用用于 Java 的 Azure 管理库时提供的身份验证选项。
创建运行 Apache Tomcat 的应用服务应用
// create a new Standard app service plan and create a single Java 8/Tomcat 8 app in it
WebApp app1 = azure.webApps().define(app1Name)
.withNewResourceGroup(rgName)
.withNewAppServicePlan(planName)
.withRegion(Region.CHINA_NORTH)
.withPricingTier(AppServicePricingTier.STANDARD_S1)
.withJavaVersion(JavaVersion.JAVA_8_NEWEST)
.withWebContainer(WebContainer.TOMCAT_8_0_NEWEST)
.create();
withJavaVersion() 和 withWebContainer() 将应用服务配置为使用 Tomcat 8 处理 HTTP 请求。
使用 FTP 部署 Java 应用程序
// pass the PublishingProfile that contains FTP information to a helper method
uploadFileToFtp(app1.getPublishingProfile(), "helloworld.war",
ManageWebAppSourceControl.class.getResourceAsStream("/helloworld.war"));
// Use the FTP classes in the Apache Commons library to connect to Azure using
// the information from the PublishingProfile
private static void uploadFileToFtp(PublishingProfile profile, String fileName, InputStream file) throws Exception {
FTPClient ftpClient = new FTPClient();
String[] ftpUrlSegments = profile.ftpUrl().split("/", 2);
String server = ftpUrlSegments[0];
// Tomcat will deploy WAR files uploaded to this directory.
String path = "./site/wwwroot/webapps";
// FTP the build WAR to Azure
ftpClient.connect(server);
ftpClient.login(profile.ftpUsername(), profile.ftpPassword());
ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
ftpClient.changeWorkingDirectory(path);
ftpClient.storeFile(fileName, file);
ftpClient.disconnect();
}
此代码将 WAR 文件上传到 /site/wwwroot/webapps 目录。 Tomcat 在应用服务中部署默认情况下置于此目录中的 WAR 文件。
从本地 Git 存储库部署 Java 应用程序
// get the publishing profile from the App Service webapp
PublishingProfile profile = app2.getPublishingProfile();
// create a new Git repo in the sample directory under src/main/resources
Git git = Git
.init()
.setDirectory(new File(ManageWebAppSourceControl.class.getResource("/azure-samples-appservice-helloworld/").getPath()))
.call();
git.add().addFilepattern(".").call();
// add the files in the sample app to an initial commit
git.commit().setMessage("Initial commit").call();
// push the commit using the Azure Git remote URL and credentials in the publishing profile
PushCommand command = git.push();
command.setRemote(profile.gitUrl());
command.setCredentialsProvider(new UsernamePasswordCredentialsProvider(profile.gitUsername(), profile.gitPassword()));
command.setRefSpecs(new RefSpec("master:master"));
command.setForce(true);
command.call();
此代码使用 JGit 库在 src/main/resources/azure-samples-appservice-helloworld 文件夹中创建新的 Git 存储库。 然后,此示例将文件夹中的所有文件添加到初始提交,并根据 webapp 的 PublishingProfile 中的 Git 部署信息将提交推送到 Azure。
备注
存储库中文件的布局必须与将文件部署到 Azure 应用服务的 /site/wwwroot/ 目录下时你希望的部署方式完全一致。
从公共 Git 存储库部署应用程序
// deploy a .NET sample app from a public GitHub repo into a new webapp
WebApp app3 = azure.webApps().define(app3Name)
.withNewResourceGroup(rgName)
.withExistingAppServicePlan(plan)
.defineSourceControl()
.withPublicGitRepository(
"https://github.com/Azure-Samples/app-service-web-dotnet-get-started")
.withBranch("master")
.attach()
.create();
应用服务运行时自动使用存储库的 master 分支上的最新代码生成和部署 .NET 项目。
从 GitHub 存储库进行持续部署
// deploy the application whenever you push a new commit or merge a pull request into your master branch
WebApp app4 = azure.webApps()
.define(app4Name)
.withExistingResourceGroup(rgName)
.withExistingAppServicePlan(plan)
// Uncomment the following lines to turn on continuous deployment scenario
//.defineSourceControl()
// .withContinuouslyIntegratedGitHubRepository("username", "reponame")
// .withBranch("master")
// .withGitHubAccessToken("YOUR GITHUB PERSONAL TOKEN")
// .attach()
.create();
username 和 reponame 值是在 GitHub 中使用的值。 请创建 GitHub 个人访问令牌(具有存储库读取权限)并将其传递至 withGitHubAccessToken。
示例说明
此示例使用 Java 8 和 Tomcat 8 创建第一个在新创建的标准应用服务计划中运行的应用程序。 然后,代码根据 PublishingProfile 对象中的信息通过 FTP 来传送 WAR 文件,由 Tomcat 进行部署。
第二个应用程序与第一个应用程序使用同一计划,也配置为 Java 8/Tomcat 8 应用程序。 JGit 库在一个文件夹中创建新的 Git 存储库,该文件夹包含解压缩的 Java Web 应用程序,后者采用映射到应用服务的目录结构。 新提交将该文件夹中的文件添加到新的 Git 存储库,再由 Git 使用 webapp 的 PublishingProfile 提供的远程 URL 和用户名/密码将提交推送到 Azure。
第三个应用程序不为 Java 和 Tomcat 而配置。 与之相反,公共 GitHub 存储库中的 .NET 示例是直接从源部署的。
每次推送更改或将拉取请求合并到 GitHub 存储库的主分支中时,第四个应用程序就会将代码部署到主分支中。
示例中使用的类
注释
从 azure.webApps().define()....create() Fluent 链创建。 创建应用服务 Web 应用以及该应用所需的任何资源。 大多数方法查询对象中的配置详细信息,而谓词方法(例如 restart())则更改 webapp 的状态。
一个类,在定义运行 Java webcontainer 的 WebApp 时将静态公共字段用做 withWebContainer() 的参数。 Jetty 和 Tomcat 版本均提供相应选项。
使用 getPublishingProfile() 方法通过 WebApp 对象获得。 包含 FTP 和 Git 部署信息,其中包括部署用户名和密码(独立于 Azure 帐户或服务主体凭据)。
由 azure.appServices().appServicePlans().getByResourceGroup() 返回。 提供相关方法来检查在计划中运行的 Web 应用的容量、层和数目。
AppServicePricingTier
一个类,使用静态公共字段来代表应用服务层。 用于定义内联计划层,可以在创建应用时使用 withPricingTier() 定义,也可以在通过 azure.appServices().appServicePlans().define() 定义计划时直接定义
一个类,使用静态公共字段来代表应用服务支持的 Java 版本。 创建新的 webapp 时在 define()...create() 链期间与 withJavaVersion() 配合使用。
后续步骤
若要详细了解如何在 Azure 中创建和部署 Java 应用,请访问 Java 开发人员中心。