增加 mongodb 整合用例
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
package com.heibaiying.bean;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description :
|
||||
*/
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class Programmer {
|
||||
|
||||
private String name;
|
||||
|
||||
private int age;
|
||||
|
||||
private float salary;
|
||||
|
||||
private Date birthday;
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import com.mongodb.MongoClient;
|
||||
import com.mongodb.MongoClientOptions;
|
||||
import com.mongodb.ServerAddress;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.mongodb.MongoDbFactory;
|
||||
import org.springframework.data.mongodb.core.MongoTemplate;
|
||||
import org.springframework.data.mongodb.core.SimpleMongoDbFactory;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Mongo 配置类
|
||||
*/
|
||||
|
||||
@Configuration
|
||||
@ComponentScan(value = "com.heibaiying.*")
|
||||
public class MongoConfig {
|
||||
|
||||
@Bean
|
||||
public MongoDbFactory mongoDbFactory(MongoProperty mongo) {
|
||||
MongoClientOptions options = MongoClientOptions.builder()
|
||||
.threadsAllowedToBlockForConnectionMultiplier(mongo.getMultiplier())
|
||||
.connectionsPerHost(mongo.getConnectionsPerHost())
|
||||
.connectTimeout(mongo.getConnectTimeout())
|
||||
.maxWaitTime(mongo.getMaxWaitTime())
|
||||
.socketTimeout(mongo.getSocketTimeout())
|
||||
.build();
|
||||
MongoClient client = new MongoClient(new ServerAddress(mongo.getHost(), mongo.getPort()), options);
|
||||
return new SimpleMongoDbFactory(client, mongo.getDbname());
|
||||
}
|
||||
|
||||
@Bean
|
||||
public MongoTemplate mongoTemplate(MongoDbFactory mongoDbFactory) {
|
||||
return new MongoTemplate(mongoDbFactory);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package com.heibaiying.config;
|
||||
|
||||
import lombok.Data;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
/**
|
||||
* @author : heibaiying
|
||||
* @description : Mongo 配置属性
|
||||
*/
|
||||
@Data
|
||||
@Configuration
|
||||
@PropertySource(value = "classpath:mongodb.properties")
|
||||
public class MongoProperty {
|
||||
|
||||
@Value("${mongo.host}")
|
||||
private String host;
|
||||
@Value("${mongo.port}")
|
||||
private int port;
|
||||
@Value("${mongo.dbname}")
|
||||
private String dbname;
|
||||
@Value("${mongo.connectionsPerHost}")
|
||||
private int connectionsPerHost;
|
||||
@Value("${mongo.threadsAllowedToBlockForConnectionMultiplier}")
|
||||
private int multiplier;
|
||||
@Value("${mongo.connectTimeout}")
|
||||
private int connectTimeout;
|
||||
@Value("${mongo.maxWaitTime}")
|
||||
private int maxWaitTime;
|
||||
@Value("${mongo.socketKeepAlive}")
|
||||
private boolean socketKeepAlive;
|
||||
@Value("${mongo.socketTimeout}")
|
||||
private int socketTimeout;
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
mongo.host=192.168.200.228
|
||||
mongo.port=27017
|
||||
# 数据库名称. 默认是'db'.
|
||||
mongo.dbname=database
|
||||
# 每个主机允许的连接数
|
||||
mongo.connectionsPerHost=10
|
||||
# 线程队列数,它和上面connectionsPerHost值相乘的结果就是线程队列最大值。如果连接线程排满了队列就会抛出异常
|
||||
mongo.threadsAllowedToBlockForConnectionMultiplier=5
|
||||
# 连接超时的毫秒 0是默认值且无限大。
|
||||
mongo.connectTimeout=1000
|
||||
# 最大等待连接的线程阻塞时间 默认是120000 ms (2 minutes).
|
||||
mongo.maxWaitTime=1500
|
||||
# 保持活动标志,控制是否有套接字保持活动超时 官方默认为true 且不建议禁用
|
||||
mongo.socketKeepAlive=true
|
||||
# 用于群集心跳的连接的套接字超时。
|
||||
mongo.socketTimeout=1500
|
||||
|
||||
Reference in New Issue
Block a user