使用 JNDI 配置数据源使用外部 Tomcat 9 服务器:Spring Boot

Nuñ*_*ada 5 java jndi spring-mvc spring-boot tomcat9

我有一个 SpringBootApplication,打包为 war 文件:

@SpringBootApplication(exclude = {SecurityAutoConfiguration.class})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}
Run Code Online (Sandbox Code Playgroud)

在 application.properties 上:

spring.datasource.jndi-name=java:comp/env/jdbc/bonanza
Run Code Online (Sandbox Code Playgroud)

但是在日志中,当我在 Tomcat 9 中部署战争时,我看到了这些消息:

Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].. Returning null.
Run Code Online (Sandbox Code Playgroud)

日志:

12:37:53.989 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.datasource.jndi-name]
12:37:53.989 [main] DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.datasource.jndi-name] not found - trying original name [spring.datasource.jndi-name]. javax.naming.NameNotFoundException: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].
12:37:53.990 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.datasource.jndi-name]
12:37:53.991 [main] DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.datasource.jndi-name] threw NamingException with message: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].. Returning null.
12:37:53.995 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [java:comp/env/spring.datasource.jndi-name]
12:37:53.996 [main] DEBUG o.s.jndi.JndiLocatorDelegate - Converted JNDI name [java:comp/env/spring.datasource.jndi-name] not found - trying original name [spring.datasource.jndi-name]. javax.naming.NameNotFoundException: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].
12:37:53.996 [main] DEBUG o.springframework.jndi.JndiTemplate - Looking up JNDI object with name [spring.datasource.jndi-name]
12:37:53.997 [main] DEBUG o.s.jndi.JndiPropertySource - JNDI lookup for name [spring.datasource.jndi-name] threw NamingException with message: Name [spring.datasource.jndi-name] is not bound in this Context. Unable to find [spring.datasource.jndi-name].. Returning null.
12:37:53.998 [main] DEBUG o.s.c.e.PropertySourcesPropertyResolver - Found key 'spring.datasource.jndi-name' in PropertySource 'configurationProperties' with value of type String
Run Code Online (Sandbox Code Playgroud)

在我的 tomcat9/conf/context.xml:

 <Resource  name="jdbc/bonanza" 
                auth="Container" 
                type="javax.sql.DataSource"
                maxTotal="100" 
                maxIdle="30" 
                maxWaitMillis="10000"
                username="a_usr" 
                password="Mu*7gydlcdstg100@" 
                driverClassName="com.mysql.jdbc.Driver"
                url="jdbc:mysql://172.175.77.55:3306/a_db"
        />
Run Code Online (Sandbox Code Playgroud)

Gov*_*are 1

正如错误所示,Spring Boot 在 JNDI 查找中找不到密钥。JNDI 在 Spring boot 的嵌入式 Tomcat 中被禁用,因此您需要使用启用它Tomcat#enableNaming,完成后您需要在 JNDI 中创建一个查找条目。您可以参考我从 Spring Boot 项目维护者存储库之一的GitHub 存储库 JNDI-Tomcat复制的以下代码

@Bean
public TomcatEmbeddedServletContainerFactory tomcatFactory() {
    return new TomcatEmbeddedServletContainerFactory() {

        @Override
        protected TomcatEmbeddedServletContainer getTomcatEmbeddedServletContainer(
                Tomcat tomcat) {
            tomcat.enableNaming();
            return super.getTomcatEmbeddedServletContainer(tomcat);
        }

        @Override
        protected void postProcessContext(Context context) {
            ContextResource resource = new ContextResource();
            resource.setName("jdbc/bonanza");
            resource.setType(DataSource.class.getName());
            resource.setProperty("driverClassName", "your.db.Driver");
            resource.setProperty("url", "jdbc:yourDb");

            context.getNamingResources().addResource(resource);
        }
    };
}
Run Code Online (Sandbox Code Playgroud)

[编辑]

由于您没有使用嵌入式 tomcat 服务器,因此可以通过使用 tomcat 配置文件来配置 JNDI:

在 server.xml 中,创建一个资源<GlobalNamingResources>

<Resource auth="Container" driverClassName="..." 
                           maxActive="..." 
                           maxIdle="..." 
                           maxWait="..." 
                           name="jdbc/bonanza"  
                           username="..."
                           password="..."
                           type="..."
                           url="..."/>
Run Code Online (Sandbox Code Playgroud)

在Context.xml中,您可以链接资源

<context>
    <ResourceLink auth="Container" name="jdbc/bonanza" global="jdbc/bonanza" type="javax.sql.DataSource" />
</context>
Run Code Online (Sandbox Code Playgroud)

另外,请确保您没有使用 spring-bootmain方法启动应用程序。您需要使用maven/gradle构建war文件,然后将其部署到tomcat并进行测试。