Honghu的消息服务平台已经抛弃了之前的ActiveMQ,改用高吞吐量比较大的Kafka分布式消息中间件方案:
kafka消息平台使用spring+kafka的集成方案,详情如下:
1. 使用最高版本2.1.0.RELEASE集成jar包:spring-integration-kafka
2. Zookeeper、Kafka分布式集群使用init.properties配置化方案。
kafka.servers=127.0.0.1:9092
kafka.topic=xxxooo
3. 使用消息生产者spring-context-producer配置化方案。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
value="org.apache.kafka.common.serialization.IntegerSerializer" />
value="org.apache.kafka.common.serialization.StringSerializer" />
class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
4. 使用消息消费者spring-context-producer配置化方案。
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
value="org.apache.kafka.common.serialization.IntegerDeserializer" />
value="org.apache.kafka.common.serialization.StringDeserializer" />
class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
class="org.springframework.kafka.listener.config.ContainerProperties">
class="org.springframework.kafka.listener.KafkaMessageListenerContainer"
init-method="doStart">
5. 使用注解方式注入消息类型
@Autowired
private KafkaTemplate
kafkaTemplate;
6. 重写MessageListener 的getMessage方法获取消息(业务实现)
7. RestFul服务方式测试消息服务
@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分布式消息服务测试");
return null;
}
@Autowired
private KafkaTemplate
kafkaTemplate;
}
@RestController
public class KafKaConsumer implements MessageListener
{
@Autowired
private LogService logService;
public void onMessage(ConsumerRecord
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分布式消息服务测试)
到此结束!(企业架构源码可以加求球:三五三六二四七二五九)