linux环境:CentOS7.8+mysql5.7.29
idea:jdk1.8+maven3.5
框架:springboot2.4.5 + mybatisplus3.4.2(mybatis-plus-boot-starter)+sharding-jdbc4.0.0-RC2(sharding-jdbc-spring-boot-starter兼容性问题由1改成2)+druid
B站项目视频:黑马2022年的瑞吉外卖,shardingjdbc读写分离案例
1、觉得有帮助的老铁,我允许你给个小赞赞鼓励一下!
2、喜欢秒的铁子可以直接看总结
错误原因: 数据库连接不上,要么数据库没开,要么访问不了10.0.0.200:3307
INFO 12396 — [ main] com.itheima.Application
ERROR 12396 — [eate-2142450580] com.alibaba.druid.pool.DruidDataSource : create connection SQLException, url: jdbc:mysql://10.0.0.200:3307/rw?characterEncoding=utf-8, errorCode 0, state 08S01
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications link failure
The last packet sent successfully to the server was 0 milliseconds ago. The driver has not received any packets from the server.
Caused by: java.net.ConnectException: Connection refused: connect
解决思路:1.检查url是否正确 2.mysql是否设置了防火墙 3.数据库是否支持远程访问(本地安装不算)
=============================================================================
INFO 13260 — [main] com.itheima.Application : No active profile set, falling back to default profiles: default
ERROR 13260 — [main] o.s.boot.SpringApplication : Application run failed
java.lang.IllegalArgumentException: Unresolvable class definition for class [org.apache.shardingsphere.shardingjdbc.spring.boot.sharding.SpringBootShardingRuleConfigurationProperties]
Caused by: java.lang.NoClassDefFoundError: org/apache/shardingsphere/core/yaml/config/sharding/YamlShardingRuleConfiguration
at java.lang.ClassLoader.defineClass1(Native Method) ~[na:1.8.0_381]
at java.lang.ClassLoader.defineClass(ClassLoader.java:756) ~[na:1.8.0_381]
Caused by: java.lang.ClassNotFoundException: org.apache.shardingsphere.core.yaml.config.sharding.YamlShardingRuleConfiguration
at java.net.URLClassLoader.findClass(URLClassLoader.java:387) ~[na:1.8.0_381]
YamlShardingRuleConfiguration有该类的话是可以打开的,但是报红了且不能打开
提示还是不够明显,加入以下坐标帮助查询错误信息
org.apache.shardingsphere
sharding-core-common
4.0.0-RC1
清除缓存maven clean+idea Invalidate Cache:
再次运行
Error starting ApplicationContext. To display the conditions report re-run your application with ‘debug’ enabled.
ERROR 17756 — [ main] o.s.boot.SpringApplication : Application run failed
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘dataSource’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [org/apache/shardingsphere/shardingjdbc/spring/boot/SpringBootConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method ‘dataSource’ threw exception; nested exception is java.lang.NoClassDefFoundError: org/apache/shardingsphere/shardingjdbc/api/MasterSlaveDataSourceFactory
Caused by: java.lang.ClassNotFoundException: org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory
at java.net.URLClassLoader.findClass(URLClassLoader.java:387) ~[na:1.8.0_381]
at java.lang.ClassLoader.loadClass(ClassLoader.java:418) ~[na:1.8.0_381]
在查看SpringBootConfiguration.class上面import导入的api都报红了
import org.apache.shardingsphere.shardingjdbc.api.EncryptDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.MasterSlaveDataSourceFactory;
import org.apache.shardingsphere.shardingjdbc.api.ShardingDataSourceFactory;
结论:说明springboot2.4.5应该是不兼容shardingjdbc4.0.0RC1的
解决方法尝试:shardingjdbc4.0.0RC1升级到shardingjdbc4.0.0RC2
思路:再次查看import导入的api是正常且不报红,说明springboot2.4.5兼容shardingjdbc4.0.0RC2,可以继续往下搞
不兼容就没有必要继续
清除缓存maven clean+idea Invalidate Cache:
再次运行
WARN 9112 — [ main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘userController’: Unsatisfied dependency expressed through field ‘dataSource’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘dataSource’ defined in class path resource [com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class]: Invocation of init method failed; nested exception is org.springframework.boot.autoconfigure.jdbc.DataSourceProperties$DataSourceBeanCreationException: Failed to determine a suitable driver class
Failed to configure a DataSource: ‘url’ attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
根据提示
查看com/alibaba/druid/spring/boot/autoconfigure/DruidDataSourceAutoConfigure.class类
这个网友给了一段注释思路:https://blog.csdn.net/evalsys/article/details/107165308
非常感谢
不然真的淦,我查的没有注释
也就是说根据spring.datasource.druid或者spring.datasource查找jdbc属性是找不到的,
因为我们yml文件配置结构是spring.shardingsphere.datasource
解决方式1:如果想继续用druid-spring-boot-starter,则在启动类上排除druid自动配置
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
解决方式2:如果不想更改启动类,则修改pom坐标
com.alibaba druid-spring-boot-starter 1.1.23 改为 com.alibaba druid 1.1.23我选择更改坐标
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.4.5
com.cctv
rw_demo
1.0-SNAPSHOT
1.8
org.apache.shardingsphere
sharding-jdbc-spring-boot-starter
4.0.0-RC2
org.apache.shardingsphere
sharding-core-common
4.0.0-RC2
org.springframework.boot
spring-boot-starter-web
compile
org.projectlombok
lombok
1.18.20
com.alibaba
fastjson
1.2.76
commons-lang
commons-lang
2.6
mysql
mysql-connector-java
runtime
com.baomidou
mybatis-plus-boot-starter
3.4.2
com.alibaba
druid
1.1.23
org.springframework.boot
spring-boot-maven-plugin
2.4.5
原demo+修改后demo链接:https://pan.baidu.com/s/1sy0V3oFh6umIBLAcBBx1Kg?pwd=1234
提取码:1234
①问题描述:2022年出的瑞吉外卖项目shardingjdbc4.0.0-RC1有依赖冲突问题,有类未实现,导致无法正常创建数据源,运行失败
②解决方法
解决方法1:(推荐,亲测可行)
解决方法2:
@SpringBootApplication(exclude = {DruidDataSourceAutoConfigure.class})
慢悠悠搞了三天,很淦
觉得有帮助的老铁,我允许你给个小赞赞鼓励一下!
其他问题–>