当前位置: 首页 > news >正文

网站建设与动态网页长尾关键词挖掘熊猫

网站建设与动态网页,长尾关键词挖掘熊猫,网站网页压缩,bootstrap学校网站模板下载Spring Boot | 如何使用 Apache Kafka 消费 JSON 消息 Apache Kafka 是一个流处理系统,可让您在进程、应用程序和服务器之间发送消息。在本文中,我们将了解如何使用 Apache Kafka 在 Spring Boot 应用程序的控制台上发布 JSON 消息。 为了了解如何创建 …

Spring Boot | 如何使用 Apache Kafka 消费 JSON 消息

Apache Kafka 是一个流处理系统,可让您在进程、应用程序和服务器之间发送消息。在本文中,我们将了解如何使用 Apache Kafka 在 Spring Boot 应用程序的控制台上发布 JSON 消息。

为了了解如何创建 Spring Boot 项目,请参阅本文。

工作步骤 

步骤 1:

转到Spring 初始化程序并创建具有以下依赖项的启动项目: 
Spring for Apache Kafka

步骤 2:

在 IDE 中打开项目并同步依赖项。在本文中,我们将创建一个学生模型,我们将在其中发布学生详细信息。因此,创建一个模型类Student。添加数据成员并创建构造函数并重写toString方法以查看 JSON 格式的消息。以下是学生类的实现:

  • 学生模型

// Java program to implement a

// student class

 

// Creating a student class

public class Student {

 

    // Data members of the class

    int id;

    String firstName;

    String lastName;

 

    // Constructor of the student

    // Class

    public Student()

    {

    }

 

    // Parameterized constructor of

    // the student class

    public Student(int id, String firstName,

                   String lastName)

    {

        this.id = id;

        this.firstName = firstName;

        this.lastName = lastName;

    }

 

    @Override

    public String toString()

    {

        return "Student{"

            + "id = " + id

            + ", firstName = '" + firstName + "'"

            + ", lastName = '" + lastName + "'"

            + "}";

    }

}

步骤 3:

创建一个新的类Config并添加注释@Configuration@EnableKafka。现在使用 Student 类对象创建 Bean ConsumerFactoryConcurrentKafkaListenerContainerFactory 。

  • 配置类

@EnableKafka

@Configuration

public class Config {

 

    // Function to establish a connection

    // between Spring application

    // and Kafka server

    @Bean

    public ConsumerFactory<String, Student>

    studentConsumer()

    {

 

        // HashMap to store the configurations

        Map<String, Object> map

            = new HashMap<>();

 

        // put the host IP in the map

        map.put(ConsumerConfig

                    .BOOTSTRAP_SERVERS_CONFIG,

                "127.0.0.1:9092");

 

        // put the group ID of consumer in the map

        map.put(ConsumerConfig

                    .GROUP_ID_CONFIG,

                "id");

        map.put(ConsumerConfig

                    .KEY_DESERIALIZER_CLASS_CONFIG,

                StringDeserializer.class);

        map.put(ConsumerConfig

                    .VALUE_DESERIALIZER_CLASS_CONFIG,

                JsonDeserializer.class);

 

        // return message in JSON formate

        return new DefaultKafkaConsumerFactory<>(

            map, new StringDeserializer(),

            new JsonDeserializer<>(Student.class));

    }

 

    @Bean

    public ConcurrentKafkaListenerContainerFactory<String,

                                                   Student>

    studentListner()

    {

        ConcurrentKafkaListenerContainerFactory<String,

                                                Student>

            factory

            = new ConcurrentKafkaListenerContainerFactory<>();

        factory.setConsumerFactory(studentConsumer());

        return factory;

    }

}

步骤 4:

创建一个带有@Service注释的KafkaService类。此类将包含用于在控制台上发布消息的侦听器方法。 

  • KafkaService 类

@Service

public class KafkaService {

 

    // Annotation required to listen

    // the message from Kafka server

    @KafkaListener(topics = "JsonTopic",

                   groupId = "id", containerFactory

                                   = "studentListner")

    public void

    publish(Student student)

    {

        System.out.println("New Entry: "

                           + student);

    }

}

步骤 5:

启动 zookeeper 和 Kafka 服务器。现在我们需要创建一个名为JsonTopic的新主题。为此,打开一个新的命令提示符窗口并将目录更改为 Kafka 目录。

步骤6:

现在使用下面给出的命令创建一个新主题: 

bin/Kafka-topics.sh –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name // 用于 mac 和 linux
.\bin\windows\Kafka-topics.bat –create –zookeeper localhost:2181 –replication-factor 1 –partitions 1 –topic topic_name // 用于 windows 

步骤 7:

现在运行 Kafka 生产者控制台,使用以下命令: 

bin/Kafka-console-producer.sh –broker-list localhost:9092 –topic Kafka_Example // 适用于 mac 和 linux
.\bin\windows\Kafka-console-producer.bat –broker-list localhost:9092 –topic Kafka_Example // 适用于 windows 

步骤 8:

运行应用程序并在 Kafka 生产器上输入消息并按回车键。

http://www.dinnco.com/news/61175.html

相关文章:

  • 佛山专业建站公司哪家好国外网站建设
  • 网站论坛怎么做拼多多seo搜索优化
  • php做简易网站北京优化网站方法
  • 如何做百度收录的网站帮别人发广告赚钱平台
  • 哪家公司做网站不错百度seo价格查询
  • 学生html个人网页代码好的seo平台
  • wordpress 繁體网站自然优化
  • 西宁好的网站建设公司seo权重是什么意思
  • 做淘宝客网站必须备案吗人工智能培训机构
  • 沈阳世纪兴网站制作公司产品软文怎么写
  • 营销网红seo优化自动点击软件
  • 少儿类网站怎么做长春今日头条新闻
  • wordpress mac客户端宁波seo外包服务
  • 手机网站开发技术搜索引擎算法
  • 几分钟做网站营销客户管理系统
  • 广州正佳广场封闭山东seo推广公司
  • 微信上的网站怎么做的吗百度网页版入口链接
  • 做外贸是什么网站企业官网怎么做
  • 国外访问国内网站速度成都网站推广哪家专业
  • 网站三大标签优化落实防控措施
  • 网站m3u8链接视频怎么做的合肥做网站哪家好
  • 做企业网站的头部什么配色长沙seo招聘
  • 网站开发学习课程怎样推广自己的app
  • wordpress修改后台登录地址最新seo视频教程
  • 苏州网站建设推荐q479185700霸屏bittorrentkitty磁力猫
  • 浏览器怎么打开网站服务器下载百度网站名称及网址
  • 加盟代理好项目哪家好seo网站优化师
  • 平江外贸网站推广找哪家互联网媒体推广
  • 北京网站建设明细潮州seo建站
  • 怎样把自己做的网站发到网上小程序开发多少钱