SpringCloud学习之SpringCloudStream&集成kafka
一、关于Spring-Cloud-Stream
Spring Cloud Stream本质上就是整合了Spring Boot和Spring Integration,实现了一套轻量级的消息驱动的微服务框架。通过使用Spring Cloud Stream,可以有效地简化开发人员对消息中间件的使用复杂度,让系统开发人员可以有更多的精力关注于核心业务逻辑的处理。
在这里我先放一张官网的图:
应用程序通过Spring Cloud Stream注入到输入和输出通道与外界进行通信。根据此规则我们很容易的实现消息传递,订阅消息与消息中转。并且当需要切换消息中间件时,几乎不需要修改代码,只需要变更配置就行了。
在用例图中 Inputs代表了应用程序监听消息 、outputs代表发送消息、binder的话大家可以理解为将应用程序与消息中间件隔离的抽象,类似于三层架构下利用dao屏蔽service与数据库的实现的原理。
springcloud默认提供了rabbitmq与kafka的实现。
二、springcloud集成kafka
1、添加gradle依赖:
dependencies{
compile('org.springframework.cloud:spring-cloud-stream')
compile('org.springframework.cloud:spring-cloud-stream-binder-kafka')
compile('org.springframework.kafka:spring-kafka')
}
2、定义一个接口:
spring-cloud-stream已经给我们定义了最基本的输入与输出接口,他们分别是 Source,Sink, Processor
Sink接口:
package org.springframework.cloud.stream.messaging; import org.springframework.cloud.stream.annotation.Input;
import org.springframework.messaging.SubscribableChannel; public interface Sink {
String INPUT = "input"; @Input("input")
SubscribableChannel input();
}
Source接口:
package org.springframework.cloud.stream.messaging; import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel; public interface Source {
String OUTPUT = "output"; @Output("output")
MessageChannel output();
}
Processor接口:
package org.springframework.cloud.stream.messaging; public interface Processor extends Source, Sink {
}
这里面Processor这个接口既定义输入通道又定义了输出通道。同时我们也可以自己定义通道接口,代码如下:
package com.bdqn.lyrk.shop.channel; import org.springframework.cloud.stream.annotation.Input;
import org.springframework.cloud.stream.annotation.Output;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel; public interface ShopChannel { /**
* 发消息的通道名称
*/
String SHOP_OUTPUT = "shop_output"; /**
* 消息的订阅通道名称
*/
String SHOP_INPUT = "shop_input"; /**
* 发消息的通道
*
* @return
*/
@Output(SHOP_OUTPUT)
MessageChannel sendShopMessage(); /**
* 收消息的通道
*
* @return
*/
@Input(SHOP_INPUT)
SubscribableChannel recieveShopMessage(); }
3、定义服务类
package com.bdqn.lyrk.shop.server; import com.bdqn.lyrk.shop.channel.ShopChannel;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController
public class ShopService { @Resource(name = ShopChannel.SHOP_OUTPUT)
private MessageChannel sendShopMessageChannel; @GetMapping("/sendMsg")
public String sendShopMessage(String content) {
boolean isSendSuccess = sendShopMessageChannel.
send(MessageBuilder.withPayload(content).build());
return isSendSuccess ? "发送成功" : "发送失败";
} @StreamListener(ShopChannel.SHOP_INPUT)
public void receive(Message<String> message) {
System.out.println(message.getPayload());
}
}
这里面大家注意 @StreamListener。这个注解可以监听输入通道里的消息内容,注解里面的属性指定我们刚才定义的输入通道名称,而MessageChannel则可以通过
输出通道发送消息。使用@Resource注入时需要指定我们刚才定义的输出通道名称
4、定义启动类
package com.bdqn.lyrk.shop; import com.bdqn.lyrk.shop.channel.ShopChannel;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.stream.annotation.EnableBinding; @SpringBootApplication
@EnableBinding(ShopChannel.class)
public class ShopServerApplication { public static void main(String[] args) {
SpringApplication.run(ShopServerApplication.class, args);
}
}
注意@EnableBinding注解,这个注解指定刚才我们定义消息通道的接口名称,当然这里也可以传多个相关的接口
5、定义application.yml文件
spring:
application:
name: shop-server
cloud:
stream:
bindings:
#配置自己定义的通道与哪个中间件交互
shop_input: #ShopChannel里Input和Output的值
destination: zhibo #目标主题
shop_output:
destination: zhibo
default-binder: kafka #默认的binder是kafka
kafka:
bootstrap-servers: localhost:9092 #kafka服务地址
consumer:
group-id: consumer1
producer:
key-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
value-serializer: org.apache.kafka.common.serialization.ByteArraySerializer
client-id: producer1
server:
port: 8100
这里是重头戏,我们必须指定所有通道对应的消息主题,同时指定默认的binder为kafka,紧接着定义Spring-kafka的外部化配置,在这里指定producer的序列化类为ByteArraySerializer
启动程序成功后,我们访问 http://localhost:8100/sendMsg?content=2 即可得到如下结果
SpringCloud学习之SpringCloudStream&集成kafka的更多相关文章
- springcloud 集成kafka问题记录,发消息报错:ERROR o.s.kafka.support.LoggingProducerListener - Exception thrown when sending a message with key='null' and payload='{-1,
在springcloud集成kafka,发送消息时报错: 2018-08-15 16:01:34.159 [http-nio-8081-exec-1] INFO org.apache.kafka.c ...
- springcloud集成kafka
项目名称:布控预警 水平拆分出来的项目,作为一个单独的可以对外提供服务的项目 项目设计:springcloud,可以集成各个不同平台的一个作为对外提供的微服务项目 项目功能:实现各个平台和本平台之间的 ...
- SpringCloud学习之Ribbon
一.负载均衡与Ribbon 负载均衡,在集群中是很常见的一个“名词”,顾名思义是根据一定的算法将请求分摊至对应的服务节点上,常见的算法有如下几种: 轮询法:所有请求被依次分发到每台应用服务器上,每台服 ...
- springCloud学习总览
写完最后一篇特意去看了看第一篇是什么时候写的---2018/11/19,到现在三个月多一点,总的来说这三个月通过<Spring 微服务实战>这本书,算是对微服务进行了一次扫盲学习. ...
- SpringCloud学习心得—1.3—Eureka与REST API
SpringCloud学习心得—1.3—Eureka与REST API Eureka的REST API接口 API的基本访问 Eureka REST APIEureka 作为注册中心,其本质是存储 ...
- SpringCloudStream(RabbitMQ&Kafka)&Spring-Kafka配置使用
目录 是什么 解决问题 使用方式 创建生产者项目 pom yml 生产消息方法 接口 实现 创建消费者项目 pom yml 接收消息方法 重复消费 消费者yml 持久化 消费者负载个性配置(预拉取) ...
- Storm集成Kafka应用的开发
我们知道storm的作用主要是进行流式计算,对于源源不断的均匀数据流流入处理是非常有效的,而现实生活中大部分场景并不是均匀的数据流,而是时而多时而少的数据流入,这种情况下显然用批量处理是不合适的,如果 ...
- Windows phone 8 学习笔记(9) 集成
原文:Windows phone 8 学习笔记(9) 集成 本节整理了之前并没有提到的Windows phone 8 系统相关集成支持,包括选择器.锁定屏幕的.联系人的访问等.选择器列举了若干内置应用 ...
- SpringCloud学习之feign
一.关于feigin feigin是一种模板化,声明式的http客户端,feign可以通过注解绑定到接口上来简化Http请求访问.当然我们也可以在创建Feign对象时定制自定义解码器(xml或者jso ...
随机推荐
- Hibernate之HQL
SQL语句的DML操作不外乎:增,删,改,查 增加 : save(),persist() 删除 : delete() 改动 : update() 查询 : get() ,load() 其 ...
- 14-TypeScript简单工厂模式
在TypeScript中,要调用功能,通常在调用方通过实例化被调用方对象来调用相关方法,但这种实现在调用方和被调用方形成了强耦合的关系. 另外如果被调用方有种实现,在调用方需要根据场景去实例化不同的类 ...
- Microsoft dynamic sdk中join应该注意的问题.
QueryExpression queryNextSeq = new QueryExpression { EntityName = "ep_prodoperationsequence&quo ...
- MSSQL---extents
一.MSSQLextent分两种: 1. Mixed extent:每个表或索引创建时,MSSQL并不给它分配一个extent,而是在mixed extnet内分配一个页,空间需求扩大时,再分配一个… ...
- Andrew Ng机器学习第一章——单变量线性回归
监督学习算法工作流程 h代表假设函数,h是一个引导x得到y的函数 如何表示h函数是监督学习的关键问题 线性回归:h函数是一个线性函数 代价函数 在线性回归问题中,常常需要解决最小化问题.代价函数常用平 ...
- Docker学习笔记 - Docker的远程访问
学习内容: 配置客户端与守护进程的远程访问 服务端配置-H选项: 使服务端支持远程被访问 客户端使用-H选项: 使客户端访问远程服务端 本地环境DOCKER_HOST设置客户端访问的默认服务端地址 准 ...
- Scala入门(1)Linux下Scala(2.12.1)安装
Scala入门(1)Linux下Scala(2.12.1)安装 一.文件准备 1.1 文件名称 scala-2.12.1.tgz 1.2 下载地址 http://www.scala-lang.org/ ...
- 详解Windows Server 2008 R2下安装Oracle 11g
本篇文章转载 http://www.it165.net/database/html/201212/3385.html 一.安装前的准备工作: 1. 修改计算机名: 服务器的计算机名称对于登录到Orac ...
- leetcode算法: Find Bottom Left Tree Value
leetcode算法: Find Bottom Left Tree ValueGiven a binary tree, find the leftmost value in the last row ...
- Spring 环境与profile(三)——利用maven的resources、filter和profile实现不同环境使用不同配置文件
基本概念 profiles定义了各个环境的变量id filters中定义了变量配置文件的地址,其中地址中的环境变量就是上面profile中定义的值 resources中是定义哪些目录下的文件会被配置文 ...