Go 클라이언트 라이브러리를 사용하여 Spanner Omni에 연결

Spanner Omni와 Spanner는 Go 클라이언트 라이브러리를 유사하게 사용합니다. 이 문서에서는 Go 클라이언트 라이브러리를 구성하여 Spanner Omni에 보안 연결을 설정하는 방법을 보여줍니다. 데이터베이스 관리 클라이언트 또는 데이터베이스 클라이언트를 만들 때 spanner.ClientConfig을 구성하여 이러한 연결을 설정합니다.

Go 클라이언트 라이브러리는 일반 텍스트, TLS, 사용자 인증 정보가 있는 TLS, mTLS 연결을 지원합니다.

자세한 내용은 Spanner 문서의 Go에서 Spanner 시작하기를 참고하세요.

시작하기 전에

Spanner Omni와 함께 Go 클라이언트 라이브러리를 사용하려면 Go 클라이언트 라이브러리 버전 v1.94.0 이상 및 Go 출시 버전 1.25 이상을 사용하세요.

go.mod 파일에 Spanner Go 모듈을 추가하려면 다음 명령어를 실행합니다.

go get cloud.google.com/go/spanner@v1.94.0

ClientConfig 객체 구성

Go 클라이언트 라이브러리를 사용하여 Client 또는 DatabaseAdminClient을 만들려면 Type: spanner.OMNI를 지정하여 ClientConfig 객체를 구성하고 option.WithEndpoint()를 사용하여 엔드포인트를 제공합니다.

다음 예에서는 지원되는 각 보안 구성에 대해 ClientConfig 객체를 구성하는 방법을 보여줍니다.

일반 텍스트

일반 텍스트 연결을 설정하려면 spanner.ClientConfig에서 UsePlainTexttrue로 설정합니다.

clientConfig := spanner.ClientConfig{
  Type:         spanner.OMNI,
  UsePlainText: true,
}

adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer adminClient.Close()

databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer databaseClient.Close()

TLS

TLS 연결을 설정하려면 CaCertificateFile를 사용하여 CA 인증서의 경로를 지정하세요.

clientConfig := spanner.ClientConfig{
  Type:              spanner.OMNI,
  CaCertificateFile: "PATH_TO_CA_CERT",
}

adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer adminClient.Close()

databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer databaseClient.Close()

사용자 인증 정보가 있는 TLS

사용자 이름 및 비밀번호 인증으로 TLS 연결을 설정하려면 CaCertificateFile, Username, Password를 지정합니다.

clientConfig := spanner.ClientConfig{
  Type:              spanner.OMNI,
  CaCertificateFile: "PATH_TO_CA_CERT",
  Username:          "USERNAME",
  Password:          []byte("PASSWORD"),
}

adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer adminClient.Close()

databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer databaseClient.Close()

mTLS

상호 TLS (mTLS) 연결을 설정하려면 CaCertificateFile, ClientCertificateFile, ClientKeyFile를 지정하세요.

clientConfig := spanner.ClientConfig{
  Type:                  spanner.OMNI,
  CaCertificateFile:     "PATH_TO_CA_CERT",
  ClientCertificateFile: "PATH_TO_CLIENT_CERT",
  ClientKeyFile:         "PATH_TO_CLIENT_KEY",
}

adminClient, err := database.NewDatabaseAdminClientWithConfig(ctx, clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer adminClient.Close()

databaseClient, err := spanner.NewClientWithConfig(ctx, "DATABASE_NAME", clientConfig,
  option.WithEndpoint("ENDPOINT"),
)
if err != nil {
  // Handle error.
}
defer databaseClient.Close()

다음을 바꿉니다.

  • PATH_TO_CA_CERT: CA 인증서 파일의 경로입니다.

  • PATH_TO_CLIENT_CERT: 클라이언트 인증서 파일의 경로입니다.

  • PATH_TO_CLIENT_KEY: 클라이언트 키 파일의 경로