הגדרת Service Directory

בדף הזה מוסבר איך להגדיר מרחב שמות של Service Directory, לרשום שירות במרחב השמות ולהוסיף נקודות קצה לשירות. אפשר ליצור מרחב שמות כשרושמים שירות או לפני שרושמים שירות.

לפני שמריצים את הפקודות שמפורטות בדף הזה, כדאי לעיין במושגים שבמאמר סקירה כללית על Service Directory ובמונחים מרכזיים שקשורים ל-Service Directory.

הגדרת הפרויקט

  1. נכנסים לחשבון Google Cloud . אם אתם משתמשים חדשים ב- Google Cloud, צרו חשבון כדי שתוכלו להעריך את הביצועים של המוצרים שלנו בתרחישים מהעולם האמיתי. לקוחות חדשים מקבלים בחינם גם קרדיט בשווי 300$ להרצה, לבדיקה ולפריסה של עומסי העבודה.
  2. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  3. Verify that billing is enabled for your Google Cloud project.

  4. Enable the Service Directory API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  5. התקינו את ה-CLI של Google Cloud.

  6. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  7. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init
  8. In the Google Cloud console, on the project selector page, select or create a Google Cloud project.

    Roles required to select or create a project

    • Select a project: Selecting a project doesn't require a specific IAM role—you can select any project that you've been granted a role on.
    • Create a project: To create a project, you need the Project Creator role (roles/resourcemanager.projectCreator), which contains the resourcemanager.projects.create permission. Learn how to grant roles.

    Go to project selector

  9. Verify that billing is enabled for your Google Cloud project.

  10. Enable the Service Directory API.

    Roles required to enable APIs

    To enable APIs, you need the Service Usage Admin IAM role (roles/serviceusage.serviceUsageAdmin), which contains the serviceusage.services.enable permission. Learn how to grant roles.

    Enable the API

  11. התקינו את ה-CLI של Google Cloud.

  12. אם אתם משתמשים בספק זהויות חיצוני (IdP), קודם אתם צריכים להיכנס ל-CLI של gcloud באמצעות המאגר המאוחד לניהול זהויות.

  13. כדי לאתחל את ה-CLI של gcloud, הריצו את הפקודה הבאה:

    gcloud init

הגדרת משאבים ב-Service Directory

כדי לאכלס את Service Directory בשירותים, צריך ליצור מרחב שמות ולרשום את השירות במרחב השמות.

הגדרת מרחב שמות

בפרויקט, יוצרים מרחב שמות באזור. האזור הזה לא חייב להיות המקום שבו כל השירותים ונקודות הקצה שלכם פועלים, אבל הוא צריך להיות קרוב ככל האפשר.

אפשר לרשום את השירותים בכל אזור של ספריית השירותים, והם עדיין ניתנים לפתרון באופן גלובלי. בתוך אזור מסוים, בפרויקטים יכולים להיות כמה מרחבי שמות, והמרחבים האלה יכולים להיות באזורים שונים. מרחב שמות יחיד לא יכול לכלול כמה אזורים.

המסוף

  1. נכנסים לדף Service Directory namespaces במסוף Google Cloud .

    מעבר למרחבי שמות של Service Directory

  2. לוחצים על יצירת מרחב שמות.
  3. ברשימה Region, בוחרים אזור למרחב השמות.
  4. בשדה שם מרחב השמות, נותנים שם למרחב השמות.
  5. לוחצים על יצירה.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

  1. יצירת מרחב שמות

    gcloud service-directory namespaces create NAMESPACE \
       --location REGION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • NAMESPACE: השם של מרחב השמות שאתם יוצרים.
    • REGION: האזור Google Cloud שמכיל את מרחב השמות.
  2. אופציונלי: מגדירים מדיניות IAM במרחב השמות. כך המשתמש או הקבוצה שצוינו מקבלים את התפקיד שצוין במרחב השמות הזה ובכל השירותים ששייכים למרחב השמות.

    gcloud service-directory namespaces add-iam-policy-binding NAMESPACE \
    --member user:someone@example.com \
    --role ROLE \
    --location REGION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • NAMESPACE: השם של מרחב השמות שיצרתם.
    • ROLE: התפקיד שאתם מקצים.
    • REGION: האזור Google Cloud שמכיל את מרחב השמות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Api.Gax.ResourceNames;
using Google.Cloud.ServiceDirectory.V1;

public class CreateNamespaceSample
{
    public Namespace CreateNamespace(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var locationName = LocationName.FromProjectLocation(projectId, locationId);
        return registrationServiceClient.CreateNamespace(locationName, new Namespace(), namespaceId);
    }
}

Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"
	"io"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func createNamespace(w io.Writer, projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Create a Namespace.
	req := &sdpb.CreateNamespaceRequest{
		Parent:      fmt.Sprintf("projects/%s/locations/%s", projectID, location),
		NamespaceId: namespaceID,
	}
	resp, err := client.CreateNamespace(ctx, req)
	if err != nil {
		return fmt.Errorf("CreateNamespace: %w", err)
	}
	fmt.Fprintf(w, "servicedirectory.CreateNamespace result: %s\n", resp.Name)
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.LocationName;
import com.google.cloud.servicedirectory.v1.Namespace;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import java.io.IOException;

public class NamespacesCreate {

  public static void createNamespace() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    String projectId = "your-project-id";
    String locationId = "your-region";
    // This is user-created; must be unique within the project/region above.
    String namespaceId = "your-namespace";
    createNamespace(projectId, locationId, namespaceId);
  }

  // Create a new namespace.
  public static void createNamespace(String projectId, String locationId, String namespaceId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The project and location to create the namespace in.
      LocationName parent = LocationName.of(projectId, locationId);

      // The namespace object to create. Here, we use the default instance.
      Namespace namespace = Namespace.newBuilder().build();

      // Send the request to create the namespace.
      Namespace createdNamespace = client.createNamespace(parent, namespace, namespaceId);

      // Process the response.
      System.out.println("Created Namespace: " + createdNamespace.getName());
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the location name
const locationName = registrationServiceClient.locationPath(
  projectId,
  locationId
);

async function createNamespace() {
  const [namespace] = await registrationServiceClient.createNamespace({
    parent: locationName,
    namespaceId: namespaceId,
  });

  console.log(`Created namespace: ${namespace.name}`);
  return namespace;
}

return createNamespace();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\RegistrationServiceClient;
use Google\Cloud\ServiceDirectory\V1\CreateNamespaceRequest;
use Google\Cloud\ServiceDirectory\V1\PBNamespace;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 */
function create_namespace(
    string $projectId,
    string $locationId,
    string $namespaceId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $locationName = RegistrationServiceClient::locationName($projectId, $locationId);
    $createNamespaceRequest = (new CreateNamespaceRequest())
        ->setParent($locationName)
        ->setNamespaceId($namespaceId)
        ->setNamespace(new PBNamespace());
    $namespace = $client->createNamespace($createNamespaceRequest);

    // Print results.
    printf('Created Namespace: %s' . PHP_EOL, $namespace->getName());
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def create_namespace(project_id: str, location: str, namespace_id: str) -> Namespace:
    """Creates a namespace in the given location.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the new namespace.
        namespace_id: A unique id for the namespace.

    Returns:
        The created namespace.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    namespace = servicedirectory_v1.Namespace(
        name=client.namespace_path(project_id, location, namespace_id)
    )

    response = client.create_namespace(
        parent=f"projects/{project_id}/locations/{location}",
        namespace=namespace,
        namespace_id=namespace_id,
    )

    print(f"Created namespace {response.name}.")

    return response

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the new namespace"
# namespace = "The name of the namespace you are creating"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The parent path of the namespace
parent = registration_service.location_path(
  project: project, location: location
)

# Use the Service Directory API to create the namespace
response = registration_service.create_namespace(
  parent: parent, namespace_id: namespace
)
puts "Created namespace: #{response.name}"

הגדרת שירות

יוצרים שירות במרחב השמות. שירות מורכב משם ומאנוטציות אופציונליות שקשורות לשירות. יש כמה הגבלות על הפורמט של שמות השירותים:

  • שמות השירותים צריכים להיות ייחודיים במרחב שמות.
  • שמות השירותים צריכים להיות בהתאם למוסכמת מתן השמות של תוויות DNS.

כדי ליצור כלל העברה של Private Service Connect ולרשום אותו כשירות ב-Service Directory, אפשר לעיין במאמר רישום של נקודת קצה של Private Service Connect ב-Service Directory.

כדי לרשום שירות רגיל ב-Service Directory, פועלים לפי השלבים הבאים:

המסוף

  1. נכנסים לדף Service Directory במסוף Google Cloud .

    כניסה ל-Service Directory

  2. לוחצים על Register Service (רישום שירות).
  3. לוחצים על Standard ואז על Next.
  4. בוחרים Region כדי לרשום את השירות.
  5. בוחרים מרחב שמות שבו רוצים לרשום את השירות. אם אין לכם מרחב שמות, אתם יכולים ליצור אותו כך:
    1. בתיבה Namespace, לוחצים על Create namespace (יצירת מרחב שמות).
    2. מזינים שם של מרחב שמות.
    3. לוחצים על יצירה.
  6. מזינים שם שירות.
  7. אופציונלי: אם רוצים להוסיף הערות לשירות, מבצעים את הפעולות הבאות:
    1. לוחצים על הוספת הערה.
    2. מוסיפים מפתח וערך.
    3. כדי להוסיף עוד הערות, לוחצים שוב על הוספת הערה.
  8. לוחצים על יצירה.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

  1. יוצרים שירות במרחב שמות.

    gcloud service-directory services create SERVICE \
       --annotations KEY_1=VALUE_1,KEY_2=VALUE_2 \
       --namespace NAMESPACE \
       --location REGION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • SERVICE: השם של השירות שאתם יוצרים.
    • NAMESPACE: השם של מרחב השמות שמכיל את השירות.
    • REGION: האזור Google Cloud שמכיל את מרחב השמות.
    • KEY_1,VALUE_1,KEY_2, VALUE_2: קבוצת מחרוזות של מפתח וערך בזוגות.
  2. אופציונלי: מגדירים מדיניות IAM בשירות. כך נותנים למשתמש או לקבוצה שצוינו את התפקיד שצוין בשירות הזה ובכל נקודות הקצה ששייכות לשירות.

    gcloud service-directory services add-iam-policy-binding SERVICE \
    --member user:someone@example.com \
    --role ROLE \
    --namespace NAMESPACE \
    --location REGION
    

    מחליפים את מה שכתוב בשדות הבאים:

    • SERVICE: השם שנתתם לשירות.
    • NAMESPACE: השם של מרחב השמות שמכיל את השירות
    • ROLE: התפקיד שאתם מקצים.
    • REGION: האזור Google Cloud שמכיל את מרחב השמות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Cloud.ServiceDirectory.V1;

public class CreateServiceSample
{
    public Service CreateService(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace",
        string serviceId = "test-service")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var namespaceName = NamespaceName.FromProjectLocationNamespace(projectId, locationId, namespaceId);
        return registrationServiceClient.CreateService(namespaceName, new Service(), serviceId);
    }
}


Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"
	"io"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func createService(w io.Writer, projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"
	serviceID := "golang-test-service"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Create a Service.
	req := &sdpb.CreateServiceRequest{
		Parent:    fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", projectID, location, namespaceID),
		ServiceId: serviceID,
		Service: &sdpb.Service{
			Annotations: map[string]string{
				"key1": "value1",
				"key2": "value2",
			},
		},
	}
	service, err := client.CreateService(ctx, req)
	if err != nil {
		return fmt.Errorf("CreateSerice: %w", err)
	}
	fmt.Fprintf(w, "servicedirectory.Createservice result %s\n", service.Name)
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.NamespaceName;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import com.google.cloud.servicedirectory.v1.Service;
import java.io.IOException;

public class ServicesCreate {

  public static void createService() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory namespace.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    // This is user-created; must be unique within the namespace above.
    String serviceId = "your-service";
    createService(projectId, locationId, namespaceId, serviceId);
  }

  // Create a new service.
  public static void createService(
      String projectId, String locationId, String namespaceId, String serviceId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The namespace to create the service in.
      NamespaceName parent = NamespaceName.of(projectId, locationId, namespaceId);

      // The service object to create.
      // Optionally add some annotations for the service.
      Service service = Service.newBuilder().putAnnotations("protocol", "tcp").build();

      // Send the request to create the namespace.
      Service createdService = client.createService(parent, service, serviceId);

      // Process the response.
      System.out.println("Created Service: " + createdService.getName());
      System.out.println("Annotations: " + createdService.getAnnotationsMap());
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the namespace name
const namespaceName = registrationServiceClient.namespacePath(
  projectId,
  locationId,
  namespaceId
);

async function createService() {
  const [service] = await registrationServiceClient.createService({
    parent: namespaceName,
    serviceId: serviceId,
  });

  console.log(`Created service: ${service.name}`);
  return service;
}

return createService();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\RegistrationServiceClient;
use Google\Cloud\ServiceDirectory\V1\CreateServiceRequest;
use Google\Cloud\ServiceDirectory\V1\Service;

/**
 * @param string $projectId Your Cloud project ID
 * @param string $locationId Your GCP region
 * @param string $namespaceId Your namespace name
 * @param string $serviceId Your service name
 */
function create_service(
    string $projectId,
    string $locationId,
    string $namespaceId,
    string $serviceId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
    $createServiceRequest = (new CreateServiceRequest())
        ->setParent($namespaceName)
        ->setServiceId($serviceId)
        ->setService(new Service());
    $service = $client->createService($createServiceRequest);

    // Print results.
    printf('Created Service: %s' . PHP_EOL, $service->getName());
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def create_service(
    project_id: str, location: str, namespace_id: str, service_id: str
) -> Service:
    """Creates a service in the given namespace.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace.
        namespace_id: The id of the parent namespace.
        service_id: The id of the service you are creating. Service names must be unique within a namespace and follow
            conventions for DNS labels.

    Returns:
        The created service.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    service = servicedirectory_v1.Service(
        name=client.service_path(project_id, location, namespace_id, service_id)
    )

    response = client.create_service(
        parent=client.namespace_path(project_id, location, namespace_id),
        service=service,
        service_id=service_id,
    )

    print(f"Created service {response.name}.")

    return response

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the parent namespace"
# service   = "The name of the service you are creating"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The parent path of the service
parent = registration_service.namespace_path(
  project: project, location: location, namespace: namespace
)

# Use the Service Directory API to create the service
response = registration_service.create_service parent: parent, service_id: service
puts "Created service: #{response.name}"

הגדרת נקודת קצה

אחרי שרושמים את השירות, מוסיפים כמה נקודות קצה. נקודת קצה מורכבת משם ייחודי, מכתובת IP ומפורט (אופציונליים) ומביאורים של צמדי מפתח-ערך. אם מציינים כתובת IP, היא צריכה להיות כתובת IPv4 או IPv6 תקינה.

המסוף

  1. נכנסים לדף Service Directory במסוף Google Cloud .

    כניסה ל-Service Directory

  2. לוחצים על שירות.
  3. לוחצים על הוספת נקודת קצה.
  4. מזינים שם של נקודת קצה.
  5. מזינים כתובת IP מסוג IPv4 או IPv6.
  6. מזינים מספר יציאה.
  7. אופציונלי: אם רוצים להוסיף הערות לנקודת הקצה, מבצעים את הפעולות הבאות:
    1. בקטע הערות, לוחצים על הוספת הערה.
    2. מוסיפים מפתח וערך.
    3. כדי להוסיף עוד הערות, לוחצים שוב על הוספת הערה.
  8. לוחצים על יצירה.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

אחרי שרושמים את השירות, מוסיפים כמה נקודות קצה.

gcloud service-directory endpoints create ENDPOINT \
   --address IP_ADDRESS \
   --port PORT_NUMBER \
   --annotations KEY_1=VALUE_1,KEY_2=VALUE_2 \
   --service SERVICE \
   --namespace NAMESPACE \
   --location REGION
gcloud service-directory endpoints create ENDPOINT2 \
   --address IP_ADDRESS2 \
   --port PORT_NUMBER2 \
   --service SERVICE \
   --namespace NAMESPACE \
   --location REGION

מחליפים את מה שכתוב בשדות הבאים:

  • ENDPOINT ו-ENDPOINT2: השמות של נקודות הקצה שאתם יוצרים בשירות.
  • IP_ADDRESS ו-IP_ADDRESS2: כתובות ה-IPv6 ו-IPv4 של נקודות הקצה, בהתאמה.
  • PORT_NUMBER ו-PORT_NUMBER2: היציאות שבהן פועלות נקודות הקצה.
  • SERVICE: השם של השירות שמכיל את נקודת הקצה.
  • NAMESPACE: השם של מרחב השמות שמכיל את השירות.
  • REGION: האזור Google Cloud שמכיל את מרחב השמות.
  • KEY_1,VALUE_1,KEY_2, VALUE_2: קבוצת מחרוזות של מפתח וערך בזוגות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Cloud.ServiceDirectory.V1;

public class CreateEndpointSample
{
	public Endpoint CreateEndpoint(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace",
        string serviceId = "test-service",
        string endpointId = "test-endpoint")
    {
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        var serviceName = ServiceName.FromProjectLocationNamespaceService(projectId, locationId, namespaceId, serviceId);
        return registrationServiceClient.CreateEndpoint(serviceName, new Endpoint(), endpointId);
    }
}

Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"
	"io"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func createEndpoint(w io.Writer, projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"
	serviceID := "golang-test-service"
	endpointID := "golang-test-endpoint"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Create an Endpoint.
	req := &sdpb.CreateEndpointRequest{
		Parent:     fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", projectID, location, namespaceID, serviceID),
		EndpointId: endpointID,
		Endpoint: &sdpb.Endpoint{
			Address: "8.8.8.8",
			Port:    8080,
			Annotations: map[string]string{
				"key1": "value1",
				"key2": "value2",
			},
		},
	}
	endpoint, err := client.CreateEndpoint(ctx, req)
	if err != nil {
		return fmt.Errorf("CreateEndpoint: %w", err)
	}
	fmt.Fprintf(w, "servicedirectory.CreateEndpoint result: %s", endpoint.Name)
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.Endpoint;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import com.google.cloud.servicedirectory.v1.ServiceName;
import java.io.IOException;

public class EndpointsCreate {

  public static void createEndpoint() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory service.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    String serviceId = "your-service";
    // This is user-created; must be unique within the service above.
    String endpointId = "your-endpoint";
    createEndpoint(projectId, locationId, namespaceId, serviceId, endpointId);
  }

  // Create a new endpoint.
  public static void createEndpoint(
      String projectId, String locationId, String namespaceId, String serviceId, String endpointId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The service to create the endpoint in.
      ServiceName parent = ServiceName.of(projectId, locationId, namespaceId, serviceId);

      // The endpoint to create, with fields filled in.
      // Optionally set an IP address and port for the endpoint.
      Endpoint endpoint = Endpoint.newBuilder().setAddress("10.0.0.1").setPort(443).build();

      // Send the request to create the endpoint.
      Endpoint createdEndpoint = client.createEndpoint(parent, endpoint, endpointId);

      // Process the response.
      System.out.println("Created Endpoint: " + createdEndpoint.getName());
      System.out.println("IP Address: " + createdEndpoint.getAddress());
      System.out.println("Port: " + createdEndpoint.getPort());
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';
// const endpointId = 'my-endpoint';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the service name
const serviceName = registrationServiceClient.servicePath(
  projectId,
  locationId,
  namespaceId,
  serviceId
);

async function createEndpoint() {
  const [endpoint] = await registrationServiceClient.createEndpoint({
    parent: serviceName,
    endpointId: endpointId,
    endpoint: {address: '10.0.0.1', port: 8080},
  });

  console.log(`Created endpoint: ${endpoint.name}`);
  return endpoint;
}

return createEndpoint();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\RegistrationServiceClient;
use Google\Cloud\ServiceDirectory\V1\CreateEndpointRequest;
use Google\Cloud\ServiceDirectory\V1\Endpoint;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 * @param string $serviceId     Your service name
 * @param string $endpointId    Your endpoint name
 * @param string $ip            (Optional) Defaults to ''
 * @param int    $port          (Optional) Defaults to 0
 */
function create_endpoint(
    string $projectId,
    string $locationId,
    string $namespaceId,
    string $serviceId,
    string $endpointId,
    string $ip = '',
    int $port = 0
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Construct Endpoint object.
    $endpointObject = (new Endpoint())
        ->setAddress($ip)
        ->setPort($port);

    // Run request.
    $serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
    $createEndpointRequest = (new CreateEndpointRequest())
        ->setParent($serviceName)
        ->setEndpointId($endpointId)
        ->setEndpoint($endpointObject);
    $endpoint = $client->createEndpoint($createEndpointRequest);

    // Print results.
    printf('Created Endpoint: %s' . PHP_EOL, $endpoint->getName());
    printf('  IP: %s' . PHP_EOL, $endpoint->getAddress());
    printf('  Port: %d' . PHP_EOL, $endpoint->getPort());
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def create_endpoint(
    project_id: str,
    location: str,
    namespace_id: str,
    service_id: str,
    endpoint_id: str,
    address: str,
    port: str,
) -> Endpoint:
    """Creates a endpoint in the given service.

    An endpoint consists of a unique name, an optional IP address and a port, and key-value annotations.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace.
        namespace_id: The id of the parent namespace.
        service_id: The id of the parent service.
        endpoint_id: A name for the endpoint you are creating.
        address: IP address for the endpoint.
        port: Port number for the endpoint.

    Returns:
        The created endpoint.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    endpoint = servicedirectory_v1.Endpoint(
        name=client.endpoint_path(
            project_id, location, namespace_id, service_id, endpoint_id
        ),
        address=address,
        port=port,
    )

    response = client.create_endpoint(
        parent=client.service_path(project_id, location, namespace_id, service_id),
        endpoint=endpoint,
        endpoint_id=endpoint_id,
    )

    print(f"Created endpoint {response.name}.")

    return response

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the parent namespace"
# service   = "The name of the parent service"
# endpoint  = "The name of the endpoint you are creating"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The parent path of the endpoint
parent = registration_service.service_path(
  project:   project,
  location:  location,
  namespace: namespace,
  service:   service
)

# Set the IP Address and Port on the Endpoint
endpoint_data = Google::Cloud::ServiceDirectory::V1::Endpoint.new(
  address: "10.0.0.1",
  port:    443
)

# Use the Service Directory API to create the endpoint
response = registration_service.create_endpoint(
  parent: parent, endpoint_id: endpoint, endpoint: endpoint_data
)
puts "Created endpoint: #{response.name}"

פתרון בעיה בשירות

באמצעות Service Directory, לקוחות יכולים לפתור בעיות בשירותים באמצעות DNS, ‏ HTTP ו-gRPC. הפעלת הפונקציה resolve מחזירה את כל המאפיינים של השירות, את כל נקודות הקצה ואת כל ההערות.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

gcloud service-directory services resolve SERVICE \
   --namespace NAMESPACE \
   --location REGION

מחליפים את מה שכתוב בשדות הבאים:

  • SERVICE: השם של השירות שרוצים לפתור.
  • NAMESPACE: השם של מרחב השמות שמכיל את השירות.
  • REGION: האזור Google Cloud שמכיל את מרחב השמות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Cloud.ServiceDirectory.V1;

public class ResolveServiceSample
{
    public Service ResolveService(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace",
        string serviceId = "test-service")
    {
        // Create client
        LookupServiceClient lookupServiceClient = LookupServiceClient.Create();
        // Initialize request argument(s)
        ResolveServiceRequest request = new ResolveServiceRequest
        {
            ServiceName = ServiceName.FromProjectLocationNamespaceService(projectId, locationId, namespaceId, serviceId),
        };
        // Make the request
        ResolveServiceResponse response = lookupServiceClient.ResolveService(request);
        return response.Service;
    }
}

Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"
	"io"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func resolveService(w io.Writer, projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"
	serviceID := "golang-test-service"

	ctx := context.Background()
	// Create a lookup client.
	resolver, err := servicedirectory.NewLookupClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewLookupClient: %w", err)
	}

	defer resolver.Close()
	// Now Resolve the service.
	req := &sdpb.ResolveServiceRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", projectID, location, namespaceID, serviceID),
	}
	result, err := resolver.ResolveService(ctx, req)
	if err != nil {
		return fmt.Errorf("ResolveService: %w", err)
	}

	fmt.Fprintf(w, "Successfully Resolved Service %v\n", result)
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.Endpoint;
import com.google.cloud.servicedirectory.v1.LookupServiceClient;
import com.google.cloud.servicedirectory.v1.ResolveServiceRequest;
import com.google.cloud.servicedirectory.v1.ResolveServiceResponse;
import com.google.cloud.servicedirectory.v1.ServiceName;
import java.io.IOException;

public class ServicesResolve {

  public static void resolveService() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory service.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    String serviceId = "your-service";
    resolveService(projectId, locationId, namespaceId, serviceId);
  }

  // Resolve a service.
  public static void resolveService(
      String projectId, String locationId, String namespaceId, String serviceId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (LookupServiceClient client = LookupServiceClient.create()) {
      // The service to resolve.
      ServiceName name = ServiceName.of(projectId, locationId, namespaceId, serviceId);

      // Construct the resolve request to be sent to the client.
      ResolveServiceRequest request =
          ResolveServiceRequest.newBuilder().setName(name.toString()).build();

      // Send the request to resolve the service.
      ResolveServiceResponse resolveResponse = client.resolveService(request);

      // Process the response.
      System.out.println("Resolved Service: " + resolveResponse.getService().getName());

      System.out.println("Endpoints found:");
      for (Endpoint endpoint : resolveResponse.getService().getEndpointsList()) {
        System.out.println(
            endpoint.getName() + " -- " + endpoint.getAddress() + ":" + endpoint.getPort());
      }
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';

// Imports the Google Cloud client library
const {LookupServiceClient} = require('@google-cloud/service-directory');

// Creates a client
const lookupServiceClient = new LookupServiceClient();

// Build the service name
const serviceName = lookupServiceClient.servicePath(
  projectId,
  locationId,
  namespaceId,
  serviceId
);

async function resolveService() {
  const [response] = await lookupServiceClient.resolveService({
    name: serviceName,
  });

  console.log(`Resolved service: ${response.service.name}`);
  for (const e of response.service.endpoints) {
    console.log(`\n${e.name}`);
    console.log(`Address: ${e.address}`);
    console.log(`Port: ${e.port}\n`);
  }
  return response.service;
}

return resolveService();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\LookupServiceClient;
use Google\Cloud\ServiceDirectory\V1\ResolveServiceRequest;
use Google\Cloud\ServiceDirectory\V1\Service;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 * @param string $serviceId     Your service name
 */
function resolve_service(
    string $projectId,
    string $locationId,
    string $namespaceId,
    string $serviceId
): void {
    // Instantiate a client.
    $client = new LookupServiceClient();

    // Run request.
    $serviceName = LookupServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
    $resolveServiceRequest = (new ResolveServiceRequest())
        ->setName($serviceName);
    $service = $client->resolveService($resolveServiceRequest)->getService();

    // Print results.
    printf('Resolved Service: %s' . PHP_EOL, $service->getName());
    print('Endpoints:' . PHP_EOL);
    foreach ($service->getEndpoints() as $endpoint) {
        printf('  Name: %s' . PHP_EOL, $endpoint->getName());
        printf('    IP: %s' . PHP_EOL, $endpoint->getAddress());
        printf('    Port: %d' . PHP_EOL, $endpoint->getPort());
    }
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def resolve_service(
    project_id: str, location: str, namespace_id: str, service_id: str
) -> ResolveServiceResponse:
    """Resolves a service in the given namespace.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace.
        namespace_id: The id of the parent namespace.
        service_id: The id of the service to resolve.

    Returns:
        The resolved service response, which returns all properties of the service, all endpoints, and all annotations.
    """

    client = servicedirectory_v1.LookupServiceClient()

    request = servicedirectory_v1.ResolveServiceRequest(
        name=servicedirectory_v1.RegistrationServiceClient().service_path(
            project_id, location, namespace_id, service_id
        )
    )

    response = client.resolve_service(request=request)

    print("Endpoints found:")
    for endpoint in response.service.endpoints:
        print(f"{endpoint.name} -- {endpoint.address}:{endpoint.port}")

    return response

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the parent namespace"
# service   = "The name of the service"

require "google/cloud/service_directory"

# Initialize the client
lookup_service = Google::Cloud::ServiceDirectory.lookup_service

# The name of the service
service_path = lookup_service.service_path(
  project:   project,
  location:  location,
  namespace: namespace,
  service:   service
)

# Use the Service Directory API to resolve the service
response = lookup_service.resolve_service name: service_path
puts "Resolved service: #{response.service.name}"
puts "Endpoints: "
response.service.endpoints.each do |endpoint|
  puts "#{endpoint.name} #{endpoint.address} #{endpoint.port}"
end

מחיקת משאבים

אם אתם לא צריכים משאב מסוים, אתם יכולים למחוק אותו מ-Service Directory.

מחיקת נקודת קצה משירות

המסוף

  1. נכנסים לדף Service Directory במסוף Google Cloud .

    כניסה ל-Service Directory

  2. לוחצים על השירות שממנו רוצים למחוק את נקודת הקצה.
  3. מסמנים את התיבה לצד נקודת הקצה שרוצים למחוק.
  4. לוחצים על Delete.
  5. בתיבת הדו-שיח לאישור, לוחצים שוב על מחיקה.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

gcloud service-directory endpoints delete ENDPOINT \
    --service=SERVICE \
    --namespace=NAMESPACE \
    --location=REGION

מחליפים את מה שכתוב בשדות הבאים:

  • ENDPOINT: השם של נקודת הקצה שרוצים למחוק.
  • SERVICE: השם של השירות שמכיל את נקודת הקצה.
  • NAMESPACE: השם של מרחב השמות שמכיל את השירות.
  • REGION: האזור Google Cloud שמכיל את מרחב השמות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Cloud.ServiceDirectory.V1;

public class DeleteEndpointSample
{
    public void DeleteEndpoint(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace",
        string serviceId = "test-service",
        string endpointId = "test-endpoint")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var endpointName = EndpointName.FromProjectLocationNamespaceServiceEndpoint(projectId, locationId, namespaceId, serviceId, endpointId);
        registrationServiceClient.DeleteEndpoint(endpointName);
    }
}

Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func deleteEndpoint(projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"
	serviceID := "golang-test-service"
	endpointID := "golang-test-endpoint"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Delete an Endpoint
	req := &sdpb.DeleteEndpointRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s/endpoints/%s", projectID, location, namespaceID, serviceID, endpointID),
	}
	if err := client.DeleteEndpoint(ctx, req); err != nil {
		return fmt.Errorf("DeleteEndpoint: %w", err)
	}
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.EndpointName;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import java.io.IOException;

public class EndpointsDelete {

  public static void deleteEndpoint() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory endpoint.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    String serviceId = "your-service";
    String endpointId = "your-endpoint";
    deleteEndpoint(projectId, locationId, namespaceId, serviceId, endpointId);
  }

  // Delete an endpoint.
  public static void deleteEndpoint(
      String projectId, String locationId, String namespaceId, String serviceId, String endpointId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The endpoint to delete.
      EndpointName endpointName =
          EndpointName.of(projectId, locationId, namespaceId, serviceId, endpointId);

      // Send the request to delete the endpoint.
      client.deleteEndpoint(endpointName);

      // Log the action.
      System.out.println("Deleted Endpoint: " + endpointName.toString());
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';
// const endpointId = 'my-endpoint';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the endpoint name
const endpointName = registrationServiceClient.endpointPath(
  projectId,
  locationId,
  namespaceId,
  serviceId,
  endpointId
);

async function deleteEndpoint() {
  await registrationServiceClient.deleteEndpoint({
    name: endpointName,
  });

  console.log(`Deleted endpoint: ${endpointName}`);
}

deleteEndpoint();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\RegistrationServiceClient;
use Google\Cloud\ServiceDirectory\V1\DeleteEndpointRequest;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 * @param string $serviceId     Your service name
 * @param string $endpointId    Your endpoint name
 */
function delete_endpoint(
    string $projectId,
    string $locationId,
    string $namespaceId,
    string $serviceId,
    string $endpointId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $endpointName = RegistrationServiceClient::endpointName($projectId, $locationId, $namespaceId, $serviceId, $endpointId);
    $deleteEndpointRequest = (new DeleteEndpointRequest())
        ->setName($endpointName);
    $client->deleteEndpoint($deleteEndpointRequest);

    // Print results.
    printf('Deleted Endpoint: %s' . PHP_EOL, $endpointName);
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def delete_endpoint(
    project_id: str,
    location: str,
    namespace_id: str,
    service_id: str,
    endpoint_id: str,
) -> bool:
    """Deletes a endpoint in the given service.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace.
        namespace_id: The id of the parent namespace.
        service_id: The id of the parent service.
        endpoint_id: The id of the endpoint to delete.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    endpoint_name = client.endpoint_path(
        project_id, location, namespace_id, service_id, endpoint_id
    )

    client.delete_endpoint(name=endpoint_name)
    print(f"Deleted endpoint {endpoint_name}.")
    return True

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the parent namespace"
# service   = "The name of the parent service"
# endpoint  = "The name of the endpoint"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The path of the endpoint
endpoint_path = registration_service.endpoint_path(
  project:   project,
  location:  location,
  namespace: namespace,
  service:   service,
  endpoint:  endpoint
)

# Use the Service Directory API to delete the endpoint
registration_service.delete_endpoint name: endpoint_path
puts "Deleted endpoint: #{endpoint_path}"

מחיקת שירות ממרחב שמות

אפשר למחוק שירות שיש לו נקודות קצה. כששירות נמחק, גם כל נקודות הקצה שלו נמחקות.

אפשר למחוק שירות שיש לו אזור בספריית השירותים שמפנה אליו. כל שאילתת DNS נוספת לגבי השירות הזה תחזיר NXDOMAIN.

המסוף

  1. נכנסים לדף Service Directory במסוף Google Cloud .

    כניסה ל-Service Directory

  2. מסמנים את התיבה לצד השירות שרוצים למחוק.
  3. לוחצים על Delete.
  4. בתיבת הדו-שיח לאישור, לוחצים שוב על מחיקה.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

gcloud service-directory services delete SERVICE \
    --namespace=NAMESPACE \
    --location=REGION

מחליפים את מה שכתוב בשדות הבאים:

  • SERVICE: השם של השירות שרוצים למחוק.
  • NAMESPACE: השם של מרחב השמות שמכיל את השירות.
  • REGION: האזור Google Cloud שמכיל את מרחב השמות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Cloud.ServiceDirectory.V1;

public class DeleteServiceSample
{
    public void DeleteService(
        string projectId = "my-project",
        string locationId = "us-east1",
        string namespaceId = "test-namespace",
        string serviceId = "test-service")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var serviceName = ServiceName.FromProjectLocationNamespaceService(projectId, locationId, namespaceId, serviceId);
        registrationServiceClient.DeleteService(serviceName);
    }
}

Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func deleteService(projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"
	serviceID := "golang-test-service"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Delete a service
	req := &sdpb.DeleteServiceRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s/services/%s", projectID, location, namespaceID, serviceID),
	}
	if err := client.DeleteService(ctx, req); err != nil {
		return fmt.Errorf("DeleteService: %w", err)
	}
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import com.google.cloud.servicedirectory.v1.ServiceName;
import java.io.IOException;

public class ServicesDelete {

  public static void deleteService() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory service.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    String serviceId = "your-service";
    deleteService(projectId, locationId, namespaceId, serviceId);
  }

  // Delete a service.
  public static void deleteService(
      String projectId, String locationId, String namespaceId, String serviceId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The service to delete.
      ServiceName serviceName = ServiceName.of(projectId, locationId, namespaceId, serviceId);

      // Send the request to delete the service.
      client.deleteService(serviceName);

      // Log the action.
      System.out.println("Deleted Service: " + serviceName.toString());
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';
// const serviceId = 'my-service';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the service name
const serviceName = registrationServiceClient.servicePath(
  projectId,
  locationId,
  namespaceId,
  serviceId
);

async function deleteService() {
  await registrationServiceClient.deleteService({
    name: serviceName,
  });

  console.log(`Deleted service: ${serviceName}`);
}

deleteService();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\RegistrationServiceClient;
use Google\Cloud\ServiceDirectory\V1\DeleteServiceRequest;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 * @param string $serviceId     Your service name
 */
function delete_service(
    string $projectId,
    string $locationId,
    string $namespaceId,
    string $serviceId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $serviceName = RegistrationServiceClient::serviceName($projectId, $locationId, $namespaceId, $serviceId);
    $deleteServiceRequest = (new DeleteServiceRequest())
        ->setName($serviceName);
    $client->deleteService($deleteServiceRequest);

    // Print results.
    printf('Deleted Service: %s' . PHP_EOL, $serviceName);
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def delete_service(
    project_id: str, location: str, namespace_id: str, service_id: str
) -> bool:
    """Deletes a service in the given namespace.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace.
        namespace_id: The id of the parent namespace.
        service_id: The id of the service to delete.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    service_name = client.service_path(project_id, location, namespace_id, service_id)

    client.delete_service(name=service_name)
    print(f"Deleted service {service_name}.")
    return True

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the parent namespace"
# service   = "The name of the service"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The path of the service
service_path = registration_service.service_path(
  project:   project,
  location:  location,
  namespace: namespace,
  service:   service
)

# Use the Service Directory API to delete the service
registration_service.delete_service name: service_path
puts "Deleted service: #{service_path}"

מחיקת מרחב שמות

אפשר למחוק מרחב שמות שיש בו שירותים ונקודות קצה. כשמרחב שמות נמחק, גם כל השירותים ונקודות הקצה שלו נמחקים.

אפשר למחוק מרחב שמות שמצביע עליו אזור של Service Directory. כל שאילתות ה-DNS הנוספות (לא כולל בקשות SOA/NS למקור האזור) מחזירות NXDOMAIN.

המסוף

  1. נכנסים לדף Service Directory namespaces במסוף Google Cloud .

    מעבר למרחבי שמות של Service Directory

  2. מסמנים את תיבת הסימון לצד מרחב השמות שרוצים למחוק.
  3. לוחצים על Delete.
  4. בתיבת הדו-שיח לאישור, לוחצים שוב על מחיקה.

gcloud

כדי להשתמש ב-Service Directory בשורת הפקודה, קודם צריך להתקין את הגרסה האחרונה של Google Cloud CLI או לשדרג אליה.

gcloud service-directory namespaces delete NAMESPACE \
    --location=REGION

מחליפים את מה שכתוב בשדות הבאים:

  • NAMESPACE: שם מרחב השמות שרוצים למחוק.
  • REGION: האזור Google Cloud שמכיל את מרחב השמות.

C#

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת C# ‎ ולהתקין את Service Directory C# SDK.


using Google.Cloud.ServiceDirectory.V1;

public class DeleteNamespaceSample
{
    public void DeleteNamespace(
        string projectId = "projectId",
        string locationId = "us-east1",
        string namespaceId = "test-namespace")
    {
        // Create client
        RegistrationServiceClient registrationServiceClient = RegistrationServiceClient.Create();
        // Initialize request argument(s)
        var namespaceName = NamespaceName.FromProjectLocationNamespace(projectId, locationId, namespaceId);
        registrationServiceClient.DeleteNamespace(namespaceName);
    }
}

Go

כדי להריץ את הקוד הזה, צריך קודם להגדיר סביבת פיתוח של Go ולהתקין את Service Directory Go SDK.

import (
	"context"
	"fmt"

	servicedirectory "cloud.google.com/go/servicedirectory/apiv1"
	sdpb "cloud.google.com/go/servicedirectory/apiv1/servicedirectorypb"
)

func deleteNamespace(projectID string) error {
	// projectID := "my-project"
	location := "us-east4"
	namespaceID := "golang-test-namespace"

	ctx := context.Background()
	// Create a registration client.
	client, err := servicedirectory.NewRegistrationClient(ctx)
	if err != nil {
		return fmt.Errorf("ServiceDirectory.NewRegistrationClient: %w", err)
	}

	defer client.Close()
	// Delete a Namespace.
	req := &sdpb.DeleteNamespaceRequest{
		Name: fmt.Sprintf("projects/%s/locations/%s/namespaces/%s", projectID, location, namespaceID),
	}
	if err := client.DeleteNamespace(ctx, req); err != nil {
		return fmt.Errorf("DeleteNamespace: %w", err)
	}
	return nil
}

Java

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Java ולהתקין את Service Directory Java SDK.


import com.google.cloud.servicedirectory.v1.NamespaceName;
import com.google.cloud.servicedirectory.v1.RegistrationServiceClient;
import java.io.IOException;

public class NamespacesDelete {

  public static void deleteNamespace() throws IOException {
    // TODO(developer): Replace these variables before running the sample.
    // These variables should refer to an existing Service Directory namespace.
    String projectId = "your-project-id";
    String locationId = "your-region";
    String namespaceId = "your-namespace";
    deleteNamespace(projectId, locationId, namespaceId);
  }

  // Delete a namespace.
  public static void deleteNamespace(String projectId, String locationId, String namespaceId)
      throws IOException {
    // Initialize client that will be used to send requests. This client only needs to be created
    // once, and can be reused for multiple requests. After completing all of your requests, call
    // the "close" method on the client to safely clean up any remaining background resources.
    try (RegistrationServiceClient client = RegistrationServiceClient.create()) {

      // The namespace to delete.
      NamespaceName namespaceName = NamespaceName.of(projectId, locationId, namespaceId);

      // Send the request to delete the namespace.
      client.deleteNamespace(namespaceName);

      // Log the action.
      System.out.println("Deleted Namespace: " + namespaceName.toString());
    }
  }
}

Node.js

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Node.js ולהתקין את Service Directory Node.js SDK.

//
// TODO(developer): Uncomment these variables before running the sample.
//
// const projectId = 'my-project';
// const locationId = 'us-central1';
// const namespaceId = 'my-namespace';

// Imports the Google Cloud client library
const {
  RegistrationServiceClient,
} = require('@google-cloud/service-directory');

// Creates a client
const registrationServiceClient = new RegistrationServiceClient();

// Build the namespace name
const namespaceName = registrationServiceClient.namespacePath(
  projectId,
  locationId,
  namespaceId
);

async function deleteNamespace() {
  await registrationServiceClient.deleteNamespace({
    name: namespaceName,
  });

  console.log(`Deleted namespace: ${namespaceName}`);
}

deleteNamespace();

PHP

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של PHP ולהתקין את Service Directory PHP SDK.

use Google\Cloud\ServiceDirectory\V1\Client\RegistrationServiceClient;
use Google\Cloud\ServiceDirectory\V1\DeleteNamespaceRequest;

/**
 * @param string $projectId     Your Cloud project ID
 * @param string $locationId    Your GCP region
 * @param string $namespaceId   Your namespace name
 */
function delete_namespace(
    string $projectId,
    string $locationId,
    string $namespaceId
): void {
    // Instantiate a client.
    $client = new RegistrationServiceClient();

    // Run request.
    $namespaceName = RegistrationServiceClient::namespaceName($projectId, $locationId, $namespaceId);
    $deleteNamespaceRequest = (new DeleteNamespaceRequest())
        ->setName($namespaceName);
    $client->deleteNamespace($deleteNamespaceRequest);

    // Print results.
    printf('Deleted Namespace: %s' . PHP_EOL, $namespaceName);
}

Python

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח בשפת Python ולהתקין את Service Directory Python SDK.

def delete_namespace(project_id: str, location: str, namespace_id: str) -> bool:
    """Deletes a namespace in the given location.

    Args:
        project_id: Your Google Cloud project id.
        location: The Google Cloud region containing the namespace to delete.
        namespace_id: The id for the namespace to delete.
    """

    client = servicedirectory_v1.RegistrationServiceClient()

    namespace_name = client.namespace_path(project_id, location, namespace_id)

    client.delete_namespace(name=namespace_name)
    print(f"Deleted namespace {namespace_name}.")
    return True

Ruby

כדי להריץ את הקוד הזה, קודם צריך להגדיר סביבת פיתוח של Ruby ולהתקין את Service Directory Ruby SDK.

# project   = "Your Google Cloud project ID"
# location  = "The Google Cloud region containing the namespace"
# namespace = "The name of the namespace"

require "google/cloud/service_directory"

# Initialize the client
registration_service = Google::Cloud::ServiceDirectory.registration_service

# The path of the namespace
namespace_name = registration_service.namespace_path(
  project: project, location: location, namespace: namespace
)

# Use the Service Directory API to delete the namespace
registration_service.delete_namespace name: namespace_name
puts "Deleted namespace: #{namespace_name}"

המאמרים הבאים