반응형
안녕하세요
코딩하는 헬린이입니다.
간만에 글을 작성하네요 ~ 아침에 일어나서 헬스도하고 회사도 갈려하니 힘이드네요..
이번에는 소스 공유좀 드릴려고 합니다.
회사에서 몽고db를 쓰는데 임베디드 몽고에 레플리카 설정을 하기위한 소스 공유하려 합니다.
@Profile("local")
@Configuration(proxyBeanMethods = false)
@AutoConfigureBefore({ MongoAutoConfiguration.class })
@ConditionalOnClass({ MongoClient.class, MongodStarter.class })
@Import({
EmbeddedMongoAutoConfiguration.class,
EmbeddedMongoReplisetConfig.DependenciesConfiguration.class
})
public class EmbeddedMongoReplisetConfig {
public static final int DFLT_PORT_NUMBER = 11046;
public static final String DFLT_REPLICASET_NAME = "rs0";
public static final int DFLT_STOP_TIMEOUT_MILLIS = 3000;
private Version.Main mFeatureAwareVersion = Version.Main.V4_0;
private int mPortNumber = DFLT_PORT_NUMBER;
private String mReplicaSetName = DFLT_REPLICASET_NAME;
private long mStopTimeoutMillis = DFLT_STOP_TIMEOUT_MILLIS;
@Bean
public IMongodConfig mongodConfig() throws UnknownHostException, IOException {
final IMongodConfig mongodConfig = new MongodConfigBuilder().version(mFeatureAwareVersion)
.withLaunchArgument("--replSet", mReplicaSetName)
.stopTimeoutInMillis(mStopTimeoutMillis)
.cmdOptions(new MongoCmdOptionsBuilder().useNoJournal(false).build())
.net(new Net(mPortNumber, Network.localhostIsIPv6())).build();
return mongodConfig;
}
/**
* Initializes a new replica set.
* Based on code from https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/257
*/
class EmbeddedMongoReplicaSetInitialization {
EmbeddedMongoReplicaSetInitialization() throws Exception {
MongoClient mongoClient = null;
try {
final BasicDBList members = new BasicDBList();
members.add(new Document("_id", 0).append("host", "localhost:" + mPortNumber));
final Document replSetConfig = new Document("_id", mReplicaSetName);
replSetConfig.put("members", members);
mongoClient = MongoClients.create("mongodb://localhost:"+DFLT_PORT_NUMBER);
final MongoDatabase adminDatabase = mongoClient.getDatabase("admin");
adminDatabase.runCommand(new Document("replSetInitiate", replSetConfig));
}
finally {
if (mongoClient != null) {
mongoClient.close();
}
}
}
}
@Bean
EmbeddedMongoReplicaSetInitialization embeddedMongoReplicaSetInitialization() throws Exception {
return new EmbeddedMongoReplicaSetInitialization();
}
/**
* Additional configuration to ensure that the replica set initialization happens after the
* {@link MongodExecutable} bean is created. That's it - after the database is started.
*/
@ConditionalOnClass({ MongoClient.class, MongodStarter.class })
protected static class DependenciesConfiguration
extends AbstractDependsOnBeanFactoryPostProcessor {
DependenciesConfiguration() {
super(EmbeddedMongoReplicaSetInitialization.class, null, MongodExecutable.class);
}
}
}
궁금하신거 있으시다면 댓글 남겨주세요 ~
ref : https://github.com/flapdoodle-oss/de.flapdoodle.embed.mongo/issues/257
반응형
'개발 > Spring' 카테고리의 다른 글
[Spring] apple client_secret (JWT) 생성 (0) | 2022.09.01 |
---|---|
[Spring] poi 병목현상 해결! (0) | 2020.11.12 |
[Spring] poi를 이용한 excel 다운로드 (0) | 2020.11.06 |
[Spring] 파일 정보를 못가져오는 에러! MultipartFile에 Xss Filter적용 (0) | 2020.10.30 |
[Spring] 스프링부트 시큐리티를 이용한 로그인! (0) | 2020.10.19 |