Join us Sept 17 at .local NYC! Use code WEB50 to save 50% on tickets. Learn more >
MongoDB Event
Docs Menu
Docs Home
/ / /
Kotlin Sync 드라이버
/ /

MongoDB 서버 선택 사용자 지정

모든 MongoDB 드라이버는 읽거나 쓰기 (write) 서버 선택할 때 정의된 알고리즘 따릅니다. MongoClientSettings 객체 의 ClusterSettings 속성 사용하면 이 알고리즘 사용자 지정하여 애플리케이션 에 가장 적합한 서버 선택할 수 있습니다.

중요

서버 선택 알고리즘 사용자 지정하면 읽기 또는 쓰기 (write) 성능이 저하되는 등 의도하지 않은 결과가 발생할 수 있습니다.

코틀린 동기 (Kotlin Sync) 운전자 읽기 작업을 실행할 때 MongoDB deployment 선택하기 위해 다음 단계를 순서대로 수행합니다.

  1. 운전자 사용할 수 없거나 문제가 있다고 판단하는 서버를 제외하고 요청된 작업에 적합한 알려진 서버 목록에서 모든 서버를 선택합니다.

    1. 읽기의 경우 활성 읽기 설정 (read preference) 일치하는 모든 서버를 선택합니다.

    2. 쓰기의 경우 쓰기 가능한 모든 서버를 선택합니다.

  2. 사용자가 제공하는 경우 사용자 정의 서버 선택기 함수를 호출하고 이전 단계의 목록을 전달합니다.

  3. 함수에서 반환된 서버 목록에 localThreshold 연결 설정을 적용합니다.

  4. 이전 기준과 일치하는 서버 목록에서 최대 2개의 무작위 서버를 선택한 다음, 처리되지 않은 동시 작업이 더 적은 서버 선택합니다.

코틀린 동기 (Kotlin Sync) 운전자 쓰기 (write) 작업을 실행할 때 활성 읽기 설정 (read preference) 일치하는 서버뿐만 아니라 알려진 서버 목록에서 모든 쓰기 가능한 서버를 선택하는 것으로 시작합니다. 나머지 단계는 앞의 목록과 동일합니다.

MongoDB 서버 선택 알고리즘에 대해 자세히 학습하려면 MongoDB Server 매뉴얼에서 서버 선택 알고리즘 을 참조하세요.

ServerSelector 인터페이스를 구현하는 클래스를 만들어 자체 사용자 지정 서버 선택 로직을 구현 수 있습니다. 다음 예시 type 값이 ServerType.REPLICA_SET_SECONDARY인 서버를 선택하는 간단한 사용자 지정 서버 선택 클래스를 보여 줍니다.

class CustomServerSelector : ServerSelector {
override fun select(cluster: ClusterDescription): List<ServerDescription> {
return cluster.serverDescriptions.filter { it.type == ServerType.REPLICA_SET_SECONDARY }
}
}

applyToClusterSettings() 메서드를 사용하여 이 클래스의 인스턴스 MongoClientSettings에 전달합니다. 다음 예시 이전 예시 의 사용자 지정 서버 선택기 클래스의 인스턴스 를 사용하여 MongoClient 를 만드는 방법을 보여 줍니다.

val settings = MongoClientSettings.builder()
.applyConnectionString(ConnectionString("<connection URI>"))
.applyToClusterSettings { builder ->
builder.serverSelector(CustomServerSelector())
}
.build()
val mongoClient = MongoClient.create(settings)

MongoClient 객체 또는 연결 URI에서 다음과 같은 서버 선택 설정을 지정할 수 있습니다.

설정
설명

localThreshold

The latency window for server eligibility. If a server's round trip takes longer
than the fastest server's round-trip time plus this value, the server isn't
eligible for selection. You can specify this setting to a ClusterSettings object in addition to the connection URI.

Data Type: Integer
Default: 15 milliseconds
Connection URI Example: localThresholdMS=0

readPreference

The client's default read-preference settings. For more information on read preference options, see Read Preference in the MongoDB Server manual. You can specify this setting to a MongoClientSettings object in addition to the connection URI.

Data Type: ReadPreference
Default: ReadPreference.primary()
Connection URI Example:
readPreference=primaryPreferred
&maxStalenessSeconds=90
&readPreferenceTags=dc:ny,rack:1

serverSelectionTimeout

The length of time the driver tries to select a server before timing out. You can specify this setting to a ClusterSettings object in addition to the connection URI.

Data Type: Long
Default: 30 seconds
Connection URI Example: serverSelectionTimeoutMS=15000

이 가이드 에 사용된 클래스 및 메서드에 대해 자세히 학습 다음 API 설명서를 참조하세요.

돌아가기

네트워크 압축

이 페이지의 내용