Sharding-Sphere读写分离

风格不统一 提交于 2019-11-30 08:14:09

摘要

示例

脚本

脚本及pom依赖参考:Sharding-JDBC数据分库分表实践(垂直分库分表)

springboot配置


spring.shardingsphere.datasource.names=sharding0,sharding1

spring.shardingsphere.datasource.sharding0.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding0.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding0.url=jdbc:mysql://139.196.229.195:3306/sharding0?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.shardingsphere.datasource.sharding0.username=root
spring.shardingsphere.datasource.sharding0.password=%Pan120%

spring.shardingsphere.datasource.sharding1.type=com.alibaba.druid.pool.DruidDataSource
spring.shardingsphere.datasource.sharding1.driver-class-name=com.mysql.cj.jdbc.Driver
spring.shardingsphere.datasource.sharding1.url=jdbc:mysql://***:3306/sharding1?useUnicode=true&characterEncoding=utf8&autoReconnect=true&allowMultiQueries=true&serverTimezone=Asia/Shanghai&zeroDateTimeBehavior=CONVERT_TO_NULL
spring.shardingsphere.datasource.sharding1.username=root
spring.shardingsphere.datasource.sharding1.password=***

spring.shardingsphere.masterslave.name=ms
spring.shardingsphere.masterslave.master-data-source-name=sharding0
spring.shardingsphere.masterslave.slave-data-source-names=sharding1

spring.shardingsphere.props.sql.show=true

读示例

	@Test
	public void testMSWrite(){
		Order order = new Order();
		order.setUserId(1l);
		order.setOrderId(1l);
		order.setTitle("测试,userId:"+order.getUserId() + " orderId:" + order.getOrderId());
		order.setContent(order.getTitle());
		orderMapper.insert(order);
	}

写示例

@Test
	public void testMSRead(){
		OrderExample ex = new OrderExample();
		ex.createCriteria().andUserIdEqualTo(1l);
		List<Order> orders = orderMapper.selectByExample(ex);
		if(CollectionUtils.isEmpty(orders)){
			System.out.println("查询结果集为空");
			return;
		}
		orders.stream().forEach(o->{
			System.out.println("userId:"+o.getUserId() + " orderId:" + o.getOrderId());
		});
			
	}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!