将 Azure Cache for Redis 绑定到 Azure Spring Cloud 应用程序Bind Azure Cache for Redis to your Azure Spring Cloud application

可以通过 Azure Spring Cloud 将所选 Azure 服务自动绑定到应用程序,而不必手动配置 Spring Boot 应用程序。Instead of manually configuring your Spring Boot applications, you can automatically bind select Azure services to your applications by using Azure Spring Cloud. 本文介绍如何将应用程序绑定到 Azure Redis 缓存。This article shows how to bind your application to Azure Cache for Redis.

先决条件Prerequisites

  • 部署的 Azure Spring Cloud 实例A deployed Azure Spring Cloud instance
  • Azure Redis 缓存服务实例An Azure Cache for Redis service instance
  • 用于 Azure CLI 的 Azure Spring Cloud 扩展The Azure Spring Cloud extension for the Azure CLI

如果没有已部署的 Azure Spring Cloud 实例,请按照有关如何部署 Azure Spring Cloud 应用的快速入门中的步骤操作。If you don't have a deployed Azure Spring Cloud instance, follow the steps in the quickstart on deploying an Azure Spring Cloud app.

绑定 Azure Redis 缓存Bind Azure Cache for Redis

  1. 在项目的 pom.xml 文件中,添加以下依赖项:Add the following dependency to your project's pom.xml file:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>
    
  2. application.properties 文件中删除任何 spring.redis.* 属性Remove any spring.redis.* properties from the application.properties file

  3. 使用 az spring-cloud app update 更新当前部署,或者使用 az spring-cloud app deployment create 创建新的部署。Update the current deployment using az spring-cloud app update or create a new deployment using az spring-cloud app deployment create.

  4. 转到 Azure 门户中的 Azure Spring Cloud 服务页面。Go to your Azure Spring Cloud service page in the Azure portal. 转到“应用程序仪表板”,选择要绑定到 Azure Cache for Redis 的应用程序。Go to Application Dashboard and select the application to bind to Azure Cache for Redis. 此应用程序是在上一步更新或部署的应用程序。This application is the same one you updated or deployed in the previous step.

  5. 选择“服务绑定”,然后选择“创建服务绑定” 。Select Service binding and select Create service binding. 填充窗体,确保选择“绑定类型”值“Azure Cache for Redis”、你的 Azure Cache for Redis 服务器,以及“主密钥”选项。 Fill out the form, being sure to select the Binding type value Azure Cache for Redis , your Azure Cache for Redis server, and the Primary key option.

  6. 重新启动应用。Restart the app. 绑定现在应该生效。The binding should now work.

  7. 若要确保服务绑定正确,请选择绑定名称并验证其详细信息。To ensure the service binding is correct, select the binding name and verify its details. property 字段应如下所示:The property field should look like this:

    spring.redis.host=some-redis.redis.cache.chinacloudapi.cn
    spring.redis.port=6380
    spring.redis.password=abc******
    spring.redis.ssl=true
    

后续步骤Next steps

本问介绍了如何将 Azure Spring Cloud 应用程序绑定到 Azure Cache for Redis。In this article, you learned how to bind your Azure Spring Cloud application to Azure Cache for Redis. 若要详细了解如何将服务绑定到应用程序,请参阅绑定到 Azure Database for MySQL 实例To learn more about binding services to your application, see Bind to an Azure Database for MySQL instance.