前言
网站建设定制开发本文重点讲ShardingSphere整合driud,原因是,网站建设定制开发网上大部分讲的重点都在ShardingSphere整合mysql网站建设定制开发的数据源上,网站建设定制开发配置文件也不是很完整,很全面,网站建设定制开发所以这里先说一下配置,均采用yml的方式。
网站建设定制开发下边开始正文,网站建设定制开发文章最后会附上完整代码,网站建设定制开发配置步骤思路不想看可以直接跳到最后。(博主这里建议大家,对于自己配置和写的东西,尽量要搞懂每项配置和每行代码的作用,而不是随便在网上一复制,项目中一粘贴,只要不报错就ok,这样是对项目的不负责,也是对自己的不负责。)
特别说明
本文针对的ShardingSphere版本为 5.0.0-alpha,maven依赖
<dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>shardingsphere-jdbc-core-spring-boot-starter</artifactId> <version>5.0.0-alpha</version></dependency>
- 1
- 2
- 3
- 4
- 5
版本的差异造成配置方法也发生很大变化,比如,在最新的5.1.1中,common配置项就无法使用了,只能把公共信息在每个数据源下都配置一遍(至少在我本文发布节点存在这个问题)。
配置
rules部分本文就不讲解和配置了,可以看完整springboot整合ShardingSphere-JDBC文章
spring: shardingsphere: datasource: common: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 6 min-idle: 3 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 #Oracle需要打开注释 #validationQuery: SELECT 1 FROM DUAL 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 wall: multi-statement-allow: true names: demo0,demo1 demo0: url: jdbc:mysql://localhost:3306/ride_share?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: 123456 demo1: url: jdbc:mysql://192.168.150.129:3306/ride_share?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: root
- 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
看网上关于druid这部分的配置,基本全部都是放在每个数据源下的,这样会造成很多冗余代码,博主有点强迫症,既然shardingsphere starter给我们提供了common,为什么不利用一下呢。
配置完之后启动会报错找不到 “url”,这个错误是druid报的,因为没有对其配置数据源,但是我们的数据源是要用shardingsphere 来配置的,所以我们先排序掉druid的核心配置,加入如下代码。
spring: autoconfigure: exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
- 1
- 2
- 3
配置完之后,我们可以顺利的启动项目了,但是你会发现druid的监控页面你打不开了,也就是下边的页面。
原因就是我们上一步的配置,启动将druid的配置排除了。
那怎么办?所以我们要在项目里给它重新开启。
druid配置文件
@Configurationpublic class DruidConfig { /** * Druid监控 */ @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean bean = new ServletRegistrationBean(new StatViewServlet(), "/druid/*"); Map<String,String> initParams = new HashMap<>();//这是配置的druid监控的登录密码 initParams.put("loginUsername","root"); initParams.put("loginPassword","root"); //默认就是允许所有访问 initParams.put("allow","127.0.0.1,192.168.3.4"); //黑名单IP initParams.put("deny","192.168.15.21"); bean.setInitParameters(initParams); return bean; }}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
这一种配置是我在网上找的,可以用,但唯一不完美的地方就是配置信息要写在代码里而不是配置文件里,当然你也可以直接把这些自定义信息定义在yml里,然后注入进来,但是还是不够优雅。
这时我注意到了之前没用ShardingSphere的时候,用mybatis动态数据源的配置。
mybatis也是排除了这个druid自动配置,但是监控页面依然能打开,怎么找到原因呢?
思路:确定druid配置是mybaits开启的,然后我们先去看看druid是怎么加载yml配置并且开启监控页面的,之后再去找到开启的核心配置类,最后看看这个配置类被谁调用了。
下边直接截图表达:
为什么找这个类,因为对应配置文件
找到了,果然mybatis引用它了,继续。
ok,把这个类复制出来,改个名字放到你的项目里就好了。
/** * @author: zhouwenjie * @description: 重新开启被排除的druid配置 * @create: 2022-04-24 16:54 **/@Configuration@ConditionalOnClass(DruidDataSourceAutoConfigure.class)@EnableConfigurationProperties({DruidStatProperties.class})@Import({ DruidSpringAopConfiguration.class, DruidStatViewServletConfiguration.class, DruidWebStatFilterConfiguration.class, DruidFilterConfiguration.class})public class DruidShardingJdbcDataSourceConfiguration {}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
于是,我们yml里就可以按照druid官方要求正常配置了。
yml完整配置
server: port: 8088 servlet: context-path: /shardingspring: autoconfigure: exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure datasource: type: com.alibaba.druid.pool.DruidDataSource druid: stat-view-servlet: enabled: true loginUsername: admin loginPassword: 123456 web-stat-filter: enabled: true shardingsphere: datasource: common: type: com.alibaba.druid.pool.DruidDataSource driver-class-name: com.mysql.cj.jdbc.Driver initial-size: 6 min-idle: 3 maxActive: 20 # 配置获取连接等待超时的时间 maxWait: 60000 # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 timeBetweenEvictionRunsMillis: 60000 # 配置一个连接在池中最小生存的时间,单位是毫秒 minEvictableIdleTimeMillis: 300000 #Oracle需要打开注释 #validationQuery: SELECT 1 FROM DUAL 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 wall: multi-statement-allow: true names: demo0,demo1 demo0: url: jdbc:mysql://localhost:3306/ride_share?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: 123456 demo1: url: jdbc:mysql://192.168.150.129:3306/ride_share?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai username: root password: root
- 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
好了,至此完成,如果小伙伴们发现有什么不对的地方,可以提示一下,我及时更正!!!
Shardingsphere与dynamic-datasource配合实现多数据源切换
当然,也有需求是,用到数据读写分离或者分片的需求就用shardingsphere,但是用不到就用正常的数据源,那么可以参考这篇文章:
读写分离参考这个: