sharding jdbc 读写分离
sharding jdbc支持读写分离,可实现一主多从,但不支持多主多从
***************************
数据库
主库 db0: person
从库 db1: person
***************
配置文件
application.yml
spring:
shardingsphere:
datasource:
names: master,slave
master:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db0?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: 123456
slave:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/db1?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8&allowPublicKeyRetrieval=true
username: root
password: 123456
masterslave:
name: master-slave
master-data-source-name: master
slave-data-source-names: slave
props:
sql:
show: true
***************
controller 层
PersonController
@RestController
@RequestMapping("/person")
public class PersonController {
@Autowired
private PersonService personService;
@RequestMapping("/save")
public String save(){
for(int i=0;i<10;i++){
Person person=new Person();
person.setName("瓜田李下"+i);
person.setAge(i+16);
personService.save(person);
}
return "success";
}
@RequestMapping("/get")
public List<Person> get(){
return personService.list();
}
}
*************************
插入读取测试
主库插入测试
从库读取测试