Sharding-Proxy 配置(读写分离)
1 、创建三个数据
2 、修改 conf 里面配置文件, config-master-slave.yaml
第一个主服务器,后面是从服务器
schemaName: master_slave_db dataSources: master_ds: url: jdbc:mysql://127.0.0.1:3306/demo_ds_master?serverTimezone=UTC&useSSL=false username: root password: root connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 slave_ds_0: url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_0?serverTimezone=UTC&useSSL=false username: root password: root connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 slave_ds_1: url: jdbc:mysql://127.0.0.1:3306/demo_ds_slave_1?serverTimezone=UTC&useSSL=false username: root password: root connectionTimeoutMilliseconds: 30000 idleTimeoutMilliseconds: 60000 maxLifetimeMilliseconds: 1800000 maxPoolSize: 50 masterSlaveRule: name: ms_ds masterDataSourceName: master_ds slaveDataSourceNames: slave_ds_0 slave_ds_1
3 、启动 Sharding-Proxy 服务
4 、通过 cmd 连接 Sharding-Proxy ,进行创建表和添加记录操作
( 1 )在主数据库和从数据库里面,都创建数据库表
( 2 )向表添加记录,不指定向哪个库添加
把添加数据添加到主数据库里面(查询默认去查从库是查不到的)
( 3 )查询数据库表数据,不指定查询哪个库 (查询默认去查从库是查不到的随机去查从库的数据)
在springboot里操作sharding-proxy
pom
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jdbc</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</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> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.1</version> <scope>test</scope> </dependency>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3307/sharding_db spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.username=root spring.datasource.password=root
position实体类
@Entity @Table(name="position") 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; //getter and setter ... }
dao
public interface PositionRepository extends JpaRepository<Position,Long> { }
启动类
@SpringBootApplication public class RunBoot { }
测试类
@RunWith(SpringRunner.class) @SpringBootTest(classes = RunBoot.class) public class TestPosition { @Resource private PositionRepository positionRepository; @Test public void test1(){ List<Position> list = positionRepository.findAll(); list.forEach(position -> System.out.println(position.getId()+" "+position.getName()+" "+position.getSalary())); } @Test public void test2(){ Position position = new Position(); position.setName("Tom"); position.setSalary("20000"); position.setCity("Shanghai"); positionRepository.save(position); } }