druid保存监控记录

kingschan 2016-10-08 16:17:05 阅读 1083 评论 0 喜欢 0
druid 

druid的datasource会记录下关于连接池各种信息,包括活动连接、已关闭连接、历史连接数,连接池的总连接数等等,同时支持将这些监控信息保存下来。

这个功能通过datasource的DruidDataSourceStatLogger实例statLogger来支持,DruidDataSource初始化的时候会创建一个LogStatsThread线程,这个线程每隔一定的时间就会将监控信息保存到特定位置,具体保存到哪里由statLogger决定,当然,如果需要我们可以自定义自己的DruidDataSourceStatLogger实现。

不过druid提供了一个默认的DruidDataSourceStatLogger实现——DruidDataSourceStatLoggerImpl,要使用它,我们需要为它指定一个Log(druid自己定义的Log接口)的实例抑或是Log的类名称,如果没特殊的需要,不防也直接使用druid提供的log4j实现Log4jImpl,如:

<!-- druid日志记录 -->
     <bean id= "statLoggerb" class="com.alibaba.druid.pool.DruidDataSourceStatLoggerImpl" >
           <property name= "logger" ref ="log4jb" />
     </bean >
     
     <bean id="log4jb" class="com.alibaba.druid.support.logging.Log4jImpl" >
           <constructor-arg><value>druid.statlog</value></constructor-arg>
     </bean >
   
<bean id="blog_ds" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> 
      <property name="url" value="${jdbc.blog.url}" />
      <property name="username" value="${jdbc.blog.username}" />
      <property name="password" value="${jdbc.blog.password}" />
      <property name="initialSize" value="1" />
      <property name="minIdle" value="1" /> 
      <property name="maxActive" value="20" />
      <property name="maxWait" value="60000" />
      <property name="timeBetweenEvictionRunsMillis" value="60000" />
      <!-- 每隔10分钟把监控数据输出到日志中 -->
      <property name ="timeBetweenLogStatsMillis" value ="600000" />
      <property name="minEvictableIdleTimeMillis" value="300000" />
      <property name="validationQuery" value="SELECT 'x'" />
      <property name="testWhileIdle" value="true" />
      <property name="testOnBorrow" value="false" />
      <property name="testOnReturn" value="false" />
      <property name="poolPreparedStatements" value="true" />
      <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
      <property name="connectionProperties" value="config.decrypt=true" />
      <property name="filters" value="config,stat,wall" /><!-- 监控统计用的filter:stat 日志用的filter:log4j 防御sql注入的filter:wall --> 
      <property name= "statLogger" ref ="statLoggerb" />
      <property name="proxyFilters">
        <list>
            <ref bean="stat-filter" />
        </list>
        </property>
  </bean>      
  
  <bean id="stat-filter" class="com.alibaba.druid.filter.stat.StatFilter">
    <property name="mergeSql" value="true" />
    <property name="slowSqlMillis" value="3000" />
    <property name="logSlowSql" value="true" />
</bean>

上面的druid.statlog是log4j中配置的logger名称,如:

###druid
log4j.logger.druid.statlog=DEBUG,druidStat
log4j.appender.druidStat =org.apache.log4j.DailyRollingFileAppender
log4j.appender.druidStat.Threshold=DEBUG
log4j.appender.druidStat.File=logs/druid.log
log4j.appender.druidStat.DatePattern='.'yyyy-MM-dd
log4j.appender.druidStat.Encoding=UTF-8
log4j.appender.druidStat.layout=org.apache.log4j.PatternLayout
log4j.appender.druidStat.layout.ConversionPattern=%d %-5p %m%n

输出日志如下所示:

2016-10-08 16:11:49,200 INFO  {"url":"jdbc:mysql://localhost:3306/blog","dbType":"mysql","name":"DataSource-2029068991",
"activeCount":0,"activePeak":1,"activePeakTime":"2016-10-08 16:11:36","poolingCount":1,
"poolingPeak":1,"poolingPeakTime":"2016-10-08 16:11:36",
"connectCount":18,"closeCount":18,"executeCount":3,"commitCount":6,
"pstmtCacheHitCount":3,"startTransactionCount":3,"transactionHistogram":[0,0,2,1],
"connectionHoldTimeHistogram":[24,6,4,2],"sqlList":[{"sql":"SELECT 'x'","executeCount":2,
"executeMillisMax":0,"executeMillisTotal":1,
"executeHistogram":[2],"executeAndResultHoldHistogram":[2],"concurrentMax":2},
{"sql":"insert into blog_log (req_agent, req_blog, req_datetime, req_ip, 
req_method, req_referer, req_run_time, req_url, id) values (?, ?, ?, ?, ?, ?, ?, ?, ?)",
"executeCount":6,"executeMillisMax":1,"executeMillisTotal":8,"executeHistogram":[2,4],
"executeAndResultHoldHistogram":[2,4],"concurrentMax":2,
"updateCount":6,"updateCountMax":1,"updateHistogram":[0,6],"inTransactionCount":6}]}


支持@用户名回复

×  

:bowtie: :smile: :laughing: :blush: :smiley: :relaxed: :smirk: :heart_eyes: :kissing_heart: :kissing_closed_eyes: :flushed: :relieved: :satisfied: :grin: :wink: :stuck_out_tongue_winking_eye: :stuck_out_tongue_closed_eyes: :grinning: :kissing: :kissing_smiling_eyes: :stuck_out_tongue: :sleeping: :worried: :frowning: :anguished: :open_mouth: :grimacing: :confused: :hushed: :expressionless: :unamused: :sweat_smile: :sweat: :weary: :pensive: :disappointed: :confounded: :fearful: :cold_sweat: :persevere: :cry: :sob: :joy: :astonished: :scream: :neckbeard: :tired_face: :angry: :rage: :triumph: :sleepy: :yum: :mask: :sunglasses: :dizzy_face: :imp: :smiling_imp: :neutral_face: :no_mouth: :innocent: :alien: :yellow_heart: :blue_heart: :purple_heart: :heart: :green_heart: :broken_heart: :heartbeat: :heartpulse: :two_hearts: :revolving_hearts: :cupid: :sparkling_heart: :sparkles: :star: :star2: :dizzy: :boom: :collision: :anger: :exclamation: :question: :grey_exclamation: :grey_question: :zzz: :dash: :sweat_drops: :notes: :musical_note: :fire: :hankey: :poop: :shit: :+1: :thumbsup: :-1: :thumbsdown: :ok_hand: :punch: :facepunch: :fist: :v: :wave: :hand: :open_hands: :point_up: :point_down: :point_left: :point_right: :raised_hands: :pray: :point_up_2: :clap: :muscle: :metal: :walking: :runner: :running: :couple: :family: :two_men_holding_hands: :two_women_holding_hands: :dancer: :dancers: :ok_woman: :no_good: :information_desk_person: :raised_hand: :bride_with_veil: :person_with_pouting_face: :person_frowning: :bow: :couplekiss: :couple_with_heart: :massage: :haircut: :nail_care: :boy: :girl: :woman: :man: :baby: :older_woman: :older_man: :person_with_blond_hair: :man_with_gua_pi_mao: :man_with_turban: :construction_worker: :cop: :angel: :princess: :smiley_cat: :smile_cat: :heart_eyes_cat: :kissing_cat: :smirk_cat: :scream_cat: :crying_cat_face: :joy_cat: :pouting_cat: :japanese_ogre: :japanese_goblin: :see_no_evil: :hear_no_evil: :speak_no_evil: :guardsman: :skull: :feet: :lips: :kiss: :droplet: :ear: :eyes: :nose: :tongue: :love_letter: :bust_in_silhouette: :busts_in_silhouette: :speech_balloon: :thought_balloon:

共0条评论

相似文章

  • 顶部
  • 关灯
  • 留言
  • 点赞