简体   繁体   English

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

[英]Configuring datasources at runtime with Camel

I did setup a very simple route in Apache Camel where a query is sent to a JDBC component to execute. 我确实在Apache Camel中设置了一条非常简单的路由,其中​​将查询发送到JDBC组件以执行。 I got the Camel project up and running. 我启动并运行了骆驼项目。 What I'm trying to accomplish is to send dataSource1's database connection parameters in a RabbitMQ message's header. 我要完成的工作是在RabbitMQ消息的标头中发送dataSource1的数据库连接参数。 By connection parameters I mean driverClassName, url, username, password. 通过连接参数,我的意思是driverClassName,URL,用户名,密码。 The client of my app would enter all those parameters to decide what database to connect to. 我的应用程序的客户端将输入所有这些参数,以确定要连接到哪个数据库。 I will probably use routing slip depending on what driverClassName user has specified but that's a different thing. 根据用户指定的driverClassName用户,我可能会使用路由清单,但这是另一回事。

Please note that in this example here I put the SQL statement in a file to make it simpler. 请注意,在此示例中,我将SQL语句放在文件中以使其更简单。 How can I accomplish that? 我该怎么做?

Here's my Camel Context: 这是我的骆驼语境:

<?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>

And Maven project: 和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>

The datasource is a Spring bean. 数据源是一个Spring bean。 Create a bean of your own and have Spring inject the datasource. 创建自己的bean,并让Spring注入数据源。 You can then plug that bean into your route before the JDBC step and use the exchange headers, which should contain the JMS headers, to update the properties of the datasource. 然后,您可以插入的JDBC步骤之前,该bean到您的路线,并使用交换顶部,它应该包含的JMS头,更新数据源的属性。

However, I think this design approach is asking for trouble: 但是,我认为这种设计方法会带来麻烦:

  • If you ever wanted to use a pooled data source (and you should), then you need to reset the pool of connections which is expensive 如果曾经(并且应该)使用池化数据源,则需要重置连接池,这很昂贵
  • You need to synchronize the state modifications of the (singleton) data source as it is not threadsafe in that respect and with Camel you are working in a multi-threaded framework 您需要同步(单例)数据源的状态修改,因为这在这方面不是线程安全的,并且您正在使用Camel在多线程框架中工作
  • You are sending user names and passwords for your DBs around the place and you should protect that information while it is in transit 您正在为周围的数据库发送用户名和密码,并且在传输过程中应保护该信息。

There is probably more... 可能还有更多...

Maybe a few years later. 也许几年后。 Buy I achieved this using AbstractRoutingDataSource which allows to change the data source dynamically (you do not need to reset the connection pool or the route) 购买后,我使用AbstractRoutingDataSource实现了该功能,该功能允许动态更改数据源(无需重置连接池或路由)

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

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

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