如何用spring boot 内嵌tomcat,用jndi方式配置多个数据源?

如何用spring boot 内嵌tomcat,用jndi方式配置多个数据源?

不知道你这个问题是否已经解决, 如果还没有解决的话:
  • 文章:SpringBoot内嵌Tomcat JNDI配置 中也许有你想要的答案,请看下吧
  • 除此之外, 这篇博客: springboot+tomcat+jndi实现外置配置文件配置多数据源中的 使用JNDI 部分也许能够解决你的问题, 你可以仔细阅读以下内容或者直接跳转源博客中阅读:
    • 引入pom指定外部配置文件路径:
             <profile>
                <id>product</id>
                <properties>
                    <logback.loglevel>INFO</logback.loglevel>
                    <spring.profiles.active>product</spring.profiles.active>
                    <profile.product>true</profile.product>
                    <profile.properties>file:/tomcat/profiles/${project.artifactId}/properties</profile.properties>
                </properties>
                <activation>
                    <activeByDefault>true</activeByDefault>
                </activation>
                <dependencies>
                    <dependency>
                        <groupId>org.springframework.boot</groupId>
                        <artifactId>spring-boot-starter-undertow</artifactId>
                    </dependency>
                </dependencies>
                <build>
                    <plugins>
                        <plugin>
                            <groupId>org.apache.maven.plugins</groupId>
                            <artifactId>maven-surefire-plugin</artifactId>
                            <version>2.10</version>
                            <configuration>
                                <argLine>-Xms512m -Xmx1024m</argLine>
                                <forkMode>once</forkMode>
                                <skip>true</skip>
                                <skipTests>true</skipTests>
                            </configuration>
                        </plugin>
                    </plugins>
                </build>
            </profile>

     根据引入的pom可知 外部配置文件路径为 /tomcat/profiles/${project.artifactId}/properties

    • 配置数据源信息并放置到指定路径下
    spring:
      datasource:
        jndi-name: jdbc/lcs_ds
    •  修改tomcat配置文件

    修改server.xml加入数据源配置

    <Resource name="jdbc/lcs_ds"
            auth="Container"
            type="javax.sql.DataSource"
            username="xxxx"
            password="xxxx"
            driverClassName="com.mysql.jdbc.Driver"
            url="jdbc:mysql://xxxx:3306/lcs?useUnicode=true&characterEncoding=utf8&useSSL=false&rewriteBatchedStatements=true"
            maxActive="100"
            maxIdle="10"
            maxWait="2000"
            removeAbandoned="true"
            logAbandoned="true"
            logExpiredConnections="true"
            maxConnLifetimeMillis="60000"
            initialSize="1"
            removeAbandonedTimeout="180"
            validationQuery="select 1"
            testOnBorrow="true"
            testWhileIdle="true"
            timeBetweenEvictionRunsMillis="3600000"
            minEvictableIdleTimeMillis="18000000"   />

     修改context.xml引入数据源配置

    <ResourceLink global="jdbc/lcs_ds" name="jdbc/lcs_ds" type="javax.sql.DataSource"/>

    也可直接将数据源信息配置到context.xml中

     项目打指定profile war包,修改数据源信息可直接在外部配置文件中修改 不用重新打包


如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^