呼家楼做网站的公司,大鹏手机网站建设,建设网站英文翻译,息壤服务器网站打不开文章目录 一#xff0c;新建Spring Boot1#xff0c;Maven配置2#xff0c;无法识别为SpringBoot项目3#xff0c;无效的源发行版4#xff0c;无法访问SpringApplication5#xff0c;运行直接Finish6#xff0c;服务运行成功 二#xff0c;安装启动Kafka1#xff0c;下… 文章目录 一新建Spring Boot1Maven配置2无法识别为SpringBoot项目3无效的源发行版4无法访问SpringApplication5运行直接Finish6服务运行成功 二安装启动Kafka1下载2配置3启动4其他命令 三生产消费消息1加入依赖2yam配置文件3报错enabled mechanisms are []4消费者5生产者6接口7测试结果 四参考博文 一新建Spring Boot
最近忙着搞低代码开发好久没新建spring项目了结果今天心血来潮准备建个springboot项目 注意Type选Mavenjava选8其他默认
1Maven配置
点下一步后完成就新建了一个spring boot项目配置下Maven环境主要是settings.xml文件里面要包含阿里云仓库不然可能依赖下载不下来
2无法识别为SpringBoot项目
在maven配置没问题的前提下IDEA无法识别这是一个Spring Boot项目倒腾半天终于发现问题原因所在是Maven版本太高的原因 把.mvn/wrapper目录下的maven-wrapper.properties文件第一行的版本号降低比如说降为3.5.4然后重新点下Maven的同步按钮
3无效的源发行版
接下来运行项目报错java: 无效的源发行版: 14 修改pom.xml中java.version值为8原来是17 propertiesjava.version17/java.version/properties4无法访问SpringApplication
继续运行继续报错 降低spring-boot-starter-parent版本原来是3.1.3改为2.7.2
5运行直接Finish
继续运行没报错服务直接Finished 需要添加web依赖 dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency6服务运行成功
终于一个空的spring boot项目成功跑起来了喜极而泣
二安装启动Kafka
1下载
官网https://kafka.apache.org/downloads下载最新版的kafka目前是3.5.1
2配置
解压到D盘Config目录下即完成安装目录为D:\Config\kafka_2.13-3.5.1 修改配置文件 (1) server.properties
broker.id1
log.dirs/Config/kafka_2.13-3.5.1/logs-kafka(2) zookeeper.properties
dataDir/Config/kafka_2.13-3.5.1/logs-zookeeper3启动
先启动zookeeper
bin\windows\zookeeper-server-start.bat config\zookeeper.properties 再启动kafka
bin\windows\kafka-server-start.bat config\server.properties停止的时候先停止kafka再停止zookeeper直接ctrlc停止
4其他命令
1查看topic列表
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:90922查看topic具体信息
bin\windows\kafka-topics.bat --describe --bootstrap-server localhost:9092 --topic test3创建topic
bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
三生产消费消息
1加入依赖 dependencygroupIdorg.springframework.kafka/groupIdartifactIdspring-kafka/artifactId/dependency2yam配置文件
application.yaml
spring:profiles:active: devapplication-dev.yaml
server:port: 8082servlet:context-path: /test-kafkaspring:cache:type: ehcacheconfig: classpath:ehcache.xmljpa:database-platform: com.enigmabridge.hibernate.dialect.SQLiteDialectkafka:bootstrap-servers: 127.0.0.1:9092consumer:group-id: kafka-demo-kafka-groupkey-deserializer: org.apache.kafka.common.serialization.StringDeserializer value-deserializer: org.apache.kafka.common.serialization.StringDeserializer producer:key-serializer: org.apache.kafka.common.serialization.StringSerializer value-serializer: org.apache.kafka.common.serialization.StringSerializer retries: 103报错enabled mechanisms are [] Connection to node -1 (activate.navicat.com/127.0.0.1:9092) failed authentication due to: Unexpected handshake request with client mechanism PLAIN, enabled mechanisms are [] 这个错误我本地测试下来是因为没把账号密码配置这块注释掉
4消费者
Slf4j
Component
public class KafkaConsumer {KafkaListener(topics {test_topic})public void handlerMsg(String content) {log.info(接收到消息消息值{} ,content);}
}5生产者
Slf4j
Component
public class KafkaProducer {Autowiredprivate KafkaTemplateString, String kafkaTemplate;public String sendMessage(String content) {String topic test_topic;kafkaTemplate.send(topic, content);return 发送成功;}
}6接口
Slf4j
RestController
public class KafkaController {Autowiredprivate KafkaProducer kafkaProducer;PostMapping(/sendMessage)public String sendMessage(RequestParam String content) {kafkaProducer.sendMessage(content);return ok;}
}7测试结果 接收到消息
四参考博文
解决IDEA无法识别SpringBoot项目SpringBoot从入门到精通十二SpringBoot集成KafkaKafka的下载安装以及使用