spring-integration-kafka xml配置demo

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

1、build.gradle

compile group: 'org.springframework.integration', name: 'spring-integration-kafka', version: '2.1.0.RELEASE'


2、生产者配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
       xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka
        http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
		http://www.springframework.org/schema/integration
		http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 生产者配置 -->
    <bean id="template" class="org.springframework.kafka.core.KafkaTemplate">
        <constructor-arg index="0">
            <bean class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
                <constructor-arg>
                    <map>
                        <entry key="bootstrap.servers" value="192.168.90.200:9092"/>
                        <entry key="acks" value="all"/>
                        <entry key="retries" value="3"/>
                        <entry key="batch.size" value="16384"/>
                        <entry key="linger.ms" value="1"/>
                        <entry key="buffer.memory" value="33554432"/>
                        <entry key="key.serializer" value="33554432"/>
                        <entry key="key.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
                        <entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer"></entry>
                    </map>
                </constructor-arg>
            </bean>
        </constructor-arg>
    </bean>

    <!-- 生产1号 -->
    <int:channel id="inputToKafka">
        <int:queue/>
    </int:channel>
    <int-kafka:outbound-channel-adapter id="kafkaOutboundChannelAdapter"
                                        kafka-template="template"
                                        auto-startup="true"
                                        channel="inputToKafka"
                                        topic="Xuesong_Topic">
        <int:poller fixed-delay="1000" time-unit="MILLISECONDS"/>
    </int-kafka:outbound-channel-adapter>

</beans>


3、消费者配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
       xsi:schemaLocation="http://www.springframework.org/schema/integration/kafka
        http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
		http://www.springframework.org/schema/integration
		http://www.springframework.org/schema/integration/spring-integration.xsd
		http://www.springframework.org/schema/beans
		http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- 消费配置 -->
    <bean id="consumerProperties" class="java.util.HashMap">
        <constructor-arg>
            <map>
                <entry key="bootstrap.servers" value="192.168.90.200:9092"/>
                <entry key="group.id" value="0"/>
                <entry key="enable.auto.commit" value="true"/>
                <entry key="auto.commit.interval.ms" value="1000"/>
                <entry key="session.timeout.ms" value="15000"/>
                <entry key="key.deserializer" value="org.apache.kafka.common.serialization.IntegerDeserializer"/>
                <entry key="value.deserializer" value="org.apache.kafka.common.serialization.StringDeserializer"/>
            </map>
        </constructor-arg>
    </bean>

    <!-- 创建consumerFactory bean -->
    <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
        <constructor-arg>
            <ref bean="consumerProperties"/>
        </constructor-arg>
    </bean>


    <!-- 消费1号 -->
    <int:channel id="inputFromKafka">
        <int:queue/>
    </int:channel>

    <int-kafka:message-driven-channel-adapter auto-startup="true" channel="inputFromKafka" listener-container="container1" />

    <bean id="container1" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
        <constructor-arg index="0" ref="consumerFactory"/>
        <constructor-arg index="1" ref="containerProperties"/>
    </bean>

    <bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties">
        <constructor-arg value="Xuesong_Topic"/>
    </bean>

</beans>


3、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath:applicationContext-kafka.xml"})
public class Test {

    @Autowired
    @Qualifier("inputToKafka")
    MessageChannel messageChannel;

    @Autowired
    @Qualifier("inputFromKafka")
    PollableChannel pollableChannel;


    @Test
    public void sendMsg() throws Exception {

        for (int i = 0; i < 15; i++) {
            Message<String> message = new GenericMessage<String>("test-------------" + (i + 100));
            boolean flag = messageChannel.send(message);

            System.out.println(flag + "=============" + (i + 100));
        }

        Message<?> received = pollableChannel.receive(10000);
        while (received != null) {
            System.out.println("|||" + received);
            received = pollableChannel.receive(10000);
        }

    }





  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 7
    评论
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

聪明的我我我

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值