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.     }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值