Spring boot项目集成Sharding Jdbc

  环境
  
  jdk:1.8
  
  framework: spring boot, sharding jdbc
  
  database: MySQL
  
  搭建步骤
  
  在pom 中加入sharding 依赖
  
  <dependency>
  
  <groupId>io.shardingjdbc</groupId>
  
  <artifactId>sharding-jdbc-core-spring-boot-starter</artifactId>
  
  <version>2.0.3</version>
  
  </dependency>
  
  将原来的 spring DataSource 注释掉,加入 sharding 的配置
  
  sharding:
  
  jdbc:
  
  datasource:
  
  names: ds_0,ds_1
  
  ds_0:
  
  type: com.zaxxer.hikari.HikariDataSource
  
  driver-class-name: com.mysql.jdbc.Driver
  
  jdbc-url: jdbc:mysql://sharding0
  
  username: mams_test
  
  password: mams_test
  
  ds_1:
  
  type: com.zaxxer.hikari.HikariDataSource
  
  driver-class-name: com.mysql.jdbc.Driver
  
  jdbc-url: jdbc:mysql://sharding1
  
  username: mams_test
  
  password: mams_test
  
  config:
  
  sharding:
  
  default-data-source-name: ds_0
  
  tables:
  
  task:
  
  actual-data-nodes: ds_${0..1}.task
  
  database-strategy:
  
  inline:
  
  sharding-column: id
  
  algorithm-expression: ds_${id % 2}
  
  key-generator-column-name: id
  
  reverse_display:
  
  actual-data-nodes: ds_${0..1}.reverse_display
  
  database-strategy:
  
  inline:
  
  sharding-column: task_id
  
  algorithm-expression: ds_${task_id % 2}
  
  key-generator-column-name: id
  
  props:
  
  sql.show: true
  
  注:
  
  这个配置只是针对分库,sharding jdbc 还支持读写分离,主从等模式,具体可以看文档sharding jdbc configuration
  
  faq
  
  只有网友的分享例子,没有文档
  
  sharding jdbc 的链接 shardingjdbc.io 访问是不可用的,在码云上找到了一份文档,地址是sharding-jdbc-doc
  
  spring boot的 health check 显示为down
  
  看了一下 DataSourceHealthIndicator 类的实现,要求 select 1 返回的大小为 1才认为数据库是健康的,但是经过分库之后,这个list size 应该了是数据库的数量,所以显示数据库为down。
  
  可以将db 个health check关掉,配置management.health.db.enabled=false 即可。
  
  不过这种情况下数据库如果挂掉的话注册中心是不知道的,需要定制一个自己的数据库检查类
  
  @Component
  
  public class CustomDataSourceHealthCheck extends AbstractHealthIndicator {
  
  private JdbcTemplate jdbcTemplate;
  
  private String databases;
  
  private int databaseCount;
  
  private String query www.tiaotiaoylzc.com= "SELECT 1 ";
  
  @PostConstruct
  
  public void init() {
  
  databaseCount = databases.split(",").length;
  
  }
  
  @Override
  
  protected void doHealthCheck(Health.Builder builder) throws Exception {
  
  try {
  
  List<Map<String, Object>> result = jdbcTemplate.queryForList(query);
  
  if (result.size(www.mytxyl1.com) == databaseCount) {
  
  builder.up();
  
  } else {
  
  builder.down();
  
  }
  
  } catch (Exception e) {
  
  builder.down(e);
  
  }
  
  }
  
  public JdbcTemplate getJdbcTemplate() {
  
  return jdbcTemplate;
  
  }
  
  @Autowired
  
  public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
  
  this.jdbcTemplate = jdbcTemplate;
  
  }
  
  public String getDatabases() {
  
  return databases;
  
  }
  
  @Value("${sharding.www.yongshi123.cn jdbc.datasource.names}")
  
  public void setDatabases(String databases) {
  
  this.databases = databases;
  
  }
  
  public int getDatabaseCount() {
  
  return databaseCount;
  
  }
  
  public void setDatabaseCount(int databaseCount) {
  
  this.databaseCount = www.zhenghongyule.com/ databaseCount;
  
  }
  
  public String getQuery(www.xtd912.com) {
  
  return query;
  
  }
  
  public void setQuery(String query) {
  
  this.query = query;
  
  }
  
  }

上一篇:MySQL读写分离(一)——sharding-jdbc


下一篇:Akka-Cluster(6)- Cluster-Sharding:集群分片,分布式交互程序核心方式