Giter Club home page Giter Club logo

dsspringbootstarter's Introduction

基于springboot的动态主从路由库

具体例子程序参考dsspringbootstart-test
添加maven快照仓库

https://oss.sonatype.org/content/repositories/snapshots

项目依赖:

<dependency>
  <groupId>io.github.jiaozi789.tool</groupId>
  <artifactId>ds-spring-boot-starter</artifactId>
  <version>1.0-SNAPSHOT</version>
</dependency>

多数据源配置

禁用jpa方式使用

@SpringBootApplication(exclude = {
        DataSourceAutoConfiguration.class,
        DataSourceTransactionManagerAutoConfiguration.class,
        HibernateJpaAutoConfiguration.class
 })
 

不禁用jpa的方式

@SpringBootApplication

spring.yml配置多数据源:

spring:
  datasource:
    primary:
        username: root
        url: jdbc:mysql://localhost/test
        driver-class-name: com.mysql.jdbc.Driver
        password: 123456
        primary:  true
    second:
        username: root
        url: jdbc:mysql://localhost/test1
        driver-class-name: com.mysql.jdbc.Driver
        password: 123456
    thrid:
        username: root
        url: jdbc:mysql://localhost/test
        driver-class-name: com.mysql.jdbc.Driver
        password: 123456

primary: true表示主数据库 不指定type设置数据源类型,默认使用druid

springboot中使用

springboot主类添加启用数据源路由

@EnableDataSourceRoute

在需要切换数据源的service方法上添加

    @DataSourceRoute(write = true)
    public List<Map<String, Object>>  queryAll(){
        List<Map<String, Object>> query = jdbcTemplate.query("select * from user", new ColumnMapRowMapper());
        return query;
    }

@DataSourceRoute(write = true)表示使用主库
@DataSourceRoute(read = true)read=true 表示使用从库
@DataSourceRoute(value=yml定义的数据源名称,使用指定名称的数据源)
比如 value=primary 将使用名称为primary的数据源
@DataSourceRoute 表示所有库都提供服务,没有主从
不添加注解,默认使用第一个数据源

多数据源负载算法

默认采用随机算法

@Bean
    public SelectRule selectRule(){
        return new RandomRule();
    }

可以实现SelectRule接口自定义算法 SelectRule接口的实现方法choose

//targetDataSource 表示对应的库名称和DataSource的键值对映射
public String choose(Map<Object, Object> targetDataSource);

dsspringbootstarter's People

Contributors

lzeqian avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.