从 Camel 和 Spring Boot 到 MS SQL Server 的 JDBC

Posted

技术标签:

【中文标题】从 Camel 和 Spring Boot 到 MS SQL Server 的 JDBC【英文标题】:JDBC from Camel and Spring Boot to MS SQL Server 【发布时间】:2021-08-27 17:57:04 【问题描述】:

我正在尝试使用 Spring Boot 从 Camel 访问 SQL Server,但我收到错误消息“com.bw.ReportRouter 中的字段数据源需要 'org.springframework.jdbc.datasource.AbstractDataSource' 类型的 bean找不到。”

我正在使用一些教程来尝试解决这个问题,主要是这个:https://www.javainuse.com/camel/camel_jdbc

如果我理解正确的话,Spring Boot 应该会自动配置这个 bean。

这是我的路线。我只是想进行一个简单的查询来建立连接:

package com.bw;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;

@Component
public class ReportRouter extends RouteBuilder 
    
    @Autowired
    AbstractDataSource dataSource;

    public DataSource getDataSource() 
        return dataSource;
    

    public void setDataSource(AbstractDataSource dataSource) 
        this.dataSource = dataSource;
        

    @Override
    public void configure()         
        from("timer:myTimer?repeatCount=1")
        .setBody(simple("select top 10 * from eai.Audit_General"))
        .log("SQL: $body")
        .to("jdbc:dataSource")
        .log("Out: $body");
    


这里是 application.properties

#spring.datasource.url=jdbc:sqlserver://sqlsrv\\sqlexpress;databaseName=customer
spring.datasource.url=jdbc:sqlserver://my-server.database.windows.net;databaseName=my-db
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.initialization-mode=always

我认为这没问题,但这是我的 pom。

<?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>com.bw</groupId>
  <artifactId>CrowdTwist-Reporting</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel Spring Boot Route</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring.boot-version>2.5.3</spring.boot-version>
    <surefire.plugin.version>3.0.0-M4</surefire.plugin.version>
    <camel-version>3.11.1</camel-version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- Spring Boot BOM -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>$spring.boot-version</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- Camel BOM -->
      <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-dependencies</artifactId>
        <version>$camel-version</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>

    <!-- Spring Boot -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- Camel -->
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-stream-starter</artifactId>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test-spring-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    
    <!-- Additions -->
    
    <!-- For receiving JMS messages from Artemis -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-jms-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-jms-starter</artifactId>
    </dependency>
    <dependency>
       <groupId>org.messaginghub</groupId>
       <artifactId>pooled-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
    </dependency>
    
    <!-- Model Object Translation -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>$camel-version</version>
    </dependency>
    
    <!-- Working with JSON -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jsonpath</artifactId>
      <version>$camel-version</version>
    </dependency>
    
    <!-- Database Access -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jdbc</artifactId>
        <version>$camel-version</version>
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <scope>runtime</scope>
    </dependency>
    
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>$spring.boot-version</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>$surefire.plugin.version</version>
      </plugin>
    </plugins>
  </build>

</project>

没有什么可展示的了。我从 Maven 原型生成了项目: mvn archetype:generate -DarchetypeGroupId=org.apache.camel.archetypes -DarchetypeArtifactId=camel-archetype-spring-boot -DarchetypeVersion=3.11.1

这是唯一的其他部分,主要过程:

package com.bw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

    @SpringBootApplication
    public class MySpringBootApplication 
    
        /**
         * A main method to start this application.
         */
        public static void main(String[] args) 
            SpringApplication.run(MySpringBootApplication.class, args);
        
    
    

【问题讨论】:

我发现了这个问题,还有其他三个问题。这个例子现在应该可以工作了。我在 OP 中更新了它。 如果您设法解决了问题,请发布解决方案的答案。不要编辑您的问题,因为解决问题中的问题会使问题变得毫无意义。 【参考方案1】:

这是一个工作示例。

package com.bw;

import org.apache.camel.builder.RouteBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.stereotype.Component;
import javax.sql.DataSource;

@Component
public class ReportRouter extends RouteBuilder 
    
    @Autowired
    DataSource dataSource;

    public DataSource getDataSource() 
        return dataSource;
    

    public void setDataSource(AbstractDataSource dataSource) 
        this.dataSource = dataSource;
        

    @Override
    public void configure()         
        from("timer:myTimer?repeatCount=1")
        .setBody(simple("select top 10 * from eai.Audit_General"))
        .log("SQL: $body")
        .to("jdbc:dataSource")
        .log("Out: $body");
    


application.properties

spring.datasource.url=jdbc:sqlserver://my-server.database.windows.net;databaseName=my-db
spring.datasource.username=user
spring.datasource.password=password
spring.datasource.driverClassName=com.microsoft.sqlserver.jdbc.SQLServerDriver

pom

<?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>com.bw</groupId>
  <artifactId>CrowdTwist-Reporting</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>

  <name>A Camel Spring Boot Route</name>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <spring.boot-version>2.5.3</spring.boot-version>
    <surefire.plugin.version>3.0.0-M4</surefire.plugin.version>
    <camel-version>3.11.1</camel-version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <!-- Spring Boot BOM -->
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>$spring.boot-version</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
      <!-- Camel BOM -->
      <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-spring-boot-dependencies</artifactId>
        <version>$camel-version</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>

    <!-- Spring Boot -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <exclusions>
        <exclusion>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
      </exclusions>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-undertow</artifactId>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    <!-- Camel -->
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
      <groupId>org.apache.camel.springboot</groupId>
      <artifactId>camel-stream-starter</artifactId>
    </dependency>

    <!-- Test -->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-test-spring-junit5</artifactId>
      <scope>test</scope>
    </dependency>
    
    <!-- Additions -->
    
    <!-- For receiving JMS messages from Artemis -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>artemis-jms-client</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-jms-starter</artifactId>
    </dependency>
    <dependency>
       <groupId>org.messaginghub</groupId>
       <artifactId>pooled-jms</artifactId>
    </dependency>
    <dependency>
        <groupId>javax.json</groupId>
        <artifactId>javax.json-api</artifactId>
    </dependency>
    
    <!-- Model Object Translation -->
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jackson</artifactId>
        <version>$camel-version</version>
    </dependency>
    
    <!-- Working with JSON -->
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jsonpath</artifactId>
      <version>$camel-version</version>
    </dependency>
    
    <!-- Database Access -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jdbc</artifactId>
    </dependency>
    <dependency>
        <groupId>org.apache.camel</groupId>
        <artifactId>camel-jdbc</artifactId>
        <version>$camel-version</version>
    </dependency>
    <dependency>
        <groupId>org.apache.camel.springboot</groupId>
        <artifactId>camel-jdbc-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>com.microsoft.sqlserver</groupId>
        <artifactId>mssql-jdbc</artifactId>
        <version>8.2.2.jre11</version>
    </dependency>
    
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.1</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <version>$spring.boot-version</version>
        <executions>
          <execution>
            <goals>
              <goal>repackage</goal>
            </goals>
          </execution>
        </executions>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>$surefire.plugin.version</version>
      </plugin>
    </plugins>
  </build>

</project>

【讨论】:

以上是关于从 Camel 和 Spring Boot 到 MS SQL Server 的 JDBC的主要内容,如果未能解决你的问题,请参考以下文章

Camel:使用 spring-boot 配置的数据源

Camel ActiveMQ + Spring boot 不读取 spring activemq 配置

如何在 Spring Boot 中为 Camel 配置 Jackson ObjectMapper

Spring Boot + Apache Camel + Freemarker 自定义模板加载器

spring boot + apache camel + mongodb 集成问题

在 Spring Boot Camel 应用程序公开的 Micrometer / Prometheus 信息中包含其他 JMX 指标