创建java工程操作kafka
2022/2/15 17:12:39
本文主要是介绍创建java工程操作kafka,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
导入pom
点击查看代码
<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.fch</groupId> <artifactId>mykafka</artifactId> <version>0.0.1-SNAPSHOT</version> <name>mykafka</name> <description>Demo project for Spring Boot</description> <properties> <java.version>1.8</java.version> </properties> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.4.5</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> <!--Spring整合Kafka--> <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.7.1</version> </dependency> <!--Swagger-UI API文档生产工具--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.7.0</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.7.0</version> </dependency> <!--lombok依赖--> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> <optional>true</optional> </dependency> <!--Hutool Java工具包--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>4.5.7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
配置yml
点击查看代码
server: port: 8088 spring: kafka: bootstrap-servers: 'xxxx:9092' consumer: group-id: "bootGroup"
KafkaController
点击查看代码
package com.fch.mykafka.controller; import com.fch.mykafka.config.KafkaProducer; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; 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.ResponseBody; /** * Kafka功能测试 * Created by macro on 2021/5/19. */ @Api(tags = "KafkaController", description = "Kafka功能测试") @Controller @RequestMapping("/kafka") public class KafkaController { @Autowired private KafkaProducer kafkaProducer; @ApiOperation("发送消息") @RequestMapping(value = "/sendMessage", method = RequestMethod.GET) @ResponseBody public String sendMessage(@RequestParam String message) { kafkaProducer.send(message); return "ok"; } }
消费者类KafkaConsumer
点击查看代码
package com.fch.mykafka.config; import lombok.extern.slf4j.Slf4j; import org.springframework.kafka.annotation.KafkaListener; import org.springframework.stereotype.Component; /** * Kafka消息消费者 * Created by macro on 2021/5/19. */ @Slf4j @Component public class KafkaConsumer { @KafkaListener(topics = "bootTopic") public void processMessage(String content) { log.info("consumer processMessage : {}",content); } }
生产者类KafkaProducer
点击查看代码
package com.fch.mykafka.config; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.kafka.core.KafkaTemplate; import org.springframework.stereotype.Component; /** * Kafka消息生产者 * Created by macro on 2021/5/19. */ @Component @Api("KafkaProducer,kafka生产者") public class KafkaProducer { @Autowired private KafkaTemplate kafkaTemplate; @ApiOperation(value = "/messger",notes = "发送方法",produces = "application/json") public void send(String message){ kafkaTemplate.send("bootTopic",message); } }
swagger配置Swagger2Config
点击查看代码
package com.fch.mykafka.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; /** * Swagger2API文档的配置 */ @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包下controller生成API文档 .apis(RequestHandlerSelectors.basePackage("com.fch.mykafka.controller")) //为有@Api注解的Controller生成API文档 // .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //为有@ApiOperation注解的方法生成API文档 // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SwaggerUI演示111") .termsOfServiceUrl("xxxxx:8088") .description("mall-tiny") .contact("macro") .version("1.0") .build(); } }
这篇关于创建java工程操作kafka的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-11-23Springboot应用的多环境打包入门
- 2024-11-23Springboot应用的生产发布入门教程
- 2024-11-23Python编程入门指南
- 2024-11-23Java创业入门:从零开始的编程之旅
- 2024-11-23Java创业入门:新手必读的Java编程与创业指南
- 2024-11-23Java对接阿里云智能语音服务入门详解
- 2024-11-23Java对接阿里云智能语音服务入门教程
- 2024-11-23JAVA对接阿里云智能语音服务入门教程
- 2024-11-23Java副业入门:初学者的简单教程
- 2024-11-23JAVA副业入门:初学者的实战指南