If you have read Chapter 5, Exploring ShardingSphere Adaptors, or you are somewhat familiar with JDBCs, you may know that they are fairly straightforward and simple to include in your system.
Using ShardingSphere-JDBC won't require you to prepare any additional deployment or services, as you will only need to input the dependencies and configurations into the project. This chapter helps you do just that.
Thanks to this chapter, you will be able to set up and get started with using ShardingSphere-JDBC. Moreover, we have prepared a few bonuses that will provide you with the necessary items to be able to prepare your own custom sharding strategies, configuration, and more.
In this chapter, we are going to cover the following topics:
By the end of the chapter, you'll be ready to start leveraging ShardingSphere-JDBC to take your system to the next level. In the first section, we will start by introducing you to the requirements and the configuration method.
For ShardingSphere-JDBC, you can use Maven to retrieve the dependencies.
To get started, we will go through a two-step procedure. The first part will help you ensure that you have correctly prepared all the requirements, while the second part will give you an overview of the configuration method.
At the very beginning, if you are looking to use ShardingSphere-JDBC, first add a ShardingSphere-JDBC dependency. Take the following Maven dependency as an example:
<dependencies>
<dependency>
<groupId>org.apache.shardingsphere</groupId>
<artifactId>shardingsphere-jdbc-core</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>
Then, create a ShardingSphere-JDBC configuration file. If you choose a Java configuration, skip this step. Take the config-sharding.yaml YAML configuration as an example. First, we need to define mode and dataSources:
mode:
type: Standalone
repository:
type: File
overwrite: true
dataSources:
ds_0:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_0?serverTimezone=UTC&useSSL=false&useUnicode=true&character Encoding=UTF-8
username: root
password:
ds_1:
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
driverClassName: com.mysql.jdbc.Driver
jdbcUrl: jdbc:mysql://localhost:3306/demo_ds_1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
username: root
password:
Then, we define rules and props, as follows:
rules:
- !SHARDING
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database_inline
shardingAlgorithms:
database_inline:
type: INLINE
props:
algorithm-expression: ds_${user_id % 2}
keyGenerators:
snowflake:
type: SNOWFLAKE
props:
worker-id: 123
props:
sql-show: false
Now, you've successfully configured the preliminary setup.
As we mentioned in the introduction, you will find that configuring ShardingSphere-JDBC is very easy. ShardingSphere-JDBC supports the following four configuration methods:
Let's now move on to learn how to configure specific features to be used with ShardingSphere-JDBC. The next section will get you started with data sharding and will be followed by other sections, introducing notable features such as read/write splitting, data encryption, a shadow database, and cluster mode.
In this section, the sharding configurations are provided to help you quickly understand sharding capabilities. ShardingSphere-JDBC provides four configuration methods, and you can choose one of them to quickly access a system.
This part will show some configuration items related to sharding.
In this section's following tables, we will see some specific configuration items for sharding rule configuration.
The main class is ShardingRuleConfiguration:
The following table gives you the binding table configuration:
This is about broadcast table configuration:
This is about strategy configuration:
This is about sharding columns and algorithms:
This is about key generators:
The following tables show you the specific configuration fields for the table-level sharding rules.
The main class is ShardingTableRuleConfiguration, and the following table shows the sharding table configuration:
This is the actual data node configuration:
In the following table, you will find the sharding strategy configuration:
The following table shows the specific configuration items of the auto-sharding tables.
The main class is ShardingAutoTableRuleConfiguration:
This is the strategy configuration:
The following table presents you with the standard sharding strategy configuration items.
The main class is StandardShardingStrategyConfiguration:
This table is about standard complex sharding strategy configuration items.
The main class is ComplexShardingStrategyConfiguration:
This table shows hint sharding strategy configuration items.
The main class is HintShardingStrategyConfiguration:
If you configure a none sharding strategy, then there is nothing else that you would need to configure.
The main class is NoneShardingStrategyConfiguration.
The following table shows the distributed key generation strategy configuration items.
The main class is KeyGenerateStrategyConfiguration:
Now that we have looked at the various Java configuration items, let's look at a few examples. This example shows you how to create a sharding rule using the Java API:
public DataSource getDataSource() throws SQLException {
return ShardingSphereDataSourceFactory.createDataSource(createModeConfiguration(), createDataSourceMap(), Collections.singleton(createShardingRuleConfiguration()), new Properties());
}
The following example shows how to create a sharding rule configuration:
private ShardingRuleConfiguration createShardingRuleConfiguration() {
ShardingRuleConfiguration result = new ShardingRuleConfiguration();
result.getTables().add(getOrderTableRuleConfiguration())
result.getKeyGenerators().put("snowflake", new ShardingSphereAlgorithmConfiguration("SNOWFLAKE", getProperties()));
return result;
}
This one shows how to create a mode configuration:
private static ModeConfiguration createModeConfiguration() {
return new ModeConfiguration("Standalone", new StandalonePersistRepositoryConfiguration("File", new Properties()), true);
}
Lastly, the following example shows how to create a table rule configuration:
private static ShardingTableRuleConfiguration getOrderTableRuleConfiguration() {
ShardingTableRuleConfiguration result = new ShardingTableRuleConfiguration("t_order");
result.setKeyGenerateStrategy(new KeyGenerateStrategyConfiguration("order_id", "snowflake"));
return result;
}
This part will show sharding configuration items in YAML that you can configure.
The following list shows tables that you can configure:
This list introduces Strategy that you can configure:
Remember that defaultDatabaseStrategy is the same as databaseStrategy.
Remember that defaultKeyGenerateStrategy is the same as keyGenerateStrategy.
Then, we have a default sharding column, sharding algorithms, and key generators that you can configure:
Now, let's look at a demo of YAML about a sharding table rule:
tables:
t_order:
actualDataNodes: ds_${0..1}.t_order
keyGenerateStrategy:
column: order_id
keyGeneratorName: snowflake
Let's take a look at an example about strategy:
defaultDatabaseStrategy:
standard:
shardingColumn: user_id
shardingAlgorithmName: database-inline
And lastly, here is an example with the algorithms:
shardingAlgorithms:
database-inline:
type: INLINE
props:
algorithm-expression: ds_${user_id % 2}
This part shows the items that you can configure in springboot:
spring.shardingsphere.rules.sharding.tables.<table-name>.actual-data-nodes
This has a data source name and a table name (separated by decimal points). Multiple table names are separated by commas; inline expressions are supported. By default, the system uses known data sources and logical table names to generate data nodes; it is applied to broadcast tables or situations where sharding databases without table sharding have exactly the same table structure.
Let's look at the following code snippet example, which provides a demo of sharding in Spring Boot:
spring.shardingsphere.rules.sharding.tables.t_order.actual-data-nodes=ds-$->{0..1}.t_order_$->{0..1}
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-column=order_id
spring.shardingsphere.rules.sharding.tables.t_order.table-strategy.standard.sharding-algorithm-name=t-order-inline
This part shows the configuration items that you can configure in SpringNameSpace.
Each table shows the configuration items for different rules, as follows:
Now, let's look at an example of a demo of SpringNameSpace about sharding rules:
<sharding:rule id="shardingRule">
<sharding:table-rules>
<sharding:table-rule logic-table="t_order" actual-data-nodes="demo_ds_${0..1}.t_order_${0..1}" database-strategy-ref="databaseStrategy" table-strategy-ref="orderTableStrategy" key-generate-strategy-ref="orderKeyGenerator" />
</sharding:table-rules>
</sharding:rule>
In this section, the read/write splitting configuration is explained to help you quickly understand its related functions. ShardingSphere-JDBC provides you with four configuration methods, and you can freely choose one of them.
The tables in this section will show you the configuration items related to read/write splitting that you can configure.
In the following table, you will see the primary-secondary data source configuration for the read/write splitting feature, with their respective names, data types, and a description for each:
The following code block provides you with a demonstration that you can refer to for configuring read/write splitting in Java:
ReadwriteSplittingDataSourceRuleConfiguration dataSourceConfig = newReadwriteSplittingDataSourceRuleConfiguration(
"demo_read_query_ds", "", "demo_write_ds", Arrays.asList("demo_read_ds_0", "demo_read_ds_1"), "demo_weight_lb");
Properties algoritProperties = new Properties();
algoritProperties.put("demo_read_ds_0", "2");
algoritProperties.put("demo_read_ds_1", "1");
ShardingSphereAlgorithmConfiguration algorithmConfiguration = newShardingSphereAlgorithmConfiguration("WEIGHT", algoritProperties);
In the list in this section, we will show you the read/write splitting items that you can configure in YAML:
This code block provides you with a demo of read/write splitting configuration in YAML:
dataSources:
pr_ds:
writeDataSourceName: write_ds
readDataSourceNames: [read_ds_0, read_ds_1]
loadBalancerName: weight_lb
loadBalancers:
weight_lb:
type: WEIGHT
props:
read_ds_0: 2
read_ds_1: 1
In the tables in this section, we will show you the items that you can configure in Spring Boot:
This code block provides you with a demo of read/write splitting configuration in Spring Boot:
Spring.shardingsphere.rules.readwrite-splitting.load-balancers.round_robin.type=ROUND_ROBIN
spring.shardingsphere.rules.readwrite-splitting.data-sources.pr_ds.write-data-source-name=write-ds
spring.shardingsphere.rules.readwrite-splitting.data-sources.pr_ds.read-data-source-names=read-ds-0,read-ds-1
spring.shardingsphere.rules.readwrite-splitting.data-sources.pr_ds.load-balancer-name=round_robin
<readwrite-splitting:load-balance-algorithm id="randomStrategy" type="RANDOM" />
<readwrite-splitting:rule id="readWriteSplittingRule">
<readwrite-splitting:data-source-rule id="demo_ds" write-data-source-name="demo_write_ds"read-data-source-names="demo_read_ds_0, demo_read_ds_1" load-balance-algorithm-ref="randomStrategy" />
</readwrite-splitting:rule>
In this section, we will show you the items that you can configure in SpringNameSpace:
This part provides a demo of the read/write splitting rule in SpringNameSpace:
<readwrite-splitting:load-balance-algorithm id="randomStrategy" type="RANDOM" />
<readwrite-splitting:rule id="readWriteSplittingRule">
<readwrite-splitting:data-source-rule id="demo_ds" write-data-source-name="demo_write_ds"read-data-source-names="demo_read_ds_0, demo_read_ds_1" load-balance-algorithm-ref="randomStrategy" />
</readwrite-splitting:rule>
In this section, the data encryption configuration is provided to help you quickly understand the related functions. ShardingSphere-JDBC provides four configuration methods, and you can choose the appropriate one to access the system.
This section gives you the configuration items related to encrypt rules that you can configure:
This code snippet provides a demo of encrypt rules in Java:
EncryptColumnRuleConfiguration columnConfigAes = new EncryptColumnRuleConfiguration("user_name", "user_name", "", "user_name_plain", "name_encryptor");
EncryptTableRuleConfiguration encryptTableRuleConfig = new EncryptTableRuleConfiguration("t_user", Arrays.asList(columnConfigAes, columnConfigTest), null);
encryptAlgorithmConfigs.put("name_encryptor", new ShardingSphereAlgorithmConfiguration("AES", props));
This part shows the configuration items that you can configure:
This code block provides you with a demo of the encrypt rule in YAML:
t_user:
columns:
user_name:
plainColumn: user_name_plain
cipherColumn: user_name
encryptorName: name-encryptor
This is about encryptors:
encryptors:
name-encryptor:
type: AES
props:
aes-key-value: 123456abc
This part shows you the configuration items in Spring Boot:
The following list introduces you to the algorithm configuration items in Spring Boot:
And finally, this gives you the cipher query configuration items with Spring Boot:
This part shows the configuration about encrypt in Spring Boot:
spring.shardingsphere.rules.encrypt.tables.t_user.columns.user_name.cipher-column=user_name
spring.shardingsphere.rules.encrypt.tables.t_user.columns.user_name.encryptor-name=name-encryptor
This part will show the configuration items related to encrypt in SpringNameSpace.
This table shows encrypt rule configuration items. Let's first start with the rules by looking at <encrypt:rule />:
Next, we can move to the table configuration items with <encrypt:table />:
The columns also have their dedicated configuration items with <encrypt:column />:
Finally, let's look at the encryption algorithm's configuration items with <encrypt:encrypt-algorithm />:
This part provides a demo of the encrypt rule in SpringNameSpace:
<encrypt:rule id="encryptRule">
<encrypt:table name="t_user">
<encrypt:column logic-column="user_name" cipher-column="user_name" plain-column="user_name_plain" encrypt-algorithm-ref="name_encryptor" />
</encrypt:table>
</encrypt:rule>
In this section, we will discuss how to use the rule configuration of a shadow database. When using Hint algorithms, you also need to turn on the sqlCommentParseEnabled SQL_PARSER-related configuration item to true.
Here is the configuration item entry – org.apache.shardingsphere.shadow.api.config.ShadowRuleConfiguration:
As an example, let's create a data source with ShadowRule using Java code:
public DataSource getDataSource() throws SQLException {
Map<String, DataSource> dataSourceMap = createDataSourceMap();
Collection<RuleConfiguration> ruleConfigurations = createRuleConfiguration();
return ShardingSphereDataSourceFactory.createDataSource(dataSourceMap, ruleConfigurations, properties);
}
If you are using YAML to configure your shadow database feature, you can refer to !SHADOW and the following list's configuration items:
Here is a YAML configuration example for shadow data sources:
rules:
- !SHADOW
dataSources:
shadowDataSource:
sourceDataSourceName: ds
shadowDataSourceName: ds_shadow
Here are the configuration examples for shadow tables:
tables:
t_user:
dataSourceNames:
- shadowDataSource
shadowAlgorithmNames:
- user_id_insert_value_match-algorithm
Here are the configuration examples for shadow algorithms:
shadowAlgorithms:
user_id_insert_value_match-algorithm:
type: VALUE_MATCH
props:
operation: insert
column: user_id
value: 1
This section introduces you to configuring ShardingSphere's Shadow DB feature while using Spring Boot. If you use Spring Boot, you can refer to the following example code.
Note that to be able to incorporate this code example, you will need to replace boilerplate values such as ds, ds_shadow, and user_id:
spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.source-data-source-name=ds
spring.shardingsphere.rules.shadow.data-sources.shadow-data-source.shadow-data-source-name=ds_shadow
spring.shardingsphere.rules.shadow.tables.t_user.data-source-names=shadow-data-source
spring.shardingsphere.rules.shadow.tables.t_user.shadow-algorithm-names=user_id_insert_value_match-algorithm
spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.type=VALUE_MATCH
spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.props.operation=insert
spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.props.column=user_id
spring.shardingsphere.rules.shadow.shadow-algorithms.user-id-insert-match-algorithm.props.value=1
The following table provides you with the shadow database configuration items for SpringNameSpace:
Let's now look at a SpringNameSpace example, including the SpringNameSpace configuration items that we introduced in the previous table:
<shadow:shadow-algorithm id="user-id-insert-match-algorithm" type="VALUE_MATCH">
<props>
<prop key="operation">insert</prop>
<prop key="column">user_id</prop>
<prop key="value">1</prop>
</props>
</shadow:shadow-algorithm>
<shadow:rule id="shadowRule">
<shadow:data-source id="shadow-data-source" source-data-source-name="ds" shadow-data-source-name="ds_shadow"/>
<shadow:shadow-table name="t_user" data-sources="shadow-data-source">
<shadow:algorithm shadow-algorithm-ref="user-id-insert-match-algorithm" />
</shadow:shadow-table>
</shadow:rule>
<shardingsphere:data-source id="shadowDataSource" data-source-names="ds,ds_shadow" rule-refs="shadowRule">
This section provides the basic operations' mode configurations. In addition to cluster deployment in a production scenario, a corresponding operation mode such as standalone mode for development and automation testing scenarios is also provided for engineers. The three operation modes provided by Apache ShardingSphere are memory mode, standalone mode, and cluster mode. Memory mode is not covered here, as it is the standard auto mode that ShardingSphere uses to run.
The tables in this section will show you the configuration items for modes that you can configure.
The main class is ModeConfiguration:
For Standalone mode, the main class is StandalonePersistRepositoryConfiguration:
When it comes to Cluster mode, the main class is ClusterPersistRepositoryConfiguration:
This part provides you with a demo of modes in Java.
The first example we provide here is code that you can refer to for configuring Standalone mode:
Standalone Mode
private ModeConfiguration getModeConfiguration(final ShardingType shardingType) {
StandalonePersistRepositoryConfiguration standaloneConfig = newStandalonePersistRepositoryConfiguration("File", new Properties());
return new ModeConfiguration("Standalone", standaloneConfig, true);
}
To configure Cluster mode, you can refer to the following code:
private ModeConfiguration getModeConfiguration(final ShardingType shardingType) {
ClusterPersistRepositoryConfiguration clusterPersistRepositoryConfiguration = newClusterPersistRepositoryConfiguration("ZooKeeper", "governance", "127.0.0.1", newProperties());
return new ModeConfiguration("Cluster", clusterPersistRepositoryConfiguration, true);
}
This section provides the configuration items to be used in case you prefer to utilize YAML:
Now that you are aware of the configuration items, we provide you with a demo of mode configuration with YAML.
As with the previous example, we will start off with an example for Standalone mode:
Standalone Mode
mode:
type: Standalone
repository:
type: File
props: Properties of persist repository
path:
overwrite: true
Next, you can refer to the following example to configure Cluster mode with YAML:
// Cluster Mode
mode:
type: Cluster
repository:
type: ZooKeeper
props:
namespace: governance
server-lists: localhost:2181
overwrite: true
This subsection introduces you to the configuration items to be used if you prefer Spring Boot:
This part provides you with a demo of configuring modes with Spring Boot.
The first example illustrates how to configure Standalone mode:
// Standalone Mode
spring.shardingsphere.mode.type=Standalone
spring.shardingsphere.mode.repository.type=File
spring.shardingsphere.mode.repository.props.path=
spring.shardingsphere.mode.overwrite=true
The second example presents you with code to configure Cluster mode:
// Cluster Mode
spring.shardingsphere.mode.type=Cluster
spring.shardingsphere.mode.repository.type=Zookeeper
spring.shardingsphere.mode.repository.props.namespace=governance
spring.shardingsphere.mode.repository.props.server-lists=localhost:2181
spring.shardingsphere.mode.overwrite=true
This part provides a demo of configuring mode in SpringNameSpace. The first example introduces Standalone mode, while the second example introduces how to configure Cluster mode:
<!-- Standalone Mode -->
<shardingsphere:mode type="Standalone" repository-ref="standaloneRepository"overwrite="true"/>
<standalone:repository id="standaloneRepository" type="File">
<props>
<prop key="path"></prop>
</props>
</standalone:repository>
<!-- Cluster Mode -->
<shardingsphere:mode type="Cluster" repository-ref="clusterRepository" overwrite="true"/>
<cluster:repository id="clusterRepository" type="ZooKeeper" namespace="governance" server-lists="localhost:2181">
<props>
<prop key="max-retries">3</prop>
<prop key="operation-timeout-milliseconds">3000</prop>
</props>
</cluster:repository>
This section introduces the props configuration for JDBC access, which includes optimization parameters for ShardingSphere internal functions and some dynamic switch configurations. With the optimization parameters in the props configuration, you can flexibly tune to achieve optimal performance on the JDBC access side, while with the dynamic switch configuration, you can quickly locate issues and improve problem-solving efficiency.
Let's follow our usual flow and start with the Java configuration items first. You will find the items classified by common, optimized, or checked props:
Now that we have looked at the configuration items, let's use them in an example, as follows:
Java
public DataSource getDataSource() throws SQLException {
Properties props = new Properties();
props.put("sql-show", false);
// Add more props
return ShardingSphereDataSourceFactory.createDataSource(createDataSourceMap(), Collections.emptyList() props);
}
Please refer to the preceding section in this chapter.
The following code is an example of YAML:
YAML
props:
sql-show: false
# Add more props
Please refer to the common props table in the preceding Java configuration items section. The configuration items are the same.
The following snippet is an example of Spring Boot:
SQL
spring.shardingsphere.props.sql-show=false
# Add more props
Please refer to all the Java configuration items sections in this chapter.
The following snippet is an example of SpringNameSpace:
XML
<shardingsphere:data-source id="shardingDataSource" data-source-names="ds_0, ds_1" rule-refs="shardingRule">
<props>
<prop key="sql-show">false</prop>
<!-- Add more props -->
</props>
</shardingsphere:data-source>
This section discusses how to achieve rule configuration of data sharding based on read/write splitting. Please note that the sharding data source should be the one aggregated after read/write splitting.
Configuration items are consistent with those used in each feature. For detailed configuration items, please refer to the following sections in this chapter:
The following example introduces the combination of sharding, read/write splitting, and Cluster mode configuration items. These steps will show you how to combine configurations for multiple features:
public final class ShardingConfigurationCreator {
public static ShardingRuleConfiguration create() {
// Create sharding rule configuration , please refer to sharding.
}
}
public final class ReadwriteSplittingConfigurationCreator {
public static ReadwriteSplittingRuleConfiguration create() {
// Create read write splitting rule configuration , please refer to read/write splitting.
}
}
public final class ModeConfigurationCreator {
public static ModeConfiguration create() {
// Create mode configuration , please refer to cluster mode.
}
}
public final class ShardingReadwriteSplittingClusterConfigurationCreator {
public DataSource create() throws SQLException {
return ShardingSphereDataSourceFactory.createDataSource(
ModeConfigurationCreator.create(), createDataSourceMap(),
Arrays.asList(ShardingConfigurationCreator.create(), ReadwriteSplittingConfigurationCreator.create()), createProperties());
}
}
Now that you have understood the basic concept for a multi-feature configuration, let's look at the different examples of configuration file structures in the following subsections.
The yaml file structure for configuring sharding and read/write splitting is as follows:
# Configure cluster mode, please refer to cluster mode.
mode:
type: Cluster
# Configure the required data source
dataSources:
- !SHARDING
# Configure sharding rules, please refer to sharding.
- !READWRITE_SPLITTING
# Configure read/write splitting rules, please refer to read/write splitting.
The properties file structure for configuring sharding and read/write splitting is as follows:
# Configure cluster mode, please refer to cluster mode.
spring.shardingsphere.mode.type=Cluster
# Configure the required data source
spring.shardingsphere.datasource....
# Configure sharding rules, please refer to sharding.
spring.shardingsphere.rules.sharding.tables....
# Configure read/write splitting rules, please refer to read/write splitting.
spring.shardingsphere.rules.readwrite-splitting.data-sources....
The xml file structure for configuring sharding and read/write splitting is as follows:
# Configure cluster mode, please refer to cluster mode.
<shardingsphere:mode />
# Configure the required data source
<bean id="demo_write_ds_0" />
# Configure read/write splitting rules, please refer to read/write splitting.
<readwrite-splitting:rule id="readWriteSplittingRule" />
# Configure sharding rules, please refer to sharding.
<sharding:table-rules>
This section discusses how to use data sharding and data encryption properties together. Please note that you only need to use the logic table name for configuration when writing encryption rules.
Configuration items are consistent with those used in each feature. For detailed configuration items, please refer to the following sections in this chapter:
Let's look at how to implement this multi-configuration with Java first. In the following steps, you can refer to this example to understand the logic to combine the configurations of multiple features:
public final class ShardingConfigurationCreator {
public static ShardingRuleConfiguration create() {
// Create sharding rule configuration, please refer to sharding.
}
}
public final class EncryptRuleConfigurationCreator {
public static EncryptRuleConfiguration create() {
// Create data encryption rule configuration , please refer to encryption.
}
}
public final class ModeConfigurationCreator {
public static ModeConfiguration create() {
// Create mode configuration, please refer to cluster mode.
}
}
public final class ShardingEncryptionClusterConfigurationCreator {
public DataSource create() throws SQLException {
return ShardingSphereDataSourceFactory.createDataSource(
ModeConfigurationCreator.create(), createDataSourceMap(),
Arrays.asList(ShardingConfigurationCreator.create(), EncryptRuleConfigurationCreator.create()), createProperties());
}
Now that you have understood the basic principle for configuring sharding and data encryption together, we can move on to see their implementation with YAML, Spring Boot, and SpringNameSpace.
The yaml file structure for configuring sharding and encryption is as follows:
# Configure cluster mode, please refer to cluster mode.
mode:
type: Cluster
# Configure the required data source
dataSources:
- !SHARDING
# Configure sharding rules, please refer to sharding.
- !ENCRYPT
# Configure encrypt rules, please refer to data encryption.
The properties file structure for configuring sharding and encryption is as follows:
# Configure cluster mode, please refer to cluster mode.
spring.shardingsphere.mode.type=Cluster
# Configure the required data source,
spring.shardingsphere.datasource....
# Configure sharding rules, please refer to sharding.
spring.shardingsphere.rules.sharding.tables....
# Configure encryption rules, please refer to data encryption.
spring.shardingsphere.rules.encypt.tables....
The xml file structure for configuring sharding and encryption is as follows:
# Configure cluster mode, please refer to cluster mode.
<shardingsphere:mode />
# Configure the required data source
<bean id="ds_0" />
# Configure encryption rules, please refer to data encryption.
<encrypt:rule id="encryptRule">
# Configure sharding rules, please refer to sharding.
Depending on your preferred method, you can easily combine the configuration of multiple ShardingSphere features simultaneously, as we saw from the previous examples.
Thanks to this chapter, you have now learned how to configure ShardingSphere-JDBC in multiple ways.
Depending on whether you prefer to work with Java, YAML, SpringNameSpace, or Spring Boot, you are now fully equipped to configure all of the features to match your preferences and requirements.
Understanding this chapter means that you have now mastered how to configure both ShardingSphere-Proxy and JDBC, giving you the power to either choose the client that you prefer or to deploy both for a hybrid architecture.
You may be wondering if that is too advanced, and that is exactly where the next chapter will take you – ShardingSphere's advanced usage and Database Plus.