最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot整合Pulsar的實(shí)現(xiàn)示例

 更新時(shí)間:2022年07月01日 16:33:32   作者:小波同學(xué)  
本文主要介紹了SpringBoot整合Pulsar的實(shí)現(xiàn)示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧

一、添加pom.xml依賴

<parent>
? ? <groupId>org.springframework.boot</groupId>
? ? <artifactId>spring-boot-starter-parent</artifactId>
? ? <version>2.7.0</version>
</parent>

<dependencies>
? ? <dependency>
? ? ? ? <groupId>org.springframework.boot</groupId>
? ? ? ? <artifactId>spring-boot-starter-web</artifactId>
? ? </dependency>

? ? <dependency>
? ? ? ? <groupId>org.apache.pulsar</groupId>
? ? ? ? <artifactId>pulsar-client</artifactId>
? ? ? ? <version>2.10.0</version>
? ? </dependency>

? ? <dependency>
? ? ? ? <groupId>org.projectlombok</groupId>
? ? ? ? <artifactId>lombok</artifactId>
? ? ? ? <version>1.18.24</version>
? ? ? ? <scope>provided</scope>
? ? </dependency>
</dependencies>

<build>
? ? <plugins>
? ? ? ? <plugin>
? ? ? ? ? ? <groupId>org.apache.maven.plugins</groupId>
? ? ? ? ? ? <artifactId>maven-compiler-plugin</artifactId>
? ? ? ? ? ? <configuration>
? ? ? ? ? ? ? ? <source>8</source>
? ? ? ? ? ? ? ? <target>8</target>
? ? ? ? ? ? </configuration>
? ? ? ? </plugin>
? ? </plugins>
</build> ? ?

二、Pulsar 參數(shù)類

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
?* @Author: huangyibo
?* @Date: 2022/5/28 2:32
?* @Description: Pulsar 參數(shù)類
?*/

@Component
@ConfigurationProperties(prefix = "tdmq.pulsar")
@Data
public class PulsarProperties {

? ? /**
? ? ?* 接入地址
? ? ?*/
? ? private String serviceurl;

? ? /**
? ? ?* 命名空間tdc
? ? ?*/
? ? private String tdcNamespace;

? ? /**
? ? ?* 角色tdc的token
? ? ?*/
? ? private String tdcToken;

? ? /**
? ? ?* 集群name
? ? ?*/
? ? private String cluster;

? ? /**
? ? ?* topicMap
? ? ?*/
? ? private Map<String, String> topicMap;

? ? /**
? ? ?* 訂閱
? ? ?*/
? ? private Map<String, String> subMap;

? ? /**
? ? ?* 開關(guān) on:Consumer可用 ||||| off:Consumer斷路
? ? ?*/
? ? private String onOff;
}

三、Pulsar 配置類

import org.apache.pulsar.client.api.AuthenticationFactory;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
?* @Author: huangyibo
?* @Date: 2022/5/28 2:33
?* @Description: Pulsar 配置類
?*/

@Configuration
@EnableConfigurationProperties(PulsarProperties.class)
public class PulsarConfig {

? ? @Autowired
? ? PulsarProperties pulsarProperties;

? ? @Bean
? ? public PulsarClient getPulsarClient() {

? ? ? ? try {
? ? ? ? ? ? return PulsarClient.builder()
? ? ? ? ? ? ? ? ? ? .authentication(AuthenticationFactory.token(pulsarProperties.getTdcToken()))
? ? ? ? ? ? ? ? ? ? .serviceUrl(pulsarProperties.getServiceurl())
? ? ? ? ? ? ? ? ? ? .build();
? ? ? ? } catch (PulsarClientException e) {
? ? ? ? ? ? System.out.println(e);
? ? ? ? ? ? throw new RuntimeException("初始化Pulsar Client失敗");
? ? ? ? }
? ? }

}

四、不同消費(fèi)數(shù)據(jù)類型的監(jiān)聽器

import com.yibo.pulsar.pojo.User;
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageListener;
import org.springframework.stereotype.Component;

/**
?* @Author: huangyibo
?* @Date: 2022/5/28 2:37
?* @Description:
?*/

@Component
public class UserMessageListener implements MessageListener<User> {

? ? @Override
? ? public void received(Consumer<User> consumer, Message<User> msg) {
? ? ? ? try {
? ? ? ? ? ? User user = msg.getValue();
? ? ? ? ? ? System.out.println(user);
? ? ? ? ? ? consumer.acknowledge(msg);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? consumer.negativeAcknowledge(msg);
? ? ? ? }
? ? }
}
import org.apache.pulsar.client.api.Consumer;
import org.apache.pulsar.client.api.Message;
import org.apache.pulsar.client.api.MessageListener;
import org.springframework.stereotype.Component;

/**
?* @Author: huangyibo
?* @Date: 2022/5/28 2:37
?* @Description:
?*/

@Component
public class StringMessageListener implements MessageListener<String> {

? ? @Override
? ? public void received(Consumer<String> consumer, Message<String> msg) {
? ? ? ? try {
? ? ? ? ? ? System.out.println(msg.getValue());
? ? ? ? ? ? consumer.acknowledge(msg);
? ? ? ? } catch (Exception e) {
? ? ? ? ? ? consumer.negativeAcknowledge(msg);
? ? ? ? }
? ? }
}

五、Pulsar的核心服務(wù)類

import com.yibo.pulsar.common.listener.StringMessageListener;
import com.yibo.pulsar.common.listener.UserMessageListener;
import com.yibo.pulsar.pojo.User;
import org.apache.pulsar.client.api.*;
import org.apache.pulsar.client.impl.schema.AvroSchema;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.concurrent.TimeUnit;

/**
?* @Author: huangyibo
?* @Date: 2022/5/28 2:35
?* @Description: Pulsar的核心服務(wù)類
?*/

@Component
public class PulsarCommon {

? ? @Autowired
? ? private PulsarProperties pulsarProperties;

? ? @Autowired
? ? private PulsarClient client;

? ? @Autowired
? ? private UserMessageListener userMessageListener;

? ? @Autowired
? ? private StringMessageListener stringMessageListener;


? ? /**
? ? ?* 創(chuàng)建一個(gè)生產(chǎn)者?
? ? ?* @param topic ? ? topic name
? ? ?* @param schema ? ?schema方式
? ? ?* @param <T> ? ? ? 泛型
? ? ?* @return ? ? ? ? ?Producer生產(chǎn)者
? ? ?*/
? ? public <T> Producer<T> createProducer(String topic, Schema<T> schema) {

? ? ? ? try {
? ? ? ? ? ? return client.newProducer(schema)
? ? ? ? ? ? ? ? ? ? .topic(pulsarProperties.getCluster() + "/" + pulsarProperties.getTdcNamespace() + "/" + topic)
? ? ? ? ? ? ? ? ? ? .batchingMaxPublishDelay(10, TimeUnit.MILLISECONDS)
? ? ? ? ? ? ? ? ? ? .sendTimeout(10, TimeUnit.SECONDS)
? ? ? ? ? ? ? ? ? ? .blockIfQueueFull(true)
? ? ? ? ? ? ? ? ? ? .create();
? ? ? ? } catch (PulsarClientException e) {
? ? ? ? ? ? throw new RuntimeException("初始化Pulsar Producer失敗");
? ? ? ? }
? ? }


? ? /**
? ? ?*?
? ? ?* @param topic ? ? ? ? ? ? topic name
? ? ?* @param subscription ? ? ?sub name
? ? ?* @param messageListener ? MessageListener的自定義實(shí)現(xiàn)類
? ? ?* @param schema ? ? ? ? ? ?schema消費(fèi)方式
? ? ?* @param <T> ? ? ? ? ? ? ? 泛型
? ? ?* @return ? ? ? ? ? ? ? ? ?Consumer消費(fèi)者
? ? ?*/
? ? public <T> Consumer<T> createConsumer(String topic, String subscription,
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?MessageListener<T> messageListener, Schema<T> schema) {
? ? ? ? try {
? ? ? ? ? ? return client.newConsumer(schema)
? ? ? ? ? ? ? ? ? ? .topic(pulsarProperties.getCluster() + "/" + pulsarProperties.getTdcNamespace() + "/" + topic)
? ? ? ? ? ? ? ? ? ? .subscriptionName(subscription)
? ? ? ? ? ? ? ? ? ? .ackTimeout(10, TimeUnit.SECONDS)
? ? ? ? ? ? ? ? ? ? .subscriptionType(SubscriptionType.Shared)
? ? ? ? ? ? ? ? ? ? .messageListener(messageListener)
? ? ? ? ? ? ? ? ? ? .subscribe();
? ? ? ? } catch (PulsarClientException e) {
? ? ? ? ? ? throw new RuntimeException("初始化Pulsar Consumer失敗");
? ? ? ? }
? ? }

? ??
? ? /**
? ? ?* 異步發(fā)送一條消息
? ? ?* @param message ? ? ? 消息體
? ? ?* @param producer ? ? ?生產(chǎn)者實(shí)例
? ? ?* @param <T> ? ? ? ? ? 消息泛型
? ? ?*/
? ? public <T> void sendAsyncMessage(T message, Producer<T> producer) {
? ? ? ? producer.sendAsync(message).thenAccept(msgId -> {
? ? ? ? });
? ? }
? ??
? ??
? ? /**
? ? ?* 同步發(fā)送一條消息
? ? ?* @param message ? ? ? 消息體
? ? ?* @param producer ? ? ?生產(chǎn)者實(shí)例
? ? ?* @param <T> ? ? ? ? ? 泛型
? ? ?* @throws PulsarClientException
? ? ?*/
? ? public <T> void sendSyncMessage(T message, Producer<T> producer) throws PulsarClientException {
? ? ? ? MessageId send = producer.send(message);
? ? ? ? System.out.println();
? ? ? ? System.out.println();
? ? ? ? System.out.println();
? ? ? ? System.out.println();
? ? ? ? System.out.println(send);
? ? }

? ??
? ? //-----------consumer-----------
? ? @Bean(name = "comment-publish-topic-consumer")
? ? public Consumer<String> getCommentPublishTopicConsumer() {
? ? ? ? return this.createConsumer(pulsarProperties.getTopicMap().get("comment-publish-topic"),
? ? ? ? ? ? ? ? pulsarProperties.getSubMap().get("comment-publish-topic-test"),
? ? ? ? ? ? ? ? stringMessageListener, Schema.STRING);
? ? }


? ? @Bean(name = "reply-publish-topic-consumer")
? ? public Consumer<User> getReplyPublishTopicConsumer() {
? ? ? ? return this.createConsumer(pulsarProperties.getTopicMap().get("reply-publish-topic"),
? ? ? ? ? ? ? ? pulsarProperties.getSubMap().get("reply-publish-topic-test"),
? ? ? ? ? ? ? ? userMessageListener, AvroSchema.of(User.class));
? ? }


? ? //-----------producer-----------
? ? @Bean(name = "comment-publish-topic-producer")
? ? public Producer<String> getCommentPublishTopicProducer() {
? ? ? ? return this.createProducer(pulsarProperties.getTopicMap().get("comment-publish-topic"),Schema.STRING);
? ? }


? ? @Bean(name = "reply-publish-topic-producer")
? ? public Producer<User> getReplyPublishTopicProducer() {
? ? ? ? return this.createProducer(pulsarProperties.getTopicMap().get("reply-publish-topic"), AvroSchema.of(User.class));
? ? }
}

六、Pulsar整合Spring Cloud

后來發(fā)現(xiàn)如上代碼會(huì)導(dǎo)致BUG-> 在更新Nacos配置之后 Consumer會(huì)掛掉
經(jīng)排查發(fā)現(xiàn)結(jié)果是由于@RefreshScope注解導(dǎo)致,此注解將摧毀Bean,PulsarConsumer和Producer都將被摧毀,只是說Producer將在下?次調(diào)?中完成重啟,Consumer則不能重啟,因?yàn)闆]有調(diào)?,那么怎么解決呢?

就是發(fā)布系列事件以刷新容器

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.stereotype.Component;

/**
?* @Author: huangyibo
?* @Date: 2022/5/28 2:34
?* @Description:
?*/

@Component
@Slf4j
public class RefreshPulsarListener implements ApplicationListener {

? ? @Autowired
? ? ApplicationContext applicationContext;

? ? @Override
? ? public void onApplicationEvent(ApplicationEvent event) {

? ? ? ? if (event.getSource().equals("__refreshAll__")) {
? ? ? ? ? ? log.info("Nacos配置中心配置修改 重啟Pulsar====================================");
? ? ? ? ? ? log.info("重啟PulsarClient,{}", applicationContext.getBean("getPulsarClient"));
? ? ? ? ? ? log.info("重啟PulsarConsumer,{}", applicationContext.getBean("comment-publish-topic-consumer"));
? ? ? ? ? ? log.info("重啟PulsarConsumer,{}", applicationContext.getBean("reply-publish-topic-consumer"));
? ? ? ? }
? ? }

}

參考:

https://wenku.baidu.com/view/4d3337ab6b0203d8ce2f0066f5335a8102d266a7.html

https://gitee.com/zhaoyuxuan66/pulsar-springcloud_boot-demo/tree/master/

https://blog.csdn.net/weixin_56227932/article/details/122897075

http://www.zzvips.com/article/219361.html

https://mp.weixin.qq.com/s/4w0eucDNcrYrsiDXHzLwuQ

到此這篇關(guān)于SpringBoot整合Pulsar的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)SpringBoot整合Pulsar內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • 分布式框架Zookeeper?api的使用介紹

    分布式框架Zookeeper?api的使用介紹

    Zookeeper作為?個(gè)分布式框架,主要用來解決分布式?致性問題,它提供了簡單的分布式原語,并且對(duì)多種編程語?提供了API,所以接下來重點(diǎn)來看下Zookeeper的java客戶端API使用方式
    2022-09-09
  • Spring Boot如何支持嵌入式Servlet容器

    Spring Boot如何支持嵌入式Servlet容器

    這篇文章主要介紹了Spring Boot如何支持嵌入式Servlet容器,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-10-10
  • 使用BigDecimal除法后保留兩位小數(shù)

    使用BigDecimal除法后保留兩位小數(shù)

    這篇文章主要介紹了使用BigDecimal除法后保留兩位小數(shù)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-06-06
  • Java實(shí)現(xiàn)圖像分割功能

    Java實(shí)現(xiàn)圖像分割功能

    這篇文章主要為大家詳細(xì)介紹了Java實(shí)現(xiàn)圖像分割功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2021-06-06
  • SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解

    SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解

    這篇文章主要介紹了SpringBoot中的ApplicationListener事件監(jiān)聽器使用詳解,ApplicationListener是應(yīng)用程序的事件監(jiān)聽器,繼承自java.util.EventListener標(biāo)準(zhǔn)接口,采用觀察者設(shè)計(jì)模式,需要的朋友可以參考下
    2023-11-11
  • Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解

    Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解

    這篇文章主要介紹了Spring中的BeanFactory對(duì)象實(shí)例化工廠詳解,BeanFactory及其子類是Spring IOC容器中最重要的一個(gè)類,BeanFactory由類名可以看出其是一個(gè)Bean工廠類,其實(shí)它確實(shí)是一個(gè)Bean工廠類,完成Bean的初始化操作,需要的朋友可以參考下
    2023-12-12
  • 淺談springboot如何保證多線程安全

    淺談springboot如何保證多線程安全

    這篇文章主要介紹了springboot如何保證多線程安全,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • MyBatis實(shí)現(xiàn)高級(jí)映射的示例代碼

    MyBatis實(shí)現(xiàn)高級(jí)映射的示例代碼

    高級(jí)映射主要還是映射,只是映射中的數(shù)據(jù)關(guān)系復(fù)雜了,其中就包括一對(duì)一、一對(duì)多、多對(duì)多的關(guān)系,本文主要介紹了MyBatis實(shí)現(xiàn)高級(jí)映射的示例代碼,感興趣的可以了解一下
    2024-06-06
  • MyBatis中多對(duì)一和一對(duì)多數(shù)據(jù)的處理方法

    MyBatis中多對(duì)一和一對(duì)多數(shù)據(jù)的處理方法

    這篇文章主要介紹了MyBatis中多對(duì)一和一對(duì)多數(shù)據(jù)的處理,本文通過示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-01-01
  • 關(guān)于Java日期工具類的編寫

    關(guān)于Java日期工具類的編寫

    這篇文章主要介紹了關(guān)于Java日期工具類的編寫,在Java開發(fā)中,經(jīng)常會(huì)遇到處理日期相關(guān)的數(shù)據(jù),那么今天我們來自己寫一個(gè)工具類,文中有詳細(xì)的實(shí)例代碼以及實(shí)現(xiàn)思路,需要的朋友可以參考下
    2023-05-05

最新評(píng)論

绥阳县| 万州区| 无棣县| 冀州市| 高阳县| 黄骅市| 天全县| 商南县| 城固县| 盱眙县| 防城港市| 利川市| 梁山县| 宁明县| 晋宁县| 炎陵县| 长兴县| 葫芦岛市| 新巴尔虎右旗| 彭山县| 巨鹿县| 富裕县| 新化县| 获嘉县| 天柱县| 巴楚县| 穆棱市| 军事| 浙江省| 正镶白旗| 永寿县| 昆明市| 色达县| 延安市| 黔南| 营山县| 卓尼县| 金乡县| 津南区| 县级市| 青川县|