本文主要是介绍spring cloud stream操作kafka和redis,以及kafka与redis队列性能比较(未完),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
上次用redis队列做了性能测试,不过redis毕竟不是专门用来做队列的,这次用kafka试试。
这次我用spring cloud stream来操作kafka。
Spring Cloud Stream是构建消息驱动的微服务应用程序的框架。Spring Cloud Stream基于Spring Boot构建独立的生产级Spring应用程序,并使用Spring Integration为消息代理提供连接。它提供了来自多个供应商的中间件的自定义配置,介绍了持久发布 - 订阅语义,消费者组和分区的概念。
好了,开始,首先修改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 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.xzg</groupId><artifactId>kafka</artifactId><version>1.0-SNAPSHOT</version><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.1.RELEASE</version></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-stream-dependencies</artifactId><version>Fishtown.BUILD-SNAPSHOT</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-stream</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-stream-kafka</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-jpa</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId></dependency></dependencies><repositories><repository><id>spring-snapshots</id><name>Spring Snapshots</name><url>https://repo.spring.io/libs-snapshot</url><snapshots><enabled>true</enabled></snapshots></repository></repositories>
</project>
等待一会儿,待jar包下载完毕
编写启动类Application
package com.xzg.kafka;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;@SpringBootApplication
public class Application {private static final Logger LOGGER = LoggerFactory.getLogger(Application.class);public static void main(String[] args){ApplicationContext ctx = SpringApplication.run(Application.class, args);LOGGER.info("app started");}
}
创建接收消息类
package com.xzg.kafka.message;
import com.xzg.kafka.repository.StockRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.cloud.stream.messaging.Sink;
import org.springframework.stereotype.Component;@Component
@EnableBinding(Sink.class)
public class Receiver {private static final Logger LOGGER = LoggerFactory.getLogger(Receiver.class);@AutowiredStockRepository stockRepository;@StreamListener(Sink.INPUT)public void receiveMessage(String message) {stockRepository.buy(Long.valueOf(message));}
}
这篇关于spring cloud stream操作kafka和redis,以及kafka与redis队列性能比较(未完)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!