栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Spring Boot 整合 Sharding-JDBC

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot 整合 Sharding-JDBC

概述

        ShardingSphere,它由Sharding-JDBC、Sharding-Proxy和Sharding-Sidecar(计划中)这3款相互独立的产品组成。定位为轻量级Java框架。其实就是一个增强版的JDBC驱动,完全兼容JDBC和各种ORM框架。内部改写了SQL的添加和查询规则。适用于任何基于Java的ORM框架,如:JPA, Hibernate, Mybatis, Spring JDBC Template或直接使用JDBC。

技术架构:

Spring Boot 2.6.3 、 Sharding-JDBC 3.0.0.M3、MySql 1主2从模式、 JDK1.8

导入依赖
        
        
            io.shardingsphere
            sharding-jdbc-spring-boot-starter
            ${sharding-sphere.version}
        

        
        
            org.mybatis.spring.boot
            mybatis-spring-boot-starter
            1.3.2
        

        
            com.alibaba
            druid
            1.1.21
        

        
            mysql
            mysql-connector-java
            5.1.41
        

       
            org.projectlombok
            lombok
            1.18.4
        

application.yml配置

#sharding-jdbc
sharding.jdbc:
  datasource:
    names: ds-master,ds-slave-0,ds-slave-1
    ds-master:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.116.105:3306/test-01
      username: root
      password: root
    ds-slave-0:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.116.106:3306/test-01
      username: root
      password: root
    ds-slave-1:
      type: com.alibaba.druid.pool.DruidDataSource
      driver-class-name: com.mysql.jdbc.Driver
      url: jdbc:mysql://192.168.116.107:3306/test-01
      username: root
      password: root
  config:
    masterslave:
      name: ds_ms
      master-data-source-name: ds-master
      slave-data-source-names: ds-slave-0,ds-slave-1
      load-balance-algorithm-type: round_robin
  props:
    sql.show: true
#mybatis
mybatis:
  config-location: classpath:mybatis/config.xml
  mapper-locations:
    - classpath:mybatis/mappers
@Data
public class User  implements Serializable {
    private  Integer id;
    private  String name;
    private  Integer age;
}

Dao层

package com.example.producers.dao;

import com.example.producers.domain.User;
import org.apache.ibatis.annotations.Mapper;


@Mapper
public interface UserMapper {
    User selectByPrimaryKey(int id);
}

UserMapper.xml




    
        
        
        
    
    
        id, name, age
    
    


UserController 测试接口
package com.example.producers.web;

import com.example.producers.dao.UserMapper;
import com.example.producers.domain.User;
import com.example.producers.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class UserController {

    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/getUser")
    public User getUser(@RequestParam("id")int id){
        User user =  userMapper.selectByPrimaryKey(id);
        return user;
    }
}

接口调用

如果在整合过程中出现如下错误:

Configuration property name 'sharding.jdbc.datasource.ds_master' is not vali

报错原因
ShardingSphere 在5.0以前的版本数据源的命名、自定义分片算法命名是支持下划线命名的,如 write_ds,但是在5.0以后就不支持了。

解决方法
多个单词之间不要使用"_"分割,使用 ”-“ 分割即可,如 ds-master

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/755516.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号