生产者:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:int-kafka="http://www.springframework.org/schema/integration/kafka"
xmlns:int="http://www.springframework.org/schema/integration"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration-4.1.xsd
http://www.springframework.org/schema/integration/kafka
http://www.springframework.org/schema/integration/kafka/spring-integration-kafka.xsd
">
<!--kafka config-->
<int:channel id="inputToKafka"/>
<int-kafka:outbound-channel-adapter kafka-producer-context-ref="kafkaProducerContext"
auto-startup="true"
channel="inputToKafka"
order="1">
</int-kafka:outbound-channel-adapter>
<bean id="producerProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="topic.metadata.refresh.interval.ms">3600000</prop>
<prop key="message.send.max.retries">5</prop>
<prop key="send.buffer.bytes">5242880</prop>
</props>
</property>
</bean>
<bean id="stringSerializer" class="org.apache.kafka.common.serialization.StringSerializer"/>
<int-kafka:producer-context id="kafkaProducerContext" producer-properties="producerProperties">
<int-kafka:producer-configurations>
<int-kafka:producer-configuration broker-list="${kafka.brokerList}"
key-class-type="java.lang.String"
key-serializer="stringSerializer"
value-class-type="java.lang.String"
value-serializer="stringSerializer"
topic="${kafka.topic.asyncNotifyProvoss}"/>
<int-kafka:producer-configuration broker-list="${kafka.brokerList}"
key-class-type="java.lang.String"
key-serializer="stringSerializer"
value-class-type="java.lang.String"
value-serializer="stringSerializer"
topic="${kafka.topic.asyncCutPayment}"/>
</int-kafka:producer-configurations>
</int-kafka:producer-context>
</beans>
<?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">
<int:channel id="inputFromKafka"/>
<int-kafka:zookeeper-connect id="zookeeperConnect" zk-connect="${kafka.zookeeper.url}"
zk-connection-timeout="6000"
zk-session-timeout="6000"
zk-sync-time="2000"/>
<int-kafka:inbound-channel-adapter
id="kafkaInboundChannelAdapter"
kafka-consumer-context-ref="consumerContext"
auto-startup="true"
channel="inputFromKafka">
<int:poller fixed-delay="1" time-unit="MILLISECONDS"/>
</int-kafka:inbound-channel-adapter>
<bean id="kafkaDecoder" class="org.springframework.integration.kafka.serializer.common.StringDecoder">
</bean>
<bean id="consumerProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties">
<props>
<prop key="auto.offset.reset">smallest</prop>
<prop key="socket.receive.buffer.bytes">10485760</prop> <!-- 10M -->
<prop key="fetch.message.max.bytes">5242880</prop>
<prop key="auto.commit.interval.ms">1000</prop>
</props>
</property>
</bean>
<bean id="kafKaManager" class="com.bestpay.posprouter.manager.util.KafKaManager"></bean>
<int:outbound-channel-adapter channel="inputFromKafka"
ref="kafKaManager" method="processMessage"/>
<int-kafka:consumer-context id="consumerContext"
consumer-timeout="1000"
zookeeper-connect="zookeeperConnect" consumer-properties="consumerProperties">
<int-kafka:consumer-configurations>
<int-kafka:consumer-configuration group-id="default-consumer"
value-decoder="kafkaDecoder"
key-decoder="kafkaDecoder"
max-messages="5000">
<int-kafka:topic id="${kafka.topic.asyncNotifyProvoss}" streams="4"/>
</int-kafka:consumer-configuration>
<int-kafka:consumer-configuration group-id="default-consumer" executor=""
value-decoder="kafkaDecoder"
key-decoder="kafkaDecoder"
max-messages="5000">
<int-kafka:topic id="${kafka.topic.asyncCutPayment}" streams="4"/>
</int-kafka:consumer-configuration>
</int-kafka:consumer-configurations>
</int-kafka:consumer-context>
</beans>
/**
*
* Copyright (c) 2011-2016 All Rights Reserved.
*/
package com.bestpay.posprouter.manager.util;/**
* Created by lxn on 2016/7/30.
*/
import com.bestpay.posprouter.manager.rebateManager.ProvossSyncManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.integration.kafka.support.KafkaHeaders;
import org.springframework.integration.support.MessageBuilder;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.stereotype.Component;
import java.util.Map;
/**
* @author lxn
* @version Id: KafKaManager.java, v 0.1 2016/7/30 12:18 lxn Exp $$
*/
@Slf4j
@Component
public class KafKaManager {
@Autowired
@Qualifier("inputToKafka")
MessageChannel channel;
@Value("${kafka.topic.asyncCutPayment}")
private String asyncCutPaymentTopic;
@Value("${kafka.topic.asyncNotifyProvoss}")
private String asyncNotifyProvossTopic;
@Autowired
private ProvossSyncManager provossSyncManager;
public boolean send(String topic,String obj) {
log.info("发送消息,topic:{},value:{}",topic,obj);
Message msg = MessageBuilder.withPayload(obj)
// .setHeader(KafkaHeaders.MESSAGE_KEY, key)
.setHeader(KafkaHeaders.TOPIC, topic).build();
return channel.send(msg);
}
public void processMessage(Map<String, Map<Integer, String>> msgs) {
log.info("收到消息,{}",msgs);
for (Map.Entry < String,Map<Integer, String>>entry:
msgs.entrySet()){
log.debug("Suchit Topic:" + entry.getKey());
if(asyncCutPaymentTopic.equals(entry.getKey())){
for (String msg : entry.getValue().values()) {
log.info("Suchit Consumed Message: " + msg);
//TODO 异步扣款
}
}else if(asyncNotifyProvossTopic.equals(entry.getKey())){
for (String msg : entry.getValue().values()) {
log.info("Suchit Consumed Message: " + msg);
provossSyncManager.onMessage(msg);
}
}
}
}
}