公司的技术栈是SpringCloud+ZoomKeeper+RabbitMq+JPA+MongoDB,利用zk去做配置文件,rabbitmq做一些异步处理,持久层采用JPA(数据库的Mysql),对于大部分都是查询的业务,数据是存在MongoDB中。
MongoDB是一个高性能、开源、无模式的文档型数据库,是当前NoSql数据库中比较热门的一种。 适合对大量或者无固定格式的数据进行存储,比如:日志、缓存等。对事物支持较弱,不适用复杂的多文档(多表)的级联查询。
MongoDB的适用场景:
在应用服务器的日志记录
存储一些监控数据
应用不需要事务及复杂 join 支持
应用需要2000-3000以上的读写QPS
应用需要TB甚至 PB 级别数据存储
应用发展迅速,需要能快速水平扩展
应用要求存储的数据不丢失
应用需要99.999%高可用
应用需要大量的地理位置查询、文本查询
下面就讲下SpringBoot利用MongoTemplate操作mongdb
1. MongDB的安装 安装MongDB可以看官网安装
英语不好的小伙伴可以看这个安装
我是在centos7.0上安装的MongDB4.2,修改配置文件,加入用户和密码,下面的linux详细命令:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 [root@VM-0-15-centos ~]# docker pull mongo:4.2.6 Trying to pull repository docker.io/library/mongo ... 4.2.6: Pulling from docker.io/library/mongo 23884877105a: Pull complete bc38caa0f5b9: Pull complete 2910811b6c42: Pull complete 36505266dcc6: Pull complete a4d269900d94: Pull complete 5e2526abb80a: Pull complete d3eece1f39ec: Pull complete 358ed78d3204: Pull complete 1a878b8604ae: Pull complete dde03a2883d0: Pull complete 4ffe534daa34: Pull complete f164ba21e17c: Pull complete 6494c387442c: Pull complete Digest: sha256:c880f6b56f443bb4d01baa759883228cd84fa8d78fa1a36001d1c0a0712b5a07 Status: Downloaded newer image for docker.io/mongo:4.2.6 [root@VM-0-15-centos ~]# docker images REPOSITORY TAG IMAGE ID CREATED SIZE docker.io/mysql 5.7 2c9028880e58 9 days ago 447 MB docker.io/tomcat 9.0.45-jdk8-adoptopenjdk-hotspot bcf990bea01a 10 days ago 341 MB docker.io/mongo 4.2.6 3f3daf863757 13 months ago 388 MB docker.io/rabbitmq 3.7.15-management f05c3eb3cf91 22 months ago 179 MB [root@VM-0-15-centos ~]# docker run -td --name mongo -p 27017:27017 3f3daf863757 --auth 44d090fd4314773ac4362298eadf5928feda9c347b50dd0ff17c40c32cb00128 [root@VM-0-15-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44d090fd4314 3f3daf863757 "docker-entrypoint..." 4 seconds ago Up 3 seconds 0.0.0.0:27017->27017/tcp [root@VM-0-15-centos ~]# docker exec -it mongo mongo admin MongoDB shell version v4.2.6 connecting to: mongodb://127.0.0.1:27017/admin?compressors=disabled&gssapiServiceName=mongodb Implicit session: session { "id" : UUID("cb9f0916-53bc-4781-aa95-4409477eb89b") } MongoDB server version: 4.2.6 Welcome to the MongoDB shell. For interactive help, type "help". For more comprehensive documentation, see http://docs.mongodb.org/ Questions? Try the support group http://groups.google.com/group/mongodb-user > db.createUser({ user:'admin' ,pwd :'123456' ,roles:[ { role:'userAdminAnyDatabase' , db: 'admin' },"readWriteAnyDatabase" ]}); Successfully added user: { "user" : "admin", "roles" : [ { "role" : "userAdminAnyDatabase", "db" : "admin" }, "readWriteAnyDatabase" ] } > db.auth('admin' , '123456' ) 1 > ^C bye [root@VM-0-15-centos ~]# docker restart 44d090fd4314 44d090fd4314 [root@VM-0-15-centos ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS NAMES 44d090fd4314 3f3daf863757 "docker-entrypoint..." 47 minutes ago Up 3 seconds 0.0.0.0:27017->27017/tcp
2. 添加Maven依赖 只需要加入web启动器和mongdb依赖即可
1 2 3 4 5 6 7 8 9 <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-web</artifactId > </dependency > <dependency > <groupId > org.springframework.boot</groupId > <artifactId > spring-boot-starter-data-mongodb</artifactId > </dependency >
3. 添加配置文件 我们提前在MongoDB中创建一个test库,并在下面的配置信息中加入刚刚配置的用户名和密码,需要注意的是密码需要用单引号
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 spring: data: mongodb: host: 127.0 .0 .1 port: 27017 database: test option: max-connection-idle-time: 1500 max-connection-per-host: 200 max-wait-time: 60000 max-connection-life-time: 0 connect-timeout: 10000 socket-timeout: 60000 password: '123456' username: admin authentication-database: admin
4. 编写实体类 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 @Data @Document(collation = "user") public class User { @Id private Integer id; private Integer age; private String name; private String role; public User (Integer id, Integer age, String name, String role) { this .id = id; this .age = age; this .name = name; this .role = role; } }
5. 编写DAO层 SpringBoot已经为我们高度封装了MongoTemplate,类似RedisTemplate
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 public class MongoTestDao { @Autowired private MongoTemplate mongoTemplate; public void save (User user) { mongoTemplate.save(user); } public User findUserById (Integer id) { Query query = new Query(Criteria.where("name" ).is(id)); User user = mongoTemplate.findOne(query, User.class); return user; } public void updateUserById (User user) { Query query = new Query(Criteria.where("id" ).is(user.getId())); Update update = new Update().set("age" , user.getAge()).set("name" , user.getName()); mongoTemplate.updateFirst(query,update,User.class); } public void deleteUserById (Integer id) { Query query = new Query(Criteria.where("id" ).is(id)); mongoTemplate.remove(query,User.class); } }
6. 测试用例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 @SpringBootTest class SpringbootMongodbApplicationTests { @Autowired private MongoTestDao mongoTestDao; @Test public void addUser () { for (int i = 0 ; i < 100 ; i++) { mongoTestDao.save(new User(i,i,String.valueOf(i+1 ),String.valueOf(i+100 ))); } System.out.println("#####插入完毕!######" ); } @Test public void updateUser () { for (int i = 0 ; i < 100 ; i++) { mongoTestDao.updateUserById(new User(i,i-1000 ,"A" ,"a" )); } System.out.println("#####修改完毕!######" ); } @Test public void deleteUser () { for (int i = 0 ; i < 100 ; i++) { mongoTestDao.deleteUserById(i); } System.out.println("#####修改完毕!######" ); } @Test public void findUserById () { for (int i = 0 ; i < 100 ; i++) { User user = mongoTestDao.findUserById(i); System.out.println(i+"-----------" +user); } System.out.println("#####查询完毕!######" ); } }
测试通过,放一张用MongDB Compass连接MongDB的图
7. GitHub代码 springboot-mongodb