简介:
在这篇文章中,我们将学习基本结构以及如何处理API调用并将其路由到RabbitMQ。我们将创建一个maven项目,在该项目中我们将处理这一场景。
项目的Maven结构。
pom.xml如下所示
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.8</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.camel</groupId>
<artifactId>ApacheCamel</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>ApacheCamel</name>
<description>Demo project for Spring Boot to consume from rabbit and publish to kafka</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!--Camel Dependencies -->
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-spring-boot-starter</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-core</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-jackson</artifactId>
<version>2.17.0</version>
</dependency>
<dependency>
<groupId>org.apache.camel</groupId>
<artifactId>camel-rabbitmq</artifactId>
<version>2.17.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
创建一个模型类:
这里我使用 Lombok 以避免模板代码。
package com.camel.ApacheCamel.model;
import lombok.*;
/**
* Model class to Process the Library Book details.
*
*/
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Getter
@Setter
public class LibraryBookDetail {
private int serialNo;
private String bookName;
private String autherName;
private double price;
}
定义Camel路线。
- 我们需要将libraryBookDetail对象编入JSON。为此,我们将使用Apache Camel JacksonDataFormat。
- 创建骆驼路线,将雇员对象编入,然后将其发送到名为libraryQueue的rabbitmq队列中。同时,创建一个名为libraryQueue.Exchange的rabbitmq交易所。
package com.camel.ApacheCamel.route;
import com.camel.ApacheCamel.model.LibraryBookDetail;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jackson.JacksonDataFormat;
import org.springframework.stereotype.Component;
/**
* This class is use to Route the Data to RabbitMQ
* which is recieved from Rest call.
*/
@Component
public class LibraryRoute extends RouteBuilder {
@Override
public void configure() throws Exception {
JacksonDataFormat jacksonDataFormat = new JacksonDataFormat(LibraryBookDetail.class);
from("direct:restPointCall").id("idToRouteRest").marshal(jacksonDataFormat)
.to("rabbitmq://localhost:5672/librayQueue.exchange?queue=librayQueue&autoDelete=false").end();
}
}
创建控制器。
- 暴露GET REST API以获取librarybookdetail参数
- 使用 Camel ProducerTemplate 将 librarybookdetail 对象发送到 RabbitMQ Queueue。
package com.camel.ApacheCamel.controller;
import com.camel.ApacheCamel.model.LibraryBookDetail;
import org.apache.camel.Produce;
import org.apache.camel.ProducerTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class LibraryController {
@Produce(uri = "direct:restPointCall")
private ProducerTemplate producerTemplate;
/**
*
* @param serialNo
* @param bookName
* @param autherName
* @param price
* This call receive the Book Detail using rest call and forward it to rabbitMQ queue.
* @return Simple message "Processed Successfully"
*/
@GetMapping(value = "/enterbookdetail")
public String enterBookDetailToLibrary(@RequestParam("serialNo") int serialNo, @RequestParam("bookName") String bookName, @RequestParam("autherName") String autherName, @RequestParam("price") Double price) {
LibraryBookDetail bookDetail = LibraryBookDetail.builder().serialNo(serialNo)
.bookName(bookName)
.autherName(autherName)
.price(price)
.build();
producerTemplate.asyncSendBody(producerTemplate.getDefaultEndpoint(), bookDetail);
return "Processed Successfully";
}
}
最后,使用 SpringBootApplication 注解创建 bootstrap 类。
package com.camel.ApacheCamel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ApacheCamelApplication {
public static void main(String[] args) {
SpringApplication.run(ApacheCamelApplication.class, args);
}
}
结论
在这篇文章中,我们已经了解了 restApi 调用的简单流程,并使用 Apache Camel 对其进行了处理,并将此 Rest Call 发送到 RabbitMQ,以便我们的内部或外部服务能够使用这些数据。
