通过javaApI来模拟生产者和消费者
生产者
添加依赖
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.3.RELEASE</version>
<relativePath/>
</parent>
<dependencies>
<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>
</dependency>
</dependencies>
<repositories>
<!--指定仓库下载地址-->
<repository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
<repository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</repository>
</repositories>
<pluginRepositories>
<!--指定插件下载地址-->
<pluginRepository>
<id>spring-snapshots</id>
<name>Spring Snapshots</name>
<url>https://repo.spring.io/snapshot</url>
<snapshots>
<enabled>true</enabled>
</snapshots>
</pluginRepository>
<pluginRepository>
<id>spring-milestones</id>
<name>Spring Milestones</name>
<url>https://repo.spring.io/milestone</url>
<snapshots>
<enabled>false</enabled>
</snapshots>
</pluginRepository>
</pluginRepositories>
配置文件
server.port=8081
spring.kafka.bootstrap-servers=Centos161:9092,Centos162:9092,Centos163:9092
在主机hosts文件中添加ip和主机名的映射
service层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Service;
@Service
public class ItemService {
@Autowired
private KafkaTemplate<String,String> kafkaTemplate;
public void delete(Long itemId){
System.out.println("调用DAO删除数据库中的商品id为"+itemId);
kafkaTemplate.send("test","id:"+itemId+";title=item.delete");
}
}
controller层
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ItemController {
@Autowired
private ItemService itemService;
@RequestMapping("/delete/{itemId}")
public String deleteItem(@PathVariable Long itemId){
this.itemService.delete(itemId);
return "success";
}
}
消费者
依赖跟生产者一样
配置文件application.properties
server.port=8082
spring.kafka.bootstrap-servers=Centos161:9092,Centos162:9092,Centos163:9092
spring.kafka.consumer.group-id=test-consumer-group
监听类
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;
import java.util.Optional;
@Component
public class Listener {
@Autowired
private ItemService itemService;
@KafkaListener(topics = {"test"})
public void listen(ConsumerRecord<?,?> record){
Optional<?> value = Optional.ofNullable(record.value());
if(value.isPresent()){
String message = (String)value.get();
System.out.println(message);
if(message.contains("item.delete")){
String id = message.split(":")[1].split(";")[0];
itemService.deleteSolrByItem(Long.parseLong(id));
}
}
}
}
service层
import org.springframework.stereotype.Service;
@Service
public class ItemService {
public void deleteSolrByItem(Long itemId){
System.out.println("从solr库中删除id为"+itemId+"的商品");
}
}