application.yml 配置

database

数据库连接池使用 druid

  1. pom.xml 添加 druid-spring-boot-starter 依赖。
  2. 配置 application.yml。
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
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai # useSSL=true 解决JDBC版本与MySQL版本不兼容问题
username: odic
password: Gw32_iuX
druid:
# 连接池配置(通常来说,只需要修改initialSize、minIdle、maxActive
# 如果用Oracle,则把poolPreparedStatements配置为true,mysql可以配置为false。分库分表较多的数据库,建议配置为false。removeabandoned不建议在生产环境中打开如果用SQL Server,建议追加配置)
initial-size: 5 # 初始化物理连接个数
min-idle: 2 # 连接池中最小空闲连接数
max-active: 20 # 连接池中最大的活跃连接数
max-wait: 60000 # 配置获取连接等待超时的时间
pool-prepared-statements: false # 开启缓存preparedStatement(PSCache)
max-pool-prepared-statement-per-connection-size: -1 # 启用PSCache后,指定每个连接上PSCache的大小
test-on-borrow: false # 申请连接时不检测连接是否有效
test-on-return: false # 归还连接时不检测连接是否有效
test-while-idle: true # 如果空闲时间大于timeBetweenEvictionRunsMillis,执行validationQuery检测连接是否有效
validation-query: SELECT 'x'
time-between-eviction-runs-millis: 60000 # 检测连接的间隔时间,若连接空闲时间 >= minEvictableIdleTimeMillis,则关闭物理连接
min-evictable-idle-time-millis: 300000 # 连接保持空闲而不被驱逐的最小时间
filter: config,stat # 配置监控统计拦截的filters(不配置则监控界面sql无法统计),监控统计filter:stat,日志filter:log4j,防御sql注入filter:wall
connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=5000;config.decrypt=false; # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
web-stat-filter:
enabled: true # 是否启用StatFilter默认值true
url-pattern: /*
exclusions: /*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*
session-stat-enable: false
session-stat-max-count: 1000
principal-session-name: admin # 从session中获取身份标识
principal-cookie-name: admin
profile-enable: true
# StatViewServlet配置
#展示Druid的统计信息,StatViewServlet的用途包括:
# 1.提供监控信息展示的html页面
# 2.提供监控信息的JSON API
stat-view-servlet:
enabled: true # 是否启用StatViewServlet默认值true
url-pattern: /druid/* # 根据配置中的url-pattern来访问内置监控页面
reset-enable: false # 禁止手动重置监控数据
login-username: admin # 设置监控页面的登录名和密码
login-password: admin
#StatViewSerlvet展示出来的监控信息比较敏感,是系统运行的内部情况,如果你需要做访问控制,可以配置allow和deny这两个参数
#deny优先于allow,如果在deny列表中,就算在allow列表中,也会被拒绝。如果allow没有配置或者为空,则允许所有访问
#配置的格式:<IP> 或者 <IP>/<SUB_NET_MASK_size>其中128.242.127.1/24
#24表示,前面24位是子网掩码,比对的时候,前面24位相同就匹配,不支持IPV6。
#deny:xxx
#allow: 127.0.0.1
aop-patterns: com.example.demo.*.*.* # 对内部各接口调用的监控

参考

SpringBoot项目集成阿里Druid连接池
Druid监控功能的深入使用与配置

mybatis

  1. pom.xml 添加 mybatis-spring-boot-starter 依赖、pagehelper-spring-boot-starter 依赖用于分页。
  2. 配置 application.yml。
1
2
3
4
5
mybatis:
mapper-locations: classpath*:mapper/*.xml
type-aliases-package: com.example.demo.dao
configuration:
map-underscore-to-camel-case: true

redis

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
spring:
redis:
database: 10 # Redis数据库索引(默认为0)
host: 127.0.0.1 # Redis服务器地址
port: 6379 # Redis服务器连接端口
password: 123456 # Redis服务器连接密码(默认为空)
ssl: false
lettuce:
pool:
max-active: 50 # 连接池最大连接数(使用负值表示没有限制)
max-wait-millis: 10000 # 连接池最大阻塞等待时间(使用负值表示没有限制)
timeout: 0 # 连接超时时间(毫秒)
max-idle: 10 # 连接池中的最大空闲连接
min-idle: 5 # 连接池中的最小空闲连接
test-on-borrow: true # 调用者获取链接时,是否检测当前链接有效性
test-on-return: true # 向链接池中归还链接时,是否检测链接有效性
test-while-idle: true # 调用者获取链接时,是否检测空闲超时, 如果超时,则会被移除
time-between-eviction-runs-millis: 30000 # 空闲链接检测线程检测周期(使用负值表示不运行检测线程)
num-tests-per-eviction-run: 10 # 空闲链接检测线程一次运行检测多少条链接
min-evictable-idle-time-millis: 60000 # 连接在池中最小的生存时间

参考

SpringBoot + Redis:基本配置及使用
redis深度历险
Spring Boot整合Redis

log

  1. pom.xml 添加 log4j-over-slf4j、jcl-over-slf4、jul-to-slf4j 依赖。log4j-over-slf4j 通过代理将系统中所有 log4j 日志(含第三方库的)路由到 slf4j上。jcl-over-slf4 将 apache commons logging 路由到 slf4j上。jul-over-slf4 将 java.util.logging 路由到 slf4j上。
  2. 配置 application.yml。
  3. 配置 logback-config.xml。
1
2
3
4
5
6
7
logging:
config: classpath:logback-config.xml
file:
path: /data
level:
all: debug
root: debug
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- https://github.com/spring-projects/spring-boot/blob/v1.4.2.RELEASE/spring-boot/src/main/resources/org/springframework/boot/logging/logback/defaults.xml -->
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>

<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<springProperty scope="context" name="LOG_BASE_PATH" source="logging.file.path"/>
<springProperty scope="context" name="BASE_LEVEL" source="logging.level.all"/>
<springProperty scope="context" name="ROOT_LEVEL" source="logging.level.root"/>

<property name="LEVEL" value="${BASE_LEVEL}"/>
<property name="LOG_PATH" value="${LOG_BASE_PATH}/logs/${APP_NAME}"/>
<property name="ARCHIVE_LOG_PATH" value="${LOG_PATH}/archive/%d{yyyy-MM-dd}"/>
<property name="LOG_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss.SSS}|%X{traceId}| %-5level %logger{80} - %msg%n"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>${LOG_PATTERN}</pattern>
<charset>utf8</charset>
</encoder>
</appender>

<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>${LOG_PATTERN}</Pattern>
</encoder>
</appender>

<appender name="INFO" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/info.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/info.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="WARN" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/warn.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>WARN</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/warn.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="ERROR" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/error.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/error.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="CACHE-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/cache.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/cache.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="SPRING-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/spring.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/spring.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="MYBATIS-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/mybatis.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/mybatis.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="NETTY-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/netty.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/netty.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="FILTER-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/filter.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/filter.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="CONTROLLER-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/controller.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/controller.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<appender name="SERVICE-LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${LOG_PATH}/service.log</file>
<encoder>
<pattern>${LOG_PATTERN}</pattern>
</encoder>
<rollingPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${ARCHIVE_LOG_PATH}/service.%d{yyyy-MM-dd}.log.%i</fileNamePattern>
<maxHistory>7</maxHistory>
<maxFileSize>50MB</maxFileSize>
<totalSizeCap>20GB</totalSizeCap>
</rollingPolicy>
</appender>

<springProfile name="prod,dev">
<logger name="org.apache.ibatis" level="${LEVEL}" additivity="false">
<appender-ref ref="MYBATIS-LOG"/>
</logger>

<logger name="org.mybatis.spring" level="${LEVEL}" additivity="false">
<appender-ref ref="MYBATIS-LOG"/>
</logger>

<logger name="com.alibaba.druid" level="${LEVEL}" additivity="false">
<appender-ref ref="MYBATIS-LOG"/>
</logger>

<logger name="io.netty" level="${LEVEL}" additivity="false">
<appender-ref ref="NETTY-LOG"/>
</logger>

<logger name="org.springframework" level="${LEVEL}" additivity="false">
<appender-ref ref="SPRING-LOG"/>
</logger>

<logger name="cache-log" level="${LEVEL}" additivity="false">
<appender-ref ref="CACHE-LOG"/>
</logger>
<logger name="javax.cache" level="${LEVEL}" additivity="false">
<appender-ref ref="CACHE-LOG"/>
</logger>

<logger name="com.example.demo.dao" level="${LEVEL}" additivity="false">
<appender-ref ref="MYBATIS-LOG"/>
</logger>

<logger name="com.example.demo.manager" level="${LEVEL}" additivity="false">
<appender-ref ref="SERVICE-LOG"/>
</logger>

<logger name="com.example.demo.service" level="${LEVEL}" additivity="false">
<appender-ref ref="SERVICE-LOG"/>
</logger>

<logger name="com.example.demo.controller" level="${LEVEL}" additivity="false">
<appender-ref ref="CONTROLLER-LOG"/>
</logger>

<logger name="com.example.demo.aop" level="${LEVEL}" additivity="false">
<appender-ref ref="CONTROLLER-LOG"/>
</logger>

<root level="${ROOT_LEVEL}">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>

<springProfile name="local">
<logger name="org.apache.ibatis" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="org.mybatis.spring" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="com.alibaba.druid" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="io.netty" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="org.springframework" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="cache-log" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>
<logger name="javax.cache" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="com.example.demo.dao" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="com.example.demo.manager" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="com.example.demo.service" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="com.example.demo.controller" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<logger name="com.example.demo.aop" level="${LEVEL}" additivity="false">
<appender-ref ref="STDOUT"/>
</logger>

<root level="${ROOT_LEVEL}">
<appender-ref ref="STDOUT"/>
</root>
</springProfile>
</configuration>

参考

log4j-over-slf4j工作原理详解

其他

1
2
3
4
5
6
7
8
9
server:
port: 18080
spring:
application:
name: demo
servlet:
multipart:
max-file-size: 100MB
max-request-size: 100MB