# Azure 为 Spring 库总结
我之前介绍了一些适用于Azure的Spring库,现在是总结的时候了。我认为以下所有需要的内容都已经写在下面了。
Azure Spring Boot 是一个在 Azure 云平台上部署和管理 Spring Boot 应用程序的解决方案。
就像其名所示,这是一组专为Spring Boot设计的库。基本上,它们与Spring Boot 2.2/2.3/2.4兼容。
以下是目前支持的服务列表:
- 
- Active Directory / Active Directory B2C
 
- 
- Cosmos DB
 
- 
- Key Vault (Secret/Certificate)
 
- 
- サービスバス
 
- ストレージ(BLOB)
另外,最新版本的设计如下所示。
- 
- バージョンが、3.0 以降
 
- 
- 名前空間が com.microsoft.azure から com.azure.spring
 
Artifact Id が azure-spring-boot-starter-* で統一
有时SDK之间会发生依赖关系的问题,所以我们应该在dependencyManagement中指定BOM。如果我们使用Spring Initializr创建项目,BOM将会被嵌入其中。据说BOM是一种特殊的POM,用于指定依赖关系的版本。
Maven仓库:com.azure.spring » azure-spring-boot-bom
  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-boot-bom</artifactId>
        <version>${azure.version}</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>
由于一些已经被废弃的库,可能有一些不适用于这个情况,所以在开始使用之前需要注意。
由于微軟的文件更新跟不上,所以經常會介紹舊版的資訊,所以我認為最好確認英文原文或查看 Github,以獲取最新的資訊。
Azure 春云
Spring Cloud是为构建基于微服务的应用程序而设计的轻量级框架。它不仅适用于Spring Boot应用,还可以无缝集成到Spring应用中。
虽然不是太强调,但之前我们解释过,Key Vault 是用于引导的,而 App Configuration 是用于云的库。不太清楚在哪个方面,有什么明确的判断准则,但是 Key Vault 也可以在微服务中使用。
以下是支持的主要服务。
- 
- キャッシュ(Azure Redis)
 
- 
- イベントハブ
 
- 
- サービスバス トピック&キュー
 
- ストレージキュー
另外,最新版本的制作如下所示。
- 
- バージョンは、2.0 以降
 
- 
- 名前空間が com.microsoft.azure から com.azure.spring
 
Artifact Id が azure-spring-cloud-* または、 azure-spring-integration-* で統一
我认为由于已经准备好了各个样品,所以可以尝试一下。
让我们试试队列协调
我想试试 azure-spring-cloud-starter-storage-queue。因为Azure Functions有一个队列触发器,所以我只是出于兴趣想知道这个库的使用感受。
我希望通过StorageQueueOperation来处理队列操作,而不是对生存的Kue客户端进行DI。
由于Post感到麻烦,我正在使用Get进行入队操作,然后使用sendAsync进行入队操作,使用receiveAsync进行出队操作。
我在检查点类中判断了成功与否,但我不太明白这意味着什么。
    @Autowired
    StorageQueueOperation storageQueueOperation;
    @GetMapping("/push")
    public String send(@QueryParam("message") String message) {
        this.storageQueueOperation
            .sendAsync(STORAGE_QUEUE_NAME, MessageBuilder.withPayload(message).build())
            .subscribe();
        LOGGER.info("Message {} has been sent successfully.", message);
        return message;
    }
    @GetMapping("/pull")
    public String receive() {
        this.storageQueueOperation.setMessagePayloadType(String.class);
        this.storageQueueOperation.setCheckpointMode(CheckpointMode.MANUAL);
        Message<?> message = this.storageQueueOperation.receiveAsync(STORAGE_QUEUE_NAME).block();
        if (message == null) {
            LOGGER.info("You have no new messages.");
            return null;
        }
        LOGGER.info("Message arrived! Payload: " + message.getPayload());
        Checkpointer checkpointer = message.getHeaders().get(AzureHeaders.CHECKPOINTER, Checkpointer.class);
        checkpointer.success()
                    .doOnSuccess(Void -> LOGGER.info("Message '{}' successfully checkpointed", message.getPayload()))
                    .doOnError(e -> LOGGER.error("Fail to checkpoint the message", e))
                    .subscribe();
        return (String) message.getPayload();
    }
概括
由于每天都在变化,所以几个月前的情况很容易变得过时。但是,如果查看一手消息(Github),源代码都是公开的,也会有示例。而且,如果有任何问题,可以提出Issue。我最近觉得这个世界真方便呢。如果遇到困难,就去看源代码吧。
 
    