SpringBoot项目中配置Tomcat、Mysql、Redis、MongoDB

一、创建SpringBoot项目

1、创建项目时选择Spring Initializr
在这里插入图片描述
2、填好相关信息
在这里插入图片描述
3、在依赖库中勾选如下内容
在这里插入图片描述
4、填写项目名称
在这里插入图片描述
5、把application.properties文件修改成application.yml

以下的环境配置,都是在application.yml中进行。特别的地方,会另作说明。

二、 配置Tomcat

server:
  tomcat:
    uri-encoding: UTF-8
    threads:
      max: 200 	#最大线程数量
      min-spare: 30	#最小
    connection-timeout: 5000ms
  port: 8080
  servlet:
    context-path: /emos-wx-api  #项目部署路径

三、 配置MySQL数据源

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource #阿里特有的DruidDataSource连接池
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/emos?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
      username: root
      password: root #本地数据库的用户名和密码
      initial-size: 8 #数据库连接池的数量
      max-active: 16
      min-idle: 8
      max-wait: 60000
      test-while-idle: true #检测连接是否有效
      test-on-borrow: false
      test-on-return: false

然后在 pom.xml文件中配置好阿里巴巴的Druid连接池所需要的依赖库。

<dependency>
	<groupId>com.alibaba</groupId>
	<artifactId>druid-spring-boot-starter</artifactId>
	<version>1.1.13</version>
</dependency>

四、 配置Redis数据库

spring:
  #这里省略MySQL配置信息
  redis:
    database: 0  #使用0号数据库
    host: localhost
    port: 6379
    password: abc123456  #redis端口的访问密码
    jedis:
      pool:
        max-active: 1000  #设置Redis的连接池
        max-wait: -1ms
        max-idle: 16	#最大空闲连接数
        min-idle: 8

五、 配置MongoDB数据源

spring:
  #这里省略MySQL配置信息
  #这里省略Redis配置信息
  data:
    mongodb:
      host: localhost
      port: 27017
      database: emos
      authentication-database: admin
      username: admin
      password: abc123456

在连接mongoDB的时候,要打开软件:
在这里插入图片描述

六、项目最终效果

如下图,出现如下字段表示已完成。
在这里插入图片描述
完整的application.yml文件如下:

server:
  tomcat:
    uri-encoding: UTF-8
    threads:
      max: 200
      min-spare: 30
    connection-timeout: 5000ms
  port: 8080
  servlet:
    context-path: /emos-wx-api

spring:
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      driver-class-name: com.mysql.cj.jdbc.Driver
      url: jdbc:mysql://localhost:3306/emos?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
      username: root
      password: root
      initial-size: 8
      max-active: 16
      min-idle: 8
      max-wait: 60000
      test-while-idle: true
      test-on-borrow: false
      test-on-return: false

  redis:
    database: 0
    host: localhost
    port: 6379
    password: abc123456
    jedis:
      pool:
        max-active: 1000
        max-wait: -1ms
        max-idle: 16
        min-idle: 8

  data:
    mongodb:
      host: localhost
      port: 27017
      database: emos
      authentication-database: admin
      username: admin
      password: abc123456
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晓梦林

都看到这里了,支持一下作者呗~

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值