使用适用于Azure Functions的新Gradle插件部署Spring Boot无服务器应用程序

Introducing the new Gradle plugins for Azure

For building and deploying Java projects, Azure always had great Maven support, but it was lacking Gradle support. As many people love Gradle, in particular when building complex projects, I was thrilled to learn that a new set of Gradle plugins were being developed! They will all be Open Source, and their code will be available on GitHub at https://github.com/microsoft/azure-gradle-plugins.

The first plugin to be released is the one for Azure Functions, at https://github.com/microsoft/azure-gradle-plugins/tree/master/azure-functions-gradle-plugin. Azure Functions is the Azure serverless offer, and you can use it to run your Java workloads in a managed, event-driven, fully scalable and inexpensive environment.

If you want to run this Gradle plugin for a simple Java serverless application, the full documentation is available on the Microsoft Docs website. This is good for a simple Java application, which you would use for a very simple Function, but what about more complex applications, the ones that typically would benefit more from using Gradle?

Running Spring Boot in a serverless environnement

Thanks to Microsoft's work with Pivotal/VMWare, you can run Spring Boot on Azure Functions using the Spring Cloud Function project, which has specific Azure support.

If you want to test Spring Cloud Function and Azure Functions, I maintain the official sample application at https://github.com/Azure-Samples/hello-spring-function-azure. This is using Maven by default, and in this blog post we are going see how to use it with the new Gradle plugin.

Using the new Gradle plugin to deploy Spring Boot to Azure Functions

I have created a specific gradle branch in the official sample project, which you can check at https://github.com/Azure-Samples/hello-spring-function-azure/tree/gradle.

使用该代码,您将能够使用新的Azure Gradle插件将Spring Cloud Function应用程序部署到Azure Functions。

它使用一种特定的技巧使Azure Functions运行“主” Spring Boot类,这就是为什么我们在build.gradle文件:

jar {
    manifest {
        attributes 'Main-Class' : 'com.example.HelloFunction'
    }
}

上面的代码块将创建一个特定的Java清单文件,该文件指向主Spring Boot类,以便Azure Functions将其执行。

如果您查看此应用程序的Maven版本,我们将复制特定的local.settings.json文件以实现相同的目标(帮助Azure Functions查找主类)。 使用Gradle,此解决方案看起来更优雅。

然后,其余配置将正常使用Gradle插件:

azurefunctions {
    resourceGroup = 'my-spring-function-resource-group'
    appName = 'my-spring-function'
    appSettings {
        WEBSITE_RUN_FROM_PACKAGE = '1'
        FUNCTIONS_EXTENSION_VERSION = '~2'
        FUNCTIONS_WORKER_RUNTIME = 'java'
        MAIN_CLASS = 'com.example.HelloFunction'
    }
    authentication {
        type = 'azure_cli'
    }
    // enable local debug
    // localDebug = "transport=dt_socket,server=y,suspend=n,address=5005"
    deployment {
        type = 'run_from_blob'
    }
}
  • 您将需要配置将在其中运行功能的特定Azure资源组。您将需要提供一个独特的appName到您的功能:此名称在所有Azure中都应该是唯一的,因此通常人们在其名称前加上用户名。我们正在使用最新的FUNCTIONS_EXTENSION_VERSION在撰写本文时可用,因为它在运行Java时具有更好的冷启动性能。

现在已经安装了插件,您可以使用以下Gradle命令:

gradle azureFunctionsRun

这将在本地运行Azure Functions,因此这是开发和调试Function的最佳方法。

gradle azureFunctionsPackage

这将打包您的应用程序,因此可以将其部署到Azure Functions。

gradle azureFunctionsDeploy

这会将您的Function部署到Azure Functions,因此可以在云中使用。

然后,该应用程序的工作原理与使用Maven构建的应用程序完全相同! 因此,您可以通过执行cURL请求来对其进行测试,如下所示:

curl https://<YOUR_SPRING_FUNCTION_NAME>.azurewebsites.net/api/hello -d "{\"name\":\"Azure\"}"

from: https://dev.to//azure/using-the-new-gradle-plugin-for-azure-functions-to-deploy-spring-boot-serverless-applications-4594

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值