网站首页 > 技术文章 正文
1. 简介
Microsoft Azure现在具有相当可靠的Java支持。在本文中,我们将逐步演示如何使我们的 Spring boot应用程序在 Azure 平台上工作。
2. Maven 依赖和配置
首先,我们需要一个 Azure 订阅才能使用那里的云服务,并注册一个账号。
接下来,登录到平台并使用 Azure CLI 创建服务主体:
> az login
To sign in, use a web browser to open the page \
https://microsoft.com/devicelogin and enter the code XXXXXXXX to authenticate.
> az ad sp create-for-rbac --name "app-name" --password "password"
{
"appId": "aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa",
"displayName": "app-name",
"name": "http://app-name",
"password": "password",
"tenant": "tttttttt-tttt-tttt-tttt-tttttttttttt"
}
现在,我们在 Maven settings.xml中配置 Azure 服务主体身份验证,借助以下部分的<servers>:
<server>
<id>azure-auth</id>
<configuration>
<client>aaaaaaaa-aaaa-aaaa-aaaa-aaaaaaaaaaaa</client>
<tenant>tttttttt-tttt-tttt-tttt-tttttttttttt</tenant>
<key>password</key>
<environment>AZURE</environment>
</configuration>
</server>
在使用 azure-webapp-maven-plugin 将 Spring Boot 应用程序上传到Microsoft平台时,我们将依赖上面的身份验证配置。
让我们将以下 Maven 插件添加到 pom.xml:
<plugin>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-webapp-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<!-- ... -->
</configuration>
</plugin>
3. 将 Spring Boot应用部署到 Azure
现在我们已经设置了环境,让我们尝试将 Spring boot应用程序部署到 Azure。
当我们访问“/hello”时,我们的应用程序回复“hello azure!”:
@GetMapping("/hello")
public String hello() {
return "hello azure!";
}
该平台现在允许为Tomcat和Jetty部署Java Web App。使用 azure-webapp-maven-plugin,我们可以将应用程序作为默认 (ROOT) 应用程序直接部署到支持的 Web 容器,或通过 FTP 进行部署。
请注意,当我们要将应用程序部署到 Web 容器时,我们应该将其打包为 WAR 存档。
3.1. Web容器部署
如果我们打算部署到 Windows 实例上的 Tomcat,我们将对 azure-webapp-maven-plugin 使用以下配置:
<configuration>
<javaVersion>1.8</javaVersion>
<javaWebContainer>tomcat 8.5</javaWebContainer>
<!-- ... -->
</configuration>
对于 Linux 实例,请尝试以下配置:
<configuration>
<linuxRuntime>tomcat 8.5-jre8</linuxRuntime>
<!-- ... -->
</configuration>
我们不要忘记 Azure 身份验证:
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<appName>spring-azure</appName>
<resourceGroup>baeldung</resourceGroup>
<!-- ... -->
</configuration>
将应用程序部署到 Azure 时,我们将看到它显示为应用服务。因此,此处我们指定了属性 <appName> 来命名应用服务。此外,应用服务作为资源需要由资源组容器保存,因此还需要<resourceGroup>。
现在,我们已准备好使用 azure-webapp:deploy Maven 目标拉动触发器,我们将看到输出:
> mvn clean package azure-webapp:deploy
...
[INFO] Start deploying to Web App spring-baeldung...
[INFO] Authenticate with ServerId: azure-auth
[INFO] [Correlation ID: cccccccc-cccc-cccc-cccc-cccccccccccc] \
Instance discovery was successful
[INFO] Target Web App doesn't exist. Creating a new one...
[INFO] Creating App Service Plan 'ServicePlanssssssss-bbbb-0000'...
[INFO] Successfully created App Service Plan.
[INFO] Successfully created Web App.
[INFO] Starting to deploy the war file...
[INFO] Successfully deployed Web App at \
https://spring-baeldung.azurewebsites.net
...
现在,我们可以访问
https://spring-baeldung.azurewebsites.net/hello 并看到响应:“hello azure!"
在部署过程中,Azure 自动为我们创建了应用服务计划。。如果已有应用服务计划,则可以设置属性 <appServicePlanName>以避免创建新的计划:
<configuration>
<!-- ... -->
<appServicePlanName>ServicePlanssssssss-bbbb-0000</appServicePlanName>
</configuration>
3.2. FTP 部署
要通过 FTP 部署,我们可以使用以下配置:
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<appName>spring-baeldung</appName>
<resourceGroup>baeldung</resourceGroup>
<javaVersion>1.8</javaVersion>
<deploymentType>ftp</deploymentType>
<resources>
<resource>
<directory>${project.basedir}/target</directory>
<targetPath>webapps</targetPath>
<includes>
<include>*.war</include>
</includes>
</resource>
</resources>
</configuration>
在上面的配置中,我们让插件在目录 ${project.basedir}/target 中找到 WAR 文件,并将其部署到 Tomcat 容器的 webapps 目录中。
假设我们的最终项目名为 azure-0.1.war,我们将在开始部署后看到如下所示的输出:
> mvn clean package azure-webapp:deploy
...
[INFO] Start deploying to Web App spring-baeldung...
[INFO] Authenticate with ServerId: azure-auth
[INFO] [Correlation ID: cccccccc-cccc-cccc-cccc-cccccccccccc] \
Instance discovery was successful
[INFO] Target Web App doesn't exist. Creating a new one...
[INFO] Creating App Service Plan 'ServicePlanxxxxxxxx-xxxx-xxxx'...
[INFO] Successfully created App Service Plan.
[INFO] Successfully created Web App.
...
[INFO] Finished uploading directory: \
/xxx/.../target/azure-webapps/spring-baeldung --> /site/wwwroot
[INFO] Successfully uploaded files to FTP server: \
xxxx-xxxx-xxx-xxx.ftp.azurewebsites.windows.net
[INFO] Successfully deployed Web App at \
https://spring-baeldung.azurewebsites.net
请注意,这里我们没有将应用程序部署为 Tomcat 的默认 Web 应用程序,因此我们只能通过“
https://spring-baeldung.azurewebsites.net/azure-0.1/hello”访问它。服务器将按预期响应“hello azure!”。
4. 使用自定义应用程序设置进行部署
大多数情况下,我们的 Spring Boot 应用程序需要数据访问才能提供服务。Azure 现在支持 SQL Server、MySQL 和 PostgreSQL 等数据库。
为简单起见,我们将使用其应用内 MySQL 作为数据源,因为它的配置与其他 Azure 数据库服务非常相似。
4.1. 在 Azure 上启用应用内 MySQL
由于没有单行代码来创建启用了应用内 MySQL 的 Web 应用程序,因此我们必须首先使用 CLI 创建 Web 应用程序:
az group create --location japanwest --name bealdung-group
az appservice plan create --name baeldung-plan --resource-group bealdung-group --sku B1
az webapp create --name baeldung-webapp --resource-group baeldung-group \
--plan baeldung-plan --runtime java|1.8|Tomcat|8.5
然后在门户中启用应用中的 MySQL:
启用应用内 MySQL 后,我们可以在文件系统的 /home/data/mysql 目录下的名为 MYSQLCONNSTR_xxx.txt 的文件中找到默认数据库、数据源 URL 和默认帐户信息。
4.2. Spring Boot应用程序使用Azure应用内的MySQL
在这里,为了满足演示需求,我们创建了一个用户实体和两个用于注册和列出用户的端点:
@PostMapping("/user")
public String register(@RequestParam String name) {
userRepository.save(userNamed(name));
return "registered";
}
@GetMapping("/user")
public Iterable<User> userlist() {
return userRepository.findAll();
}
我们将在本地环境中使用 H2 数据库,并将其切换到 Azure 上的 MySQL。通常,我们在 application.properties 文件中配置数据源属性:
spring.datasource.url=jdbc:h2:file:~/test
spring.datasource.username=sa
spring.datasource.password=
对于 Azure 部署,我们需要在 <appSettings> 中配置 azure-webapp-maven-plugin :
<configuration>
<authentication>
<serverId>azure-auth</serverId>
</authentication>
<javaVersion>1.8</javaVersion>
<resourceGroup>baeldung-group</resourceGroup>
<appName>baeldung-webapp</appName>
<appServicePlanName>bealdung-plan</appServicePlanName>
<appSettings>
<property>
<name>spring.datasource.url</name>
<value>jdbc:mysql://127.0.0.1:55738/localdb</value>
</property>
<property>
<name>spring.datasource.username</name>
<value>uuuuuu</value>
</property>
<property>
<name>spring.datasource.password</name>
<value>pppppp</value>
</property>
</appSettings>
</configuration>
现在我们可以开始部署了:
> mvn clean package azure-webapp:deploy
...
[INFO] Start deploying to Web App custom-webapp...
[INFO] Authenticate with ServerId: azure-auth
[INFO] [Correlation ID: cccccccc-cccc-cccc-cccc-cccccccccccc] \
Instance discovery was successful
[INFO] Updating target Web App...
[INFO] Successfully updated Web App.
[INFO] Starting to deploy the war file...
[INFO] Successfully deployed Web App at \
https://baeldung-webapp.azurewebsites.net
我们可以从日志中看到部署已完成。让我们测试一下我们的新端点:
> curl -d "" -X POST https://baeldung-webapp.azurewebsites.net/user\?name\=baeldung
registered
> curl https://baeldung-webapp.azurewebsites.net/user
[{"id":1,"name":"baeldung"}]
5. 将容器化 Spring Boot应用部署到 Azure
在前面的部分中,我们已经展示了如何将应用程序部署到 servlet 容器(在本例中为 Tomcat)。如何部署为独立的可运行 jar?
现在,我们可能需要容器化我们的 Spring Boot 应用程序。具体来说,我们可以将其 docker 化并将镜像上传到 Azure。在这里我们将使用另一个maven插件:docker-maven-plugin,为我们自动化dockerization:
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.1.0</version>
<configuration>
<!-- ... -->
</configuration>
</plugin>
5.1. Azure 容器注册表
首先,我们需要 Azure 上的容器注册表来上传我们的 docker 映像。
因此,让我们创建一个:
az acr create --admin-enabled --resource-group baeldung-group \
--location japanwest --name baeldungadr --sku Basic
我们还需要容器注册表的身份验证信息,可以使用以下方法进行查询:
> az acr credential show --name baeldungadr --query passwords[0]
{
"additionalProperties": {},
"name": "password",
"value": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}
然后在 Maven 的settings.xml设置中添加以下服务器身份验证:
<server>
<id>baeldungadr</id>
<username>baeldungadr</username>
<password>xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx</password>
</server>
5.2. Maven插件配置
让我们将以下 Maven 插件配置添加到 pom.xml:
<properties>
<!-- ... -->
<azure.containerRegistry>baeldungadr</azure.containerRegistry>
<docker.image.prefix>${azure.containerRegistry}.azurecr.io</docker.image.prefix>
</properties>
<build>
<plugins>
<plugin>
<groupId>com.spotify</groupId>
<artifactId>docker-maven-plugin</artifactId>
<version>1.0.0</version>
<configuration>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<registryUrl>https://${docker.image.prefix}</registryUrl>
<serverId>${azure.containerRegistry}</serverId>
<dockerDirectory>docker</dockerDirectory>
<resources>
<resource>
<targetPath>/</targetPath>
<directory>${project.build.directory}</directory>
<include>${project.build.finalName}.jar</include>
</resource>
</resources>
</configuration>
</plugin>
<!-- ... -->
</plugins>
</build>
在上面的配置中,我们指定了 docker 映像名称、注册表 URL 和一些类似于 FTP 部署的属性。
请注意,该插件将使用 <dockerDirectory> 中的值来定位 Dockerfile。我们把 Dockerfile 放在 docker 目录中,它的内容是:
FROM frolvlad/alpine-oraclejdk8:slim
VOLUME /tmp
ADD azure-0.1.jar app.jar
RUN sh -c 'touch /app.jar'
EXPOSE 8080
ENTRYPOINT [ "sh", "-c", "java -Djava.security.egd=file:/dev/./urandom -jar /app.jar" ]
5.3. 在 Docker 实例中运行 Spring Boot应用程序
现在,我们可以生成一个 Docker 映像并将其推送到 Azure 注册表:
> mvn docker:build -DpushImage
...
[INFO] Building image baeldungadr.azurecr.io/azure-0.1
...
Successfully built aaaaaaaaaaaa
Successfully tagged baeldungadr.azurecr.io/azure-0.1:latest
[INFO] Built baeldungadr.azurecr.io/azure-0.1
[INFO] Pushing baeldungadr.azurecr.io/azure-0.1
The push refers to repository [baeldungadr.azurecr.io/azure-0.1]
...
latest: digest: sha256:0f0f... size: 1375
上传完成后,让我们检查一下 baeldungadr 注册表。我们将在存储库列表中看到该图像:
现在我们准备运行映像的实例:
实例启动后,我们可以通过其公有 IP 地址访问应用程序提供的服务:
> curl http://a.x.y.z:8080/hello
hello azure!
5.4. Docker容器部署
假设我们有一个容器注册表,无论它是来自 Azure、Docker Hub 还是我们的专用注册表。
借助以下 azure-webapp-maven-plugin 配置,我们还可以将 Spring Boot Web 应用程序部署到容器中:
<configuration>
<containerSettings>
<imageName>${docker.image.prefix}/${project.artifactId}</imageName>
<registryUrl>https://${docker.image.prefix}</registryUrl>
<serverId>${azure.containerRegistry}</serverId>
</containerSettings>
<!-- ... -->
</configuration>
一旦我们运行 mvn azure-webapp:deploy,该插件将帮助将我们的 Web 应用程序存档部署到指定映像的实例。
然后,我们可以通过实例的 IP 地址或 Azure 应用服务的 URL 访问 Web 服务。
猜你喜欢
- 2025-09-02 19.提取HFM数据进数据库_怎么提取数据库的信息
- 2025-09-02 这样优化Spring Boot,启动速度快到飞起
- 2025-09-02 什么是便携式应用程序,为什么它很重要?
- 2025-09-02 Axence NetTools 5.0.2.35140主机监控、测量网络性能并快速诊断
- 2025-09-02 eNSP和HCL Cloud兼容性的问题,你都会解决吗?
- 2025-09-02 如何成为一名职业黑客?_如何成为一名黑客 eric raymond
- 2025-09-02 万变不离其宗,spring常考知识点总结
- 2025-09-02 log4j2 JNDI注入分析笔记_log4j2 additivity
- 2025-09-02 SQLMAP注入参数-其他参数介绍_sqlmap注入原理
- 2025-09-02 windows几个常用的命令_window 常用命令
你 发表评论:
欢迎- 最近发表
-
- Druid 1.2.4 版本发布,增强对 JDK 8 的支持
- Python设计模式 第 1 章 Python 设计模式概述
- RAD Studio 、Delphi或C++Builder设计代码编译上线缩短开发时间
- Hive如何比较两张表所有字段的一致性
- Java 中 java.util.Date 与 java.sql.Date 有什么区别?
- 主流CDC工具_cd软件是做什么的
- 19.提取HFM数据进数据库_怎么提取数据库的信息
- 将Spring Boot应用部署到 Azure_springboot部署到windows
- 这样优化Spring Boot,启动速度快到飞起
- 什么是便携式应用程序,为什么它很重要?
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (58)
- oracle面试 (55)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)