SpringBoot 整合 MongoDB 6 以上版本副本集及配置 SSL / TLS 协议

续上一篇 Linux 中使用 docker-compose 部署 MongoDB 6 以上版本副本集及配置 SSL / TLS 协议

前提:此篇文章是对上一篇文章的实战和项目中相关配置的使用,我这边针对 MongoDB 原有基础上做了增强,简化了 MongoDB 配置 SSL / TLS 协议上的支持, 目前支持 SpringBoot、SpringCloud 等场景下,支持 MongoDB 单机、副本集、分片集群部署上的使用,具体功能可通过 https://github.com/mbql/mbql-mongodb-model 链接访问。

一、项目中引入 MongoDB 依赖

 <dependency>
     <groupId>io.github.mbql</groupId>
     <artifactId>mbql-mongodb-model</artifactId>
     <version>1.0.2</version>
 </dependency>

二、通过 yaml 文件配置(有三种模式配置,同时也支持 Java API 方式配置)

# mongodb 默认模式配置(适用于单机模式)
mongodb:
  url: mongodb://root:[email protected]:27017/test?authSource=admin
# 或者
mongodb:
  url: mongodb://127.0.0.1:27017/test
  auth-user-name: root
  password: 123456
  database: admin
  
# mongodb SSL / TLS 静态模式配置(适用于单机、副本集、分片模式)
mongodb:
  url: mongodb://root:[email protected]:27017,127.0.0.1:27018,127.0.0.1:27019/test?   replicaSet=rs0&tls=true&authSource=admin&tlsAllowInvalidHostnames=true&compressors=zstd&zlibCompressionLevel=6
  enable-ssl: true # 开启 SSL / TLS 模式
  use-static-mode: true # 使用静态模式 
  cluster-type: replica_set # 集群类型
  cluster-connection-mode: multiple # 集群连接模式
  certs:
    password: 123456  # 证书密钥密码
    trustStoreName: cacerts  # JVM 信任证书名称
    keyStoreName: keystore.pkcs12 # JVM 密钥证书名称
# 或者
mongodb:
  url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test
  enable-ssl: true
  use-static-mode: true
  cluster-type: replica_set
  cluster-connection-mode: multiple
  database: admin # 认证数据库
  repl-set-name: rs0 # 副本集名称
  invalid-host-name-allowed: true # 允许无效主机连接, 默认是只能本机
  auth-user-name: root # 认证用户名
  password: 123456 # 认证用户密码
  certs:
    password: 123456  # 证书密钥密码
    trustStoreName: cacerts  # JVM 信任证书名称
    keyStoreName: keystore.pkcs12 # JVM 密钥证书名称
    
# mongodb SSL / TLS 动态模式配置(适用于单机、副本集、分片模式)
mongodb:
  url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test
  invalid-host-name-allowed: true
  enable-ssl: true
  cluster-type: replica_set
  cluster-connection-mode: multiple
  repl-set-name: rs0
  certs:
    password: 123456
    trustStoreName: cacerts
    keyStoreName: keystore.pkcs12
# 或者
mongodb:
  url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test?   replicaSet=rs0&tlsAllowInvalidHostnames=true&compressors=zstd&zlibCompressionLevel=6
  enable-ssl: true # 开启 SSL / TLS 模式
  cluster-type: replica_set # 集群类型
  cluster-connection-mode: multiple # 集群连接模式
  certs:
    password: 123456  # 证书密钥密码
    trustStoreName: cacerts  # JVM 信任证书名称
    keyStoreName: keystore.pkcs12 # JVM 密钥证书名称

# 具体配置可参考:https://github.com/mbql/mbql-mongodb-model/blob/master/README.md

三、演示上面 3 种不同模式的 Monogo 客户端配置及使用

1、默认模式使用(单机模式示列)
配置 mongo 客户端连接信息
mongodb:
  url: mongodb://127.0.0.1:27017/test
  auth-user-name: root
  password: 123456
  database: admin
启动项目并且没有出现错误,就正常连接成功,如下图:

在这里插入图片描述

2、SSL / TLS 静态模式(副本集模式演示)
配置 mongo 客户端连接信息
mongodb:
  url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test
  enable-ssl: true
  use-static-mode: true
  cluster-type: replica_set
  cluster-connection-mode: multiple
  repl-set-name: rs0 # 副本集名称
  invalid-host-name-allowed: true # 允许无效主机连接, 默认是只能本机
  certs:
    password: 123456  # 证书密钥密码
    trustStoreName: cacerts  # JVM 信任证书名称
    keyStoreName: keystore.pkcs12 # JVM 密钥证书名称

在项目 classpath 路径下创建 certs 目录,并将 JVM 信任证书 cacerts 和 客户端密钥证书 keystore.pkcs12 放到该目录里面,具体怎么生成自签名证书可参考上一篇文章,并且 JVM 相关证书的生成可参考 https://github.com/mbql/mbql-mongodb-model/blob/master/README.md

启动项目并且没有出现错误,就正常连接成功,如下图:

在这里插入图片描述

3、SSL / TLS 动态模式(副本集模式演示)
mongodb:
  url: mongodb://127.0.0.1:27017,127.0.0.1:27018,127.0.0.1:27019/test
  invalid-host-name-allowed: true
  enable-ssl: true
  cluster-type: replica_set
  cluster-connection-mode: multiple
  repl-set-name: rs0
  certs:
    password: 123456
    trustStoreName: cacerts
    keyStoreName: keystore.pkcs12

在项目 classpath 路径下创建 certs 目录,并将 JVM 信任证书 cacerts 和 客户端密钥证书 keystore.pkcs12 放到该目录里面,具体怎么生成自签名证书可参考上一篇文章,并且 JVM 相关证书的生成可参考 https://github.com/mbql/mbql-mongodb-model/blob/master/README.md

启动项目并且没有出现错误,就正常连接成功,如下图:

在这里插入图片描述

代码仓库地址:https://github.com/mbql/mbql-dev-lib

猜你喜欢

转载自blog.csdn.net/weixin_43322048/article/details/129199707
今日推荐