Closed abcdwd closed 2 months ago
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc</artifactId>
<version>5.5.0</version>
<exclusions>
<exclusion>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-test-util</artifactId>
</exclusion>
</exclusions>
@Configuration public class ShardingSphereJDBCConfig { @Bean(name = "shardingDataSource") public DataSource shardingDataSource(@Qualifier("masterDataSource") DataSource masterDataSource, @Qualifier("rpaDataSource") DataSource rpaDataSource) throws SQLException { Map<String, DataSource> dataSourceMap = new HashMap<>(); dataSourceMap.put("ds0", masterDataSource); dataSourceMap.put("ds1", rpaDataSource);
// 获取数据源对象
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, getTableRuleConfigurations(), getProperties());
}
private List<RuleConfiguration> getTableRuleConfigurations() {
return Collections.singletonList(createShardingRuleConfiguration());
}
private ShardingRuleConfiguration createShardingRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
//使用自动分片表规则列表
// result.getAutoTables().add(ShardingAutoTableRuleConfiguration)
//分片表规则列表
result.getTables().add(getOrderTableRuleConfiguration());
result.getTables().add(getOrderTableRuleConfiguration2());
//绑定表规则列表
// result.getBindingTableGroups().add(ShardingTableReferenceRuleConfiguration); // result.getBindingTableGroups().add(new ShardingTableReferenceRuleConfiguration("foo", "t_order, t_order_item"));
//添加分片算法
putShardingAlgorithms(result.getShardingAlgorithms());
//主键生成策略 雪花算法
result.getKeyGenerators().put("snowflake", new AlgorithmConfiguration("SNOWFLAKE", new Properties()));
//审计策略 用于检查DML(数据操作语言如INSERT, UPDATE, DELETE)语句是否包含了必要的分片键,确保操作的正确性。这里的具体审计逻辑由DML_SHARDING_CONDITIONS算法控制。
result.getAuditors().put("sharding_key_required_auditor", new AlgorithmConfiguration("DML_SHARDING_CONDITIONS", new Properties()));
return result;
}
private void putShardingAlgorithms(Map<String, AlgorithmConfiguration> shardingAlgorithms) {
// 分片算法 key=自定义 value=AlgorithmConfiguration(算法名称,配置)
shardingAlgorithms.put("interval", new AlgorithmConfiguration("INTERVAL", getProps1()));//基于固定时间范围的分片算法 IntervalShardingAlgorithm类
shardingAlgorithms.put("inline1", new AlgorithmConfiguration("INLINE", getProps2()));//基于行表达式的分片算法 InlineShardingAlgorithm类
shardingAlgorithms.put("inline2", new AlgorithmConfiguration("INLINE", getProps3()));
}
private static Properties getProps1() {
Properties props1 = new Properties();
props1.setProperty("datetime-pattern", "yyyy-MM-dd HH:mm:ss");
props1.setProperty("datetime-lower", "2024-01-01 00:00:00");//分片的起始时间范围,时间戳格式:yyyy-MM-dd HH:mm:ss
props1.setProperty("datetime-upper", "2025-01-01 00:00:00");//分片的结束时间范围,时间戳格式:yyyy-MM-dd HH:mm:ss
props1.setProperty("sharding-suffix-pattern", "yyyyMM");//分片数据源或真实表的后缀格式,必须遵循 Java DateTimeFormatter 的格式,必须和 datetime-interval-unit 保持一致。例如:yyyyMM
props1.setProperty("datetime-interval-amount", "6");//分片键时间间隔,超过该时间间隔将进入下一分片
props1.setProperty("datetime-interval-unit", "MONTHS");//分片键时间间隔单位,必须遵循 Java ChronoUnit 的枚举值。
return props1;
}
private static Properties getProps2() {
Properties props = new Properties();
props.setProperty("algorithm-expression", "ds$->{user_id % 2}");
return props;
}
private static Properties getProps3() {
Properties props = new Properties();
props.setProperty("algorithm-expression", "sys_order_$->{order_id % 2}");
return props;
}
//sys_order_test
private ShardingTableRuleConfiguration getOrderTableRuleConfiguration2() {
ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("sys_order_test", "ds$->{0}.sys_order_test_$->{[202401,202407,202501]}");
result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), false));
result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("create_time", "interval"));
return result;
}
//sys_order
private ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("sys_order", "ds$->{0..1}.sys_order_$->{0..1}");
result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
result.setAuditStrategy(new ShardingAuditStrategyConfiguration(Collections.singleton("sharding_key_required_auditor"), false));
result.setDatabaseShardingStrategy(new StandardShardingStrategyConfiguration("user_id", "inline1"));
result.setTableShardingStrategy(new StandardShardingStrategyConfiguration("order_id", "inline2"));
return result;
}
private Properties getProperties() {
Properties shardingProperties = new Properties();
shardingProperties.put("sql.show", true);
return shardingProperties;
}
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource) {
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
setDataSource(targetDataSources, DataSourceType.PROFIT.name(), "profitDataSource");
setDataSource(targetDataSources, DataSourceType.RPA.name(), "rpaDataSource");
setDataSource(targetDataSources, DataSourceType.SHARDING.name(), "shardingDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources);
}
zip file closed
. Is your config file existed?
Is it same with #31593?
17:40:28.271 [main] INFO c.l.w.c.c.XxlJobConfig - [xxlJobExecutor,47] - ====xxl-job config init==== 17:40:28.273 [main] INFO c.l.w.c.c.XxlJobConfig - [xxlJobExecutor,56] - ====xxl-job config start success==== 17:40:28.301 [main] ERROR c.x.j.c.util.IpUtil - [getLocalAddress0,108] - ecs-336e-1105011: ecs-336e-1105011: Name or service not known java.net.UnknownHostException: ecs-336e-1105011: ecs-336e-1105011: Name or service not known at java.net.InetAddress.getLocalHost(InetAddress.java:1505) at com.xxl.job.core.util.IpUtil.getLocalAddress0(IpUtil.java:102) at com.xxl.job.core.util.IpUtil.getLocalAddress(IpUtil.java:161) at com.xxl.job.core.util.IpUtil.getIp(IpUtil.java:172) at com.xxl.job.core.executor.XxlJobExecutor.initEmbedServer(XxlJobExecutor.java:142) at com.xxl.job.core.executor.XxlJobExecutor.start(XxlJobExecutor.java:81) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeCustomInitMethod(AbstractAutowireCapableBeanFactory.java:1930) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1872) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1800) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:620) 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.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:453) at org.springframework.boot.SpringApplication.run(SpringApplication.java:343) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1359) at com.lmyk.LmykApplication.main(LmykApplication.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) Caused by: java.net.UnknownHostException: ecs-336e-1105011: Name or service not known at java.net.Inet6AddressImpl.lookupAllHostAddr(Native Method) at java.net.InetAddress$2.lookupAllHostAddr(InetAddress.java:928) at java.net.InetAddress.getAddressesFromNameService(InetAddress.java:1323) at java.net.InetAddress.getLocalHost(InetAddress.java:1500) ... 36 common frames omitted 17:40:28.317 [main] WARN o.s.b.w.s.c.AnnotationConfigServletWebServerApplicationContext - [refresh,591] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSource' defined in class path resource [com/lmyk/framework/config/sharding/ShardingSphereJDBCConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'shardingDataSource' threw exception; nested exception is java.lang.IllegalStateException: zip file closed 17:40:28.317 [main] INFO c.x.j.c.s.EmbedServer - [stop,125] - >>>>>>>>>>> xxl-job remoting server destroy success. 17:40:28.318 [Thread-5] INFO c.x.j.c.s.EmbedServer - [run,96] - >>>>>>>>>>> xxl-job remoting server stop. 17:40:28.318 [xxl-job, executor JobLogFileCleanThread] INFO c.x.j.c.t.JobLogFileCleanThread - [run,99] - >>>>>>>>>>> xxl-job, executor JobLogFileCleanThread thread destory. 17:40:28.320 [xxl-job, executor TriggerCallbackThread] INFO c.x.j.c.t.TriggerCallbackThread - [run,98] - >>>>>>>>>>> xxl-job, executor callback thread destory. 17:40:28.320 [Thread-4] INFO c.x.j.c.t.TriggerCallbackThread - [run,128] - >>>>>>>>>>> xxl-job, executor retry callback thread destory. 17:40:28.393 [main] INFO sys-user - [shutdownAsyncManager,31] - ====关闭后台任务任务线程池==== 17:40:28.394 [main] WARN o.s.c.a.CommonAnnotationBeanPostProcessor - [postProcessBeforeDestruction,185] - Destroy method on bean with name 'shutdownManager' threw an exception: java.lang.ExceptionInInitializerError 17:40:28.404 [main] INFO c.a.d.p.DruidDataSource - [close,2043] - {dataSource-0} closing ... 17:40:28.405 [main] INFO c.a.d.p.DruidDataSource - [close,2043] - {dataSource-0} closing ... 17:40:28.405 [main] INFO c.a.d.p.DruidDataSource - [close,2043] - {dataSource-1} closing ... 17:40:28.413 [main] INFO c.a.d.p.DruidDataSource - [close,2116] - {dataSource-1} closed 17:40:28.533 [main] INFO o.a.c.c.StandardService - [log,173] - Stopping service [Tomcat] 17:40:28.562 [main] ERROR o.s.b.SpringApplication - [reportFailure,870] - Application run failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'shardingDataSource' defined in class path resource [com/lmyk/framework/config/sharding/ShardingSphereJDBCConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'shardingDataSource' threw exception; nested exception is java.lang.IllegalStateException: zip file closed at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:658) at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:638) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1352) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1195) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:582) 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.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:953) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:145) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:780) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:453) at org.springframework.boot.SpringApplication.run(SpringApplication.java:343) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1370) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1359) at com.lmyk.LmykApplication.main(LmykApplication.java:18) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) at org.springframework.boot.loader.JarLauncher.main(JarLauncher.java:51) Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [javax.sql.DataSource]: Factory method 'shardingDataSource' threw exception; nested exception is java.lang.IllegalStateException: zip file closed at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:185) at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:653) ... 27 common frames omitted Caused by: java.lang.IllegalStateException: zip file closed at java.util.zip.ZipFile.ensureOpen(ZipFile.java:686) at java.util.zip.ZipFile.access$200(ZipFile.java:60) at java.util.zip.ZipFile$ZipEntryIterator.(ZipFile.java:499)
at java.util.zip.ZipFile.entries(ZipFile.java:552)
at java.util.jar.JarFile.access$001(JarFile.java:71)
at java.util.jar.JarFile$JarEntryIterator.(JarFile.java:250)
at java.util.jar.JarFile$JarEntryIterator.(JarFile.java:247)
at java.util.jar.JarFile.stream(JarFile.java:279)
at org.apache.shardingsphere.infra.util.directory.ClasspathResourceDirectoryReader.readDirectoryInJar(ClasspathResourceDirectoryReader.java:134)
at org.apache.shardingsphere.infra.util.directory.ClasspathResourceDirectoryReader.lambda$read$0(ClasspathResourceDirectoryReader.java:122)
at java.util.stream.ReferencePipeline$7$1.accept(ReferencePipeline.java:267)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1359)
at java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$0(StreamSpliterators.java:294)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
at java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
at java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:300)
at java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
at org.apache.shardingsphere.mode.repository.standalone.jdbc.sql.JDBCRepositorySQLLoader.load(JDBCRepositorySQLLoader.java:54)
at org.apache.shardingsphere.mode.repository.standalone.jdbc.JDBCRepository.init(JDBCRepository.java:60)
at org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader.findDefaultService(TypedSPILoader.java:73)
at org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader.findService(TypedSPILoader.java:57)
at org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader.getService(TypedSPILoader.java:110)
at org.apache.shardingsphere.mode.manager.standalone.StandaloneContextManagerBuilder.build(StandaloneContextManagerBuilder.java:48)
at org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource.createContextManager(ShardingSphereDataSource.java:79)
at org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource.(ShardingSphereDataSource.java:67)
at org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory.createDataSource(ShardingSphereDataSourceFactory.java:95)
at org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory.createDataSource(ShardingSphereDataSourceFactory.java:78)
at org.apache.shardingsphere.driver.api.ShardingSphereDataSourceFactory.createDataSource(ShardingSphereDataSourceFactory.java:139)
at com.lmyk.framework.config.sharding.ShardingSphereJDBCConfig.shardingDataSource(ShardingSphereJDBCConfig.java:42)
at com.lmyk.framework.config.sharding.ShardingSphereJDBCConfig$$EnhancerBySpringCGLIB$$9fd27476.CGLIB$shardingDataSource$0()
at com.lmyk.framework.config.sharding.ShardingSphereJDBCConfig$$EnhancerBySpringCGLIB$$9fd27476$$FastClassBySpringCGLIB$$8d23e3e6.invoke()
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:331)
at com.lmyk.framework.config.sharding.ShardingSphereJDBCConfig$$EnhancerBySpringCGLIB$$9fd27476.shardingDataSource()
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 28 common frames omitted