springboot multiple data sources, the easiest way to integrate

Brief introduction

I believe we have too many data source configuration, or about to configure multiple data friends, you will find about online there are several options:

  • 1. AOP slice switching dynamic data sources
  • 2. Use MapperScanof basePackagesa different configuration directory and template mapper
  • 3. Database Agent Middleware these two methods can achieve multiple data sources but have disadvantages:
  • 1. multiple data sources can not achieve XAthings (things global management | JTA) this shortcoming is very deadly, with a number of data sources but no global things, then what use is purely pit father, there are a lot of posts online tutorials using this configuration, although somewhat simple but If you want to use into the global things they did not use.
  • 2. In this way can be combined JTAto achieve a global thing, it is currently also used in this way on-line business to run, after several commissioning and operation and maintenance of the line a little problem no doubt is in trouble! You will meet the need to integrate druidthe connection pool, and if you find something global online posts to follow the process go more or less likely to finish with still some minor problems.
  • 3. Data source agent might be good one way, mostly giant companies are this way, business people and developers need to consider these issues, and cut using a proxy connection pools can be achieved, but there is a very awkward question is open source does not work well, because many are not suitable for their own business, and to develop their own is not realistic costs are too high.

This time I'm thinking about a problem, why not like other language frameworks directly dependent on the introduction of a ymlconfiguration file on it yet, have all now Minimalist Program.

Source Address

I hope you can support what star, follow-up will join Easy integration with other dependent. github.com/louislivi/f...

Start

version:

springboot:2.0+

The introduction of dependence:
  • Maven
<dependency>
    <groupId>com.louislivi.fastdep</groupId>
    <artifactId>fastdep-datasource</artifactId>
    <version>1.0.1</version>
</dependency>
复制代码
  • Gradle
compile group: 'com.louislivi.fastdep', name: 'fastdep-datasource', version: '1.0.1'
复制代码
application.yml Profiles:
fastdep:
  datasource:
    test: #数据源名称可随意取
      mapper: com.louislivi.fastdep.test.mapper.test #当前数据源对应的mapper目录不能多个数据源相同
      password: 123456
      url: jdbc:mysql://127.0.0.1:3306/douyin?serverTimezone=Asia/Chongqing&useLegacyDatetimeCode=false&nullNamePatternMatchesAll=true&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false&autoReconnect=true&useSSL=false&pinGlobalTxToPhysicalConnection=true
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
#      # 下面为druid连接池的补充设置
#      initialSize: 10
#      minIdle: 5
#      maxActive: 100
#      connectionInitSqls: 'set names utf8mb4;'
    test2: #数据源名称可随意取
      mapper: com.louislivi.fastdep.test.mapper.test2 #当前数据源对应的mapper目录不能多个数据源相同
      password: 123456
      url: jdbc:mysql://127.0.0.1:3306/test2?serverTimezone=Asia/Chongqing&useLegacyDatetimeCode=false&nullNamePatternMatchesAll=true&zeroDateTimeBehavior=CONVERT_TO_NULL&tinyInt1isBit=false&autoReconnect=true&useSSL=false&pinGlobalTxToPhysicalConnection=true
      driverClassName: com.mysql.cj.jdbc.Driver
      username: root
#      # 下面为druid连接池的补充设置
#      initialSize: 10
#      minIdle: 5
#      maxActive: 100
#      connectionInitSqls: 'set names utf8mb4;'
复制代码

This is the end? Yes It's that simple. Is not very simple, you go online you will find similar postings trick is to teach you've been added to the configuration file, if it is MapperScanadded to a data source, every time they add a javaclass.

principle

Use ImportBeanDefinitionRegistrar BeanDefinitionBuilder.genericBeanDefinitiondynamic injection Beanis actually very simple are interested can go look at the source.

At last

I have additional questions posted to prevent complete pom.xmlreliance under introduced:

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>2.1.5.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.11</version>
        </dependency>
        <dependency>
            <groupId>com.louislivi.fastdep</groupId>
            <artifactId>fastdep-datasource</artifactId>
            <version>1.0.1</version>
        </dependency>
    </dependencies>
复制代码

I hope you can support open source, to a small star, follow-up will continue to develop other dependent integration and optimization currently ongoing support of multiple data Hibernate, and even compatible with other framework. fastdepLet javaintegrate rely easier. Here also recruit like-minded codertogether to improve the project.

Guess you like

Origin juejin.im/post/5ddcd502e51d4523551669d4