springboot应用kafka实现分布式队列服务

pom.xml文件内容

	 <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
        <relativePath/> 
    </parent>



		<dependency>
		    <groupId>org.springframework.kafka</groupId>
		    <artifactId>spring-kafka</artifactId>
		    <version>2.2.0.RELEASE</version>
		</dependency>

注:这里要注意springboot版本与spring-kafka版本对应关系,org.springframework.boot 2.1.0对应org.springframework.kafka 2.2.0

controller类

 @PostMapping("/startKafkaQueue")
    public Result startKafkaQueue(long seckillId,long userId){
        try {
            redisUtil.cacheValue(seckillId+"", null);//秒杀结束
            if(redisUtil.getValue(seckillId+"")==null){
               
                kafkaSender.sendChannelMess("seckill",seckillId+";"+userId);
                return Result.ok();
            }else{
                return Result.error();
            }

        }catch (Exception e){
            System.out.println(e);
        }
        return Result.error();
    }

KafkaSend类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.stereotype.Component;
/**
 * 生产者

 */
@Component
public class KafkaSender {
    @Autowired
    private KafkaTemplate<String,String> kafkaTemplate;

    /**
     * 发送消息到kafka
     */
    public void sendChannelMess(String channel, String message){
        kafkaTemplate.send(channel,message);
    }
}

KafkaConsumer类


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import com.itstyle.seckill.common.entity.Result;
import com.itstyle.seckill.common.redis.RedisUtil;
import com.itstyle.seckill.common.webSocket.WebSocketServer;
import com.itstyle.seckill.service.ISeckillService;
/**
 * 消费者 spring-kafka 2.0 + 依赖JDK8
 
 */
@Component
public class KafkaConsumer {
	@Autowired
	private ISeckillService seckillService;
	
	private static RedisUtil redisUtil = new RedisUtil();
    /**
     * 监听seckill主题,有消息就读取
     * @param message
     */
    @KafkaListener(topics = {"seckill"})
    public void receiveMessage(String message){
    	//收到通道的消息之后执行秒杀操作
    	String[] array = message.split(";"); 
    	if(redisUtil.getValue(array[0])==null){//control层已经判断了,其实这里不需要再判断了,这个接口有限流 注意一下
    		Result result = seckillService.startSeckil(Long.parseLong(array[0]), Long.parseLong(array[1]));

    		if(result.equals(Result.ok())){
    			WebSocketServer.sendInfo(array[0].toString(), "秒杀成功");//推送给前台
    		}else{
    			WebSocketServer.sendInfo(array[0].toString(), "秒杀失败");//推送给前台
    			redisUtil.cacheValue(array[0], "ok");//秒杀结束
    		}
    	}else{
    		WebSocketServer.sendInfo(array[0].toString(), "秒杀失败");//推送给前台
    	}
    }
}

application.properties

#kafka相关配置 
spring.kafka.bootstrap-servers=192.168.1.180:9092
#设置一个默认组
spring.kafka.consumer.group-id=0
#key-value序列化反序列化
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.StringDeserializer
spring.kafka.producer.key-serializer=org.apache.kafka.common.serialization.StringSerializer
spring.kafka.producer.value-serializer=org.apache.kafka.common.serialization.StringSerializer
#每次批量发送消息的数量
spring.kafka.producer.batch-size=65536
spring.kafka.producer.buffer-memory=524288

你可能感兴趣的:(springboot,消息队列,kafka)