spring+springmvc+kafka分布式消息中間件集成方案

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

[java]view plaincopyprint?

kafka.servers=127.0.0.1:9092

kafka.topic=xxxooo

kafka.servers=127.0.0.1:9092

kafka.topic=xxxooo

3.?使用消息生產(chǎn)者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. 使用消息消費(fèi)者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方法獲取消息(業(yè)務(wù)實(shí)現(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")

publicclassKafKaProducer?{

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

publicJSONObject?save()?{

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

kafkaTemplate.sendDefault("HongHu?KAFKA分布式消息服務(wù)測試");

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分布式消息服務(wù)測試");

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分布式消息服務(wù)測試");

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

}

}

愿意了解框架技術(shù)或者源碼的朋友直接求求交流分享技術(shù):3133806896

分布式的一些解決方案,有愿意了解的朋友可以找我們團(tuán)隊(duì)探討

更多詳細(xì)源碼參考來源

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末彻亲,一起剝皮案震驚了整個濱河市孕锄,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌苞尝,老刑警劉巖畸肆,帶你破解...
    沈念sama閱讀 219,427評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異野来,居然都是意外死亡恼除,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,551評論 3 395
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來豁辉,“玉大人令野,你說我怎么就攤上這事』占叮” “怎么了气破?”我有些...
    開封第一講書人閱讀 165,747評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長餐抢。 經(jīng)常有香客問我现使,道長,這世上最難降的妖魔是什么旷痕? 我笑而不...
    開封第一講書人閱讀 58,939評論 1 295
  • 正文 為了忘掉前任碳锈,我火速辦了婚禮,結(jié)果婚禮上欺抗,老公的妹妹穿的比我還像新娘售碳。我一直安慰自己,他們只是感情好绞呈,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,955評論 6 392
  • 文/花漫 我一把揭開白布喻频。 她就那樣靜靜地躺著尘颓,像睡著了一般订晌。 火紅的嫁衣襯著肌膚如雪迄委。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,737評論 1 305
  • 那天圾亏,我揣著相機(jī)與錄音十拣,去河邊找鬼。 笑死召嘶,一個胖子當(dāng)著我的面吹牛父晶,可吹牛的內(nèi)容都是我干的哮缺。 我是一名探鬼主播弄跌,決...
    沈念sama閱讀 40,448評論 3 420
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼尝苇!你這毒婦竟也來了铛只?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,352評論 0 276
  • 序言:老撾萬榮一對情侶失蹤糠溜,失蹤者是張志新(化名)和其女友劉穎淳玩,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體非竿,經(jīng)...
    沈念sama閱讀 45,834評論 1 317
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡蜕着,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,992評論 3 338
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片承匣。...
    茶點(diǎn)故事閱讀 40,133評論 1 351
  • 序言:一個原本活蹦亂跳的男人離奇死亡蓖乘,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出韧骗,到底是詐尸還是另有隱情嘉抒,我是刑警寧澤,帶...
    沈念sama閱讀 35,815評論 5 346
  • 正文 年R本政府宣布袍暴,位于F島的核電站些侍,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏政模。R本人自食惡果不足惜岗宣,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,477評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望淋样。 院中可真熱鬧狈定,春花似錦、人聲如沸习蓬。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,022評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽躲叼。三九已至芦缰,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間枫慷,已是汗流浹背让蕾。 一陣腳步聲響...
    開封第一講書人閱讀 33,147評論 1 272
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留或听,地道東北人探孝。 一個月前我還...
    沈念sama閱讀 48,398評論 3 373
  • 正文 我出身青樓,卻偏偏與公主長得像誉裆,于是被迫代替她去往敵國和親顿颅。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,077評論 2 355

推薦閱讀更多精彩內(nèi)容