Spring Mongo配置多个Mongos

由于数据存储使用MongoDB集群,在对外访问的时候,地址是Mongos的地址,在使用的过程中没有发现任何问题,配置如下:

<mongo:mongo  host="${mongodb.hostname}" port="${mongodb.port}">
		<mongo:options connections-per-host="${mongodb.port}"
			threads-allowed-to-block-for-connection-multiplier="${mongodb.threads-allowed-to-block-for-connection-multiplier}"
			connect-timeout="${mongodb.connect-timeout}" max-wait-time="${mongodb.max-wait-time}" auto-connect-retry="${mongodb.auto-connect-retry}"
			socket-keep-alive="${mongodb.socket-keep-alive}" socket-timeout="${mongodb.socket-timeout}" slave-ok="${mongodb.slave-ok}"
			write-number="${mongodb.write-number}" write-timeout="${mongodb.write-timeout}" write-fsync="${mongodb.write-fsync}" />
	</mongo:mongo>

 但是,经过测试几轮性能测试以后,发现在大并发的时候Mongos机器负载过高,而其他存储Mongod机器负载很小,于是让我们解决该问题。

经过几番分析,原来由以下原因:

1、Mongos、config server、mongod三个进程都部署在一台机子。

2、没有考虑使用多个Mongos来均摊外部请求。

于是,另外部署几个Mongos,使用同一个配置库,问题解决,具体配置如下:

<mongo:mongo  id="mongo"  replica-set="${mongodb.replica-set}">
		<mongo:options connections-per-host="${mongodb.port}"
			threads-allowed-to-block-for-connection-multiplier="${mongodb.threads-allowed-to-block-for-connection-multiplier}"
			connect-timeout="${mongodb.connect-timeout}" max-wait-time="${mongodb.max-wait-time}" auto-connect-retry="${mongodb.auto-connect-retry}"
			socket-keep-alive="${mongodb.socket-keep-alive}" socket-timeout="${mongodb.socket-timeout}" slave-ok="${mongodb.slave-ok}"
			write-number="${mongodb.write-number}" write-timeout="${mongodb.write-timeout}" write-fsync="${mongodb.write-fsync}" />
	</mongo:mongo>

 其中,replica-set格式:ip1:port,ip2:port,...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot 中使用 MongoDB 进行多表关联查询可以使用 `MongoTemplate` 类和 `org.springframework.data.mongodb.core.aggregation.Aggregation` 类来实现。 以下是一个简单的例子,假设我们有两个集合:`users` 和 `orders`,其中 `users` 集合包含了用户的基本信息,而 `orders` 集合包含了用户的订单信息,两个集合通过 `userId` 字段关联: ```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.aggregation.Aggregation; import org.springframework.data.mongodb.core.aggregation.LookupOperation; import org.springframework.data.mongodb.core.aggregation.TypedAggregation; import org.springframework.stereotype.Service; @Service public class UserService { @Autowired private MongoTemplate mongoTemplate; public List<User> findUserWithOrders() { // 关联查询 orders 集合 LookupOperation lookupOperation = LookupOperation.newLookup() .from("orders") .localField("_id") .foreignField("userId") .as("orders"); // 定义聚合查询 TypedAggregation<User> aggregation = Aggregation .newAggregation(User.class, lookupOperation) .withOptions(Aggregation.newAggregationOptions().allowDiskUse(true).build()); // 执行聚合查询 AggregationResults<User> results = mongoTemplate.aggregate(aggregation, User.class); return results.getMappedResults(); } } ``` 上述代码中,我们定义了一个 `UserService` 类,其中注入了一个 `MongoTemplate` 对象,然后定义了一个 `findUserWithOrders` 方法用于查询用户及其订单信息。 在 `findUserWithOrders` 方法中,我们首先创建了一个 `LookupOperation` 对象来进行关联查询,然后定义了一个 `TypedAggregation` 对象来表示聚合查询,最后利用 `MongoTemplate` 的 `aggregate` 方法执行聚合查询,并返回结果。 需要注意的是,聚合查询的结果类型需要与 `TypedAggregation` 中的泛型类型保持一致。 更多关于 Spring Data MongoDB 的聚合查询可以参考官方文档:https://docs.spring.io/spring-data/mongodb/docs/current/reference/html/#mongo.aggregation

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值