spring stream http 流式计算

   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


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值