[BAEL-4897] Shardingshere (#14470)
* [BAEL-4897] Shardingshere * [BAEL-4897] Fixes * [BAEL-4897] delete read me
This commit is contained in:
parent
17e24fc87e
commit
fa88f6c181
@ -97,6 +97,7 @@
|
|||||||
<module>spring-data-rest-2</module>
|
<module>spring-data-rest-2</module>
|
||||||
<module>spring-data-rest-querydsl</module>
|
<module>spring-data-rest-querydsl</module>
|
||||||
<module>spring-data-solr</module>
|
<module>spring-data-solr</module>
|
||||||
|
<module>spring-data-shardingsphere</module>
|
||||||
<!-- <module>spring-hibernate-3</module> FAILED -->
|
<!-- <module>spring-hibernate-3</module> FAILED -->
|
||||||
<!-- <module>spring-hibernate-5</module> FAILED --> <!-- long running -->
|
<!-- <module>spring-hibernate-5</module> FAILED --> <!-- long running -->
|
||||||
<module>spring-jpa</module>
|
<module>spring-jpa</module>
|
||||||
|
80
persistence-modules/spring-data-shardingsphere/pom.xml
Normal file
80
persistence-modules/spring-data-shardingsphere/pom.xml
Normal file
@ -0,0 +1,80 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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>
|
||||||
|
<artifactId>spring-data-shardingsphere</artifactId>
|
||||||
|
<version>1.0</version>
|
||||||
|
<name>spring-data-shardingsphere</name>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
|
||||||
|
<parent>
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>parent-boot-3</artifactId>
|
||||||
|
<version>0.0.1-SNAPSHOT</version>
|
||||||
|
<relativePath>../../parent-boot-3</relativePath>
|
||||||
|
</parent>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<shardingsphere.version>5.3.2</shardingsphere.version>
|
||||||
|
<mysql.version>8.0.33</mysql.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencyManagement>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>testcontainers-bom</artifactId>
|
||||||
|
<version>1.18.3</version>
|
||||||
|
<type>pom</type>
|
||||||
|
<scope>import</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</dependencyManagement>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>mysql</groupId>
|
||||||
|
<artifactId>mysql-connector-java</artifactId>
|
||||||
|
<version>${mysql.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.shardingsphere</groupId>
|
||||||
|
<artifactId>shardingsphere-jdbc-core</artifactId>
|
||||||
|
<version>${shardingsphere.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.testcontainers</groupId>
|
||||||
|
<artifactId>mysql</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-surefire-plugin</artifactId>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
</project>
|
@ -0,0 +1,12 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
import org.springframework.boot.SpringApplication;
|
||||||
|
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||||
|
|
||||||
|
@SpringBootApplication
|
||||||
|
public class Main {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
SpringApplication.run(Main.class, args);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,110 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
import jakarta.persistence.Column;
|
||||||
|
import jakarta.persistence.Entity;
|
||||||
|
import jakarta.persistence.EnumType;
|
||||||
|
import jakarta.persistence.Enumerated;
|
||||||
|
import jakarta.persistence.Id;
|
||||||
|
import jakarta.persistence.Table;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
|
|
||||||
|
@Entity
|
||||||
|
@Table(name = "`order`")
|
||||||
|
public class Order {
|
||||||
|
|
||||||
|
@Id
|
||||||
|
@Column(name = "order_id")
|
||||||
|
private Long orderId;
|
||||||
|
|
||||||
|
@Column(name = "customer_id")
|
||||||
|
private Long customerId;
|
||||||
|
|
||||||
|
@Column(name = "total_price")
|
||||||
|
private BigDecimal totalPrice;
|
||||||
|
|
||||||
|
@Enumerated(EnumType.STRING)
|
||||||
|
@Column(name = "order_status")
|
||||||
|
private Status orderStatus;
|
||||||
|
|
||||||
|
@Column(name = "order_date")
|
||||||
|
private LocalDate orderDate;
|
||||||
|
|
||||||
|
@Column(name = "delivery_address")
|
||||||
|
private String deliveryAddress;
|
||||||
|
|
||||||
|
public Long getOrderId() {
|
||||||
|
return orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderId(Long orderId) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getCustomerId() {
|
||||||
|
return customerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCustomerId(Long customerId) {
|
||||||
|
this.customerId = customerId;
|
||||||
|
}
|
||||||
|
|
||||||
|
public BigDecimal getTotalPrice() {
|
||||||
|
return totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setTotalPrice(BigDecimal totalPrice) {
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Status getOrderStatus() {
|
||||||
|
return orderStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderStatus(Status orderStatus) {
|
||||||
|
this.orderStatus = orderStatus;
|
||||||
|
}
|
||||||
|
|
||||||
|
public LocalDate getOrderDate() {
|
||||||
|
return orderDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrderDate(LocalDate orderDate) {
|
||||||
|
this.orderDate = orderDate;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getDeliveryAddress() {
|
||||||
|
return deliveryAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDeliveryAddress(String deliveryAddress) {
|
||||||
|
this.deliveryAddress = deliveryAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected Order() {}
|
||||||
|
|
||||||
|
public Order(Long orderId, Long customerId, BigDecimal totalPrice, Status orderStatus, LocalDate orderDate, String deliveryAddress) {
|
||||||
|
this.orderId = orderId;
|
||||||
|
this.customerId = customerId;
|
||||||
|
this.totalPrice = totalPrice;
|
||||||
|
this.orderStatus = orderStatus;
|
||||||
|
this.orderDate = orderDate;
|
||||||
|
this.deliveryAddress = deliveryAddress;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean equals(Object o) {
|
||||||
|
if (this == o) return true;
|
||||||
|
if (o == null || getClass() != o.getClass()) return false;
|
||||||
|
Order order = (Order) o;
|
||||||
|
return Objects.equals(orderId, order.orderId);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int hashCode() {
|
||||||
|
return Objects.hash(orderId);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,26 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.http.ResponseEntity;
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/orders")
|
||||||
|
public class OrderController {
|
||||||
|
|
||||||
|
private final OrderService orderService;
|
||||||
|
|
||||||
|
public OrderController(OrderService orderService) {
|
||||||
|
this.orderService = orderService;
|
||||||
|
}
|
||||||
|
|
||||||
|
@PostMapping
|
||||||
|
public ResponseEntity<Order> createOrder(@RequestBody Order order) {
|
||||||
|
return ResponseEntity.ok(orderService.createOrder(order));
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/{id}")
|
||||||
|
public ResponseEntity<Order> getOrder(@PathVariable Long id) {
|
||||||
|
return ResponseEntity.ok(orderService.getOrder(id));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
import org.springframework.data.jpa.repository.JpaRepository;
|
||||||
|
|
||||||
|
public interface OrderRepository extends JpaRepository<Order, Long> { }
|
@ -0,0 +1,22 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class OrderService {
|
||||||
|
|
||||||
|
private final OrderRepository orderRepository;
|
||||||
|
|
||||||
|
public OrderService(OrderRepository orderRepository) {
|
||||||
|
this.orderRepository = orderRepository;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Order createOrder(Order order) {
|
||||||
|
return orderRepository.save(order);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Order getOrder(Long id) {
|
||||||
|
return orderRepository.findById(id)
|
||||||
|
.orElseThrow(() -> new IllegalArgumentException("Order not found"));
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,6 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
|
||||||
|
public enum Status {
|
||||||
|
PROCESSING, SHIPPED, DELIVERED, CANCELLED
|
||||||
|
}
|
@ -0,0 +1,10 @@
|
|||||||
|
spring:
|
||||||
|
datasource:
|
||||||
|
driver-class-name: org.apache.shardingsphere.driver.ShardingSphereDriver
|
||||||
|
url: jdbc:shardingsphere:classpath:sharding.yml
|
||||||
|
jpa:
|
||||||
|
properties:
|
||||||
|
hibernate:
|
||||||
|
dialect: org.hibernate.dialect.MySQL8Dialect
|
||||||
|
hibernate:
|
||||||
|
ddl-auto: create-drop
|
@ -0,0 +1,31 @@
|
|||||||
|
dataSources:
|
||||||
|
ds0:
|
||||||
|
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
|
||||||
|
driverClassName: com.mysql.jdbc.Driver
|
||||||
|
jdbcUrl: jdbc:mysql://localhost:13306/ds0?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||||
|
username: test
|
||||||
|
password: test
|
||||||
|
ds1:
|
||||||
|
dataSourceClassName: com.zaxxer.hikari.HikariDataSource
|
||||||
|
driverClassName: com.mysql.jdbc.Driver
|
||||||
|
jdbcUrl: jdbc:mysql://localhost:13307/ds1?serverTimezone=UTC&useSSL=false&useUnicode=true&characterEncoding=UTF-8
|
||||||
|
username: test
|
||||||
|
password: test
|
||||||
|
rules:
|
||||||
|
- !SHARDING
|
||||||
|
tables:
|
||||||
|
order:
|
||||||
|
actualDataNodes: ds${0..1}.order
|
||||||
|
defaultDatabaseStrategy:
|
||||||
|
standard:
|
||||||
|
shardingColumn: order_id
|
||||||
|
shardingAlgorithmName: database_inline
|
||||||
|
defaultTableStrategy:
|
||||||
|
none:
|
||||||
|
shardingAlgorithms:
|
||||||
|
database_inline:
|
||||||
|
type: INLINE
|
||||||
|
props:
|
||||||
|
algorithm-expression: ds${order_id % 2}
|
||||||
|
props:
|
||||||
|
sql-show: false
|
@ -0,0 +1,86 @@
|
|||||||
|
package com.baeldung.shardingsphere;
|
||||||
|
|
||||||
|
import org.assertj.core.api.Assertions;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.boot.test.context.SpringBootTest;
|
||||||
|
import org.springframework.test.context.DynamicPropertyRegistry;
|
||||||
|
import org.springframework.test.context.DynamicPropertySource;
|
||||||
|
import org.testcontainers.containers.MySQLContainer;
|
||||||
|
import org.testcontainers.junit.jupiter.Container;
|
||||||
|
import org.testcontainers.junit.jupiter.Testcontainers;
|
||||||
|
|
||||||
|
import java.math.BigDecimal;
|
||||||
|
import java.sql.Connection;
|
||||||
|
import java.sql.DriverManager;
|
||||||
|
import java.sql.PreparedStatement;
|
||||||
|
import java.sql.ResultSet;
|
||||||
|
import java.sql.SQLException;
|
||||||
|
import java.time.LocalDate;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Testcontainers
|
||||||
|
@SpringBootTest
|
||||||
|
class OrderServiceIntegrationTest {
|
||||||
|
|
||||||
|
@Container
|
||||||
|
static MySQLContainer<?> mySQLContainer1 = new MySQLContainer<>("mysql:8.0.23")
|
||||||
|
.withDatabaseName("ds0")
|
||||||
|
.withUsername("test")
|
||||||
|
.withPassword("test");
|
||||||
|
|
||||||
|
@Container
|
||||||
|
static MySQLContainer<?> mySQLContainer2 = new MySQLContainer<>("mysql:8.0.23")
|
||||||
|
.withDatabaseName("ds1")
|
||||||
|
.withUsername("test")
|
||||||
|
.withPassword("test");
|
||||||
|
|
||||||
|
static {
|
||||||
|
mySQLContainer2.setPortBindings(List.of("13307:3306"));
|
||||||
|
mySQLContainer1.setPortBindings(List.of("13306:3306"));
|
||||||
|
}
|
||||||
|
@Autowired
|
||||||
|
private OrderService orderService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private OrderRepository orderRepository;
|
||||||
|
|
||||||
|
@DynamicPropertySource
|
||||||
|
static void setProperties(DynamicPropertyRegistry registry) {
|
||||||
|
registry.add("spring.jpa.hibernate.ddl-auto", () -> "create-drop");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void shouldFindOrderInCorrectShard() {
|
||||||
|
// given
|
||||||
|
Order order1 = new Order(1L, 1L, BigDecimal.TEN, Status.PROCESSING, LocalDate.now(), "123 Main St");
|
||||||
|
Order order2 = new Order(2L, 2L, BigDecimal.valueOf(12.5), Status.SHIPPED, LocalDate.now(), "456 Main St");
|
||||||
|
|
||||||
|
// when
|
||||||
|
Order savedOrder1 = orderService.createOrder(order1);
|
||||||
|
Order savedOrder2 = orderService.createOrder(order2);
|
||||||
|
|
||||||
|
// then
|
||||||
|
// Assuming the sharding strategy is based on the order id, data for order1 should be present only in ds0
|
||||||
|
// and data for order2 should be present only in ds1
|
||||||
|
Assertions.assertThat(orderService.getOrder(savedOrder1.getOrderId())).isEqualTo(savedOrder1);
|
||||||
|
Assertions.assertThat(orderService.getOrder(savedOrder2.getOrderId())).isEqualTo(savedOrder2);
|
||||||
|
|
||||||
|
// Verify that the orders are not present in the wrong shards.
|
||||||
|
// You would need to implement these methods in your OrderService.
|
||||||
|
// They should use a JdbcTemplate or EntityManager to execute SQL directly against each shard.
|
||||||
|
Assertions.assertThat(assertOrderInShard(savedOrder1, mySQLContainer2)).isTrue();
|
||||||
|
Assertions.assertThat(assertOrderInShard(savedOrder2, mySQLContainer1)).isTrue();
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean assertOrderInShard(Order order, MySQLContainer<?> container) {
|
||||||
|
try (Connection conn = DriverManager.getConnection(container.getJdbcUrl(), container.getUsername(), container.getPassword())) {
|
||||||
|
PreparedStatement stmt = conn.prepareStatement("SELECT * FROM `order` WHERE order_id = ?");
|
||||||
|
stmt.setLong(1, order.getOrderId());
|
||||||
|
ResultSet rs = stmt.executeQuery();
|
||||||
|
return rs.next();
|
||||||
|
} catch (SQLException ex) {
|
||||||
|
throw new RuntimeException("Error querying order in shard", ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user