spring-integration-kafka xml配置demo

参照java代码配置,转换成xml

1、build.gradle

[html]  view plain  copy
  1. compile group: 'org.springframework.integration', name: 'spring-integration-kafka', version: '2.1.0.RELEASE'  


2、生产者配置

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:int="http://www.springframework.org/schema/integration"  
  5.        xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka  
  7.         http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd  
  8.         http://www.springframework.org/schema/integration  
  9.         http://www.springframework.org/schema/integration/spring-integration.xsd  
  10.         http://www.springframework.org/schema/beans  
  11.         http://www.springframework.org/schema/beans/spring-beans.xsd">  
  12.   
  13.     <!-- 生产者配置 -->  
  14.     <bean id="template" class="org.springframework.kafka.core.KafkaTemplate">  
  15.         <constructor-arg index="0">  
  16.             <bean class="org.springframework.kafka.core.DefaultKafkaProducerFactory">  
  17.                 <constructor-arg>  
  18.                     <map>  
  19.                         <entry key="bootstrap.servers" value="192.168.90.200:9092"/>  
  20.                         <entry key="acks" value="all"/>  
  21.                         <entry key="retries" value="3"/>  
  22.                         <entry key="batch.size" value="16384"/>  
  23.                         <entry key="linger.ms" value="1"/>  
  24.                         <entry key="buffer.memory" value="33554432"/>  
  25.                         <entry key="key.serializer" value="33554432"/>  
  26.                         <entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>  
  27.                         <entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>  
  28.                     </map>  
  29.                 </constructor-arg>  
  30.             </bean>  
  31.         </constructor-arg>  
  32.     </bean>  
  33.   
  34.     <!-- 生产1号 -->  
  35.     <int:channel id="inputToKafka">  
  36.         <int:queue/>  
  37.     </int:channel>  
  38.     <int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter"  
  39.                                         kafka-template="template"  
  40.                                         auto-startup="true"  
  41.                                         channel="inputToKafka"  
  42.                                         topic="Xuesong_Topic">  
  43.         <int:poller fixed-delay="1000" time-unit="MILLISECONDS"/>  
  44.     </int-kafka:outbound-channel-adapter>  
  45.   
  46. </beans>  


3、消费者配置

[html]  view plain  copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  4.        xmlns:int="http://www.springframework.org/schema/integration"  
  5.        xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"  
  6.        xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka  
  7.         http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd  
  8.         http://www.springframework.org/schema/integration  
  9.         http://www.springframework.org/schema/integration/spring-integration.xsd  
  10.         http://www.springframework.org/schema/beans  
  11.         http://www.springframework.org/schema/beans/spring-beans.xsd">  
  12.   
  13.     <!-- 消费配置 -->  
  14.     <bean id="consumerProperties" class="java.util.HashMap">  
  15.         <constructor-arg>  
  16.             <map>  
  17.                 <entry key="bootstrap.servers" value="192.168.90.200:9092"/>  
  18.                 <entry key="group.id" value="0"/>  
  19.                 <entry key="enable.auto.commit" value="true"/>  
  20.                 <entry key="auto.commit.interval.ms" value="1000"/>  
  21.                 <entry key="session.timeout.ms" value="15000"/>  
  22.                 <entry key="key.deserializer" value="org.apache.kafka.common.serialization.IntegerDeserializer"/>  
  23.                 <entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"/>  
  24.             </map>  
  25.         </constructor-arg>  
  26.     </bean>  
  27.   
  28.     <!-- 创建consumerFactory bean -->  
  29.     <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">  
  30.         <constructor-arg>  
  31.             <ref bean="consumerProperties"/>  
  32.         </constructor-arg>  
  33.     </bean>  
  34.   
  35.   
  36.     <!-- 消费1号 -->  
  37.     <int:channel id="inputFromKafka">  
  38.         <int:queue/>  
  39.     </int:channel>  
  40.   
  41.     <int-kafka:message-driven-channel-adapter auto-startup="true" channel="inputFromKafka" listener-container="container1" />  
  42.   
  43.     <bean id="container1" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">  
  44.         <constructor-arg index="0" ref="consumerFactory"/>  
  45.         <constructor-arg index="1" ref="containerProperties"/>  
  46.     </bean>  
  47.   
  48.     <bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties">  
  49.         <constructor-arg value="Xuesong_Topic"/>  
  50.     </bean>  
  51.   
  52. </beans>  


3、测试

[java]  view plain  copy
  1. @RunWith(SpringJUnit4ClassRunner.class)  
  2. @ContextConfiguration(locations = {"classpath:applicationContext-kafka.xml"})  
  3. public class Test {  
  4.   
  5.     @Autowired  
  6.     @Qualifier("inputToKafka")  
  7.     MessageChannel messageChannel;  
  8.   
  9.     @Autowired  
  10.     @Qualifier("inputFromKafka")  
  11.     PollableChannel pollableChannel;  
  12.   
  13.   
  14.     @Test  
  15.     public void sendMsg() throws Exception {  
  16.   
  17.         for (int i = 0; i < 15; i++) {  
  18.             Message<String> message = new GenericMessage<String>("test-------------" + (i + 100));  
  19.             boolean flag = messageChannel.send(message);  
  20.   
  21.             System.out.println(flag + "=============" + (i + 100));  
  22.         }  
  23.   
  24.         Message<?> received = pollableChannel.receive(10000);  
  25.         while (received != null) {  
  26.             System.out.println("|||" + received);  
  27.             received = pollableChannel.receive(10000);  
  28.         }  
  29.   
  30.     }  
动手写了一个Ibatis的Demo,反正啥东西,看着都是挺停简单的,Ibatis不就是一个ORM访问数据的东西嘛,不过在实际的动手的过程之中,还是需要到一些问题: 1)缺少这包,缺少那包的;还好用maven只要知道包的版本和2个ID,就不用自己到处找下载地址了。 2)使用的第三方框架太多,log4j输出太多,看起来费劲啊,另外一般异常日志也是一出就是一堆,不认真看,根本看不出问题在哪。在windows下用.net开发,早习惯了debugview去看(用好degbugview的过滤和高亮,可以让你第一眼就看到你想关注的信息);好几天一直在找一个类似的eclipse插件,不过还是无果而终。现在暂时自定义一个LogAppender去自己搞一个简单的过滤吧(这块有兴趣的可以去看org.ibatis.demo.core.log下代码)。 不知道为啥Spring这家伙为啥在java下有那么高的地位,感觉走到哪里都要用到它,不过无可否认java下Spring本身对于常见的框架都提供了很好的集成。很早以前.net和java程序员互相说自己好,对方不好的时候,看过过一句话:.net程序员里blog里都是大把的demo,java程序员blog经常是鸟文的摘抄或是翻译。感觉确实如此啊,java很少搜索到完整的demo,几乎都是断章取义的文字和代码。既然我是.net程序员现在再搞java,那么还延续.net程序员良好的习惯吧,上完整demo,也为一些需要的人,提供完整的参考。 Demo代码,没有啥太多的功能,就是对一张表的getAll(),然后输出结果的count值(.net下习惯了list.count,java是size,唉,老不习惯了,多写写java估计以后又习惯size,为啥人总是那么纠结呢?),数据库就不上了,大家要跑起来的话自己改改连库信息和实体代码以及xml配置
使用 KafkaListener 注解可以让 Spring Boot 应用轻松地消费 Kafka 消息。 步骤如下: 1. 引入 spring-kafka 依赖。 ```xml <dependency> <groupId>org.springframework.kafka</groupId> <artifactId>spring-kafka</artifactId> <version>2.2.2.RELEASE</version> </dependency> ``` 2. 在 Spring Boot 应用的配置文件中配置 Kafka 生产者和消费者的相关信息。 ```yaml spring.kafka.bootstrap-servers=localhost:9092 spring.kafka.consumer.group-id=my-group spring.kafka.consumer.auto-offset-reset=earliest 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 ``` 3. 创建一个 Kafka 消费者,定义一个方法用于处理消息。 ```java @Component public class KafkaConsumer { @KafkaListener(topics = "my-topic", groupId = "my-group") public void receive(String message) { System.out.println("Received message: " + message); } } ``` 4. 创建一个 Kafka 生产者,发送消息。 ```java @Component public class KafkaProducer { @Autowired private KafkaTemplate<String, String> kafkaTemplate; public void send(String message) { kafkaTemplate.send("my-topic", message); } } ``` 5. 在任何需要发送消息的地方注入 KafkaProducer,调用 send 方法发送消息即可。 ```java @Autowired private KafkaProducer kafkaProducer; public void sendMessage() { kafkaProducer.send("hello, kafka"); } ``` 以上就是使用 spring-kafka 中的 KafkaListener 注解的基本使用方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值