mongo微服务搭建

Posted 5sdba-notes

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mongo微服务搭建相关的知识,希望对你有一定的参考价值。

(1)写了个yaml配置文件,可以再根据mongo参数增添,我的都没配置什么。

 [root@dev01 mongo]# cat mongo_cluster.yaml 

version: 3
services:
  node1:
    container_name: node1
    image: mongo
    networks:
      mongo:
        ipv4_address: 172.20.0.11
    ports:
      - 27018:27018
    volumes:
      - ./node1/data/db:/data/db
      - ./node1/data/configdb:/data/configdb
      - ./node1/data/backup:/data/backup
    command: --shardsvr --bind_ip_all
    restart: always
    depends_on:
      - config1
      - config2
      - config3
  node2:
    container_name: node2
    networks:
      mongo:
        ipv4_address: 172.20.0.12
    image: mongo
    ports:
      - 27028:27018
    volumes:
      - ./node2/data/db:/data/db
      - ./node2/data/configdb:/data/configdb
      - ./node2/data/backup:/data/backup
    command: --shardsvr --bind_ip_all
    restart: always
    depends_on:
      - config1
      - config2
      - config3
  node3:
    container_name: node3
    networks:
      mongo:
        ipv4_address: 172.20.0.13
    image: mongo
    ports:
      - 27038:27018
    volumes:
      - ./node3/data/db:/data/db
      - ./node3/data/configdb:/data/configdb
      - ./node3/data/backup:/data/backup
    command: --shardsvr --bind_ip_all
    restart: always
    depends_on:
      - config1
      - config2
      - config3
  config1:
    container_name: config1
    networks:
      mongo:
        ipv4_address: 172.20.0.14
    image: mongo
    ports:
      - 27019:27019
    volumes:
      - ./config1/data/db:/data/db
      - ./config1/data/configdb:/data/configdb
    command: --configsvr --replSet "config_server" --bind_ip_all
    restart: always
  config2:
    container_name: config2
    networks:
      mongo:
        ipv4_address: 172.20.0.15
    image: mongo
    ports:
      - 27029:27019
    volumes:
      - ./config2/data/db:/data/db
      - ./config2/data/configdb:/data/configdb
    command: --configsvr --replSet "config_server" --bind_ip_all
    restart: always
  config3:
    container_name: config3
    networks:
      mongo:
        ipv4_address: 172.20.0.16
    image: mongo
    ports:
      - 27039:27019
    volumes:
      - ./config3/data/db:/data/db
      - ./config3/data/configdb:/data/configdb
    command: --configsvr --replSet "config_server" --bind_ip_all
    restart: always
  mongos1:
    container_name: mongos1
    networks:
      mongo:
        ipv4_address: 172.20.0.17
    image: mongo
    ports:
      - 27017:27017
    volumes:
      - ./mongos1/data/db:/data/db
      - ./mongos1/data/configdb:/data/configdb
    entrypoint: mongos
    command: --configdb config_server/172.20.0.14:27019,172.20.0.15:27019,172.20.0.16:27019 --bind_ip_all
    depends_on:
      - node1
      - node2
      - node3
  mongos2:
    container_name: mongos2
    networks:
      mongo:
        ipv4_address: 172.20.0.18
    image: mongo
    ports:
      - 27027:27017
    volumes:
      - ./mongos2/data/db:/data/db
      - ./mongos2/data/configdb:/data/configdb
    entrypoint: mongos
    command: --configdb config_server/172.20.0.14:27019,172.20.0.15:27019,172.20.0.16:27019 --bind_ip_all
    depends_on:
      - node1
      - node2
      - node3
  mongos3:
    container_name: mongos3
    networks:
      mongo:
        ipv4_address: 172.20.0.19
    image: mongo
    ports:
      - 27037:27017
    volumes:
      - ./mongos3/data/db:/data/db
      - ./mongos3/data/configdb:/data/configdb
    entrypoint: mongos
    command: --configdb config_server/172.20.0.14:27019,172.20.0.15:27019,172.20.0.16:27019 --bind_ip_all
    depends_on:
      - node1
      - node2
      - node3
networks:
  mongo:
    driver: bridge
    ipam:
      config:
        - subnet: 172.20.0.10/24

(2)本地新建映射目录:

mkdir -p ./{node1,node2,node3,mongos1,mongos2,mongos3,config1,config2,config3}/data

(3)检测下洗的yaml有没有问题

 [root@dev01 mongo]# docker-compose -f mongo_cluster.yaml config 

没问题会提示。

(4)直接拉起mongo服务:

  docker-compose -f mongo_cluster.yaml up -d 

(5)config server配置:

## 登录config
mongo --port 27019 --host 172.20.0.14
?
## 创建配置(_id : "config" 等于上边的 replSetName: config)
config = {
    _id: "config_server",
    configsvr: true,
    members : [
        {_id: 0, host: "172.20.0.14:27019"},
        {_id: 1, host: "172.20.0.15:27019"},
        {_id: 2, host: "172.20.0.16:27019"}
    ]
};
?
## 初始化副本集配置
rs.initiate(config)
?
## 查看状态
rs.status()

(6)mongo配置:

## 登录mongos
mongo --port 27017 --host 172.20.0.17
## 添加分片
sh.addShard("172.20.0.11:27018")
sh.addShard("172.20.0.12:27018")
sh.addShard("172.20.0.13:27018")

然后可以通过27017,27027,27037任意一个本地端口进行连接测试,可以研究mongo rs集群了。

以上是关于mongo微服务搭建的主要内容,如果未能解决你的问题,请参考以下文章

maven+spring boot搭建简单微服务

maven+spring boot搭建简单微服务

Mongo分片+副本集集群搭建

Mongo集群搭建之MongoDB基础

Java 微服务 day02 源代码 SpringBoot 实战开发 创建相关数据库,搭建服务,搭建普通工程,设置访问后缀以及端口号

mongo数据库主从备份服务集群搭建