Honghu的消息服務(wù)平臺已經(jīng)拋棄了之前的ActiveMQ,改用高吞吐量比較大的Kafka分布式消息中間件方案: kafka消息平臺使用spring+kafka的集成方案,,詳情如下: 1. 使用最高版本2.1.0.RELEASE集成jar包:spring-integration-kafka 2. Zookeeper,、Kafka分布式集群使用init.properties配置化方案。
Java代碼 kafka.servers=127.0.0.1:9092 kafka.topic=xxxooo
3. 使用消息生產(chǎn)者spring-context-producer配置化方案,。 Java代碼 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd http://www./schema/context http://www./schema/context/spring-context.xsd"> <!-- 定義producer的參數(shù) --> <bean id="producerProperties" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="localhost:9092" /> <entry key="group.id" value="2" /> <entry key="retries" value="10" /> <entry key="batch.size" value="16384" /> <entry key="linger.ms" value="1" /> <entry key="buffer.memory" value="33554432" /> <entry key="key.serializer" value="org.apache.kafka.common.serialization.IntegerSerializer" /> <entry key="value.serializer" value="org.apache.kafka.common.serialization.StringSerializer" /> </map> </constructor-arg> </bean> <!-- 創(chuàng)建kafkatemplate需要使用的producerfactory bean --> <bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory"> <constructor-arg> <ref bean="producerProperties" /> </constructor-arg> </bean> <!-- 創(chuàng)建kafkatemplate bean,,使用的時候,,只需要注入這個bean,即可使用template的send消息方法 --> <bean id="KafkaTemplate" class="org.springframework.kafka.core.KafkaTemplate"> <constructor-arg ref="producerFactory" /> <constructor-arg name="autoFlush" value="true" /> <property name="defaultTopic" value="test" /> </bean> </beans>
4. 使用消息消費(fèi)者spring-context-producer配置化方案,。 Java代碼 <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www./schema/beans" xmlns:xsi="http://www./2001/XMLSchema-instance" xmlns:context="http://www./schema/context" xsi:schemaLocation="http://www./schema/beans http://www./schema/beans/spring-beans.xsd http://www./schema/context http://www./schema/context/spring-context.xsd"> <!-- 定義consumer的參數(shù) --> <bean id="consumerProperties" class="java.util.HashMap"> <constructor-arg> <map> <entry key="bootstrap.servers" value="localhost: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> <!-- 創(chuàng)建consumerFactory bean --> <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory"> <constructor-arg> <ref bean="consumerProperties" /> </constructor-arg> </bean> <!-- 實際執(zhí)行消息消費(fèi)的類 --> <bean id="messageListernerConsumerService" class="com.sml.sz.kafka.KafKaConsumer" /> <!-- 消費(fèi)者容器配置信息 --> <bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties"> <constructor-arg value="test" /> <property name="messageListener" ref="messageListernerConsumerService" /> </bean> <!-- 創(chuàng)建kafkatemplate bean,,使用的時候,只需要注入這個bean,,即可使用template的send消息方法 --> <bean id="messageListenerContainer" class="org.springframework.kafka.listener.KafkaMessageListenerContainer" init-method="doStart"> <constructor-arg ref="consumerFactory" /> <constructor-arg ref="containerProperties" /> </bean> </beans>
5. 使用注解方式注入消息類型
@Autowired private KafkaTemplate<xxx, ooo> kafkaTemplate; 6. 重寫MessageListener 的getMessage方法獲取消息(業(yè)務(wù)實現(xiàn))
7. RestFul服務(wù)方式測試消息服務(wù)
Java代碼 @CrossOrigin(origins = "*", maxAge = 3600, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE, RequestMethod.PUT }) @RestController @RequestMapping(value = "/rest/kafka") public class KafKaProducer { @RequestMapping(value = "/send", method = RequestMethod.GET) public JSONObject save() { System.out.println("+++++++++++++++++++++++++++++++"); kafkaTemplate.sendDefault("HongHu KAFKA分布式消息服務(wù)測試"); return null; } @Autowired private KafkaTemplate<Integer, String> kafkaTemplate; }
Java代碼 @RestController public class KafKaConsumer implements MessageListener<Integer, String> { @Autowired private LogService logService; public void onMessage(ConsumerRecord<Integer, String> records) { System.out.println("====================" + records); Object o = records.value(); Log log = new Log(); log.setIsNewRecord(true); log.setId(IdGen.uuid()); log.setTitle(String.valueOf(o)); logService.save(log); } }
接受消息了------------------:ConsumerRecord(topic = xxxooo, partition = 0, offset = 2489, CreateTime = 1479647648299, checksum = 3372898135, serialized key size = -1, serialized value size = 40, key = null, value = HongHu KAFKA分布式消息服務(wù)測試) 到此結(jié)束?。ㄆ髽I(yè)架構(gòu)源碼可以加求球:三五三六二四七二五九)
|