mongodb复制集连接Java,Redis sentinel 连接配置,MongoDB 复制集连接配置 java Srping

Redis

>

>

lazy-init="true">

MongoDB

public class DBUtil {

private MongoClient mongoClient;

private MongoDatabase database;

private String host;

private String dbName;

private String username;

private String password;

private String replSetName;

public String getHost() {

return this.host;

}

public void setHost(String host) {

this.host = host;

}

public String getDbName() {

return this.dbName;

}

public void setDbName(String dbName) {

this.dbName = dbName;

}

public void setUsername(String username) {

this.username = username;

}

public void setPassword(String password) {

this.password = password;

}

@SuppressWarnings({ "unchecked", "rawtypes" })

public void init() throws Exception {

String[] hostArr = this.host.split(",");

List serverList = new ArrayList<>();

for (String h : hostArr) {

serverList.add(new ServerAddress(h));

}

Object credentialsList = null;

if ((this.username != null) && (this.username.trim().length() > 0) && (this.password != null)

&& (this.password.trim().length() > 0)) {

credentialsList = new ArrayList<>();

((List) credentialsList).add(MongoCredential.createCredential(this.username, this.dbName, this.password.toCharArray()));

}

//this.mongoClient = new MongoClient(serverList, (List) credentialsList);

MongoClientOptions options = MongoClientOptions.builder()

.requiredReplicaSetName(replSetName)

.readPreference(ReadPreference.secondaryPreferred())

.build();

this.mongoClient = new MongoClient(serverList, options);

this.database = this.mongoClient.getDatabase(this.dbName);

}

public void destroy() {

if (this.mongoClient != null) {

this.mongoClient.close();

}

}

public MongoCollection getColl(String name) {

return this.database.getCollection(name);

}

public int nextValue(String counterName) {

Document filter = new Document("name", counterName);

Document fields = new Document("value", Boolean.valueOf(true));

Document update = new Document("$inc", new Document("value", Integer.valueOf(1)));

FindOneAndUpdateOptions options = new FindOneAndUpdateOptions().projection(fields).upsert(true);

Document newObj = (Document) this.database.getCollection("counter").findOneAndUpdate(filter, update, options);

return newObj.getInteger("value").intValue();

}

}

mongodb.replSetName= mongodb.dbName= mongodb.userName= mongodb.password=

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值