spring cloud stream环境搭建

1 简介

SpringCloud Stream 是基于消息驱动微服务应用框架。

Spring Cloud Stream is a framework for building message-driven microservice applications. Spring Cloud Stream builds upon Spring Boot to create standalone, production-grade Spring applications, and uses Spring Integration to provide connectivity to message brokers. It provides opinionated configuration of middleware from several vendors, introducing the concepts of persistent publish-subscribe semantics, consumer groups, and partitions.

1.1 spring cloud stream结构

术语:

bindding:连接channel和binder的组件

binder:粘合剂,对外连接brokers

group:对消费者分组,实现类似于active mq中topic的发布订阅

out: 输出channel

input: 输入channel

框架结构如下图所示:

1.2 搭建工程

1.2.1  idea创建: File -> new -> spring initializr -> next -> 填写工程名,maven路径,next -> 选择spring boot 版本,选择spring组件(本工程选择spring-cloud,spring-web)

1.2.2  spring boot cli创建:spring init -dweb,cloud(该命令需要安装spring boot cli,默认创建为maven结构的工程-d为maven依赖)

1.2.3 jdk版本,官方建议为1.6以上。推荐使用1.7+。

1.3 版本选择

本文选择的spring cloud stream的版本为Chelsea.SR1,对应的kafka版本为0.10.1.1,对接的kafka集群最好是0.10版本以上的。如果低于0.10,会出现工程启动失败的错误。有2种解决方案:

1)、加验证,具体操作请参考,连接:验证

2)、降低spring cloud stream版本至1.0.2.RELEASE,该版本对应的kafka-client版本为0.8.2.2

1.4 配置

spring cloud stream默认提供了一套基础配置。broker为localhost:9092,zkNode为localhost:2181,其他选项也均为默认值(所以,如果你为本地部署,原则上不需要添加任何配置项)。具体可参考spring-scloud-stream jar中的org.springframework.cloud.stream.config包中的javaconfig

spring 1.x xml配置,spring 2.x 注解配置,spring 3.x java 配置,spring boot习惯性配置。官方推荐为java config+习惯性配置

1.5 注解

SpringBootApplication:继承了EnableAutoConfiguration、ComponentScan。主要完成自动化配置和路径扫描。

EnableBinding:开启stream功能。参数可以source、sink、process。也可以自己实现(主要为声明channel)。

RestController:开启web功能。与Controller类似。官方解释:@RestController is a stereotype annotation that combines @ResponseBody and @Controller.

RequestMapping:开启绑定url功能,url可通过参数指定。

StreamListener:开启监听channel功能,参数为默认或实现的channel接口。

EnableAutoConfiguration:开启自动配置功能。继承自

ComponentScan:开启扫描功能,主要针对bean和配置文件。

1.6 代码示例

1.6.1 生产者代码示例:

//启动类

@SpringBootApplication//自动化配置

@EnableBinding(Source.class)//开启与消息应用代理的连接

public class SenderApp {

public static void main(String[] args) {

SpringApplication.run(SenderApp.class, args);//启动spring应用

}

}

//控制类

@RestController//web 注解

public class ProducerController {

@Autowired// output channel注入

private Source source;

//写入channel

@RequestMapping(value = "/push/{msg}", method = RequestMethod.GET)

public String send(@PathVariable("msg") String msg){

source.output().send(MutableMessageBuilder.withPayload(msg).build());

return "success";

}

}

1.6.2 消费者代码示例:

//启动类

@SpringBootApplication

@EnableBinding(Sink.class)

public class ReceiverApp{

public static void main(String[] args) {

SpringApplication.run(ReceiverApp.class, args);

}

}

//消费者类

@RestController

public class KafkaStreamConsumer {

//display msg for html

private Map msgCache = new TreeMap();

@RequestMapping("/pull")

public Message display(){

return msgCache.get(0);

}

@StreamListener(Sink.INPUT)

public void process(Message message){

//add msg handler------------------>

{

//to do

msgCache.put(0,message);

System.out.println(message.getPayload().toString());

}

//handle msg end-------------------->

Acknowledgment acknowledgment = message.getHeaders().get(KafkaHeaders.ACKNOWLEDGMENT, Acknowledgment.class);

if (acknowledgment != null) {

acknowledgment.acknowledge();

}

}

}

1.6.3 maven示例:

org.springframework.cloud

spring-cloud-starter-stream-kafka

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-starter-actuator

1.7 问题

1.7.1 配置问题

spring cloud stream官网介绍太过简单,如按官方例子配置,通常工程无法启动或抛出各种运行时错误。建议从github上搜索项目sample作参考。本文给出经过验证使用的连接kafka的配置样例。

生产者yml配置:

server:

port: 8081 #web应用端口号

spring:

application:

name: kafka-output

cloud:

stream:

binders:

kafka_output:#为binding output绑定的名字

type: kafka

environment:

spring:

cloud:

stream:

kafka:

binder:

brokers: localhost:9092  #kafka集群host,逗号分隔。host:port或host均可以,如果不设置端口号,spring boot会选用默认的9092端口号

zknodes: localhost:2181  #zookeeper集群host,逗号分隔。host:port或host均可以,如果不设置端口号,spring boot会选用默认的2181端口号

instanceCount: 1

instanceIndex: 0

bindings:

output: #channel名字,项目中指定

auto-add-partitions: false

auto-create-topics: true

min-partition-count: 1

destination: test

producer:

partitionCount: 1

binder: kafka_output #output绑定名

group: s1            #分组名

#actuator j监控运行jar信息,关闭验证

management:

health:

mail:

enabled:false

management:

security:

enabled:false

消费者 properties配置:

server.port = 8082

spring.application.name = input

#actuator j监控运行jar信息,关闭验证

management.health.mail.enabled=false

management.security.enabled=false

spring.cloud.stream.instancecount = 1

spring.cloud.stream.instanceIndex = 0

#binding config

spring.cloud.stream.bindings.input.destination= test

spring.cloud.stream.bindings.input.group = s1

spring.cloud.stream.bindings.input.autoCommitOffset = false

spring.cloud.stream.bindings.input.concurrency = 1

spring.cloud.stream.bindings.input.partitioned = false

spring.cloud.stream.bindings.input.binder = kafka_input

spring.cloud.stream.bindings.input.consumer.headerMode = raw

spring.cloud.stream.bindings.input.content-type = text/plain;

#binder config

spring.cloud.stream.binders.kafka_input.type = kafka

spring.cloud.stream.binders.kafka_input.environment.spring.cloud.stream.kafka.binder.brokers = localhost:9092

spring.cloud.stream.binders.kafka_input.environment.spring.cloud.stream.kafka.binder.zkNodes = localhost:2181

1.7.2 私服问题

公司私服缺少spring cloud stream应用中大部分的jar包,maven中央仓库因国内环境下载速度过慢。建议仓库配置添加国内地址。

添加方法:修改maven根目录conf 文件夹中的 server.xml;或复制该文件到本地.m2文件夹中;或者项目pom中添加。

http://maven.aliyun.com/nexus/content/groups/public/

http://uk.maven.org/maven2/

http://maven.oschina.net/content/groups/public/

http://maven.aliyun.com/nexus/content/repositories/snapshots/

1.7.3 actuator监控问题

问题:type=Unauthorized, status=401

添加配置参数:

#actuator j监控运行jar信息,关闭验证

management.health.mail.enabled=false

management.security.enabled=false

1.8 actuator 使用说明

/beans:查看当前应用Ioc容器bean状态

/mappings:查看web url和方法映射

/trace:查看最近http请求

/env:查看环境变量

/configprops:查看配置变量

/info:查看基础信息

/dump:查看线程堆栈生成的信息

/shutdown:优雅关闭

/autoconfig:自动装配信息

/health:当前健康状况(可自定义配置)

排查问题神器,建议搭建spring boot应用整合actuator。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 194,491评论 5 459
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 81,856评论 2 371
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 141,745评论 0 319
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 52,196评论 1 263
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 61,073评论 4 355
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 46,112评论 1 272
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 36,531评论 3 381
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 35,215评论 0 253
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 39,485评论 1 290
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 34,578评论 2 309
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 36,356评论 1 326
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 32,215评论 3 312
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 37,583评论 3 299
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 28,898评论 0 17
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,174评论 1 250
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 41,497评论 2 341
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 40,697评论 2 335

推荐阅读更多精彩内容