繁体   English   中英

使用Camel在运行时配置数据源

[英]Configuring datasources at runtime with Camel

我确实在Apache Camel中设置了一条非常简单的路由,其中​​将查询发送到JDBC组件以执行。 我启动并运行了骆驼项目。 我要完成的工作是在RabbitMQ消息的标头中发送dataSource1的数据库连接参数。 通过连接参数,我的意思是driverClassName,URL,用户名,密码。 我的应用程序的客户端将输入所有这些参数,以确定要连接到哪个数据库。 根据用户指定的driverClassName用户,我可能会使用路由清单,但这是另一回事。

请注意,在此示例中,我将SQL语句放在文件中以使其更简单。 我该怎么做?

这是我的骆驼语境:

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:camel="http://camel.apache.org/schema/spring"
       xmlns:jdbc="http://www.springframework.org/schema/jdbc"
       xsi:schemaLocation="
       http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
       http://camel.apache.org/schema/spring http://camel.apache.org/schema/spring/camel-spring.xsd
       http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd">

  <camel:camelContext xmlns="http://camel.apache.org/schema/spring">
    <camel:route>
      <camel:from uri="file:src/data?noop=true"/>
      <camel:to uri="jdbc:dataSource1"/>
      <camel:convertBodyTo type="java.lang.String"/>
      <camel:log message="${body}"/>
    </camel:route>
  </camel:camelContext>

  <bean id="dataSource1"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver"/>
    <property name="url" value="jdbc:mysql://localhost:3306/employees"/>
    <property name="username" value="root"/>
    <property name="password" value=""/>
  </bean>
</beans>

和Maven项目:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>group1</groupId>
  <artifactId>com.mycompany</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel Spring Route</name>
  <url>http://www.myorganization.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-core</artifactId>
      <version>2.12.2</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring</artifactId>
      <version>2.12.2</version>
    </dependency>

    <!-- logging -->
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-api</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>org.slf4j</groupId>
      <artifactId>slf4j-log4j12</artifactId>
      <version>1.7.5</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.17</version>
    </dependency>

    <!-- testing -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test-spring</artifactId>
      <version>2.12.2</version>
      <scope>test</scope>
    </dependency>

      <dependency>
          <groupId>mysql</groupId>
          <artifactId>mysql-connector-java</artifactId>
          <version>5.1.26</version>
      </dependency>

      <!-- Jdbc -->
      <dependency>
          <groupId>org.apache.camel</groupId>
          <artifactId>camel-jdbc</artifactId>
          <version>2.12.2</version>
      </dependency>

      <dependency>
          <groupId>org.springframework</groupId>
          <artifactId>spring-jdbc</artifactId>
          <version>4.0.0.RELEASE</version>
      </dependency>
  </dependencies>

  <build>
    <defaultGoal>install</defaultGoal>

    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>2.5.1</version>
        <configuration>
          <source>1.6</source>
          <target>1.6</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-resources-plugin</artifactId>
        <version>2.6</version>
        <configuration>
          <encoding>UTF-8</encoding>
        </configuration>
      </plugin>

      <!-- allows the route to be ran via 'mvn camel:run' -->
      <plugin>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-maven-plugin</artifactId>
        <version>2.12.2</version>
      </plugin>
    </plugins>
  </build>
</project>

数据源是一个Spring bean。 创建自己的bean,并让Spring注入数据源。 然后,您可以插入的JDBC步骤之前,该bean到您的路线,并使用交换顶部,它应该包含的JMS头,更新数据源的属性。

但是,我认为这种设计方法会带来麻烦:

  • 如果曾经(并且应该)使用池化数据源,则需要重置连接池,这很昂贵
  • 您需要同步(单例)数据源的状态修改,因为这在这方面不是线程安全的,并且您正在使用Camel在多线程框架中工作
  • 您正在为周围的数据库发送用户名和密码,并且在传输过程中应保护该信息。

可能还有更多...

也许几年后。 购买后,我使用AbstractRoutingDataSource实现了该功能,该功能允许动态更改数据源(无需重置连接池或路由)

参考链接: https : //spring.io/blog/2007/01/23/dynamic-datasource-routing/

暂无
暂无

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

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