亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

spring boot 與kafka集成的示例代碼

 更新時(shí)間:2018年04月19日 16:57:47   作者:https://blog.csdn.net/baifanwudi/article/details/78282620?fps=1&locationNum=10  
這篇文章主要介紹了spring boot 與kafka集成的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧

新建spring boot項(xiàng)目

這里使用intellij IDEA

這里寫(xiě)圖片描述

這里寫(xiě)圖片描述

這里寫(xiě)圖片描述

這里寫(xiě)圖片描述

添加kafka集成maven

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>com.example</groupId>
  <artifactId>demo</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>demo</name>
  <description>Demo project for Spring Boot</description>

  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.8.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
  </parent>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
  </properties>

  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

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

    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
      <scope>test</scope>
    </dependency>
  </dependencies>

  <build>
    <plugins>
      <plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
      </plugin>
    </plugins>
  </build>
</project>

項(xiàng)目中application.properties 添加

spring.kafka.bootstrap-servers=vm208:9092,vm:9092,vm50:9092
spring.kafka.consumer.auto-offset-reset=latest
spring.kafka.consumer.group-id=local_test
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.acks=1

新建KafkaConsumer消費(fèi)類(lèi)

package com.example.demo.consumer;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
@Component
public class KafkaConsumer {

  private Logger logger = LoggerFactory.getLogger(this.getClass());

  @KafkaListener(topics = {"test"})
  public void listen(ConsumerRecord<?, ?> record) {
    System.out.printf("offset = %d,key =%s,value=%s\n", record.offset(), record.key(), record.value());
  }
}

啟動(dòng)spring-boot程序,在kafka集群,模擬發(fā)送topic,檢驗(yàn)接收

復(fù)制代碼 代碼如下:
bin/kafka-console-producer.sh --broker-list    vm208:9092,vm210:9092,vm50:9092  --topic  test

編寫(xiě)producer代碼

package com.example.demo.producer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
@Component
public class KafkaProducer {

  @Autowired
  private KafkaTemplate kafkaTemplate;
  String topic="test";
  public void sendMessage(String key,String data){
    kafkaTemplate.send(new ProducerRecord(topic,key,data));
  }
}

建立一個(gè)restful模擬發(fā)送( //http://localhost:8080/kafka/send.do?key=2&data=allen-test-message)

package com.example.demo.controller;
import com.example.demo.producer.KafkaProducer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProducerController {
  @Autowired
  private KafkaProducer kafkaProducer;
  @RequestMapping(value = "/kafka/send.do", method = RequestMethod.GET)
  public String sendMessage(@RequestParam(value = "key") String key, @RequestParam(value = "data") String data) {
    kafkaProducer.sendMessage(key, data);
    return "sucess";
  }
}

可以發(fā)現(xiàn) spring-kafka大大減少了代碼工作量.

官方文檔: https://docs.spring.io/spring-kafka/docs/1.2.2.RELEASE/reference/html/

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java棋類(lèi)游戲?qū)嵺`之中國(guó)象棋

    Java棋類(lèi)游戲?qū)嵺`之中國(guó)象棋

    這篇文章主要為大家詳細(xì)介紹了Java棋類(lèi)游戲中的中國(guó)象棋實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-02-02
  • springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶(hù)身份跳轉(zhuǎn)不同頁(yè)面

    springsecurity實(shí)現(xiàn)登錄驗(yàn)證以及根據(jù)用戶(hù)身份跳轉(zhuǎn)不同頁(yè)面

    Spring?Security是一種基于Spring框架的安全技術(shù),用于實(shí)現(xiàn)身份驗(yàn)證和訪(fǎng)問(wèn)控制,本文介紹了如何使用Spring?Security,結(jié)合session和redis來(lái)存儲(chǔ)用戶(hù)信息,并通過(guò)編寫(xiě)特定的登錄處理類(lèi)和Web配置,實(shí)現(xiàn)用戶(hù)登錄和注銷(xiāo)功能
    2024-09-09
  • Java中的裝箱和拆箱深入理解

    Java中的裝箱和拆箱深入理解

    裝箱和拆箱是java中老生常談的問(wèn)題,下面小編通過(guò)本文給大家介紹java裝箱和拆箱最基本的東西,感興趣的朋友一起看下吧
    2016-07-07
  • Java如何向Word模板中插入Base64圖片和數(shù)據(jù)信息

    Java如何向Word模板中插入Base64圖片和數(shù)據(jù)信息

    這篇文章主要介紹了Java如何向Word模板中插入Base64圖片和數(shù)據(jù)信息問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-07-07
  • 使用Mybatis生成樹(shù)形菜單的方法詳解

    使用Mybatis生成樹(shù)形菜單的方法詳解

    開(kāi)發(fā)中我們難免會(huì)遇到各種樹(shù)形結(jié)構(gòu)展示的場(chǎng)景,比如用戶(hù)登錄系統(tǒng)后菜單的展示等,本文為大家整理了使用Mybatis生成樹(shù)形菜單的方法,感興趣的小伙伴可以了解一下
    2023-06-06
  • java.lang.IncompatibleClassChangeError異常的問(wèn)題解決

    java.lang.IncompatibleClassChangeError異常的問(wèn)題解決

    本文主要介紹了java.lang.IncompatibleClassChangeError異常的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2024-06-06
  • 身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn)

    身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn)

    這篇文章主要介紹了身份證號(hào)碼驗(yàn)證算法深入研究和Java實(shí)現(xiàn),本文講解了18身份證號(hào)碼的結(jié)構(gòu)、根據(jù)17位數(shù)字本體碼獲取最后一位校驗(yàn)碼程序?qū)嵗葍?nèi)容,需要的朋友可以參考下
    2015-06-06
  • 詳解Zookeeper基礎(chǔ)知識(shí)

    詳解Zookeeper基礎(chǔ)知識(shí)

    本文主要講解了Zookeeper的基礎(chǔ)知識(shí),ZooKeeper提供了一個(gè)通用協(xié)調(diào)模式實(shí)現(xiàn)方法的開(kāi)源共享庫(kù),使程序員免于寫(xiě)這類(lèi)通用的協(xié)議。關(guān)于Zookeeper更多相關(guān)知識(shí),感興趣的小伙伴參考一下這篇文章
    2021-09-09
  • android中GridView的用法示例

    android中GridView的用法示例

    這篇文章主要介紹了android中GridView的用法,對(duì)于Android初學(xué)者很有參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2014-08-08
  • Java數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)與面試題匯總

    Java數(shù)據(jù)結(jié)構(gòu)之單鏈表的實(shí)現(xiàn)與面試題匯總

    由于順序表的插入刪除操作需要移動(dòng)大量的元素,影響了運(yùn)行效率,因此引入了線(xiàn)性表的鏈?zhǔn)酱鎯?chǔ)——單鏈表。本文為大家介紹了單鏈表的實(shí)現(xiàn)與面試題匯總,感興趣的可以了解一下
    2022-10-10

最新評(píng)論