Share via


使用 Tanzu 服務登錄

注意

Azure Spring Apps 是 Azure Spring Cloud 服務的新名稱。 雖然服務有新的名稱,但是您暫時還是會在某些位置看到舊的名稱。我們正在致力更新螢幕擷取畫面、影片和圖表等資產。

本文適用於:❌ 基本/標準✔️企業

本文說明如何使用 VMware Tanzu Service Registry 搭配 Azure Spring Apps 企業版方案。

Tanzu 服務登錄是其中一個商業 VMware Tanzu 元件。 此元件可協助您將 服務探索 設計模式套用至您的應用程式。

服務探索是微服務架構的主要概念之一。 如果沒有服務探索,您必須手動設定服務的每個用戶端,或採用某種形式的存取慣例。 此程式可能很困難,而且設定和慣例在生產環境中可能會很脆弱。 相反地,您可以使用 Tanzu 服務登錄,在應用程式中動態探索和叫用已註冊的服務。

使用 Azure Spring Apps 企業版方案,您不需要自行建立或啟動 Service Registry。 您可以在建立 Azure Spring Apps Enterprise 方案實例時選取它,以使用 Tanzu 服務登錄。

必要條件

  • 已啟用 Tanzu Service Registry 的已布建 Azure Spring Apps 企業方案實例。 如需詳細資訊,請參閱 快速入門:使用企業方案建置和部署應用程式至 Azure Spring Apps。
  • Azure Spring Apps 企業版方案延伸模組。 使用下列命令來移除舊版並安裝最新的 Enterprise 方案擴充功能。 如果您先前已安裝擴充 spring-cloud 功能,請將其卸載,以避免設定和版本不符。
    az extension add --upgrade --name spring
    az extension remove --name spring-cloud
    

建立使用 Service Registry 的應用程式

在本文中,您會建立兩個服務,並將其註冊至 Azure Spring Apps Service Registry。 註冊之後,一個服務將能夠使用服務登錄來探索並叫用另一個服務。 下圖摘要說明所需的步驟:

此圖顯示建立、部署及註冊服務 A 和服務 B 的步驟。

下列各節將詳細說明這些步驟。

  1. 建立服務 A。
  2. 將服務 A 部署至 Azure Spring Apps,並將其註冊至 Service Registry。
  3. 建立服務 B 並加以實作以呼叫服務 A。
  4. 部署服務 B,並將其註冊到 Service Registry。
  5. 透過服務 B 叫用服務 A。

建立環境變數

本文使用下列環境變數。 將這些變數設定為您在建立 Azure Spring Apps 企業版方案實例時所使用的值。

變數 描述
$RESOURCE_GROUP 資源組名。
$AZURE_SPRING_APPS_NAME Azure Spring Apps 實例名稱。

使用 Spring Boot 建立服務 A

流覽至 Spring Initializr 以建立範例服務 A。此連結會使用下列 URL 來初始化設定。

https://start.spring.io/#!type=maven-project&language=java&packaging=jar&groupId=com.example&artifactId=Sample%20Service%20A&name=Sample%20Service%20A&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.Sample%20Service%20A&dependencies=web,cloud-eureka

下列螢幕快照顯示具有必要設定的 Spring Initializr。

顯示必要設定之 Spring Initializr 頁面的螢幕快照。

接下來,選取 [產生 ] 以取得具有下列目錄結構的 Spring Boot 範例專案。

├── HELP.md
├── mvnw
├── mvnw.cmd
├── pom.xml
└── src
    ├── main
    │   ├── java
    │   │   └── com
    │   │       └── example
    │   │           └── Sample
    │   │               └── Service
    │   │                   └── A
    │   │                       └── SampleServiceAApplication.java
    │   └── resources
    │       ├── application.properties
    │       ├── static
    │       └── templates
    └── test
        └── java
            └── com
                └── example
                    └── Sample
                        └── Service
                            └── A
                                └── SampleServiceAApplicationTests.java

確認 Service Registry 用戶端的相依連結庫設定 (Eureka 用戶端)

接下來,確認 專案的pom.xml 檔案包含下列相依性。 如果遺漏相依性,請新增相依性。

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

實作 Service Registry 用戶端

@EnableEurekaClient將批注新增至SampleServiceAApplication.java檔案,將其設定為 Eureka 用戶端。

package com.example.Sample.Service.A;

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

@SpringBootApplication
@EnableEurekaClient
public class SampleServiceAApplication {

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

建立 REST 端點以進行測試

您現在可以將服務註冊至服務登錄,但在您實作服務端點之前,無法驗證它。 若要建立外部服務可以呼叫的 RESTful 端點,請使用下列程式代碼,將ServiceAEndpoint.java檔案新增至您的專案。

package com.example.Sample.Service.A;
import java.util.Map;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ServiceAEndpoint {

    @GetMapping("/serviceA")
    public String getServiceA(){
        return "This is a result of Service A";
    }

    @GetMapping("/env")
    public Map<String, String> getEnv(){
        Map<String, String> env = System.getenv();
        return env;
    }
}

建置 Spring Boot 應用程式

既然您有簡單的服務,請執行下列命令來編譯和建置原始碼:

mvn clean package

部署服務 A 並向服務登錄註冊

本節說明如何將服務 A 部署至 Azure Spring Apps Enterprise 方案實例,並將其註冊至 Service Registry。

建立 Azure Spring Apps 應用程式

首先,使用下列命令在 Azure Spring Apps 中建立應用程式:

az spring app create \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME  \
    --name serviceA \
    --instance-count 1 \
    --memory 2Gi \
    --assign-endpoint

--assign-endpoint 變數會授與公用IP以進行驗證,並啟用來自外部網路的存取。

從應用程式 連線 至 Service Registry

使用 Spring Boot 建立服務實例並在 Azure Spring Apps 中建立應用程式之後,您會部署應用程式並確認作業。 不過,在此之前,您必須將應用程式系結至服務登錄,以便從登錄取得連線資訊。

一般而言,Eureka 用戶端必須在 Spring Boot 應用程式的 application.properties 組態檔中撰寫下列連線資訊設定,以便您可以連線到伺服器:

eureka.client.service-url.defaultZone=http://eureka:8761/eureka/

不過,如果您直接在應用程式中撰寫這些設定,則每次 Service Registry 伺服器變更時,都必須重新編輯並重建專案。 為避免這項工作,Azure Spring Apps 可讓應用程式透過系結至服務登錄,從服務登錄取得連線資訊。 具體而言,將應用程式系結至服務登錄之後,您可以從 Java 環境變數取得服務登錄連線資訊 (eureka.client.service-url.defaultZone)。 如此一來,您可以在應用程式啟動時載入環境變數的內容,以連線到服務登錄。

實際上,下列環境變數會新增至 JAVA_TOOL_OPTIONS 變數:

-Deureka.client.service-url.defaultZone=https://$AZURE_SPRING_APPS_NAME.svc.azuremicroservices.io/eureka/default/eureka

將服務系結至服務登錄

使用下列命令將服務系結至 Azure 服務登錄,讓它能夠連線到伺服器。

az spring service-registry bind \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --app serviceA

您也可以從 Azure 入口網站 設定應用程式系結,如下列螢幕快照所示:

顯示 [服務登錄] 頁面的 Azure 入口網站 螢幕快照,其中已醒目提示 [應用程式系結] 下拉式清單。

注意

當服務登錄狀態變更時,這些變更需要幾分鐘的時間傳播到所有應用程式。

如果您變更系結/解除系結狀態,則必須重新啟動或重新部署應用程式。

您現在可以使用下列命令,選擇在建立新的應用程式時,直接將應用程式系結至 Service Registry:

az spring app create \ 
    --resource-group <resource-group> \ 
    --service <service-name> \ 
    --name <app-name> \ 
    --bind-service-registry

您也可以從 Azure 入口網站 將應用程式系結至服務登錄,如下列螢幕快照所示:

顯示 [建立應用程式] 頁面的螢幕快照,其中已醒目提示 [系結] 下拉式清單。 Azure 入口網站

將應用程式部署至 Azure Spring Apps

現在您已系結應用程式,請將 Spring Boot 成品檔案 Sample-Service-A-A-0.0.1-SNAPSHOT.jar 部署至 Azure Spring Apps。 若要部署,請使用下列命令:

az spring app deploy \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --name serviceA \
    --artifact-path ./target/Sample-Service-A-0.0.1-SNAPSHOT.jar \
    --jvm-options="-Xms1024m -Xmx1024m"

使用下列命令來查看您的部署是否成功。

az spring app list \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --output table

此命令會產生類似下列範例的輸出。

Name                      Location       ResourceGroup           Public Url                                                           Production Deployment    Provisioning State    CPU    Memory    Running Instance    Registered Instance    Persistent Storage    Bind Service Registry    Bind Application Configuration Service
------------------------  -------------  ----------------------  -------------------------------------------------------------------  -----------------------  --------------------  -----  --------  ------------------  ---------------------  --------------------  -----------------------  ----------------------------------------
servicea                  southeastasia  $RESOURCE_GROUP         https://$AZURE_SPRING_APPS_NAME-servicea.azuremicroservices.io       default                  Succeeded             1      2Gi       1/1                 N/A                    -                     default                  -

確認服務 A 應用程式正在執行

上一個命令的輸出包含服務的公用URL。 若要存取 RESTful 端點,請附加 /serviceA 至 URL,如下列命令所示:

curl https://$AZURE_SPRING_APPS_NAME-servicea.azuremicroservices.io/serviceA

此命令會產生下列輸出。

This is a result of Service A

服務 A 包含可顯示環境變數清單的 RESTful 端點。 使用 存取端點 /env 以查看環境變數,如下列命令所示:

curl https://$AZURE_SPRING_APPS_NAME-servicea.azuremicroservices.io/env

此命令會產生下列輸出。

"JAVA_TOOL_OPTIONS":"-Deureka.client.service-url.defaultZone=https://$AZURE_SPRING_APPS_NAME.svc.azuremicroservices.io/eureka/default/eureka

如您所見, eureka.client.service-url.defaultZone 已新增至 JAVA_TOOL_OPTIONS。 如此一來,應用程式就可以向服務登錄註冊服務,並從其他服務取得該服務。

您現在可以在 Azure Spring Apps 中向 Service Registry (Eureka Server) 註冊服務。 其他服務現在可以使用服務登錄來存取服務。

實作透過服務登錄存取服務 A 的新服務 B

使用 Spring Boot 實作服務 B

流覽至 Spring Initializr 以建立服務 B 的新專案。此連結會使用下列 URL 來初始化設定:

https://start.spring.io/#!type=maven-project&language=java&packaging=jar&groupId=com.example&artifactId=Sample%20Service%20B&name=Sample%20Service%20B&description=Demo%20project%20for%20Spring%20Boot&packageName=com.example.Sample%20Service%20B&dependencies=web,cloud-eureka

然後,選取 [ 產生 ] 以取得新的專案。

實作服務 B 即服務登入用戶端 (Eureka 用戶端)

如同服務 A,請將 @EnableEurekaClient 批註新增至服務 B,將其設定為 Eureka 用戶端。

package com.example.Sample.Service.B;

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

@SpringBootApplication
@EnableEurekaClient
public class SampleServiceBApplication {

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

在服務 B 中實作服務端點

接下來,實作會叫用服務 A 的新服務端點 (/invoke-serviceA。使用下列程式代碼將ServiceBEndpoint.java檔案新增至您的專案。

package com.example.Sample.Service.B;
import java.util.List;
import java.util.stream.Collectors;
import com.netflix.discovery.EurekaClient;
import com.netflix.discovery.shared.Application;
import com.netflix.discovery.shared.Applications;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ServiceBEndpoint {
    @Autowired
    private EurekaClient discoveryClient;

    @GetMapping(value = "/invoke-serviceA")
    public String invokeServiceA()
    {
        RestTemplate  restTemplate = new RestTemplate();
        String response = restTemplate.getForObject("http://servicea/serviceA",String.class);
        return "INVOKE SERVICE A FROM SERVICE B: " + response;
    }

    @GetMapping(value = "/list-all")
    public List<String> listsAllServices() {
        Applications applications = discoveryClient.getApplications();
        List<Application> registeredApplications = applications.getRegisteredApplications();
        List<String> appNames = registeredApplications.stream().map(app -> app.getName()).collect(Collectors.toList());
        return appNames;
    }
}

此範例會為了簡單起見而使用 RestTemplate 。 端點會傳回回應字串與另一個字元串 (INVOKE SERVICE A FROM SERVICE B: ") 表示服務 B 呼叫它。

此範例也會實作另一個端點 (/list-all) 以進行驗證。 此實作可確保服務與服務登錄正確通訊。 您可以呼叫此端點,以取得服務登錄中註冊的應用程式清單。

這個範例會將服務 A 叫用為 http://servicea。 服務名稱是您在建立 Azure Spring Apps 應用程式期間指定的名稱。 (例如: az spring app create --name ServiceA.)應用程式名稱會與您向服務登錄註冊的服務名稱相符,讓您更輕鬆地管理服務名稱。

建置服務 B

使用下列命令來建置您的專案。

mvn clean package

將服務 B 部署至 Azure Spring Apps

使用下列命令在 Azure Spring Apps 中建立應用程式以部署服務 B。

az spring app create \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --name serviceB \
    --instance-count 1 \
    --memory 2Gi \
    --assign-endpoint

接下來,使用下列命令將應用程式系結至 Service Registry。

az spring service-registry bind \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --app serviceB

接下來,使用下列命令來部署服務。

az spring app deploy \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --name serviceB \
    --artifact-path ./target/Sample-Service-B-0.0.1-SNAPSHOT.jar \
    --jvm-options="-Xms1024m -Xmx1024m"

接下來,使用下列命令來檢查應用程式的狀態。

az spring app list \
    --resource-group $RESOURCE_GROUP \
    --service $AZURE_SPRING_APPS_NAME \
    --output table

如果正確部署服務 A 和服務 B,此命令會產生類似下列範例的輸出。

Name      Location       ResourceGroup           Public Url                                                       Production Deployment    Provisioning State    CPU    Memory    Running Instance    Registered Instance    Persistent Storage    Bind Service Registry    Bind Application Configuration Service
--------  -------------  ----------------------  ---------------------------------------------------------------  -----------------------  --------------------  -----  --------  ------------------  ---------------------  --------------------  -----------------------  ----------------------------------------
servicea  southeastasia  SpringCloud-Enterprise  https://$AZURE_SPRING_APPS_NAME-servicea.azuremicroservices.io  default                  Succeeded             1      2Gi       1/1                 1/1                    -                     default                  -
serviceb  southeastasia  SpringCloud-Enterprise  https://$AZURE_SPRING_APPS_NAME-serviceb.azuremicroservices.io  default                  Succeeded             1      2Gi       1/1                 1/1                    -                     default                  -

從服務 B 叫用服務 A

上一個命令的輸出包含服務的公用URL。 若要存取 RESTful 端點,請附加 /invoke-serviceA 至 URL,如下列命令所示:

curl https://$AZURE_SPRING_APPS_NAME-serviceb.azuremicroservices.io/invoke-serviceA

這個指令會產生下列輸出:

INVOKE SERVICE A FROM SERVICE B: This is a result of Service A

從服務登錄取得一些資訊

最後,存取端點, /list-all 並從服務登錄擷取一些資訊。 下列命令會擷取服務登錄中註冊的服務清單。

curl https://$AZURE_SPRING_APPS_NAME-serviceb.azuremicroservices.io/list-all

此命令會產生下列輸出。

["SERVICEA","EUREKA-SERVER","SERVICEB"]

如此一來,您可以視需要從程式取得詳細資訊。

在建立服務之後啟用/停用服務登錄

您可以使用 Azure 入口網站 或 Azure CLI,在建立服務之後啟用和停用服務登錄。 停用 Service Registry 之前,您必須將所有應用程式解除系結。

使用下列步驟,使用 Azure 入口網站 來啟用或停用服務登錄:

  1. 流覽至您的服務資源,然後選取 [服務登錄]。
  2. 選取 [管理]
  3. 選取或取消選取 [啟用服務登錄],然後選取 [ 儲存]。
  4. 您現在可以在 [服務登錄] 頁面上檢視服務登錄 的狀態。

下一步