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、数据库连接性问题
错误原因: 数据库连接不上,要么数据库没开,要么访问不了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.数据库是否支持远程访问(本地安装不算)
=============================================================================
2、兼容性问题排查
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有该类的话是可以打开的,但是报红了且不能打开
提示还是不够明显,加入以下坐标帮助查询错误信息
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-common</artifactId>
<version>4.0.0-RC1</version>
</dependency>
清除缓存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我选择更改坐标
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<groupId>com.cctv</groupId>
<artifactId>rw_demo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-jdbc-spring-boot-starter</artifactId>
<version>4.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>sharding-core-common</artifactId>
<version>4.0.0-RC2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.20</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.76</version>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.23</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.4.5</version>
</plugin>
</plugins>
</build>
</project>
原demo+修改后demo链接:https://pan.baidu.com/s/1sy0V3oFh6umIBLAcBBx1Kg?pwd=1234
提取码:1234
慢悠悠搞了三天,很淦