共用方式為


探索並註冊 Spring Boot 應用程式

注意

基本標準和企業方案將從 2025 年 3 月中旬開始淘汰,並停用 3 年。 建議您轉換至 Azure Container Apps。 如需詳細資訊,請參閱 Azure Spring Apps 淘汰公告

標準 耗用量和專用 方案將從 2024 年 9 月 30 日起淘汰,並在六個月後完成關閉。 建議您轉換至 Azure Container Apps。 如需詳細資訊,請參閱 將 Azure Spring Apps 標準取用和專用方案遷移至 Azure Container Apps

本文適用於: ✔️標準取用和專用 (預覽) ✔️ 基本/標準 ❌ 企業

本文說明如何使用 Spring Cloud Service Registry 註冊應用程式。

服務註冊和探索是維護即時應用程式實例清單來呼叫、路由和負載平衡輸入要求的重要需求。 手動設定每個用戶端需要時間,因此可能引進人為錯誤。 Azure Spring Apps 提供兩個選項供您解決此問題:

注意

若要在標準取用和專用方案中使用服務登錄,您必須先加以啟用。 如需詳細資訊,請參閱 在 Azure Spring Apps 中啟用和停用 Eureka 伺服器。

  • 使用 Kubernetes Service Discovery 方法來叫用應用程式之間的呼叫。

    Azure Spring Apps 會使用應用程式名稱做為 Kubernetes 服務名稱,為其中執行的每個應用程式建立對應的 Kubernetes 服務。 您可以使用 HTTP/HTTPS 要求中的應用程式名稱,叫用從某個應用程式到另一個應用程式的呼叫,例如 http(s)://{app name}/path。 此方法也適用於企業方案。 如需詳細資訊,請參閱 Kubernetes 登錄程式代碼範例

    注意

    此方法不適用於標準取用和專用 (預覽)。

  • 在 Azure Spring Apps 中使用受控 Spring Cloud Service Registry (OSS)。

    設定之後,服務登錄伺服器會控制應用程式的服務註冊和探索。 Service Registry 伺服器會維護即時應用程式實例的登錄、啟用用戶端負載平衡,並將服務提供者與用戶端分離,而不需要依賴 DNS。

如需如何為 Steeltoe 應用程式設定服務註冊的資訊,請參閱 準備 Java Spring 應用程式以在 Azure Spring Apps 中部署。

使用 Spring Cloud Service Registry 註冊您的應用程式

在您的應用程式可以使用 Spring Cloud Service Registry 管理服務註冊和探索之前,您必須在pom.xml檔案中包含下列相依性spring-cloud-starter-netflix-eureka-client

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

更新最上層類別

最後,將註釋新增至應用程式的最上層類別,如下列範例所示:

package foo.bar;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;

@SpringBootApplication
@EnableEurekaClient
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

Spring Cloud Service Registry 伺服器端點會插入為應用程式中的環境變數。 應用程式可向 Service Registry 伺服器註冊,並探索其他相依的應用程式。

注意

變更可能需要幾分鐘的時間,才能從伺服器傳播到所有應用程式。

下一步

在本文中,您已瞭解如何使用 Spring Cloud Service Registry 註冊應用程式。 若要瞭解如何使用 Microsoft Entra 角色型訪問控制來存取 Spring Cloud Service Registry,請參閱 Access Config Server and Service Registry