mongodb副本集与spring的整合
程序员文章站
2024-03-20 11:20:34
...
1.先吧mongdb压缩包放到/usr/local 目录下
2.
解压命令
tar zxvf
解压三次
3.
分别在3个mongodb文件夹下创建文件夹mongodata
4.
/usr/local/mongodb1/bin/mongod --dbpath /usr/local/mongodb1/mongodata --port 27017 --replSet repl
/usr/local/mongodb2/bin/mongod --dbpath /usr/local/mongodb2/mongodata --port 27018 --replSet repl
/usr/local/mongodb3/bin/mongod --dbpath /usr/local/mongodb3/mongodata --port 27019 --replSet repl
5.创建副本集,任意启动一个mongodb之后,复制下代码
config = { _id:"repl", members:[{_id:0,host:"192.168.126.153:27017"},{_id:1,host:"192.168.126.153:27018"},
{_id:2,host:"192.168.126.153:27019",arbiterOnly:true}]}
[aaa@qq.com ~]# cd ../
[aaa@qq.com /]# cd usr
[aaa@qq.com usr]# cd local
[aaa@qq.com local]# ll
total 37440
drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin
drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc
drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
drwxr-xr-x. 2 root root 4096 Sep 23 2011 include
drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib
drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec
drwxr-xr-x. 4 root root 4096 May 10 02:26 mongodb1
drwxr-xr-x. 4 root root 4096 May 10 02:27 mongodb2
drwxr-xr-x. 4 root root 4096 May 10 02:27 mongodb3
-rw-r--r--. 1 root root 38288414 May 10 02:24 mongodb-linux-i686-3.0.5.tgz
drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin
drwxr-xr-x. 5 root root 4096 May 10 2019 share
drwxr-xr-x. 2 root root 4096 Sep 23 2011 src
[aaa@qq.com local]# cd mongodb1
[aaa@qq.com mongodb1]# cd bin
[aaa@qq.com bin]# ./mongo
MongoDB shell version: 3.0.5
connecting to: test
Server has startup warnings:
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** WARNING: You are running this process as the root user, which is not recommended.
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten]
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten]
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** NOTE: This is a 32 bit MongoDB binary.
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** 32 bit builds are limited to less than 2GB of data (or less with --journal).
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** Note that journaling defaults to off for 32 bit and is currently off.
2019-05-10T04:40:49.800-0700 I CONTROL [initandlisten] ** See http://dochub.mongodb.org/core/32bit
2019-05-10T04:40:49.801-0700 I CONTROL [initandlisten]
> config = { _id:"repl", members:[{_id:0,host:"192.168.126.159:27017"},{_id:1,host:"192.168.126.159:27018"},
... {_id:2,host:"192.168.126.159:27019",arbiterOnly:true}]}
{
"_id" : "repl",
"members" : [
{
"_id" : 0,
"host" : "192.168.126.159:27017"
},
{
"_id" : 1,
"host" : "192.168.126.159:27018"
},
{
"_id" : 2,
"host" : "192.168.126.159:27019",
"arbiterOnly" : true
}
]
}
>
> rs.initiate(config)
{ "ok" : 1 }
repl:OTHER> rs.status()
{
"set" : "repl",
"date" : ISODate("2019-05-10T12:11:10.401Z"),
"myState" : 1,
"members" : [
{
"_id" : 0,
"name" : "192.168.126.159:27017",
"health" : 1,
"state" : 1,
"stateStr" : "PRIMARY",
"uptime" : 1821,
"optime" : Timestamp(1557489733, 2),
"optimeDate" : ISODate("2019-05-10T12:02:13Z"),
"electionTime" : Timestamp(1557488573, 1),
"electionDate" : ISODate("2019-05-10T11:42:53Z"),
"configVersion" : 1,
"self" : true
},
{
"_id" : 1,
"name" : "192.168.126.159:27018",
"health" : 1,
"state" : 2,
"stateStr" : "SECONDARY",
"uptime" : 1698,
"optime" : Timestamp(1557489733, 2),
"optimeDate" : ISODate("2019-05-10T12:02:13Z"),
"lastHeartbeat" : ISODate("2019-05-10T12:11:09.335Z"),
"lastHeartbeatRecv" : ISODate("2019-05-10T12:11:09.374Z"),
"pingMs" : 0,
"syncingTo" : "192.168.126.159:27017",
"configVersion" : 1
},
{
"_id" : 2,
"name" : "192.168.126.159:27019",
"health" : 1,
"state" : 7,
"stateStr" : "ARBITER",
"uptime" : 1698,
"lastHeartbeat" : ISODate("2019-05-10T12:11:09.334Z"),
"lastHeartbeatRecv" : ISODate("2019-05-10T12:11:09.391Z"),
"pingMs" : 0,
"configVersion" : 1
}
],
"ok" : 1
}
6.初始化
rs.initiate(config)
7
与idea整合
spring项目
1.导jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
2.application.yml中配置
# MongoDB URI配置 重要,添加了用户名和密码验证
spring:
data:
mongodb:
uri: mongodb://192.168.126.153:27017,192.168.126.153:27018,192.168.126.153:27019/testu?slaveOk=true&replicaSet=repl&write=1&readPreference=secondaryPreferred&connectTimeoutMS=300000
#每个主机的连接数
connections-per-host: 50
#线程队列数,它以上面connectionsPerHost值相乘的结果就是线程队列最大值
threads-allowed-to-block-for-connection-multiplier: 50
connect-timeout: 5000
socket-timeout: 3000
max-wait-time: 1500
#控制是否在一个连接时,系统会自动重试
auto-connect-retry: true
socket-keep-alive: true
3.在controler中
package com.jk.controller;
import com.jk.model.user;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.List;
/**
* @author :mmzs
* @date :Created in 2019/5/7 21:18
* @description:
* @modified By:
* @version: $
*/
@Controller
public class usecontroller {
@Autowired
private MongoTemplate mongoTemplate;
@RequestMapping("finduse")
@ResponseBody
public int finduse() {
List<user> users = mongoTemplate.find(new Query(), user.class);
System.out.println(users);
return 1;
}
@RequestMapping("addUser")
@ResponseBody
public int addUser() {
user user = new user();
user.setId(1);
user.setName("222");
mongoTemplate.save(user);
return 2;
}
}
model层
package com.jk.model;
import java.io.Serializable;
/**
* @author :mmzs
* @date :Created in 2019/5/7 21:17
* @description:
* @modified By:
* @version: $
*/
public class user implements Serializable {
private static final long serialVersionUID = 9147550436076328527L;
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
最后访问
http://127.0.0.1:8080/addUser
http://127.0.0.1:8080/finduse
上一篇: 第一周作业