休息DSL路由-由于不允许同一端点有多个消费者,导致路由启动失败。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了休息DSL路由-由于不允许同一端点有多个消费者,导致路由启动失败。相关的知识,希望对你有一定的参考价值。

我试图通过一个例子来建立一个spring boot Hello World REST DSL camel应用程序,当我试图通过spring boot启动该应用程序时,我收到了以下异常。

[ERROR] Failed to execute goal org.springframework.boot:spring-boot-maven-plugin:1.5.4.RELEASE:run (default-cli) on project helloworld-example: An exception occurred while running. null: InvocationTargetException: org.apache.camel.FailedToStartRouteException: Failed to start route route3 because of Multiple consumers for the same endpoint is not allowed: direct://hello -> [Help 1]

据我所知,我只配置了一个路由,它将 "hello "路由连接到执行方法的Bean。

我的路由类。

package my.project.route;

import my.project.model.ResponseObject;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.model.rest.RestBindingMode;
import org.springframework.stereotype.Component;

@Component
public class MyRoute extends RouteBuilder {

    @Override
    public void configure() throws Exception {
        // configures REST DSL to use servlet component and in JSON mode
        restConfiguration()
                .component("servlet")
                .bindingMode(RestBindingMode.json);

        // REST DSL with a single GET /hello service
        rest()
                .get("/hello")
                .to("direct:hello");

        // route called from REST service that builds a response message
        from("direct:hello")
                .log("Hello World")
                .bean(this, "createResponse");
    }

    public ResponseObject createResponse() {
        ResponseObject response = new ResponseObject();
        response.setResponse("Hello World");
        response.setName("stack overflow");
        return response;
    }
}

我的应用程序代码

package my.project;

import org.apache.camel.component.servlet.CamelHttpTransportServlet;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

@SpringBootApplication
@Configuration
@ComponentScan("my.project")
public class Application {

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

    @Bean
    public ServletRegistrationBean camelServletRegistrationBean() {
        ServletRegistrationBean registration = new ServletRegistrationBean(new CamelHttpTransportServlet(),"/camel/*");
        registration.setName("CamelServlet");
        return registration;
    }

}

POM。

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<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/xsd/maven-4.0.0.xsd">

  <modelVersion>4.0.0</modelVersion>

  <groupId>my.project</groupId>
  <artifactId>helloworld-example</artifactId>
  <version>1.0-SNAPSHOT</version>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <name>Fabric8 :: Quickstarts :: Spring-Boot :: Camel</name>
  <description>Spring Boot example running a Camel route</description>

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



    <!-- Fuse 6.3 GA version for Spring Boot -->
    <!--<spring-boot.version>1.4.1.RELEASE</spring-boot.version>-->
    <!-- Fuse 7 EA / GA version for Spring Boot -->
    <spring-boot.version>1.5.4.RELEASE</spring-boot.version>

    <maven-compiler-plugin.version>3.3</maven-compiler-plugin.version>
    <maven-surefire-plugin.version>2.18.1</maven-surefire-plugin.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>1.5.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-actuator</artifactId>
      <version>1.5.4.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-spring-boot-starter</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-servlet-starter</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-jackson-starter</artifactId>
      <version>2.25.1</version>
    </dependency>
    <dependency>
      <groupId>org.apache.camel</groupId>
      <artifactId>camel-swagger-java-starter</artifactId>
      <version>2.25.1</version>
    </dependency>

  </dependencies>

  <build>
    <defaultGoal>spring-boot:run</defaultGoal>

    <plugins>

      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>${maven-compiler-plugin.version}</version>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>

      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>${maven-surefire-plugin.version}</version>
        <inherited>true</inherited>
        <configuration>
          <excludes>
            <exclude>**/*KT.java</exclude>
          </excludes>
        </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>

    </plugins>

  </build>

  <!-- used for adding maven repositories to download Fuse JARs -->
  <profiles>
    <profile>
      <id>fuse.repos</id>
      <activation>
        <activeByDefault>true</activeByDefault>
      </activation>

      <repositories>
        <repository>
          <id>maven.central</id>
          <name>Maven Central</name>
          <url>https://repo1.maven.org/maven2</url>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </releases>
        </repository>


      </repositories>

      <pluginRepositories>
        <pluginRepository>
          <id>maven.central</id>
          <name>Maven Central</name>
          <url>https://repo1.maven.org/maven2</url>
          <snapshots>
            <enabled>false</enabled>
          </snapshots>
          <releases>
            <enabled>true</enabled>
            <updatePolicy>never</updatePolicy>
          </releases>
        </pluginRepository>

      </pluginRepositories>
    </profile>
  </profiles>

</project>
答案

首先,你不需要 camelServletRegistrationBean() 应用类中的 bean。这只有在camel 2.19及以下版本中才需要。相反,在Application类中添加 camel.component.servlet.mapping.context-path=/* 中的@ComponentScan。

另外,你可以去掉@ComponentScan。因为你的路由上有一个@Component注解,spring会自动将其添加到上下文中。

以上是关于休息DSL路由-由于不允许同一端点有多个消费者,导致路由启动失败。的主要内容,如果未能解决你的问题,请参考以下文章

Apache驼峰单个文件的多个动态路由

同一端点(svc 文件)上的两个休息式 WCF 合同

dubbo-路由规则

在 Vertex AI 中将多个模型部署到同一端点

具有多个合同的 Mono WCF 休息服务“配置文件中未定义端点”

Apache Camel端点注入直接路由“端点上没有可用的消费者”