最近忙着搞低代码开发,好久没新建spring项目了,结果今天心血来潮准备建个springboot项目
注意Type选Maven,java选8,其他默认
点下一步后完成就新建了一个spring boot项目,配置下Maven环境,主要是settings.xml文件,里面要包含阿里云仓库,不然可能依赖下载不下来
在maven配置没问题的前提下,IDEA无法识别这是一个Spring Boot项目,倒腾半天,终于发现问题原因所在=======>是Maven版本太高的原因
把.mvn/wrapper目录下的maven-wrapper.properties文件第一行的版本号降低,比如说降为3.5.4,然后重新点下Maven的同步按钮
接下来运行项目报错:java: 无效的源发行版: 14
修改pom.xml中java.version值为8,原来是17
<properties>
<java.version>17java.version>
properties>
继续运行,继续报错
降低spring-boot-starter-parent版本,原来是3.1.3,改为2.7.2
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-webartifactId>
dependency>
终于,一个空的spring boot项目成功跑起来了,喜极而泣
官网=>https://kafka.apache.org/downloads,下载最新版的kafka,目前是3.5.1
解压到D盘Config目录下即完成安装,目录为D:\Config\kafka_2.13-3.5.1
修改配置文件
(1) server.properties
broker.id=1
log.dirs=/Config/kafka_2.13-3.5.1/logs-kafka
(2) zookeeper.properties
dataDir=/Config/kafka_2.13-3.5.1/logs-zookeeper
先启动zookeeper
bin\windows\zookeeper-server-start.bat config\zookeeper.properties
再启动kafka
bin\windows\kafka-server-start.bat config\server.properties
停止的时候,先停止kafka,再停止zookeeper,直接ctrl+c停止
1,查看topic列表
bin\windows\kafka-topics.bat --list --bootstrap-server localhost:9092
2,查看topic具体信息
bin\windows\kafka-topics.bat --describe --bootstrap-server localhost:9092 --topic test
3,创建topic
bin\windows\kafka-topics.bat --create --bootstrap-server localhost:9092 --replication-factor 1 --partitions 1 --topic test
<dependency>
<groupId>org.springframework.kafkagroupId>
<artifactId>spring-kafkaartifactId>
dependency>
application.yaml
spring:
profiles:
active: dev
application-dev.yaml
server:
port: 8082
servlet:
context-path: /test-kafka
spring:
cache:
type: ehcache
config: classpath:ehcache.xml
jpa:
database-platform: com.enigmabridge.hibernate.dialect.SQLiteDialect
kafka:
bootstrap-servers: 127.0.0.1:9092
consumer:
group-id: kafka-demo-kafka-group
key-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: 10
@Slf4j
@Component
public class KafkaConsumer {
@KafkaListener(topics = {"test_topic"})
public void handlerMsg(String content) {
log.info("接收到消息:消息值:{} ",content);
}
}
@Slf4j
@Component
public class KafkaProducer {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
public String sendMessage(String content) {
String topic = "test_topic";
kafkaTemplate.send(topic, content);
return "发送成功";
}
}
@Slf4j
@RestController
public class KafkaController {
@Autowired
private KafkaProducer kafkaProducer;
@PostMapping("/sendMessage")
public String sendMessage(@RequestParam String content) {
kafkaProducer.sendMessage(content);
return "ok";
}
}
https://www.yii666.com/blog/317108.html
https://zhuanlan.zhihu.com/p/591219820
https://blog.csdn.net/qq_41428418/article/details/132226864