SDK Overview
Preparation
To use the SDK provided by Pub/Sub, you must perform the following pre-work.
Issue access key
To obtain a user authentication token (API authentication token), first obtain an access key, then issue an access key ID and a security access key.
You can only access resources belonging to the project for which you have been issued an access key.
-
Issue an access key using your credentials in the KakaoCloud Console > User Profile (Top right) > Access Key tab.
-
After issuing the user access key, refer to the API Preparations to issue an access key ID and a security access key.
SDK requirements
- Java
- Go
To use SDK Java, you must meet the following prerequisites:
-
Check JDK, Java version.
- JDK 1.8 or higher
- Java 8 or higher
-
Add SDK Dependency.
Download jar file$ wget https://objectstorage.kr-central-1.kakaocloud.com/v1/e9130193fc734337b2b0c1da50e44395/pub-sub-java-sdk/v0.1.5/kc-pub-sub-0.1.5.jar
# Then move to the libs folder of the project you want to use.
$ mkdir {your-project}/libs
$ mv kc-pub-sub-0.1.5.jar {your-project}/libsMaven(pom.xml)<repositories>
<repository>
<id>java-pubsub</id>
<url>file://${project.basedir}/libs/kc-pub-sub-0.1.5.jar</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.kakaocloud.pubsub</groupId>
<artifactId>kc-pub-sub</artifactId>
<version>1.0.0</version>
<scope>system</scope>
<systemPath>${project.basedir}/libs/kc-pub-sub-0.1.5.jar</systemPath>
</dependency>
</dependencies>Gradle(build.gradle)dependencies {
implementation files("$rootProject.projectDir/libs/kc-pub-sub-0.1.5.jar")
}infoFor versions prior to kc-pub-sub-0.1.5.jar, the token server needs to be changed as follows.
DefaultCredentialsProvider.newBuilder()
.setTokenServerUri("https://iam.kakaocloud.com")
.build();
PublisherStubSettings settings = PublisherStubSettings.newBuilder()
.setCredentialsProvider(provider)
.build();
SubscriberStubSettings settings = SubscriberStubSettings.newBuilder()
.setCredentialsProvider(provider)
.build(); -
Refer to [Issue Access Key](#Issue Access Key) to obtain a Credentials key. By default, the SDK attempts authentication using the access key. Authentication uses the
CredentialsProvider
type, and by default,credential-id
andcredential-secret
are set inDefaultCredentialsProvider
as follows.Example using Credentials keyDefaultCredentialsProvider provider = DefaultCredentialsProvider.newBuilder()
.setCredentialId("credentialId")
.setCredentialSecret("credentialSecret")
.build()
PublisherStubSettings settings = PublisherStubSettings.newBuilder()
.setCredentialsProvider(provider)
.build();
TopicAdminClient topicAdminClient = TopicAdminClient.create(settings);
To use SDK Go, you must meet the following prerequisites:
-
Download the Go Package (1.19 or later).
Download Go Packagewget https://objectstorage.kr-central-1.kakaocloud.com/v1/e9130193fc734337b2b0c1da50e44395/pub-sub-go-sdk/v0.1.5/pubsub.tgz
tar -xf pubsub.tgz
cd {your-project}
go mod edit -require github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go@v0.1.5
go mod edit -replace github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go@v0.1.5={pubsub-sdk-path}go.mod 파일 결과module ...
require (
github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go v0.1.5
)
replace github.kakaoenterprise.in/cloud-platform/kc-pub-sub-sdk-go v0.1.5 => {pubsub-sdk-path}infoFor versions prior to kc-pub-sub-sdk-go v0.1.5, the token server needs to be changed as follows.
opts := []option.ClientOption{
option.WithTokenServer("https://iam.kakaocloud.com"),
}
client, err := pubsub.NewClient(ctx, domainID, projectID, opts...) -
Refer to [Issue Access Key](#Issue Access Key) to obtain a Credentials key. By default, the SDK attempts authentication using the access key. Authentication uses the
CredentialsProvider
type, and by default,credential-id
andcredential-secret
are set inDefaultCredentialsProvider
as follows.Example using Credentials keyaccessKey := pubsub.AccessKey{CredentialID: "credentialID", CredentialSecret: "credentialSecret"}
opts := []option.ClientOption{
option.WithAccessKey(accessKey),
}
client, err := pubsub.NewClient(ctx, domainID, projectID, opts...)
You can only access resources that belong to the project for which you have been issued an access key.
Therefore, when calling the API, the domain and project must be set to information that includes the issued access key.
Client
This is an object used to manipulate topics and subscriptions and to send and receive messages.
The Client object must release resources such as threads, so the close()
method must be called.
Custom settings
When creating a Client object, the user can arbitrarily set values such as authentication and call endpoint.
- Java
- Go
PublisherStubSettings settings =
PublisherStubSettings.newBuilder()
.setEndpoint("{endpoint-url}:10443")
.build();
TopicAdminClient topicAdminClient = TopicAdminClient.create(settings);
SubscriberStubSettings settings =
SubscriberStubSettings.newBuilder()
.setEndpoint("{endpoint-url}:10443")
.build();
SubscriptionAdminClient subscriptionAdminClient = SubscriptionAdminClient.create(settings);
opts := []option.ClientOption{
option.WithEndpoint("{endpoint-url}:10443"),
}
client, err := pubsub.NewClient(ctx, domainID, projectID, opts...)