• Welcome to the world's largest Chinese hacker forum

    Welcome to the world's largest Chinese hacker forum, our forum registration is open! You can now register for technical communication with us, this is a free and open to the world of the BBS, we founded the purpose for the study of network security, please don't release business of black/grey, or on the BBS posts, to seek help hacker if violations, we will permanently frozen your IP and account, thank you for your cooperation. Hacker attack and defense cracking or network Security

    business please click here: Creation Security  From CNHACKTEAM

Recommended Posts

跳羚是一个在运行模式开发中非常常用的组件。可以用于卡夫卡的生产者,也可以用于跳羚的消费者。

image

1)在 IDEA 中安装 lombok 插件

在插件下搜索龙目岛然后在线安装即可,安装后注意重启

image

2)SpringBoot 环境准备

(1)创建一个弹簧初始化r

注意:有时候跳羚官方脚手架不稳定,我们切换国内地址https://start.aliyun.com

(2)项目名称跳羚

image

image

(4)检查自动生成的配置文件

?可扩展标记语言版本='1.0 '编码='UTF八号'?

项目xmlns=' http://maven。阿帕奇。org/POM/4。0 .0 ' xmlns : xsi=' http://www。w3。' org/2001/XML架构-实例'

xsi :架构位置=' http://maven。阿帕奇。org/POM/4。0 .0 https://maven.apache.org/xsd/maven-4.0.0.xsd'

型号版本4 .0 .0/型号版本

父母

groupIdorg.springframework.boot/groupId

artifact id spring-boot-starter-parent/artifact id

版本2 .6 .7/版本

relativePath/!-从存储库中查找父级-

/父母

groupIdcom.atguigu/groupId

artifactId弹簧靴/artifactId

版本0 .0 .1-快照/版本

namespringboot/name

描述跳靴/描述

性能

Java。1.8版/java.version

/属性

属国

属国

groupIdorg.springframework.boot/groupId

artifact id spring-boot-starter-web/artifact id

/依赖关系

属国

groupIdorg.springframework.kafka/groupId

artifactIdspri

ng-kafka</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-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <configuration> <excludes> <exclude> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </exclude> </excludes> </configuration> </plugin> </plugins> </build> </project>

3.1 SpringBoot 生产者

(1)修改 SpringBoot 核心配置文件 application.yml,添加生产者相关信息

spring:
  application:
    name: atguigu-springboot-kafka
  kafka:
    bootstrap-servers: hadoop102:9092,hadoop103:9092,hadoop104:9092
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer

(2)创建 controller 从浏览器接收数据, 并写入指定的 topic

package com.atguigu.springboot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ProducerController {
    /**
     *  Kafka 模板用来向 kafka 发送数据
     */
    @Autowired
    private KafkaTemplate<String, String> kafka;
    @RequestMapping("/atguigu")
    public String data(String msg) {
        kafka.send("first", msg);
        return "ok";
    }
}

(3)给/atguigu 接口发送数据:http://localhost:8080/atguigu?msg=hello

image

3.2 SpringBoot 消费者

(1)修改 SpringBoot 核心配置文件 application.propeties

spring:
  application:
    name: atguigu-springboot-kafka
  kafka:
    bootstrap-servers: hadoop102:9092,hadoop103:9092,hadoop104:9092
    producer:
      key-serializer: org.apache.kafka.common.serialization.StringSerializer
      value-serializer: org.apache.kafka.common.serialization.StringSerializer
    consumer:
      key-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      value-deserializer: org.apache.kafka.common.serialization.StringDeserializer
      group-id: atguigu

(2)创建类消费 Kafka 中指定 topic 的数据

package com.atguigu.springboot.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.kafka.annotation.KafkaListener;
@Slf4j
@Configuration
public class KafkaConsumer {
    /**
     * 指定要监听的topic
     *
     * @param msg 收到的value
     */
    @KafkaListener(topics = "first")
    public void consumeTopic(String msg) {
        log.info("收到的信息: {}", msg);
    }
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now