SpringCloudBus使用Kafka实现消息总线

转自: https://blog.csdn.net/wqh8522/article/details/79193873

Spring Cloud构建微服务架构(七)消息总线(续:Kafka): http://blog.didispace.com/springcloud7-2/

spring cloud bus整合kafka问题:  https://bbs.csdn.net/topics/392179985

springboot 2.0.0.RELEASE +spring cloud Finchley.M7 +springcloud bus+kafka实现配置动态刷新: https://my.oschina.net/u/2263272/blog/1634010?nocache=1531813104906

SpringCloud消息总线BUS: https://cloud.tencent.com/developer/news/251479

Spring Cloud Bus整合Kafkahttps://segmentfault.com/a/1190000011752466

Spring Cloud Bus 消息总线集成Kafka: https://www.cppentry.com/bencandy.php?fid=120&id=183602

SpringCloud实战8-Bus消息总线:  https://www.cnblogs.com/huangjuncong/p/9077099.html

SpringCloudBus使用Kafka实现消息总线:  https://blog.csdn.net/wqh8522/article/details/79193873

SpringCloudBus使用Kafka实现消息总线https://segmentfault.com/a/1190000013031488

SpringCloud 分布式配置:   https://www.cnblogs.com/zhangjianbin/p/6347247.html

spring cloud 系列第8篇 —— config+bus 分布式配置中心与配置热刷新 (F版本): https://blog.csdn.net/m0_37809146/article/details/86677462

Spring Cloud (20) | Spring Cloud Bus 使用kafka消息总线、gitlab添加webhooks实现自动刷新配置: https://blog.csdn.net/superdangbo/article/details/78776532

[菜鸟SpringCloud实战入门]第八章:通过消息总线Spring Cloud Bus实现配置文件刷新(使用Kafka):  https://blog.csdn.net/qqxx6661/article/details/88701051

Spring Cloud Bus 消息总线 kafka 集成配置:  https://blog.csdn.net/skywoodsky/article/details/80772848

学习笔记:微服务-22 spring cloud config+kafka+spring cloud bus 消息总线实现配置自动更新: https://blog.csdn.net/olinbsoft/article/details/86586467

使用kafka作为springcloud bus的消息总线,以及如何自定义总线事件: https://blog.csdn.net/xiao_jun_0820/article/details/78115746

Spring Cloud Config + Spring Cloud Bus + kafka实现配置中心配置动态更新: https://blog.csdn.net/lixiaxin200319/article/details/82811047

Config Server——使用Spring Cloud Bus自动刷新配置: http://www.itmuch.com/spring-cloud/spring-cloud-bus-auto-refresh-configuration/

SpringCloud学习之SpringCloudBus: https://yq.aliyun.com/articles/613228

Spring Cloud构建微服务架构(七)消息总线(续:Kafka): https://www.jianshu.com/p/730d86030a41

spring cloud bus整合kafka问题https://geekori.com/details.php?que_id=8168

springcloudbus自定义event消息kafka中收不到消息,springcloud Greenwich版不行,daston版本可以:  https://ask.csdn.net/questions/761217

Spring Cloud Bus: https://springcloud.cc/spring-cloud-bus.html

Spring Cloud构建微服务架构(七)消息总线(续:Kafka): http://blog.didispace.com/springcloud7-2/

Spring Cloud构建微服务架构(七)消息总线: http://blog.didispace.com/springcloud7/

[Spring Cloud] 6 Spring Cloud Bus :  https://my.oschina.net/roccn/blog/832059

jfinal-admin 3.6 正式发布,更新 jfinal 及 beetl 到最新版本: https://www.xujun.org/article308898.html

Flume + Kafka学习——整合Demo:  https://www.ucloud.cn/yun/8346.html

Spring Cloud Alibaba系列教程-05-Sentinel入门与整合:  http://ldnmen.com/

Spring Cloud 微服务分布式架构项目开发实战: https://www.douban.com/group/topic/146012302/

Spring_Cloud构建云架构: https://www.iteye.com/blogs/subjects/Spring-Cloud#4370

跟我学SpringCloud | 第一篇:介绍:  http://www.hellojava.com/a/79952.html

Spring Cloud笔记:  https://www.bcoder.top/2018/12/08/SpringCloud%E7%AC%94%E8%AE%B0/

Spring Cloud微服务实践:  http://server.51cto.com/Micro-550525.htm

Spring Cloud Stream kafka在集群时候遇到的消息重复消费问题:  http://www.spring4all.com/article/773

mhuang / mhuang-cloud-study:   https://gitee.com/mhuang/mhuang-cloud-study

Spring cloud实践之道四(消息处理): http://www.imooc.com/article/275387

Spring Cloud 入门教程(七): 消息总线(Spring Cloud Bus)(Greenwich.RELEASE):  https://www.liangzl.com/get-article-detail-134439.html

spring-cloud-starter-bus-kafka-1.3.1.RELEASE:  https://www.mvnjar.com/org.springframework.cloud/spring-cloud-starter-bus-kafka/1.3.1.RELEASE/detail.html

SpringCloud实战8-Bus消息总线https://www.cnblogs.com/huangjuncong/p/9077099.html

spring cloud : 消息中间件kafka:  https://www.cppentry.com/bencandy.php?fid=120&id=193966

spring cloud : 消息中间件kafka: https://www.cppentry.com/bencandy.php?fid=120&id=193966

SpringCloud Bus消息总线:  https://www.cnblogs.com/toov5/p/10293755.html

Spring Cloud Bus整合Kafka: https://blog.csdn.net/u012702547/article/details/78630436?utm_source=tuicool&utm_medium=referral

Spring cloud 加入消息总线后,不能访问controller: https://bbs.csdn.net/topics/392260453

Spring Cloud Bus + RabbitMq 自动刷新:  https://my.oschina.net/u/3747963/blog/1595746

spring cloud bus原理总结https://www.cnblogs.com/songxh-scse/p/7833963.html

springcloud微服务追踪使用kafka对zipkin进行传输:  https://bbs.csdn.net/topics/392482048

Spring Cloud Starter Bus Kafka » 2.0.0.RELEASEhttps://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-bus-kafka/2.0.0.RELEASE

Spring Cloud 基于Bus 的AB-TEST组件:  https://cloud.tencent.com/developer/news/381266

SpringCloud 微服务 (十) 消息队列MQ 基础:  https://my.oschina.net/u/3829444/blog/1836946

Spring Cloud Bus + kafka实现配置中心配置动态更新: https://www.jianshu.com/p/cc8fa1d58e3f

SpringCloud学习之SpringCloudStream&集成kafkahttps://www.cnblogs.com/niechen/p/8687206.html

SpringCloud学习之SpringCloudStream&集成kafka:  https://www.cnblogs.com/niechen/p/8687206.html

Spring Cloud 基于Bus 的AB-TEST组件: https://www.jianshu.com/p/d2d4075888dd

Spring Cloud入门教程(十):消息总线(Bus): https://www.jianshu.com/p/bab2da26f282

一站通关Spring、SpringBoot与SpringCloud视频: http://blog.sina.com.cn/s/blog_139f782260102yfjx.html

spring cloud bus整合kafka问题:  https://bbs.csdn.net/topics/392179985?page=1

Spring Cloud系列教程 | 第十篇:Spring Cloud Config Server和Spring Cloud Bus以及Kafka和数据库动态刷新配置: https://blog.csdn.net/qq_43253123/article/details/82884027

SpringCloud 分布式配置: https://www.cnblogs.com/zhangjianbin/p/6347247.html

Spring boot2.0 springcloud Finchley配置kafka消息总线bus:  https://www.cppentry.com/bencandy.php?fid=120&id=204263

3.《kafka》SpringCloud学习之SpringCloudStream&集成kafka: https://www.jianshu.com/p/412f2887992a

spring cloud bus 配置服务端无法连接kafka: https://segmentfault.com/q/1010000018531564?utm_source=tag-newest

原 史上最简单的SpringCloud教程 | 第八篇: 消息总线(Spring Cloud Bus)(Finchley版本): https://blog.csdn.net/forezp/article/details/81041062

SpringCloud Bus: https://www.cnblogs.com/ming-blogs/p/10940900.html

Spring Cloud构建微服务架构(七)消息总线(续:Kafka): https://blog.csdn.net/zl1zl2zl3/article/details/60318644

Spring Cloud Config + Spring Cloud Bus + kafka实现配置中心配置动态更新:  https://www.cppentry.com/bencandy.php?fid=120&id=213780

Spring Cloud Bus 消息总线 kafka 集成配置:  https://www.cppentry.com/bencandy.php?fid=120&id=201500

spring cloud 使用spring cloud bus自动刷新配置: https://www.cnblogs.com/pypua/p/10129734.html

Spring Cloud Bus中的事件的订阅与发布(一): https://www.jianshu.com/p/d62612947e9e

springcloud配置中心刷新失败http://localhost:8881/actuator/bus-refresh(已解决): https://blog.csdn.net/firestarwolf/article/details/88718983

跟我学SpringCloud | 第八篇:Spring Cloud Bus 消息总线: http://www.ppkanshu.com/index.php/post/2423.html

Spring Cloud Bus: https://blog.csdn.net/xichenguan/article/details/77535694

Config Server——使用Spring Cloud Bus自动刷新配置: https://blog.csdn.net/weixin_34252686/article/details/86879431

SpringCloud Bus 消息总线: https://segmentfault.com/a/1190000017391824?utm_source=tag-newest

SpringCloud Bus 消息总线: https://segmentfault.com/a/1190000017391824

SpringCloud教程 | 第13篇:高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ):  https://blog.csdn.net/yangliuhbhd/article/details/80526525

spring cloud bus: https://blog.csdn.net/weixin_34387284/article/details/86815694

Spring Cloud构建微服务架构(七)消息总线(续:Kafka): https://www.jianshu.com/p/730d86030a41?nomobile=yes

3:SpringCloud Config配置中心的bus动态刷新: http://www.luyixian.cn/news_show_36597.aspx

 

SpringCloud微服务实战之消息总线BUS-RabbitMQ: https://blog.csdn.net/u012343297/article/details/78872564

Spring Cloud (3) | spring cloud bus 消息总线kafka应用:  https://www.cppentry.com/bencandy.php?fid=120&id=196368

SpringCloud Bus 整合 RabbitMQ: https://blog.csdn.net/qq_36657997/article/details/83070498

第八章: 通过消息总线 Spring Cloud Bus 实现配置文件刷新 (使用 Kafka): https://www.h3399.cn/201903/672369.html

微服务和大数据性能指标参考和相关组件:http://www.sohu.com/a/327523699_120232014

idea创建springcloud项目图文教程(bus 消息总线)(十二): https://blog.csdn.net/hcmony/article/details/82777986

23SpringCloud - Spring Cloud Config 中使用 Refresh: https://blog.csdn.net/qq_20042935/article/details/90475315

java B2B2C Springcloud多租户电子商城系统-spring cloud bus原理总结: https://yq.aliyun.com/articles/684959

SpringCloud消息总线: https://blog.csdn.net/orangeccx/article/details/78604914

SpringCloud学习之一前置知识理解: https://blog.csdn.net/kaishizige/article/details/80232099

Spring Cloud Config + Spring Cloud Bus 实现配置文件动态刷新(版本 Finchley.RC1): https://blog.csdn.net/zth0825/article/details/81080642

Spring Cloud Bus 消息总线介绍: https://yq.aliyun.com/articles/697913

springcloud+sleuth+zipkin+kafka+es: https://blog.csdn.net/niemingming/article/details/79002846

SpringCloud学习笔记: https://blog.csdn.net/luohongtuCSDN/article/details/80968174

Spring Cloud 微服务架构学习笔记与示例: https://www.cnblogs.com/edisonchou/p/java_spring_cloud_foundation_sample_list.html

 

SpringCloud入门最佳实践(一)微服务概述: https://blog.csdn.net/qq_33746789/article/details/82496932

SpringCloud Stream-----2、消息生产与消费; https://blog.csdn.net/hekewangzi/article/details/78466176

Spring Cloud—十一、使用Spring Cloud Bus(消息总线)实现自动更新: https://www.jianshu.com/p/97aefedec1b7

Spring Cloud学习系列第七篇【消息总线】: https://www.cnblogs.com/yipaihushuo/p/9428146.html

Spring Cloud(十一)高可用的分布式配置中心 Spring Cloud Bus 消息总线集成(RabbitMQ): https://blog.csdn.net/yanpenglei/article/details/78891620

通过Spring Cloud Bus配置Webhooks不能自动刷新的问题"message":"No message available","path":"/monitor": https://blog.csdn.net/jun_1990/article/details/93622946

springcloud(九):配置中心和消息总线(配置中心终结版): https://blog.csdn.net/liu_dong_kang/article/details/87881331

事件监听机制(四)Spring Cloud Bus自定义实现RemoteApplicationEvent: https://blog.csdn.net/shang_xs/article/details/88051207

 

 

 版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wqh8522/article/details/79193873
Kafka是分布式发布-订阅消息系统,最初由LinkedIn公司开发,之后成为之后成为Apache基金会的一部分,由Scala和Java编写。Kafka是一种快速、可扩展的、设计内在就是分布式的,分区的和可复制的提交日志服务。

在开始本文前,需要搭建kafka的环境,如果是在CentOS环境下,可以看看我前面的文章:CentOS7下Kafka的安装介绍 。其他平台下可以自行百度或Google。

在之前的环境中,需要修改server.properties文件,开启9092端口的监听:

listeners=PLAINTEXT://your.host.name:9092
1
SpringBoot简单整合Kafka
因为SpringCloud是基于SpringBoot的,所以在使用SpringCloudBus整合之前先用SpringBoot整合并记录下来。

创建项目
这里创建一个名为kafka-hello的SpringBoot项目,并添加以下依赖:

<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>

  <dependency>
    <groupId>org.springframework.kafka</groupId>
    <artifactId>spring-kafka</artifactId>
    <version>1.1.1.RELEASE</version>
  </dependency>

  <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.2</version>
  </dependency>
</dependencies>

消息实体类
@Data
public class Message {
    private Long id;//id
    private String msg; //消息
    private Date sendTime; //发送时间
}

消息产生者
在该类中创建一个消息发送的方法,使用KafkaTemplate.send()发送消息,wqh是Kafka里的Topic。

@Component
@Slf4j
public class KafkaSender {

    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    private Gson gson = new GsonBuilder().create();

    public void send(Long i){
        Message message = new Message();
        message.setId(i);
        message.setMsg(UUID.randomUUID().toString());
        message.setSendTime(new Date());
        log.info("========发送消息  "+i+" >>>>{}<<<<<==========",gson.toJson(message));
        kafkaTemplate.send("wqh",gson.toJson(message));
    }
}

消息接收类,
在这个类中,创建consumer方法,并使用@KafkaListener注解监听指定的topic,如这里是监听wanqh和wqh两个topic。

@Component
@Slf4j
public class KafkaConsumer {

    @KafkaListener(topics = {"wanqh","wqh"})
    public void consumer(ConsumerRecord<?,?> consumerRecord){
        //判断是否为null
        Optional<?> kafkaMessage = Optional.ofNullable(consumerRecord.value());
        log.info(">>>>>>>>>> record =" + kafkaMessage);
        if(kafkaMessage.isPresent()){
            //得到Optional实例中的值
            Object message = kafkaMessage.get();
            log.info(">>>>>>>>接收消息message =" + message);
        }
    }
}

修改启动类
@SpringBootApplication
public class KafkaApplication {

    @Autowired
    private KafkaSender kafkaSender;

    @PostConstruct
    public void init(){
      for (int i = 0; i < 10; i++) {
        //调用消息发送类中的消息发送方法
        kafkaSender.send((long) i);
      }
    }
    public static void main(String[] args) {
       SpringApplication.run(KafkaApplication.class, args);
    }
}

配置文件
spring.application.name=kafka-hello
server.port=8080
#============== kafka ===================
# 指定kafka 代理地址,可以多个
spring.kafka.bootstrap-servers=192.168.18.136:9092

#=============== provider  =======================
spring.kafka.producer.retries=0
# 每次批量发送消息的数量
spring.kafka.producer.batch-size=16384
spring.kafka.producer.buffer-memory=33554432

# 指定消息key和消息体的编解码方式
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer

#=============== consumer  =======================
# 指定默认消费者group id
spring.kafka.consumer.group-id=test-consumer-group

spring.kafka.consumer.auto-offset-reset=earliest
spring.kafka.consumer.enable-auto-commit=true
spring.kafka.consumer.auto-commit-interval=100

# 指定消息key和消息体的编解码方式
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer

测试
直接启动该项目:

SpringCloudBus整合Kafka
前面介绍使用RabbitMQ整合SpringCloudBus实现了消息总线,并且测试了动态刷新配置文件。RabbitMQ是通过引入spring-cloud-starter-bus-amqp模块来实现消息总线。若使用Kafka实现消息总线,我们可以直接将之前添加的spring-cloud-starter-bus-amqp替换成spring-cloud-starter-bus-kafka 。

这里我将前面的config-client复制一份,改名config-client-kafka。传送门:SpingCloudBus整合RabbitMQ

所添加的依赖:
<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-config</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.retry</groupId>
            <artifactId>spring-retry</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bus-kafka</artifactId>
        </dependency>
    </dependencies>

添加kafka的配置信息
#Kafka的服务端列表,默认localhost
spring.cloud.stream.kafka.binder.brokers=192.168.18.136:9092
#Kafka服务端的默认端口,当brokers属性中没有配置端口信息时,就会使用这个默认端口,默认9092
spring.cloud.stream.kafka.binder.defaultBrokerPort=9092
#Kafka服务端连接的ZooKeeper节点列表,默认localhost
spring.cloud.stream.kafka.binder.zkNodes=192.168.18.136:2181
#ZooKeeper节点的默认端口,当zkNodes属性中没有配置端口信息时,就会使用这个默认端口,默认2181
spring.cloud.stream.kafka.binder.defaultZkPort=2181

测试方法与前一篇一样,不介绍了。

参考:

《SpringCloud微服务实战》
SpringBoot Kafka 整合使用
Spring Cloud构建微服务架构(七)消息总线(续:Kafka)
项目地址:

https://gitee.com/wqh3520/
原文[地址:

SpringCloudBus使用Kafka实现消息总线
--------------------- 
作者:wqh3520 
来源:CSDN 
原文:https://blog.csdn.net/wqh8522/article/details/79193873 
版权声明:本文为博主原创文章,转载请附上博文链接!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值