shardingsphere整合框架使用报错

功能说明

垂直拆库: 仅仅把一个数据库中的多个表放到不同的数据库中
比如: 本来数据库中有2个表'book'和'type', 现在拆分成一个数据库一张表

版本一(5.2.1版本)

JAVA基础编码(正常运行没有错误)

 public static void main(String[] args) throws Exception {
        //设置模式
        StandalonePersistRepositoryConfiguration preConfiguration =
                new StandalonePersistRepositoryConfiguration("JDBC",null);
        ModeConfiguration modeConfiguration  = new ModeConfiguration("Standalone",preConfiguration);

        //设置数据源
        Map<String, DataSource> mapSource = new HashMap<>();
        DruidDataSource bookSource  = new DruidDataSource();
        bookSource.setDriverClassName(Driver.class.getName());
        bookSource.setUrl("jdbc:mysql://192.168.191.148:3306/db_book?serverTimezone=UTC");
        bookSource.setUsername("root");
        bookSource.setPassword("rootroot");

        DruidDataSource typeSource  = new DruidDataSource();
        typeSource.setDriverClassName(Driver.class.getName());
        typeSource.setUrl("jdbc:mysql://192.168.191.148:3307/db_type?serverTimezone=UTC");
        typeSource.setUsername("root");
        typeSource.setPassword("rootroot");
        mapSource.put("xxxx",bookSource);
        mapSource.put("yyyy",typeSource);


        DataSource xyz = ShardingSphereDataSourceFactory.createDataSource("abc",modeConfiguration,mapSource, null, null);

        Connection connection = xyz.getConnection();
        System.out.println(connection);
        connection.prepareStatement("insert into book values(1,'九阴真经')").execute();
        connection.prepareStatement("insert into type values(1,'武侠')").execute();
        connection.close();

命名空间写法就报错了

环境  spring springmvc jpa shardingsphere-core spring-namespace

1. 核心依赖

    <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core</artifactId>
            <version>5.2.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.apache.shardingsphere/shardingsphere-jdbc-core-spring-namespace -->
        <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core-spring-namespace</artifactId>
            <version>5.2.1</version>
        </dependency>

2. 核心配置

<standard:repository  id="standaloneRepository" type="JDBC"></standard:repository>

    <bean id="xxxx" class="com.zaxxer.hikari.HikariDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://192.168.191.148:3306/db_book?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="rootroot"/>
    </bean>
    
    <bean id="yyyy" class="com.zaxxer.hikari.HikariDataSource">
        <property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql://192.168.191.148:3307/db_type?serverTimezone=UTC"/>
        <property name="username" value="root"/>
        <property name="password" value="rootroot"/>
    </bean>
    
    <sharding:rule id="shardingRule">
    </sharding:rule>

    <shardingsphere:data-source id="shardingDatasource" data-source-names="xxxx, yyyy" rule-refs="shardingRule">
        <shardingsphere:mode type="Standalone" repository-ref="standaloneRepository"/>
    </shardingsphere:data-source>

3. 启动报错:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in class path resource [spring-jpa.xml]: Cannot resolve reference to bean 'shardingDatasource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDatasource': Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource]: Constructor threw exception; nested exception is java.lang.RuntimeException: Failed to get driver instance for jdbcUrl=jdbc:h2:mem:config;DB_CLOSE_DELAY=-1;DATABASE_TO_UPPER=false;MODE=MYSQL
		at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:342)
		at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveValueIfNecessary(BeanDefinitionValueResolver.java:113)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1707)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1452)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
		at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
		at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
		at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
		at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
		at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
		at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1154)
		at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:908)
		at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)

版本二(5.3.1版本)

这个版本 不支持命名空间的写法了 使用Driver+yaml的方式替代

1. 核心依赖

    <dependency>
            <groupId>org.apache.shardingsphere</groupId>
            <artifactId>shardingsphere-jdbc-core</artifactId>
            <version>5.3.1</version>
        </dependency>

2. yml配置


# 逻辑库的库名  不写默认值:logic_db
databaseName: abc
## 配置单机模式 不配置默认就是单机
mode:
  type: Standalone
  repository:
    type: JDBC

dataSources:
  xxxx: # 名称:等价于Map集合中的key
    dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.191.148:3306/db_book?serverTimezone=UTC
    username: root
    password: rootroot
  yyyy:
    dataSourceClassName: com.alibaba.druid.pool.DruidDataSource
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://192.168.191.148:3307/db_type?serverTimezone=UTC
    username: root
    password: rootroot

3. java写法(正常运行没有报错)

   public static void main(String[] args) throws SQLException, IOException {

        String path = _1_VerticalShardingDataBaseDemo.class.getResource("/abc.yml").getPath();
        File file = new File(path);
        DataSource dataSource = YamlShardingSphereDataSourceFactory.createDataSource(file);
        Connection connection = dataSource.getConnection();
        System.out.println(connection);
        connection.prepareStatement("insert into book values(4,'九阴真经')").execute();
        connection.prepareStatement("insert into type values(4,'武侠')").execute();
        connection.close();
    }

4. 整合写法报错

    <bean id="shardingDatasource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
        <property name="driverClass" value="org.apache.shardingsphere.driver.ShardingSphereDriver"/>
        <property name="url" value="jdbc:shardingsphere:classpath:abc.yml"/>

    </bean>

5. 报错信息

问题说明

5.2.1版本 同样的功能 javaAPI的方式运行成功 命名空间的方式运行失败
5.3.1版本 同样的功能 java读取yml文件运行成功  使用bean的方式整合 运行失败

总结一句话:同样的功能 java基础编码是成功的 整合时会报错 怎么解决呢
京ICP备2021015875号