Springboot multiple data sources, the simplest way to integrate

Time:2021-12-4

brief introduction

I believe that if you have configured too many data sources or are about to configure multiple data sources, you will find that there are probably the following schemes on the Internet:

  • 1. Use AOP slice for dynamic data source switching
  • 2. UseMapperScanofbasePackagesConfigure different mapper directories and templates
  • 3. Database agent middleware

    Both methods can realize multiple data sources, but each has its own disadvantages:
  • 1. Multiple data sources cannot be realizedXAThings (Global things Management)|JTA)This disadvantage is very fatal. It is useless to have multiple data sources but no global things. There are many posts and tutorials on the Internet. Although the configuration is slightly simple, it is useless if you want to use global things.
  • 2. This method can be combinedJTARealize the overall situation. At present, I also run online in this way in my business. After many debugging and online operation and maintenance, there is no doubt that there is a problem is trouble! You will encounter the need for integrationdruidConnection pool and global things. If you find online posts and follow the process, there may still be some small problems after matching.
  • 3. Data source proxy may be a good way. Most giant companies adopt this method, and developers do not have to consider these problems. They can use proxy for cutting and connection pool. However, there is a very embarrassing problem that open source is not easy to use, because many businesses are not suitable for their own business, And their own development is unrealistic and the cost is too high.

At this time, I’m thinking about why we can’t directly introduce a dependency like other language frameworksymlJust configure it in the file, and all have the simplest scheme now.

Source address

I hope you can support star and add other dependent simple integration in the future.

https://github.com/louislivi/fastdep

start

edition:

springboot:2.0+

Import dependency:
  • Maven
    <dependency>
    <groupId>com.louislivi.fastdep</groupId>
    <artifactId>fastdep-datasource</artifactId>
    <version>1.0.0</version>
    </dependency>
  • Gradle
    compile group: 'com.louislivi.fastdep', name: 'fastdep-datasource', version: '1.0.0'
application.ymlProfile:
fastdep:

Is this over?

Yes, it’s that simple.

Is it very simple? If you go to the online poster, you will find that it is similar. It teaches you to add files for configuration all the time. If it isMapperScanAdd a data source, and add another one every timejavaClass.

principle

useImportBeanDefinitionRegistrar BeanDefinitionBuilder.genericBeanDefinitionDynamic injectionBeanIn fact, it is very simple. If you are interested, you can see the source code.

last

Prevent other questions from posting me completelypom.xmlDependencies introduced under:

<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.0</version>
        </dependency>
    </dependencies>

I hope you can support open source and give me a little star. In the future, we will continue to develop other dependent integration and current multi data optimization supportHibernateAnd even compatible with other frameworks.fastdepGive WayjavaIntegration dependency is simpler. We also recruit like-minded people herecoderWork together to improve the project.

This work adoptsCC agreement, reprint must indicate the author and the link to this article