biz业务工程启动加载不到租户业务库

Blade 未结 2 1058
yinhaibo
yinhaibo 2021-08-19 15:05

一、该问题的重现步骤是什么?

  1. 多租户我采用数据库隔离模式配置,整体流程都是按照手册来进行的,租户已经绑定好数据源,且昨天之前是能进行正常操作的,

    image.pngimage.png

    这是租户库插入的数据,但是查询的时候tenant_id 拦截失效,这是第一个疑问点

    


2 .  今天对biz工程整体进行一次maven clear  maven install 后启动dynamic-datasource无法加载到租户库了,而bladex主工程任意一个工程启动都能加载到租户库

租户绑定数据源

image.png

biz 工程启动加载数据源的日志

image.png

user服务启动加载的数据源日志

image.png

nacos blade.yaml image.png

biz工程maven依赖                      image.png

biz工程controller类加入注解image.png

biz工程实体类image.png



2条回答
  •  admin
    admin (楼主)
    2021-08-20 00:57

    执行mvn clean只是把生成的编译文件都删除,就相当于你在另一个目录重新下载了源码启动。所以应该不是这块的问题。

    麻烦再把详细的重现步骤以及相关截图提供一下,否则我们无法抓到你想提问的点,也很难帮忙排查

    作者追问:2021-08-20 00:57

    新下载一个biz工程,改动如下:

    image.png

    修改nacos sentinel 地址

    image.png

    增加数据库隔离注解

    image.png

    修改端口和数据源配置

    image.png

    blade.yaml 和 blade-dev.yaml 文件都是从bladex主工程拷贝出来的

    blade.yaml修改

    image.png

    blade-dev.yaml  文件

    #spring配置

    spring:

    redis:

    ##redis 单机环境配置

    host192.168.14.206

    port6379

    password:

    database0

    sslfalse

    ##redis 集群环境配置

    #cluster:

    #  nodes: 127.0.0.1:7001,127.0.0.1:7002,127.0.0.1:7003

    #  commandTimeout: 5000

    datasource:

    driver-class-namecom.mysql.cj.jdbc.Driver

    #driver-class-name: org.postgresql.Driver

    #driver-class-name: oracle.jdbc.OracleDriver

    #driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver

    druid:

    # MySql、PostgreSQL、SqlServer校验

    validation-queryselect 1

    # Oracle校验

    #validation-query: select 1 from dual


    #项目模块集中配置

    blade:

    #分布式锁配置

    lock:

    enabledfalse

    addressredis://192.168.14.206:6379

    #多团队协作服务配置

    ribbon:

    rule:

    #开启配置

    enabledtrue

    #负载均衡优先调用的ip段

    prior-ip-pattern:

    192.168.0.*

    127.0.0.1

    #通用开发生产环境数据库地址(特殊情况可在对应的子工程里配置覆盖)

    datasource:

    dev:

    # MySql

    urljdbc:mysql://192.168.14.206:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true

    usernameroot

    passwordroot

    # PostgreSQL

    #url: jdbc:postgresql://127.0.0.1:5432/bladex

    #username: postgres

    #password: 123456

    # Oracle

    #url: jdbc:oracle:thin:@127.0.0.1:1521:orcl

    #username: BLADEX

    #password: BLADEX

    # SqlServer

    #url: jdbc:sqlserver://127.0.0.1:1433;DatabaseName=bladex

    #username: bladex

    #password: bladex

    租户与租户库之间的绑定

    image.png


    image.png

    应该biz  demo  默认是开启了blade-starter-tenant  所以到了此步,不管我有没用@TenantDS启动也会加载key为651437的数据源,然后启动demo日志如下。成功加载到了651437数据源

    image.png

    然后执行maven clear 操作

    image.png

    再次重启demo  报出多数据源的错误

    image.png

    demo默认是开启了多数据源,所以要指定主库

    Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.

    2021-08-20 09:47:49.046 ERROR 20724 --- [           main] o.s.boot.SpringApplication               : Application run failed


    org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dynamicController' defined in file [D:\java\IdeaProjects\BladeX-Biz\blade-service\blade-demo\target\classes\com\example\demo\controller\DynamicController.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dynamicServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'noticeMapper' defined in file [D:\java\IdeaProjects\BladeX-Biz\blade-service\blade-demo\target\classes\com\example\demo\mapper\NoticeMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary

    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:799)

    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:228)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1356)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1203)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)

    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:897)

    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:879)

    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:551)

    at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:141)

    at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:747)

    at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:405)

    at org.springframework.boot.SpringApplication.run(SpringApplication.java:315)

    at org.springframework.boot.builder.SpringApplicationBuilder.run(SpringApplicationBuilder.java:140)

    at org.springblade.core.launch.BladeApplication.run(BladeApplication.java:50)

    at com.example.demo.DemoApplication.main(DemoApplication.java:32)

    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'dynamicServiceImpl': Unsatisfied dependency expressed through field 'baseMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'noticeMapper' defined in file [D:\java\IdeaProjects\BladeX-Biz\blade-service\blade-demo\target\classes\com\example\demo\mapper\NoticeMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)

    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1420)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)

    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)

    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:886)

    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:790)

    ... 19 common frames omitted

    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'noticeMapper' defined in file [D:\java\IdeaProjects\BladeX-Biz\blade-service\blade-demo\target\classes\com\example\demo\mapper\NoticeMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1524)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1404)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:593)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)

    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)

    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)

    ... 33 common frames omitted

    Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [com/baomidou/mybatisplus/autoconfigure/MybatisPlusAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionFactory' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary

    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:799)

    at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:540)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1336)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1176)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:556)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)

    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireByType(AbstractAutowireCapableBeanFactory.java:1509)

    ... 44 common frames omitted

    Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [com/baomidou/dynamic/datasource/spring/boot/autoconfigure/DynamicDataSourceAutoConfiguration.class]: Invocation of init method failed; nested exception is java.lang.RuntimeException: dynamic-datasource Please check the setting of primary

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1794)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:594)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:516)

    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:324)

    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)

    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:322)

    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:202)

    at org.springframework.beans.factory.config.DependencyDescriptor.resolveCandidate(DependencyDescriptor.java:276)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1307)

    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)

    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:886)

    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:790)

    ... 57 common frames omitted

    Caused by: java.lang.RuntimeException: dynamic-datasource Please check the setting of primary

    at com.baomidou.dynamic.datasource.DynamicRoutingDataSource.afterPropertiesSet(DynamicRoutingDataSource.java:253)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1853)

    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1790)

    ... 68 common frames omitted

    于是我放开了多数据源配置

    image.png

    blade-demo.yaml配置如下:

    #自定义配置

    platform:

    namethdata-platform


    #放行配置

    blade:

    secure:

    skip-url:

    /demo/**

    datasource:

    demo:

    master:

    urljdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8

    usernameroot

    passwordroot

    slave:

    urljdbc:mysql://localhost:3306/bladex?useSSL=false&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&transformedBitIsBoolean=true&tinyInt1isBit=false&allowMultiQueries=true&serverTimezone=GMT%2B8

    usernameroot

    passwordroot

    再次启动demo 未加载到651437 数据源

    image.png

    启动多数据源后,无法获取的租户数据源。


    再试试我写的业务系统的,也是在demo 模块基础上做的,用的同一套环境,配置都是一致的。

    启动服务,第一次启动成功加载到651437 

    image.png

    执行maven clean,再次启动,就无法加载到651437 

    image.png

    以上操作和截图是完整的复现步骤,是否是多数据源组件的问题,如果是,不能一起使用吗?


    回答: 2021-08-20 00:57

    收到,回头我本地测试一下看看

    作者追问:2021-08-20 00:57

    您好,请问有结论了吗

    作者追问:2021-08-20 00:57

    您好,请问有结论了吗

提交回复