http 请求源,是监听http请求,并发送消息给kafka 消息中间件,其中,消息的格式为 Content-Type matches text/*
or application/json
发送的string 类型,否则为字节数组的形式。应用场景,比如流量统计,http的监听等。
数据源需要引入的包为
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-stream-kafka</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud.stream.app</groupId> <artifactId>spring-cloud-starter-stream-source-http</artifactId> <version>1.2.1.BUILD-SNAPSHOT</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency>
设置数据源
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.stream.app.http.source.HttpSourceConfiguration; import org.springframework.context.annotation.Import; @SpringBootApplication @Import(HttpSourceConfiguration.class) public class YtxNginxSourceApplication { public static void main(String[] args) { SpringApplication.run(YtxNginxSourceApplication.class, args); } }
http 接受的sink为
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.stream.annotation.EnableBinding; import org.springframework.cloud.stream.annotation.StreamListener; import org.springframework.cloud.stream.messaging.Sink; @SpringBootApplication @EnableBinding(Sink.class) public class YtxNginxTransformApplication { public static void main(String[] args) { SpringApplication.run(YtxNginxTransformApplication.class, args); } @StreamListener(Sink.INPUT) public void log(String payload){ System.out.println(payload); } }
注意:
1、需要导入仓库地址
<repositories> <repository> <id>libs-snapshot-local</id> <url>http://repo.spring.io/libs-snapshot-local</url> </repository> </repositories>2、需要指定路径为/foo等,不能使用默认的/
3、指定参数为
http.secured=false http.pathPattern=/foo
配置好以上参数后,可以通过http://localhost:8080/foo 的post 请求数据,请求数据的body 可以设置为任何字符串
其中参考的源代码为:
https://github.com/spring-cloud-stream-app-starters/http