访问 Config Server 和服务注册表

注意

Azure Spring Apps 是 Azure Spring Cloud 服务的新名称。 虽然该服务有新名称,但一些地方仍会使用旧名称,我们仍在更新屏幕截图、视频和图形等资产。

本文介绍了如何使用 Microsoft Entra 基于角色的访问控制 (RBAC) 来访问由 Azure Spring Apps 托管的 Spring Cloud Config Server 和 Spring Cloud 服务注册表。

注意

在 Azure Spring Apps 服务中部署和运行的应用程序在访问托管的 Spring Cloud Config Server 和服务注册表时会自动连接到基于证书的身份验证和授权。 对于这些应用程序,不需要遵循此指南。 相关证书完全由 Azure Spring Apps 平台管理,并在连接到 Config Server 和服务注册表时自动注入到你的应用程序中。

将角色分配给 Microsoft Entra 用户/组、MSI 或服务主体

在 [管理组 | 订阅 | 资源组 | 资源] 范围中向 [用户 | 组 | 服务主体 | 托管标识] 分配角色。

角色名称 说明
Azure Spring Apps Config Server 读者 允许对 Azure Spring Apps Config Server 进行读取访问。
Azure Spring Apps Config Server 参与者 允许对 Azure Spring Apps Config Server 进行读取、写入和删除访问。
Azure Spring Apps 服务注册表读者 允许对 Azure Spring Apps 服务注册表进行读取访问。
Azure Spring Apps 服务注册表参与者 允许对 Azure Spring Apps 服务注册表进行读取、写入和删除访问。

有关详细步骤,请参阅使用 Azure 门户分配 Azure 角色

访问 Config Server 和服务注册表终结点

分配角色后,被分派人可通过以下过程访问 Spring Cloud Config Server 和 Spring Cloud 服务注册表终结点:

  1. 获取访问令牌。 为 Microsoft Entra 用户分配角色后,他们可运行以下命令,使用用户身份、服务主体或托管标识登录到 Azure CLI 以获取访问令牌。 有关详细信息,请参阅 Azure CLI 身份验证

    az login
    az account get-access-token
    
  2. 编写终结点。 我们支持 Azure Spring Apps 托管 Spring Cloud Config Server 和 Spring Cloud 服务注册表的默认终结点。

    • 'https://SERVICE_NAME.svc.microservices.azure.cn/eureka/{path}'
    • 'https://SERVICE_NAME.svc.microservices.azure.cn/config/{path}'

    注意

    如果使用由世纪互联运营的 Microsoft Azure,请使用 *.microservices.azure.cn。 有关详细信息,请参阅由世纪互联运营的 Microsoft Azure 开发人员指南中的检查 Azure 中的终结点部分。

  3. 使用访问令牌访问编写的终结点。 将访问令牌放在标头中以提供授权:--header 'Authorization: Bearer {TOKEN_FROM_PREVIOUS_STEP}'

    例如:

    a. 访问诸如 https://SERVICE_NAME.svc.microservices.azure.cn/config/actuator/health 的终结点可查看 Config Server 的运行状况。

    b. 访问诸如 https://SERVICE_NAME.svc.microservices.azure.cn/eureka/eureka/apps 的终结点可以查看 Spring Cloud 服务注册表(此处为 Eureka)中的已注册应用。

    如果响应为 401 Unauthorized,则检查是否已成功分配角色。 角色生效或验证访问令牌是否未过期需要几分钟时间。

有关执行器终结点的详细信息,请参阅生产就绪的终结点

有关 Eureka 终结点,请参阅 Eureka-REST-operations

有关 Config Server 终结点和详细路径信息,请参阅 ResourceController.javaEncryptionController.java

将 Spring Boot 应用注册到 Azure Spring Apps 托管 Spring Cloud Config Server 和服务注册表

分配角色后,可使用 Microsoft Entra 令牌身份验证将 Spring Boot 应用注册到由 Azure Spring Apps 托管的 Spring Cloud Config Server 和服务注册表。 Config Server 和服务注册表都支持自定义 REST 模板来注入持有者令牌进行身份验证。

有关更多信息,请参阅示例访问 Azure Spring Apps 托管 Config Server 以及访问 Azure Spring Apps 托管服务注册表。 以下部分介绍了这些示例的一些重要详细信息。

在 AccessTokenManager.java 中:

AccessTokenManager 负责从 Microsoft Entra ID 获取访问令牌。 在 application.properties 文件中配置服务主体的登录信息并初始化 ApplicationTokenCredentials 以获取令牌。 可以在这两个示例中查找此文件。

prop.load(in);
tokenClientId = prop.getProperty("access.token.clientId");
String tenantId = prop.getProperty("access.token.tenantId");
String secret = prop.getProperty("access.token.secret");
String clientId = prop.getProperty("access.token.clientId");
credentials = new ApplicationTokenCredentials(
    clientId, tenantId, secret, AzureEnvironment.AZURE);

在 CustomConfigServiceBootstrapConfiguration.java 中:

CustomConfigServiceBootstrapConfiguration 可实现 Config Server 的自定义 REST 模板,并将来自 Microsoft Entra ID 的令牌作为 Authorization 标头注入。 可在 Config Server 示例中找到此文件。

public class RequestResponseHandlerInterceptor implements ClientHttpRequestInterceptor {

    @Override
    public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException {
        String accessToken = AccessTokenManager.getToken();
        request.getHeaders().remove(AUTHORIZATION);
        request.getHeaders().add(AUTHORIZATION, "Bearer " + accessToken);

        ClientHttpResponse response = execution.execute(request, body);
        return response;
    }

}

在 CustomRestTemplateTransportClientFactories.java 中:

前两个类用于实现 Spring Cloud 服务注册表的自定义 REST 模板。 intercept 部分与上面的 Config Server 中相同。 确保将 factory.mappingJacksonHttpMessageConverter() 添加到消息转换器。 可以在 Spring Cloud 服务注册表示例中找到此文件。

private RestTemplate customRestTemplate() {
    /*
     * Inject your custom rest template
     */
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.getInterceptors()
        .add(new RequestResponseHandlerInterceptor());
    RestTemplateTransportClientFactory factory = new RestTemplateTransportClientFactory();

    restTemplate.getMessageConverters().add(0, factory.mappingJacksonHttpMessageConverter());

    return restTemplate;
}

如果在 Kubernetes 群集上运行应用程序,我们建议使用 IP 地址注册 Spring Cloud 服务注册表以进行访问。

eureka.instance.prefer-ip-address=true

后续步骤