设为首页收藏本站我的广告

运维网

 找回密码
 注册

QQ登录

只需一步,快速开始

搜索
总共321条微博

每日一博

查看: 181|回复: 1

[其它] 【站长推荐】spring+springmvc+kafka分布式消息中间件集成方案【顶】

[复制链接]

该用户从未签到

啊拉 发表于 2017-11-9 10:52:12 | 显示全部楼层 |阅读模式
【站长推荐】Honghu的消息服务平台已经抛弃了之前的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

[java]view plaincopyprint?

kafka.servers=127.0.0.1:9092

kafka.topic=xxxooo

kafka.servers=127.0.0.1:9092

kafka.topic=xxxooo

3. 使用消息生产者spring-context-producer配置化方案。

Java代码















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">



[java]view plaincopyprint?



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">





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配置化方案。

Java代码















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">

[java]view plaincopyprint?



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">



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服务方式测试消息服务

Java代码













@CrossOrigin(origins ="*", maxAge =3600, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,

RequestMethod.PUT })

@RestController

@RequestMapping(value ="/rest/kafka")

publicclassKafKaProducer {

@RequestMapping(value ="/send", method = RequestMethod.GET)

publicJSONObject save() {

System.out.println("+++++++++++++++++++++++++++++++");

kafkaTemplate.sendDefault("HongHu KAFKA分布式消息服务测试");

returnnull;

}

@Autowired

privateKafkaTemplate kafkaTemplate;

}

[java]view plaincopyprint?

@CrossOrigin(origins ="*", maxAge =3600, methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE,

RequestMethod.PUT })

@RestController

@RequestMapping(value ="/rest/kafka")

publicclassKafKaProducer {

@RequestMapping(value ="/send", method = RequestMethod.GET)

publicJSONObject save() {

System.out.println("+++++++++++++++++++++++++++++++");

kafkaTemplate.sendDefault("HongHu KAFKA分布式消息服务测试");

returnnull;

}

@Autowired

privateKafkaTemplate kafkaTemplate;

}

@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;

}

Java代码













@RestController

publicclassKafKaConsumerimplementsMessageListener {

@Autowired

privateLogService logService;

publicvoidonMessage(ConsumerRecord records) {

System.out.println("===================="+ records);

Object o = records.value();

Log log =newLog();

log.setIsNewRecord(true);

log.setId(IdGen.uuid());

log.setTitle(String.valueOf(o));

logService.save(log);

}

}

[java]view plaincopyprint?

@RestController

publicclassKafKaConsumerimplementsMessageListener {

@Autowired

privateLogService logService;

publicvoidonMessage(ConsumerRecord records) {

System.out.println("===================="+ records);

Object o = records.value();

Log log =newLog();

log.setIsNewRecord(true);

log.setId(IdGen.uuid());

log.setTitle(String.valueOf(o));

logService.save(log);

}

}

@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);

}

}

愿意了解框架技术或者源码的朋友直接求求交流分享技术:3133806896

分布式的一些解决方案,有愿意了解的朋友可以找我们团队探讨

更多详细源码参考来源

运维网 感谢您的阅读
[发帖际遇]: 一个袋子砸在了 啊拉 头上,啊拉 赚了 5 金钱. 幸运榜 / 衰神榜
您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|申请友链|sitemap|手机版|小黑屋|Archiver|运维网 ( 京ICP备16008201号  

GMT+8, 2018-1-23 11:33 , Processed in 0.285199 second(s), 43 queries , XCache On.

Powered by Discuz! X3.4 Licensed

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表