New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
default-database-strategy:hint hint can't use #10699
Comments
like #5970 |
@stelin Can you submit this issue in English? |
@strongduanmu |
Hi @stelin, I used the version public class HintTest {
public static void main(String[] args) throws SQLException {
DataSource dataSource = new HintTest().getShardingDataSource();
Connection connection = dataSource.getConnection();
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM t_order");
HintManager.getInstance().setDatabaseShardingValue(1);
ResultSet resultSet = preparedStatement.executeQuery();
while (resultSet.next()) {
System.out.println(resultSet.getString("ORDER_ID") + " " + resultSet.getString("USER_ID"));
}
HintManager.getInstance().close();
resultSet.close();
preparedStatement.close();
connection.close();
}
private DataSource getShardingDataSource() throws SQLException {
ShardingRuleConfiguration shardingRuleConfig = new ShardingRuleConfiguration();
shardingRuleConfig.getTableRuleConfigs().add(getOrderTableRuleConfiguration());
shardingRuleConfig.getTableRuleConfigs().add(getOrderItemTableRuleConfiguration());
shardingRuleConfig.getBindingTableGroups().add("t_order, t_order_item");
shardingRuleConfig.getBroadcastTables().add("t_config");
shardingRuleConfig.setDefaultDatabaseShardingStrategyConfig(new HintShardingStrategyConfiguration(new AnnotationHintShardingAlgorithm()));
shardingRuleConfig.setDefaultTableShardingStrategyConfig(new InlineShardingStrategyConfiguration("order_id", "ds_${order_id % 2}"));
Properties props = new Properties();
props.setProperty("sql.show", "true");
return ShardingDataSourceFactory.createDataSource(createDataSourceMap(), shardingRuleConfig, props);
}
private static KeyGeneratorConfiguration getKeyGeneratorConfiguration() {
KeyGeneratorConfiguration result = new KeyGeneratorConfiguration("SNOWFLAKE", "order_id");
return result;
}
TableRuleConfiguration getOrderTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order", "ds_${0..1}.t_order_${0..1}");
result.setKeyGeneratorConfig(getKeyGeneratorConfiguration());
return result;
}
TableRuleConfiguration getOrderItemTableRuleConfiguration() {
TableRuleConfiguration result = new TableRuleConfiguration("t_order_item", "ds_${0..1}.t_order_item_${0..1}");
return result;
}
Map<String, DataSource> createDataSourceMap() {
Map<String, DataSource> result = new HashMap<>();
result.put("ds_0", DataSourceUtil.createDataSource("demo_ds_0"));
result.put("ds_1", DataSourceUtil.createDataSource("demo_ds_1"));
return result;
}
}
public final class DataSourceUtil {
private static final String HOST = "localhost";
private static final int PORT = 3306;
private static final String USER_NAME = "root";
private static final String PASSWORD = "123456";
public static DataSource createDataSource(final String dataSourceName) {
HikariDataSource result = new HikariDataSource();
result.setDriverClassName(com.mysql.jdbc.Driver.class.getName());
result.setJdbcUrl(String.format("jdbc:mysql://%s:%s/%s?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8", HOST, PORT, dataSourceName));
result.setUsername(USER_NAME);
result.setPassword(PASSWORD);
return result;
}
}
public class AnnotationHintShardingAlgorithm implements HintShardingAlgorithm<Integer> {
@Override
public Collection<String> doSharding(final Collection<String> availableTargetNames, final HintShardingValue<Integer> shardingValue) {
for (String each : availableTargetNames) {
if (each.endsWith(String.valueOf(shardingValue.getValues().iterator().next() % 2))) {
return Collections.singletonList(each);
}
}
return null;
}
} |
When i set
When i set
When i don't set
So I want to know what is your configuration? What is the abnormal phenomenon? |
I work will like your method. but I do not configure |
It's very troublesome, because every table must to be configured. |
We only want to switch datasource(database), It has nothing to do with tables. |
@stelin Do you mean that your tables are all non-sharding tables, so |
@stelin In version 4.x, you need to configure |
Yes my tables are all non-sharding tables. Now in version 4.x is not supported! But version 5.x do not released. If 5.x can use? |
@stelin In version 4.x, you can configure |
I configured shardingRuleConfig.setDefaultDataSourceName("ds_0"), But can not to switch datasource; |
Want to switch datasource by application context(like http header) |
hint:algorithm-class-name can not worker for this. |
Single table can not solved.My scenario is one datasource has many tables. Olny sharding for database(datasource). Tables not to sharding |
@stelin If you need sharding, then you need to configure sharding rules. You can either shard the database and the table at the same time, or you can only shard the database or only the table. Without configuration rules, the Hint sharding algorithm will definitely not take effect. |
If only want to sharding for database(datasource), then with configuration table rules is very troublesome. If add one table ,must to add configuration |
@stelin If you have a better solution about sharding api, please submit an issue to the community for discussion. |
Now can not use shardingjdbc to my scenario. You have solution? |
@stelin As I said before, configure sharding rules. |
Understanded |
@stelin If this issue has been resolved, please close it. If you have a better idea, please submit an issue to discuss about sharding api. |
A a better method to supported switch datasource without any table sharding rules configuration. |
But now is not supported. |
@stelin Your idea is to change the sharding api and the underlying implementation, which needs to be discussed by the community. |
Thanks |
What are the results of your discussion? Can supported table match rules, like below
|
@stelin How did you solve it in the end? I have the same problem |
In version 4.1.1,when i add spring.shardingsphere.sharding.default-data-source-name=ds0.when start service,meta data load cost about 204022 milliseconds,then service starts very slowly. how to solution? |
I have the same problem too |
@tuziaifendou I think you can submit a new issue to describe your problem. |
您好,我现在正在工作中,无法亲自回复您的邮件。我将在工作结束后,尽快给您回复。
|
ShardingSphere-JDBC 4.1.1 default-database-strategy:hint can't use
spring:
shardingsphere:
sharding:
default-data-source-name: demo-1
default-database-strategy:
hint:
algorithm-class-name: com.ai94.mysql.annotation.AnnotationHintShardingAlgorithm
datasource:
names: demo-1,demo-2
datasource one
demo-1:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/db_A?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
datasource two
demo-2:
type: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/db_B?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC
username: root
password: root
The text was updated successfully, but these errors were encountered: