创建springboot工程
创建springboot工程
集成mybatis
连接mysql
连接redis(可选)
修改pom.xml
添加nacos依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
创建 bootstrap.yml
配置nacos
spring:
application:
name: goods-ms
cloud:
nacos:
# discovery: # 注册
# server-addr: 127.0.0.1:8848
config: # 配置中心
server-addr: 127.0.0.1:8848
file-extension: yml
profiles:
active: dev # 拉取dev配置文件
nacos添加配置
新建
配置
- Data ID: 这里的
Data ID不能改变,与上面bootstrap.yml配置相关,原因见:
https://www.malaoshi.top/show_1EF5lgtr12b8.html
- 配置内容: 将原本
application.yml的内容粘贴到 nacos 配置内容中
配置内容如下:
application.yml的内容粘贴到 nacos 配置内容中:
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3308/test?characterEncoding=utf8 #不能使用域名
username: root
password:
druid:
# 连接池的配置信息
# 初始化大小,最小,最大
initial-size: 5
min-idle: 5
maxActive: 20
# 配置获取连接等待超时的时间
maxWait: 60000
# 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
timeBetweenEvictionRunsMillis: 60000
# 配置一个连接在池中最小生存的时间,单位是毫秒
minEvictableIdleTimeMillis: 300000
validationQuery: SELECT 1
testWhileIdle: true
testOnBorrow: false
testOnReturn: false
# 打开PSCache,并且指定每个连接上PSCache的大小
poolPreparedStatements: true
maxPoolPreparedStatementPerConnectionSize: 20
# 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
filters: stat,wall,slf4j
# 通过connectProperties属性来打开mergeSql功能;慢SQL记录
connectionProperties: druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
redis:
host: localhost
port: 6379
password:
timeout: 1000 # 连接超时时间(毫秒)
database: 0 # Redis数据库索引(默认为0)
jedis:
pool:
max-wait: 30000
max-active: 200 # 连接池最大连接数(使用负值表示没有限制)
max-idle: 8 # 连接池中的最大空闲连接
min-idle: 2 # 连接池中的最小空闲连接
# application:
# name: NOTICE-SERVICE # 指定服务名字,在eureka管理页面中可查看
# 整合mybatis
mybatis:
config-location: classpath:mybatis/mybatis-config.xml #mybatis总配置文件
mapperLocations: classpath:mybatis/mapper/*.xml # xxxMapper.xml配置文件
server:
port: 8080
发布
如下图:
测试
启动springboot工程,先是从 nacos 中读取配置,然后启动连接mysql、redis等
访问 查询mysql数据库


