Spring boot using sharding-jdbc completed with separate read and write

1. dependence introduction

        <dependency>
            <groupId>io.shardingjdbc</groupId>
            <artifactId>sharding-jdbc-core</artifactId>
            <version>2.0.3</version>
        </dependency>        

  

2. Add profile sharding-jdbc.yml in src / main / resources in

dataSources:
  db_master: !!com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.31.128:3307/gmall_pms?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
  db_slave1: !!com.zaxxer.hikari.HikariDataSource
    driverClassName: com.mysql.jdbc.Driver
    jdbcUrl: jdbc:mysql://192.168.31.128:3316/gmall_pms?useUnicode=true&characterEncoding=utf-8&useSSL=false
    username: root
    password: root
  db_slave2: !!com.zaxxer.hikari.HikariDataSource
      driverClassName: com.mysql.jdbc.Driver
      jdbcUrl: jdbc: MySQL: //192.168.31.128: 3317 / = to true gmall_pms useUnicode & characterEncoding = UTF-8 & useSSL = false? 
      username: root
      password: root 
masterSlaveRule: 
  name: db_ms 
  masterDataSourceName: db_master 
  slaveDataSourceNames: [db_slave1, db_slave2] 

### Note that this documents copied directly from the official website. Behind !! DataSource implementation class is full class name can not be omitted. 
### parts of the plurality of slave Cadogan db_slaveXX arranged to, those listed in slaveDataSourceNames slaver

  

3. Add the configuration class data source

/ ** 
 * This is a configuration class 
 * 
 * SpringBoot introduction of a scene, the scene of the components will be automatically configured. 
 . 1 *), * 
 / 
@Configuration 
public class PmsDataSourceConfig { 

    @Bean 
    public the dataSource the DataSource () throws IOException, SQLException { 

        File File = ResourceUtils.getFile ( "CLASSPATH: Sharding-jdbc.yml"); 
        the DataSource MasterSlaveDataSourceFactory.createDataSource the dataSource = (File ); 
        return the dataSource; 
    } 
}

  

 

Guess you like

Origin www.cnblogs.com/shifu204/p/12626886.html