简体   繁体   English

使用 JNDI 在 Spring Boot 中配置多个数据源

[英]Configure Multiple DataSource in Spring Boot with JNDI

I want to manage multiple DataSource using your Application Servers built-in features and access it using JNDI.我想使用您的应用程序服务器内置功能管理多个数据源并使用 JNDI 访问它。 I am using Spring boot with Spring JPA data.我正在将 Spring boot 与 Spring JPA 数据一起使用。

I am able to configure the application.properties for single datasource:我能够为单个数据源配置 application.properties:

spring.datasource.jndi-name=jdbc/customers

And my configuration in context.xml file as below:我在 context.xml 文件中的配置如下:

<Resource name="jdbc/customer" auth="Container" type="javax.sql.DataSource"
               maxTotal="100" maxIdle="30" maxWaitMillis="10000"
               username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/customer"/>

Everything works fine.一切正常。

But when I am unable to configure for two datasource.但是当我无法配置两个数据源时。

I am sure on the configuration in context.xml file:我确定 context.xml 文件中的配置:

 <Resource name="jdbc/customer" auth="Container" type="javax.sql.DataSource"
                   maxTotal="100" maxIdle="30" maxWaitMillis="10000"
                   username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/customer"/>

 <Resource name="jdbc/employee" auth="Container" type="javax.sql.DataSource"
                   maxTotal="100" maxIdle="30" maxWaitMillis="10000"
                   username="root" password="root" driverClassName="com.mysql.jdbc.Driver"
                   url="jdbc:mysql://localhost:3306/employee"/>

I am in doubt about the application.properties file configuration.我对 application.properties 文件配置有疑问。

I tried the below options with no success:我尝试了以下选项但没有成功:

spring.datasource.jndi-name=jdbc/customers,jdbc/employee

Please let me know any details on Spring boot with JNDI for multiple data source.请让我知道有关使用 JNDI 进行多数据源 Spring Boot 的任何详细信息。 I was looking for this configuration for days now.我一直在寻找这个配置好几天了。

Second Trial As per Spring Boot Documentation根据Spring Boot 文档进行第二次试用

spring.datasource.primary.jndi-name=jdbc/customer
spring.datasource.secondary.jndi-name=jdbc/project

Configuration class.配置类。

@Bean
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public DataSource primaryDataSource() {
    return DataSourceBuilder.create().build();
}

@Bean
@ConfigurationProperties(prefix="datasource.secondary")
public DataSource secondaryDataSource() {
    return DataSourceBuilder.create().build();
}

The application does not get started.应用程序无法启动。 Though the tomcat server is getting started.虽然tomcat服务器正在启动。 No errors are printed in the log.日志中没有打印错误。

Third Trial: With JndiObjectFactoryBean第三次尝试:使用 JndiObjectFactoryBean

I have the below application.properties我有以下 application.properties

spring.datasource.primary.expected-type=javax.sql.DataSource
spring.datasource.primary.jndi-name=jdbc/customer
spring.datasource.primary.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.primary.jpa.show-sql=false
spring.datasource.primary.jpa.hibernate.ddl-auto=validate

spring.datasource.secondary.jndi-name=jdbc/employee
spring.datasource.secondary.expected-type=javax.sql.DataSource
spring.datasource.secondary.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
spring.datasource.secondary.jpa.show-sql=false
spring.datasource.secondary.jpa.hibernate.ddl-auto=validate

And the below java configuration:以及下面的java配置:

@Bean(destroyMethod="")
@Primary
@ConfigurationProperties(prefix="spring.datasource.primary")
public FactoryBean primaryDataSource() {
    return new JndiObjectFactoryBean();
}

@Bean(destroyMethod="")
@ConfigurationProperties(prefix="spring.datasource.secondary")
public FactoryBean secondaryDataSource() {
    return new JndiObjectFactoryBean();
}

But still getting error:但是还是报错:

Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'primaryDataSource' defined in class path resource [com/web/initializer/MvcConfig.class]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/customer] is not bound in this Context. Unable to find [jdbc].
Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'secondaryDataSource' defined in class path resource [com/web/initializer/MvcConfig.class]: Invocation of init method failed; nested exception is javax.naming.NameNotFoundException: Name [jdbc/employee] is not bound in this Context. Unable to find [jdbc].
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:686)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at org.springframework.boot.context.web.SpringBootServletInitializer.run(SpringBootServletInitializer.java:117)
        at org.springframework.boot.context.web.SpringBootServletInitializer.createRootApplicationContext(SpringBootServletInitializer.java:108)
        at org.springframework.boot.context.web.SpringBootServletInitializer.onStartup(SpringBootServletInitializer.java:68)
        at org.springframework.web.SpringServletContainerInitializer.onStartup(SpringServletContainerInitializer.java:175)

Update: Trial using the below properties file:更新:使用以下属性文件进行试用:

  spring.datasource.primary.expected-type=javax.sql.DataSource
   spring.datasource.primary.jndi-name=java:comp/env/jdbc/customer

   spring.datasource.secondary.jndi-name=java:comp/env/jdbc/employee
   spring.datasource.secondary.expected-type=javax.sql.DataSource

   spring.jpa.database-platform=org.hibernate.dialect.MySQL5Dialect
   spring.jpa.show-sql=false
   spring.jpa.hibernate.ddl-auto=validate

It creates all the tables in customer schema, but fails trying to find the other tables also.(from the second schema)它在客户模式中创建所有表,但也无法尝试找到其他表。(来自第二个模式)

This is the solution for your third trial a little bit modified.这是您第三次试用的解决方案,稍作修改。 Consider this solution (Spring Boot 1.3.2):考虑这个解决方案(Spring Boot 1.3.2):

application.properties file: application.properties 文件:

spring.datasource.primary.jndi-name=java:/comp/env/jdbc/SecurityDS
spring.datasource.primary.driver-class-name=org.postgresql.Driver

spring.datasource.secondary.jndi-name=java:/comp/env/jdbc/TmsDS
spring.datasource.secondary.driver-class-name=org.postgresql.Driver

spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQL9Dialect
spring.jpa.show-sql=false

Configuration:配置:

@Configuration@ EnableConfigurationProperties
public class AppConfig {

    @Bean@ ConfigurationProperties(prefix = "spring.datasource.primary")
    public JndiPropertyHolder primary() {
        return new JndiPropertyHolder();
    }

    @Bean@ Primary
    public DataSource primaryDataSource() {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource(primary().getJndiName());
        return dataSource;
    }

    @Bean@ ConfigurationProperties(prefix = "spring.datasource.secondary")
    public JndiPropertyHolder secondary() {
        return new JndiPropertyHolder();
    }

    @Bean
    public DataSource secondaryDataSource() {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        DataSource dataSource = dataSourceLookup.getDataSource(secondary().getJndiName());
        return dataSource;
    }

    private static class JndiPropertyHolder {
        private String jndiName;

        public String getJndiName() {
            return jndiName;
        }

        public void setJndiName(String jndiName) {
            this.jndiName = jndiName;
        }
    }
}

And then you can follow guide http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html to use your datasources with jpa repositories.然后您可以按照指南http://docs.spring.io/spring-data/jpa/docs/1.3.0.RELEASE/reference/html/jpa.repositories.html使用带有 jpa 存储库的数据源。

It works for me and contains less code它对我有用并且包含更少的代码

@Configuration
public class Config {
    @Value("${spring.datasource.primary.jndi-name}")
    private String primaryJndiName;

    @Value("${spring.datasource.secondary.jndi-name}")
    private String secondaryJndiName;

    private JndiDataSourceLookup lookup = new JndiDataSourceLookup();

    @Primary
    @Bean(destroyMethod = "") // destroy method is disabled for Weblogic update app ability
    public DataSource primaryDs() {
        return lookup.getDataSource(primaryJndiName);
    }

    @Bean(destroyMethod = "") // destroy method is disabled for Weblogic update app ability
    public DataSource secondaryDs() {
        return lookup.getDataSource(secondaryJndiName);
    }
}

You could use a plain JndiObjectFactoryBean for this.您可以为此使用普通的JndiObjectFactoryBean Simply replace the DataSourceBuilder with a JndiObjectFactoryBean should do the trick.只需用JndiObjectFactoryBean替换DataSourceBuilder就可以了。

Java configuration Java配置

@Bean(destroyMethod="")
@Primary
@ConfigurationProperties(prefix="datasource.primary")
public FactoryBean primaryDataSource() {
    return new JndiObjectFactoryBean();
}

@Bean(destroyMethod="")
@ConfigurationProperties(prefix="datasource.secondary")
public FactoryBean secondaryDataSource() {
    return new JndiObjectFactoryBean();
}

Properties特性

datasource.primary.jndi-name=jdbc/customer
datasource.primary.expected-type=javax.sql.DataSource
datasource.secondary.jndi-name=jdbc/project
datasource.secondary.expected-type=javax.sql.DataSource

You can set every property of the JndiObjectFactoryBean using the @ConfigurationProperties annotation.您可以使用@ConfigurationProperties注释设置JndiObjectFactoryBean每个属性。 (See the expected-type I added, but you could also set cache or lookup-on-startup etc.). (请参阅我添加的expected-type ,但您也可以设置cachelookup-on-startup等)。

Note: when doing a JNDI lookup set the destroyMethod to an "" else you might get the situation that when the application is shutdown your JNDI resource is getting closed/shutdown as well.注意:在执行 JNDI 查找时,将destroyMethod设置为""否则您可能会遇到这样的情况,即当应用程序关闭时,您的 JNDI 资源也会关闭/关闭。 This is not something you want in a shared environment.这不是您在共享环境中想要的。

In my case, when i start my application using Spring Boot App, the database configurations are read on application-dev.properties, when I publish on tomcat, using datasources, was necessary add a validation to check if my profile is prod, in this case, i do a jndi lookup在我的情况下,当我使用 Spring Boot App 启动我的应用程序时,数据库配置是在 application-dev.properties 上读取的,当我在 tomcat 上发布时,使用数据源,有必要添加一个验证来检查我的配置文件是否是 prod,在这个案例,我做一个 jndi 查找

@Bean(name = "dsName")
@ConfigurationProperties("ds.datasource.configuration")
public DataSource dataSource(@Qualifier("dsProperties") DataSourceProperties db1DataSourceProperties)
{

    if(Arrays.asList(environment.getActiveProfiles()).contains("prod"))
    {
        final JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        return dataSourceLookup.getDataSource("java:comp/env/jdbc/DS1");
    }
    else
    {
        return db1DataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
    }
}

The concise way I get success and explore more我获得成功和探索更多的简洁方式

  1. set up many jndi resources in external tomcat , that you can start/stop in eclipse.Note- double click the tomcat in eclipse and select use workspace metedata , means dont deploy the app to tomcat webapp folder.在外部 tomcat 中设置了许多 jndi 资源,您可以在 eclipse 中启动/停止。注意 - 在 eclipse 中双击 tomcat 并选择 use workspace metadata ,意味着不要将应用程序部署到 tomcat webapp 文件夹。 Add jndi resources in respective eclipse server files( context.xml - ResourceLink, server.xml - Resource , web.xml - resource-ref).在各自的 eclipse 服务器文件中添加 jndi 资源(context.xml - ResourceLink、server.xml - Resource、web.xml - resource-ref)。

  2. no need to set spring.datasource.* in application.properties.无需在 application.properties 中设置 spring.datasource.*。 since jndi-contest which is a datasource type( ie type="javax.sql.DataSource" ) is exported to external server.因为 jndi-contest 是一种数据源类型(即type="javax.sql.DataSource" )被导出到外部服务器。

  3. in SpringBootApplication annotated class , create the datasource beans from all the jndi resources(those setup as per #1) through jndi lookup在 SpringBootApplication 注释类中,通过 jndi 查找从所有 jndi 资源(按照 #1 设置的那些)创建数据源 bean

     @Bean(name = "abcDataSource") public DataSource getAbcDataSource() { JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup(); DataSource dataSource = dataSourceLookup.getDataSource("java:comp/env/jdbc/abcDataSource"); return dataSource; }
  4. if spring jdbc used in your project then provide the above datasource to create a jdbcTemplate bean如果您的项目中使用了 spring jdbc,则提供上述数据源以创建 jdbcTemplate bean

     @Bean(name = "jdbcAbcTemplate") public JdbcTemplate abcJdbcTemplate(@Lazy @Qualifier("abcDataSource") DataSource refDS) { return new JdbcTemplate(refDS); }
  5. just autowire a property of DataSource type and get systemout its details to explore more.只需自动装配 DataSource 类型的属性并获取系统详细信息以探索更多信息。

While the above answers are good I am going to add one more to illustrate a deadly gotcha if mixing jndi and full data connection configuration.虽然上述答案很好,但我将再添加一个来说明混合 jndi 和完整数据连接配置时的致命问题。 In a typical development environment you may fully qualify the database connection in your local dev environment then use jndi as you push to qa, etc.. Your application .properties looks like so:在典型的开发环境中,您可以完全限定本地开发环境中的数据库连接,然后在推送到 qa 等时使用 jndi。您的应用程序 .properties 如下所示:

spring.datasource.url=jdbc:sqlserver://server.domain.org:1433;databaseName=dbxx
spring.datasource.username=userxxyyzz
spring.datasource.password=passxxyyzz
spring.datasource.platform=mssql
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

and application-qa.properties like so:和 application-qa.properties 像这样:

spring.datasource.jndi-name=java:jboss/datasources/dbxx

The problem arises when you have to define your own beans to have multiple datasources.当您必须定义自己的 bean 以拥有多个数据源时,就会出现问题。 If you use the default Spring managed datasource then it automatically detects jndi vs fully qualified connection and returns a datasource with no change needed in the application code.如果您使用默认的 Spring 托管数据源,那么它会自动检测 jndi 与完全限定的连接,并返回一个无需更改应用程序代码的数据源。 If you define your own datasource it no longer does this.如果您定义自己的数据源,则不再执行此操作。 If you have application.properties like so:如果你有这样的 application.properties:

spring.custom.datasource.url=jdbc:sqlserver://server.domain.org:1433;databaseName=dbxx
    spring.custom.datasource.username=userxxyyzz
    spring.custom.datasource.password=passxxyyzz
    spring.custom.datasource.platform=mssql
    spring.custom.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

and application-qa.properties like so:和 application-qa.properties 像这样:

spring.custom.datasource.jndi-name=java:jboss/datasources/dbxx

with a datasource bean like so, as suggested in Spring docs https://docs.spring.io/spring-boot/docs/2.1.11.RELEASE/reference/html/howto-data-access.html使用像这样的数据源 bean,如 Spring 文档中所建议的https://docs.spring.io/spring-boot/docs/2.1.11.RELEASE/reference/html/howto-data-access.html

  @Primary
  @Bean(name="customDataSourcePropertiesBean")
  @ConfigurationProperties("spring.custom.datasource")
  public DataSourceProperties customDataSourceProperties() {
      return new DataSourceProperties();
  }

  @Primary
  @Bean(name="customDataSourceBean")
  @ConfigurationProperties("spring.custom.datasource") 
  public HiakriDataSource customDataSource(@Qualifier("customDataSourcePropertiesBean") DataSourceProperties properties) {
    return properties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
  }

This datasource builder does not attempt to read the jndi config in application-qa.properties and silently fails back to application.properties returning the WRONG database connection.此数据源构建器不会尝试读取 application-qa.properties 中的 jndi 配置,并且会静默故障返回到 application.properties 返回错误的数据库连接。 Resolution is fairly simple - test which environment you are in and customize the type of database connection created.解决方法相当简单 - 测试您所在的环境并自定义创建的数据库连接类型。 Debugging this was a bear, as the symptom was that the app appeared to be ignoring application-qa.properties.调试这是一个熊,因为症状是应用程序似乎忽略了 application-qa.properties。 I share to spare others the pain.我分享是为了免除他人的痛苦。 Add spring.profiles.active=qa etc. to your properties files to know which environment you are in then:将 spring.profiles.active=qa 等添加到您的属性文件中以了解您所在的环境:

  @Value("${spring.profiles.active}")
  String profile;

  @Value("${spring.custom.jndi-name}")
  String jndi;

  @Primary
  @Bean(name="customDataSourcePropertiesBean")
  @ConfigurationProperties("spring.custom.datasource")
  public DataSourceProperties customDataSourceProperties() {
      return new DataSourceProperties();
  }

  @Primary
  @Bean(name="customDataSourceBean")
  @ConfigurationProperties("spring.custom.datasource") 
  public DataSource customDataSource(@Qualifier("customDataSourcePropertiesBean") DataSourceProperties properties) {
    if(profile.equals("localhost")) {
        return DataSourceBuilder
            .create()
                .username(properties.getDataUsername())
                .password(properties.getPassword())
                .url(properties.getUrl())
                .driverClassName(properties.getDriverClassName())
                .build();
    }else {
        JndiDataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
        return dataSourceLookup.getDataSource(jndi);
    }
  }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM