由于项目需要,springboot项目需基于jdk17环境开发,结合nacos2.0.3、分布式事务seata-server-1.7.1、分库分表shardingSphere5.2.1等,项目启动过程中出现的问题解决方式小结。
问题一:
Caused by: java.lang.RuntimeException: java.lang.reflect.InaccessibleObjectException: Unable to make field protected java.lang.reflect.InvocationHandler java.lang.reflect.Proxy.h accessible: module java.base does not "opens java.lang.reflect" to unnamed module @194fad1at io.seata.spring.annotation.GlobalTransactionScanner.wrapIfNecessary(GlobalTransactionScanner.java:322)at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:318)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.postProcessObjectFromFactoryBean(AbstractAutowireCapableBeanFactory.java:1885)at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:105)... 46 more
Caused by: java.lang.reflect.InaccessibleObjectException: Unable to make field protected java.lang.reflect.InvocationHandler java.lang.reflect.Proxy.h accessible: module java.base does not "opens java.lang.reflect" to unnamed module @194fad1at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:354)at java.base/java.lang.reflect.AccessibleObject.checkCanSetAccessible(AccessibleObject.java:297)at java.base/java.lang.reflect.Field.checkCanSetAccessible(Field.java:178)at java.base/java.lang.reflect.Field.setAccessible(Field.java:172)at io.seata.spring.util.SpringProxyUtils.getAdvisedSupport(SpringProxyUtils.java:94)at io.seata.spring.util.SpringProxyUtils.findInterfaces(SpringProxyUtils.java:64)at io.seata.spring.annotation.GlobalTransactionScanner.wrapIfNecessary(GlobalTransactionScanner.java:289)... 50 more
编辑项目启动配置:Edit Configurations:
注意:刚打开没有Add VM options输入框,可快捷键Alt+V输入:
--add-opens java.base/java.lang.reflect=ALL-UNNAMED,如下所示:
问题二:(此问题研究了好久,尝试了网上提到的各种解决办法都没成功,最终还是结合自己的经验判断搞定)
Caused by: org.springframework.aop.framework.AopConfigException: Could not generate CGLIB subclass of class org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource: Common causes of this problem include using a final class or a non-visible classat org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:216)at org.springframework.aop.framework.CglibAopProxy.getProxy(CglibAopProxy.java:158)at org.springframework.aop.framework.ProxyFactory.getProxy(ProxyFactory.java:110)at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.buildProxy(AbstractAutoProxyCreator.java:517)at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.createProxy(AbstractAutoProxyCreator.java:464)at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.wrapIfNecessary(AbstractAutoProxyCreator.java:369)at io.seata.spring.annotation.datasource.SeataAutoDataSourceProxyCreator.wrapIfNecessary(SeataAutoDataSourceProxyCreator.java:82)at org.springframework.aop.framework.autoproxy.AbstractAutoProxyCreator.postProcessAfterInitialization(AbstractAutoProxyCreator.java:318)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsAfterInitialization(AbstractAutowireCapableBeanFactory.java:434)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1773)at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:598)... 83 more
Caused by: java.lang.IllegalArgumentException: Cannot subclass final class org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSourceat org.springframework.cglib.proxy.Enhancer.generateClass(Enhancer.java:653)at org.springframework.cglib.core.DefaultGeneratorStrategy.generate(DefaultGeneratorStrategy.java:26)at org.springframework.cglib.core.ClassLoaderAwareGeneratorStrategy.generate(ClassLoaderAwareGeneratorStrategy.java:57)at org.springframework.cglib.core.AbstractClassGenerator.generate(AbstractClassGenerator.java:362)at org.springframework.cglib.proxy.Enhancer.generate(Enhancer.java:575)at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.lambda$new$1(AbstractClassGenerator.java:103)at org.springframework.cglib.core.internal.LoadingCache.lambda$createEntry$1(LoadingCache.java:52)at java.base/java.util.concurrent.FutureTask.run$$$capture(FutureTask.java:264)at java.base/java.util.concurrent.FutureTask.run(FutureTask.java)at org.springframework.cglib.core.internal.LoadingCache.createEntry(LoadingCache.java:57)at org.springframework.cglib.core.internal.LoadingCache.get(LoadingCache.java:34)at org.springframework.cglib.core.AbstractClassGenerator$ClassLoaderData.get(AbstractClassGenerator.java:126)at org.springframework.cglib.core.AbstractClassGenerator.create(AbstractClassGenerator.java:313)at org.springframework.cglib.proxy.Enhancer.createHelper(Enhancer.java:562)at org.springframework.cglib.proxy.Enhancer.createClass(Enhancer.java:407)at org.springframework.aop.framework.ObjenesisCglibAopProxy.createProxyClassAndInstance(ObjenesisCglibAopProxy.java:62)at org.springframework.aop.framework.CglibAopProxy.buildProxy(CglibAopProxy.java:213)... 93 more
看到错误中显示shardingsphere未成功代理到数据源。
开始是判断可能数据库连接配置可能有问题,开始测试,修改一个错误连接,看到错误为不能找到数据源,这说明已经可以连接数据源了,只是连接数据源后可能哪边导致了问题。
经过多次查看控制台信息,发现部分还是跟seata相关,通过在nacos中多次引入通用seata配置还是提示未成功代理数据源,于是通过查看以前成功启动的seata配置,讲通用seata配置移植到开发环境yml配置中,成功启动。
整体上看,主要原因是seata配置未能成功加载导致,seata跟shardingsphere有关联。具体关联后续再细致研究。
注意:在调试sql执行过程中发现sql日志打印不了,最终发现未配置logback-spring.xml日志导致。