SpringBoot集成Druid+ShardingShare+监控

(1)引入pom

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

(2)application.properties配置

server.port=8000
server.additionalPorts=8001
server.tomcat.max-connections=300
server.tomcat.max-threads=300
server.tomcat.uri-encoding=UTF-8
spring.application.name=datacenter
logging.level.com.bethlabs=debug
mybatis-plus.mapper-locations=classpath:mapper/*.xml
redis.hostName=192.168.1.155
redis.port=9081
redis.timeout=300
redis.maxIdle =300
redis.maxTotal=1000
redis.maxWaitMillis=3000
redis.minEvictableIdleTimeMillis =300000
redis.numTestsPerEvictionRun =1024
redis.timeBetweenEvictionRunsMillis =30000
redis.testOnBorrow =true
redis.testWhileIdle =true
#==================================shardingsphere 配置开始========数据分片======================================
**spring.shardingsphere.datasource.names=ds0
# 配置数据源
#spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource
spring.shardingsphere.datasource.ds0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.ds0.url=jdbc:mysql://192.168.1.155:9080/datacenter?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf8&allowMultiQueries=true
spring.shardingsphere.datasource.ds0.username=root
spring.shardingsphere.datasource.ds0.password=bethlabs
spring.shardingsphere.datasource.ds0.initial-size=5
spring.shardingsphere.datasource.ds0.min-idle=5
spring.shardingsphere.datasource.ds0.maxActive=20
spring.shardingsphere.datasource.ds0.maxWait=60000
spring.shardingsphere.datasource.ds0.timeBetweenEvictionRunsMillis=60000
spring.shardingsphere.datasource.ds0.minEvictableIdleTimeMillis=300000
spring.shardingsphere.datasource.ds0.validationQuery=SELECT 1 
spring.shardingsphere.datasource.ds0.testWhileIdle=true
spring.shardingsphere.datasource.ds0.testOnBorrow=false
spring.shardingsphere.datasource.ds0.testOnReturn=false
spring.shardingsphere.datasource.ds0.poolPreparedStatements=true
spring.shardingsphere.datasource.ds0.maxPoolPreparedStatementPerConnectionSize=20
spring.shardingsphere.datasource.ds0.filters=stat,wall
spring.shardingsphere.datasource.ds0.connectionProperties=druid.stat.mergeSql\=true;druid.stat.slowSqlMillis\=5000
spring.shardingsphere.datasource.ds0.web-stat-filter.enabled=true
spring.shardingsphere.datasource.ds0.web-stat-filter.url-pattern=/*
spring.shardingsphere.datasource.ds0.web-stat-filter.exclusions=*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico,/druid/*
spring.shardingsphere.datasource.ds0.stat-view-servlet.deny=192.168.1.73
spring.shardingsphere.datasource.ds0.stat-view-servlet.reset-enable=false
#以下4项,将来上生产环境以后一定要改!!!!!一定要改!!!!!一定要改!!!!!
spring.shardingsphere.datasource.ds0.stat-view-servlet.url-pattern=/druid/*
spring.shardingsphere.datasource.ds0.stat-view-servlet.allow=127.0.0.1,*
spring.shardingsphere.datasource.ds0.stat-view-servlet.login-username=admin
# #shardingjdbc解析上下文和改写后的SQL
spring.shardingsphere.props.sql.show=false**
###########################################################
#
#                       默认分片规则配置--字典表使用
#
###########################################################
#未配置分片规则的表将通过默认数据源定位-适用于单库单表,该表无需配置分片规则
sharding.jdbc.config.sharding.defaultDataSourceName=ds0
#主键
spring.shardingsphere.sharding.tables.t_record.key-generator.column=id
#主键生成策略,用雪花算法
spring.shardingsphere.sharding.tables.t_record.key-generator.type=SNOWFLAKE
#====================分表策略,根据年份月份分表   开始=========================================
#实际的数据分支,查询时根据这个进行查询
#spring.shardingsphere.sharding.tables.t_record.actual-data-nodes=ds0.t_record_${['20200326','20200327','20200328','20200329','20200330','20200331']}
spring.shardingsphere.sharding.tables.t_record.actual-data-nodes=ds0.t_record_$->{[]}
# 根据order_id字段进行分片
spring.shardingsphere.sharding.tables.t_record.table-strategy.standard.sharding-column=time
# 精确分片算法。按单月分表,采用标准的分片策略,指向插入数据时的处理,自己实现的逻辑,每次插入和根据分片键查询数据的时候都会被调用
spring.shardingsphere.sharding.tables.t_record.table-strategy.standard.precise-algorithm-class-name=com.bethlabs.shardingjdbctablestrategy.config.TimeShardingTableAlgorithm
# 范围分片算法。根据分片键使用between and 的时候才会走,其他方式查询则没用
spring.shardingsphere.sharding.tables.t_record.table-strategy.standard.range-algorithm-class-name=com.bethlabs.shardingjdbctablestrategy.config.TimeRangeShardingAlgorithm
#====================分表策略,根据年份月份分表   结束=========================================

(3)访问监控页面
启动项目后,访问页面http://127.0.0.1:8001/druid/sql.html,就可以访问监控页面了,其中ip和端口号为项目的ip和端口号。页面效果如下:

在这里插入图片描述
(4)设置druid访问页面的用户名和密码,只需要在springboot启动类中加入@bean配置即可:

@Bean
public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
	ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(),  "/druid/*");
	registrationBean.addInitParameter("allow", "127.0.0.1");// IP白名单 (没有配置或者为空,则允许所有访问)
	registrationBean.addInitParameter("deny", "");// IP黑名单 (存在共同时,deny优先于allow)
	registrationBean.addInitParameter("loginUsername", "root");
	registrationBean.addInitParameter("loginPassword", "1234");
	registrationBean.addInitParameter("resetEnable", "false");
	return registrationBean;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

AK@

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值