摘要: 本文介紹SpringBoot使用噹噹Sharding-JDBC進行讀寫分離。 1.有關Sharding-JDBC 本文仍是基於噹噹網Sharding-Jdbc的依賴,與上一篇使用Sharding-Jdbc進行分庫分表依賴一致,而且本文大體內容與上一篇文章類似,建議先查看個人另外一篇在查看這篇會簡單許多,傳送門《SpringBoot使用Sharding-JDBC分庫分表》。
本文介紹SpringBoot使用噹噹Sharding-JDBC進行讀寫分離。
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
|
package
com.dangdang.ddframe.rdb.sharding.api.strategy.slave;
public
enum
MasterSlaveLoadBalanceStrategyType {
ROUND_ROBIN(
new
RoundRobinMasterSlaveLoadBalanceStrategy()),
RANDOM(
new
RandomMasterSlaveLoadBalanceStrategy());
private
final
MasterSlaveLoadBalanceStrategy strategy;
public
static
MasterSlaveLoadBalanceStrategyType getDefaultStrategyType() {
return
ROUND_ROBIN;
}
private
MasterSlaveLoadBalanceStrategyType(MasterSlaveLoadBalanceStrategy strategy) {
this
.strategy = strategy;
}
public
MasterSlaveLoadBalanceStrategy getStrategy() {
return
this
.strategy;
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
CREATE DATABASE database0;
USE database0;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id bigint(
64
) not
null
,
city varchar(
20
) not
null
,
name varchar(
20
) not
null
,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
CREATE DATABASE database1;
USE database1;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id bigint(
64
) not
null
,
city varchar(
20
) not
null
,
name varchar(
20
) not
null
,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `database1`.`user`(`id`, `city`, `name`) VALUES (
101
,
'beijing'
,
'dalaoyang1'
);
CREATE DATABASE database2;
USE database2;
DROP TABLE IF EXISTS `user`;
CREATE TABLE `user`(
id bigint(
64
) not
null
,
city varchar(
20
) not
null
,
name varchar(
20
) not
null
,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `database2`.`user`(`id`, `city`, `name`) VALUES (
102
,
'beijing'
,
'dalaoyang2'
);
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
<?
xml
version
=
"1.0"
encoding
=
"UTF-8"
?>
<
project
xmlns
=
"http://maven.apache.org/POM/4.0.0"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation
=
"http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<
modelVersion
>4.0.0</
modelVersion
>
<
parent
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-parent</
artifactId
>
<
version
>2.0.3.RELEASE</
version
>
<
relativePath
/>
<!-- lookup parent from repository -->
</
parent
>
<
groupId
>com.dalaoyang</
groupId
>
<
artifactId
>springboot2_shardingjdbc_dxfl</
artifactId
>
<
version
>0.0.1-SNAPSHOT</
version
>
<
name
>springboot2_shardingjdbc_dxfl</
name
>
<
description
>springboot2_shardingjdbc_dxfl</
description
>
<
properties
>
<
java.version
>1.8</
java.version
>
</
properties
>
<
dependencies
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-data-jpa</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-web</
artifactId
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-devtools</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>mysql</
groupId
>
<
artifactId
>mysql-connector-java</
artifactId
>
<
scope
>runtime</
scope
>
</
dependency
>
<
dependency
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-starter-test</
artifactId
>
<
scope
>test</
scope
>
</
dependency
>
<!-- lombok -->
<
dependency
>
<
groupId
>org.projectlombok</
groupId
>
<
artifactId
>lombok</
artifactId
>
<
optional
>true</
optional
>
</
dependency
>
<!-- druid -->
<
dependency
>
<
groupId
>com.alibaba</
groupId
>
<
artifactId
>druid</
artifactId
>
<
version
>1.1.9</
version
>
</
dependency
>
<!-- sharding-jdbc -->
<
dependency
>
<
groupId
>com.dangdang</
groupId
>
<
artifactId
>sharding-jdbc-core</
artifactId
>
<
version
>1.5.4</
version
>
</
dependency
>
</
dependencies
>
<
build
>
<
plugins
>
<
plugin
>
<
groupId
>org.springframework.boot</
groupId
>
<
artifactId
>spring-boot-maven-plugin</
artifactId
>
</
plugin
>
</
plugins
>
</
build
>
</
project
>
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
##Jpa配置
spring.jpa.database=mysql
spring.jpa.show-sql=
true
spring.jpa.hibernate.ddl-auto=none
##數據庫配置
##數據庫database0地址
database0.url=jdbc:mysql:
//localhost:3306/database0?characterEncoding=utf8&useSSL=false
##數據庫database0用戶名
database0.username=root
##數據庫database0密碼
database0.password=root
##數據庫database0驅動
database0.driverClassName=com.mysql.jdbc.Driver
##數據庫database0名稱
database0.databaseName=database0
##數據庫database1地址
database1.url=jdbc:mysql:
//localhost:3306/database1?characterEncoding=utf8&useSSL=false
##數據庫database1用戶名
database1.username=root
##數據庫database1密碼
database1.password=root
##數據庫database1驅動
database1.driverClassName=com.mysql.jdbc.Driver
##數據庫database1名稱
database1.databaseName=database1
##數據庫database2地址
database2.url=jdbc:mysql:
//localhost:3306/database2?characterEncoding=utf8&useSSL=false
##數據庫database1用戶名
database2.username=root
##數據庫database1密碼
database2.password=root
##數據庫database1驅動
database2.driverClassName=com.mysql.jdbc.Driver
##數據庫database1名稱
database2.databaseName=database2
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
package
com.dalaoyang;
import
org.springframework.boot.SpringApplication;
import
org.springframework.boot.autoconfigure.SpringBootApplication;
import
org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import
org.springframework.boot.context.properties.EnableConfigurationProperties;
import
org.springframework.transaction.annotation.EnableTransactionManagement;
@SpringBootApplication
(exclude = {DataSourceAutoConfiguration.
class
})
@EnableTransactionManagement
(proxyTargetClass =
true
)
@EnableConfigurationProperties
public
class
Springboot2ShardingjdbcDxflApplication {
public
static
void
main(String[] args) {
SpringApplication.run(Springboot2ShardingjdbcDxflApplication.
class
, args);
}
}
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
|
package
com.dalaoyang.entity;
import
lombok.Data;
import
javax.persistence.*;
@Entity
@Table
(name=
"user"
)
@Data
public
class
User {
@Id
private
Long id;
private
String city;
private
String name;
}
|
做者簡介:小黑哎代碼,一個有故事的程序員。但願用文字,讓你讀懂代碼世界。微信號gzitcast,歡迎你們找我獲取各類資源。
java