探索消息中间件Kafka与Spring进行整合

SpringBoot整合Kafka

第一步:添加依赖

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.6.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <dependencies>
        <!-- SpringBoot -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--单元测试-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--引入SpringBoot与Kafka进行整合-->
        <dependency>
            <groupId>org.springframework.kafka</groupId>
            <artifactId>spring-kafka</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>

    </dependencies>

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

第二步:代码实现

@RestController
@SpringBootApplication
@RequestMapping
public class KafkaApplication {

    private static final Logger logger = LoggerFactory.getLogger(KafkaApplication.class);

    public static void main(String[] args) {
        SpringApplication.run(KafkaApplication.class, args);
    }

    @RequestMapping("/index")
    public String index() {
        return "Hello,Kafka!";
    }

    @Autowired
    private KafkaTemplate template;
    private static final String topic = "kafkaDemo";

    /**
     * 普通实现方式
     * @param input
     * @return
     */
    @GetMapping("/send/{input}")
    public String sendToKafka(@PathVariable String input) {
        this.template.send(topic, input);
        return "send success";
    }

    /**
     * 发送消息 带事务  第一种方式实现
     */
    @GetMapping("/send/{input}")
    public String sendTranceKafka(@PathVariable String input) throws ExecutionException, InterruptedException{
        /*ListenableFuture<SendResult<Object, Object>> send = this.template.send(topic, input);*/
        template.executeInTransaction(t->{
            t.send(topic, input);
            if ("error".equals(input)) {
                throw new RuntimeException("input is error");
            }
            t.send(topic, input + "anthor");
            return true;
        });
        return "success";
    }

    @KafkaListener(id = "", topics = topic, groupId = "group.demo")
    public void listener(String input) {
        logger.info("input value:{}", input);
    }


    /**
     * 第二种方式实现  发送消息 带事务
     */
    @GetMapping("/sendMessage/{input}")
    @Transactional(rollbackFor = RuntimeException.class)
    public String sendTranceMessage(@PathVariable String input) throws ExecutionException, InterruptedException{
        template.send(topic, input);
        if ("error".equals(input)) {
            throw new RuntimeException("input is error");
        }
        template.send(topic, input + " anthor");
        return "send success";
    }
}
spring.kafka.producer.bootstrap-servers=192.168.247.188:9092
spring.kafka.consumer.bootstrap-servers=192.168.247.188:9092

# 事务支持
spring.kafka.producer.transaction-id-prefix=kafka_tx.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值