前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Java进阶实录—pringmvc+kafka分布式消息中间件集成方案

Java进阶实录—pringmvc+kafka分布式消息中间件集成方案

作者头像
慕容千语
发布2019-06-11 23:13:50
4200
发布2019-06-11 23:13:50
举报

Honghu的消息服务平台已经抛弃了之前的ActiveMQ,改用高吞吐量比较大的Kafka分布式消息中间件方案: kafka消息平台使用spring+kafka的集成方案, 详情如下: 1 . 使用最高版本2.1.0.RELEASE集成jar包:spring-integration-kafka 2 . Zookeeper、Kafka分布式集群使用init.properties配置化方案。

代码语言:javascript
复制
kafka.servers=127.0.0.1:9092    
kafka.topic=xxxooo  

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

代码语言:javascript
复制
<?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: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">
    <!-- 定义producer的参数 -->
    <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>
    <!-- 创建kafkatemplate需要使用的producerfactory bean -->
    <bean id="producerFactory" class="org.springframework.kafka.core.DefaultKafkaProducerFactory">
        <constructor-arg>
            <ref bean="producerProperties" />
        </constructor-arg>
    </bean>
    <!-- 创建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 . 使用消息消费者spring-context-producer配置化方案。

代码语言:javascript
复制
<?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: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">
    <!-- 定义consumer的参数 -->
    <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>
    <!-- 创建consumerFactory bean -->
    <bean id="consumerFactory" class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
        <constructor-arg>
            <ref bean="consumerProperties" />
        </constructor-arg>
    </bean>
    <!-- 实际执行消息消费的类 -->
    <bean id="messageListernerConsumerService" class="com.sml.sz.kafka.KafKaConsumer" />
    <!-- 消费者容器配置信息 -->
    <bean id="containerProperties" class="org.springframework.kafka.listener.config.ContainerProperties">
        <constructor-arg value="test" />
        <property name="messageListener" ref="messageListernerConsumerService" />
    </bean>
    <!-- 创建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方法获取消息(业务实现)

7 . RestFul服务方式测试消息服务

代码语言:javascript
复制
@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<Integer, String> kafkaTemplate;  
}  
代码语言:javascript
复制
@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分布式消息服务测试)

本文参与?腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019.04.03 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客?前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
消息队列 TDMQ
消息队列 TDMQ (Tencent Distributed Message Queue)是腾讯基于 Apache Pulsar 自研的一个云原生消息中间件系列,其中包含兼容Pulsar、RabbitMQ、RocketMQ 等协议的消息队列子产品,得益于其底层计算与存储分离的架构,TDMQ 具备良好的弹性伸缩以及故障恢复能力。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
http://www.vxiaotou.com