sharding-jdbc在目前微服務和各類saas軟件等數據量日益加大的狀況下變得使用的人多了起來。java
數據庫實體node
@Data @Table(name = "city") @Entity public class City implements Serializable { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long id; @Column(name = "name") private String name; @Column(name = "province") private String province; }
Jpa操做類mysql
public interface CityRepository extends JpaRepository<City,Long> { }
SpringBoot啓動類git
@SpringBootApplication @EnableTransactionManagement public class RunBoot { public static void main(String[] args) { SpringApplication.run(RunBoot.class,args); } }
具體讀寫分離配置github
spring.shardingsphere.props.sql.show=true spring.shardingsphere.datasource.names=master,slave0 spring.shardingsphere.datasource.master.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.master.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.master.jdbc-url=jdbc:mysql://localhost:3306/test1 spring.shardingsphere.datasource.master.username=root spring.shardingsphere.datasource.master.password=351848327 spring.shardingsphere.datasource.slave0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.slave0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.slave0.jdbc-url=jdbc:mysql:/localhost:3306/test2 spring.shardingsphere.datasource.slave0.username=root spring.shardingsphere.datasource.slave0.password=351848327 #master-slave spring.shardingsphere.masterslave.name=datasource spring.shardingsphere.masterslave.master-data-source-name=master spring.shardingsphere.masterslave.slave-data-source-names=slave0 spring.shardingsphere.masterslave.load-balance-algorithm-type=ROUND_ROBIN spring.shardingsphere.sharding.tables.city.key-generator.column=id spring.shardingsphere.sharding.tables.city.key-generator.type=SNOWFLAKE
測試類和方法面試
@RunWith(SpringRunner.class) @SpringBootTest(classes = RunBoot.class) public class TestMasterSlave { @Resource CityRepository cityRepository; @Test public void testMasterSlave(){ City city = new City(); city.setName("shanghai"); city.setProvince("shanghai"); cityRepository.save(city); } @Test public void findAll(){ List<City> list = cityRepository.findAll(); list.forEach(c->{ System.out.println(c.getId()+" "+c.getName()+" "+c.getProvince()); }); } }
分別執行兩個方法能夠看到日誌,實際操做寫的時候是在master庫,操做讀的時候是在slave庫
spring
這種方法主要是針對那些數據量大,數據沒法及時同步到從庫,能夠直接從主庫讀取
具體配置sql
spring.shardingsphere.props.sql.show=true spring.shardingsphere.datasource.names=ds0,ds1 spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/test1 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=351848327 spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/test2 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=351848327 spring.shardingsphere.sharding.tables.city.database-strategy.hint.algorithm-class-name=com.zhao.hint.MyHint
具體Hint的規則數據庫
public class MyHint implements HintShardingAlgorithm<Long> { @Override public Collection<String> doSharding(Collection<String> targetNames, HintShardingValue<Long> hintShardingValue) { Collection<String> results = new ArrayList<>(); for (String each :targetNames){ for (Long value:hintShardingValue.getValues()){ if (each.endsWith(String.valueOf(value%2))){ results.add(each); } } } return results; } }
Hint測試類express
@RunWith(SpringRunner.class) @SpringBootTest(classes = RunBoot.class) public class TestHint { @Resource CityRepository cityRepository; @Test public void testHint(){ HintManager hintManager =HintManager.getInstance(); hintManager.setDatabaseShardingValue(0L); List<City> list = cityRepository.findAll(); System.out.println("主庫表大小"+list.size()); hintManager.setDatabaseShardingValue(1L); List<City> list1 = cityRepository.findAll(); System.out.println("從庫表大小"+list1.size()); } }
執行後能夠看到兩次查詢分別從主庫和從庫中獲取了數據
分庫分表中要處理的包括id生成,分表列,廣播表,字表跟隨父表進行分表等配置
測試試題類
@Data @Table(name = "position") @Entity @ToString public class Position implements Serializable { @Id @Column(name = "Id") @GeneratedValue(strategy = GenerationType.IDENTITY) private Long Id; @Column(name = "name") private String name; @Column(name = "salary") private String salary; @Column(name = "city") private String city; } @Data @Table(name = "position_detail") @Entity public class PositionDetail implements Serializable { @Id @GeneratedValue(strategy = GenerationType.IDENTITY) @Column(name = "id") private Long id; @Column(name = "pid") private Long pid; @Column(name = "description") private String description; } @Data @Table(name = "b_order") @Entity public class Border implements Serializable { @Id @Column(name = "id") @GeneratedValue(strategy = GenerationType.IDENTITY) private long id; @Column(name="is_del") private Boolean isDel; @Column(name = "company_id") private Integer companyId; @Column(name = "position_id") private long positionId; @Column(name = "user_id") private Integer userId; @Column(name = "publish_user_id") private Integer publishUserId; @Column(name = "resume_type") private Integer resumeType; @Column(name = "status") private String status; @Column(name = "create_time") private Date createTime; @Column(name = "operate_time") private Date operateTime; @Column(name = "work_year") private String workYear; @Column(name = "name") private String name; @Column(name = "position_name") private String positionName; @Column(name = "resume_id") private Integer resumeId; }
spring.shardingsphere.datasource.names=ds0,ds1 spring.shardingsphere.datasource.ds0.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds0.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/test1 spring.shardingsphere.datasource.ds0.username=root spring.shardingsphere.datasource.ds0.password=351848327 spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSource spring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driver spring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://rlocalhost:3306/test2 spring.shardingsphere.datasource.ds1.username=root spring.shardingsphere.datasource.ds1.password=351848327 #sharding spring.shardingsphere.sharding.tables.position.database-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.position.database-strategy.inline.algorithm-expression=ds$->{id % 2} spring.shardingsphere.sharding.tables.position.key-generator.column=id #spring.shardingsphere.sharding.tables.position.key-generator.type=SNOWFLAKE spring.shardingsphere.sharding.tables.position.key-generator.type=zhao-sharding-key spring.shardingsphere.sharding.tables.position_detail.database-strategy.inline.sharding-column=pid spring.shardingsphere.sharding.tables.position_detail.database-strategy.inline.algorithm-expression=ds$->{pid % 2} spring.shardingsphere.sharding.tables.position_detail.key-generator.column=id #spring.shardingsphere.sharding.tables.position.key-generator.type=SNOWFLAKE spring.shardingsphere.sharding.tables.position_detail.key-generator.type=zhao-sharding-key #broadcast spring.shardingsphere.sharding.broadcast-tables=city spring.shardingsphere.sharding.tables.city.key-generator.column=id spring.shardingsphere.sharding.tables.city.key-generator.type=SNOWFLAKE #sharding-database-table spring.shardingsphere.sharding.tables.b_order.database-strategy.inline.sharding-column=company_id spring.shardingsphere.sharding.tables.b_order.database-strategy.inline.algorithm-expression=ds$->{company_id % 2} spring.shardingsphere.sharding.tables.b_order.table-strategy.inline.sharding-column=id spring.shardingsphere.sharding.tables.b_order.table-strategy.inline.algorithm-expression=b_order$->{id % 2} spring.shardingsphere.sharding.tables.b_order.actual-data-nodes=ds${0..1}.b_order${0..1} spring.shardingsphere.sharding.tables.b_order.key-generator.column=id spring.shardingsphere.sharding.tables.b_order.key-generator.type=SNOWFLAKE
自定義id生成器
@Slf4j public class MyShardingId implements ShardingKeyGenerator { private SnowflakeShardingKeyGenerator shardingKeyGenerator = new SnowflakeShardingKeyGenerator(); @Override public Comparable<?> generateKey() { log.info("執行了自定義的id生成器"); return shardingKeyGenerator.generateKey(); } @Override public String getType() { return "zhao-sharding-key"; } @Override public Properties getProperties() { return null; } @Override public void setProperties(Properties properties) { } }
執行的測試方法
@RunWith(SpringRunner.class) @SpringBootTest(classes = RunBoot.class) public class TestingShardingDatabase { @Resource PositionRepository positionRepository; @Resource PositionDetailRepository positionDetailRepository; @Resource CityRepository cityRepository; @Resource BorderRepository borderRepository; /** * 測試是否分庫 */ @Test public void testAdd(){ for (long i=1;i<=20;i++){ Position position = new Position(); //position.setId(i); position.setCity("shanghai"); position.setName("zhao"); position.setSalary("100.86"); positionRepository.save(position); } } /** * 分庫時主表和字表配置的規則同樣,因此這裏測試的是主表和字表在同庫對應 */ @Test public void testAddDetail(){ for (long i=1;i<=20;i++){ Position position = new Position(); //position.setId(i); position.setCity("shanghai"); position.setName("zhao"); position.setSalary("100.86"); positionRepository.save(position); PositionDetail detail = new PositionDetail(); detail.setPid(position.getId()); detail.setDescription("This is message "+i); positionDetailRepository.save(detail); } } /** * 測試加載數據分庫時走的那個庫 */ @Test public void testLoadData(){ Object object = positionRepository.findPositionLoadById(607972934107004929L); Object[] objects = (Object[]) object; System.out.println(objects[0]+" "+objects[1]); } /** * 廣播表會寫入多個庫 */ @Test public void testBroadCast(){ City city = new City(); city.setName("shanghai"); city.setProvince("shanghai"); cityRepository.save(city); } /** * 測試分表時如何插入 */ @Test @Repeat(100) public void testShardingBorder(){ Random random = new Random(); int companyId = random.nextInt(10); Border border = new Border(); border.setIsDel(false); border.setCompanyId(companyId); border.setCreateTime(new Date()); border.setName("zhao"); border.setWorkYear("2"); borderRepository.save(border); } }
以上操做依賴的pom文件爲
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> <version>2.2.5.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <version>2.2.5.RELEASE</version> <scope>test</scope> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.48</version> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <version>1.18.20</version> </dependency> <dependency> <groupId>org.apache.shardingsphere</groupId> <artifactId>sharding-jdbc-spring-boot-starter</artifactId> <version>4.1.0</version> </dependency> <!--<dependency>--> <!--<groupId>org.apache.shardingsphere</groupId>--> <!--<artifactId>sharding-transaction-xa-core</artifactId>--> <!--<version>4.1.0</version>--> <!--</dependency>--> <!--<dependency>--> <!--<groupId>org.apache.shardingsphere</groupId>--> <!--<artifactId>sharding-transaction-base-saga</artifactId>--> <!--</dependency>--> </dependencies>
以上就是sharding-jdbc的一些基礎使用的介紹。本文github地址https://github.com/zhendiao/d...
歡迎搜索關注本人與朋友共同開發的微信面經小程序【大廠面試助手】和公衆號【微瞰技術】