创建类并实现接口、创建配置信息(指定强制路由的库:五种分片策略)、添加测试类
1、创建自定义的分配类
package com.slin.hint;import org.apache.shardingsphere.api.sharding.hint.HintShardingAlgorithm;import org.apache.shardingsphere.api.sharding.hint.HintShardingValue;import java.util.ArrayList;import java.util.Collection;public class MyHintShardingAlgorithm implements HintShardingAlgorithm<Long> {@Overridepublic Collection<String> doSharding(Collection<String> collection, // 对那些数据表进行分片HintShardingValue<Long> hintShardingValue) {// 具体的分配值Collection<String> result =new ArrayList<>();// 循环对谁进行分片for(String each: collection){// 循环拿到分片键的值for (Long value: hintShardingValue.getValues()){//如果满足指定的条件,就将结果返回回去进行路由操作if(each.endsWith(String.valueOf(value % 2))){//如果ds0满足了,则就会路由到ds0这个库中result.add(each);}}}return result;}}
2、创建并添加配置信息
a、主配置文件,指向新的配置文件
在resource包下面创建application-hint-database.properties配置文件
b、给新建的文件添加信息

完整的配置信息如下
######datasource(数据源,有两个库)#######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#数据库连接(如果是服务器的就只需要把localhost改为服务器的ip即可)spring.shardingsphere.datasource.ds0.jdbc-url=jdbc:mysql://localhost:3306/lagou1#数据库用户和密码spring.shardingsphere.datasource.ds0.username=rootspring.shardingsphere.datasource.ds0.password=root######配置第二个库#######spring.shardingsphere.datasource.ds1.type=com.zaxxer.hikari.HikariDataSourcespring.shardingsphere.datasource.ds1.driver-class-name=com.mysql.jdbc.Driverspring.shardingsphere.datasource.ds1.jdbc-url=jdbc:mysql://localhost:3306/lagou2spring.shardingsphere.datasource.ds1.username=rootspring.shardingsphere.datasource.ds1.password=root######配置自定义的强制路由类使用是(包名.类名 )#####hintspring.shardingsphere.sharding.tables.city.database-strategy.hint.algorithm-class-name=com.slin.hint.MyHintShardingAlgorithm
3、添加测试
package com.slin.test;
import com.slin.RunBoot;
import com.slin.entity.City;
import com.slin.repository.CityRepository;
import org.apache.shardingsphere.api.hint.HintManager;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.List;
//通过它来启动
@RunWith(SpringRunner.class)
//通过RunBoot来创建boot容器
@SpringBootTest(classes = RunBoot.class)
public class HintShardingAlgorithmTest {
@Resource
private CityRepository cityRepository;
@Test
public void HintDBTest(){
HintManager hinstance = HintManager.getInstance();
//指定路由库用set、如果是路由库的表用add
// 强制路由到ds0库中
hinstance.setDatabaseShardingValue(0L);
List<City> all = cityRepository.findAll();
System.out.println("**************start********************");
all.forEach(city -> {
System.out.println(city.getId()+" "+city.getName()+" "+city.getProvince());
});
System.out.println("**************end********************");
}
}
4、展示结果
从主库lagou1也就是ds0中查询出的结果和数据库的结果一致

总结:主要和强制指定的那个库、以及自定义的路由算法
强制路由库和表也可以按照同样的思路去做即可
