掃二維碼與項(xiàng)目經(jīng)理溝通
我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
哈嘍,大家好,我是了不起。

創(chuàng)新互聯(lián)-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比徽縣網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式徽縣網(wǎng)站制作公司更省心,省錢,快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋徽縣地區(qū)。費(fèi)用合理售后完善,十載實(shí)體公司更值得信賴。
之前寫過(guò)關(guān)于 Apache Pulsar 的簡(jiǎn)單示例,用來(lái)了解如何使用 Pulsar 這個(gè)新生代的消息隊(duì)列中間件,但是如果想要在項(xiàng)目中使用,還會(huì)欠缺很多,最明顯的就是 集成復(fù)雜,如果你用過(guò)其他消息中間件,比如 Kafka、RabbitMq,只需要簡(jiǎn)單的引入 jar,就可以通過(guò)注解+配置快速集成到項(xiàng)目中。
既然已經(jīng)了解了 Apache Pulsar,又認(rèn)識(shí)了 spring-boot-starter,今天不妨來(lái)看下如何寫一個(gè) pulsar-spring-boot-starter 模塊。
寫一個(gè)完整的類似 kafka-spring-boot-starter(springboot 項(xiàng)目已經(jīng)集成到 spring-boot-starter 中),需要考慮到很多 kafka 的特性, 今天我們主要實(shí)現(xiàn)下面幾個(gè)模板
└── pulsar-starter
├── pulsar-spring-boot-starter
├── pulsar-spring-boot-autoconfigure
├── spring-pulsar
├── spring-pulsar-xx
├── spring-pulsar-sample
└── README.md
整個(gè)模塊的結(jié)構(gòu)如上其中pulsar-starter作為一個(gè)根模塊,主要控制子模塊依賴的其他 jar 的版本以及使用到的插件版本。類似于 Spring-Bom,這樣我們?cè)诤罄m(xù)升級(jí) 時(shí),就可以解決各個(gè)第三方 jar 的可能存在版本沖突導(dǎo)致的問(wèn)題。
該模塊作為外部項(xiàng)目集成的直接引用 jar,可以認(rèn)為是 pulsar-spring-boot-starter 組件的入口,里面不需要寫任何代碼,只需要引入需要的依賴(也就是下面的子模塊)即可
該模塊主要定義了 spring.factories 以及 AutoConfigure、Properties。也就是自動(dòng)配置的核心(配置項(xiàng)+Bean 配置)
該模塊是核心模塊,主要的實(shí)現(xiàn)都在這里
擴(kuò)展模塊,可以對(duì) spring-pulsar 做更細(xì)化的劃分
starter 的使用示例項(xiàng)目
上面我們說(shuō)到實(shí)現(xiàn)目標(biāo),現(xiàn)在看下各個(gè)模塊應(yīng)該包含什么內(nèi)容,以及怎么實(shí)現(xiàn)我們的目標(biāo)
上面說(shuō)到 starter 主要是引入整個(gè)模塊基礎(chǔ)的依賴即可,里面不用寫代碼。
com.sucl
spring-pulsar
${project.version}
com.sucl
pulsar-spring-boot-autoconfigure
${project.version}
org.springframework.boot
spring-boot
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-configuration-processor
true
@Configuration
@EnableConfigurationProperties({PulsarProperties.class})
@Import({PulsarAnnotationDrivenConfiguration.class})
public class PulsarAutoConfiguration {
private final PulsarProperties properties;
public PulsarAutoConfiguration(PulsarProperties properties) {
this.properties = properties;
}
@Bean(destroyMethod = "close")
public PulsarClient pulsarClient() {
ClientBuilder clientBuilder = new ClientBuilderImpl(properties);
return clientBuilder.build();
}
@Bean
@ConditionalOnMissingBean(ConsumerFactory.class)
public ConsumerFactory pulsarConsumerFactory() {
return new DefaultPulsarConsumerFactory(pulsarClient(), properties.getConsumer().buildProperties());
}
@Bean
@ConditionalOnMissingBean(ProducerFactory.class)
public ProducerFactory pulsarProducerFactory() {
return new DefaultPulsarProducerFactory(pulsarClient(), properties.getProducer().buildProperties());
}
}
在目錄src/main/resources/META-INF下創(chuàng)建spring.factories,內(nèi)容如下:
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
com.sucl.pulsar.autoconfigure.PulsarAutoConfiguration
org.apache.pulsar
pulsar-client
org.springframework.boot
spring-boot-autoconfigure
org.springframework
spring-messaging
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Import({PulsarListenerConfigurationSelector.class})
public @interface EnablePulsar {
}
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface PulsarListener {
/**
*
* @return TOPIC 支持SPEL
*/
String[] topics() default {};
/**
*
* @return TAGS 支持SPEL
*/
String[] tags() default {};
}
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface PulsarHandler {
}
flow
按照下面的流程,你會(huì)發(fā)現(xiàn)通過(guò)簡(jiǎn)單的幾行代碼就能夠?qū)崿F(xiàn)消息的生產(chǎn)與消費(fèi),并集成到項(xiàng)目中去。
com.sucl
pulsar-spring-boot-starter
${project.version}
org.springframework.boot
spring-boot-starter-web
2.添加配置
cycads:
pulsar:
service-url: pulsar://localhost:6650
listener-topics: TOPIC_TEST
3.編寫對(duì)應(yīng)消費(fèi)代碼
@Slf4j
@Component
@PulsarListener(topics = "#{'${cycads.listener-topics}'.split(',')}")
public class PulsarDemoListener {
@PulsarHandler
public void onConsumer(Message message){
log.info(">>> 接收到消息:{}", message.getPayload());
}
}
@Slf4j
@RunWith(SpringRunner.class)
@ContextConfiguration(classes = {ContextConfig.class})
@Import({PulsarAutoConfiguration.class})
public class ProducerTests {
@Autowired
private ProducerFactory producerFactory;
@Test
public void sendMessage() {
Producer producer = producerFactory.createProducer("TOPIC_TEST");
MessageId messageId = producer.send("this is a test message");
log.info(">>>>>>> 消息發(fā)送完成:{}", messageId);
}
@Configuration
@PropertySource(value = "classpath:application-test.properties")
static class ContextConfig {
//
}
}
2023-02-26 19:57:15.572 INFO 26520 --- [pulsar-01] c.s.p.s.listener.PulsarDemoListener : >>> 接收到消息:GenericMessage [payload=this is a test message, headers={id=f861488c-2afb-b2e7-21a1-f15e9759eec5, timestamp=1677412635571}]
基于 pulsar-client 提供的 ConfigurationData 擴(kuò)展 Properties;了解 Pulsar Client 如何連接 Broker 并進(jìn)行消息消費(fèi),包括同步消費(fèi)、異步消費(fèi)等等
實(shí)現(xiàn) starter 自動(dòng)配置的關(guān)鍵,基于 SPI 完成配置的自動(dòng)加載
通過(guò) Bean 生命周期相關(guān)擴(kuò)展實(shí)現(xiàn)注解的解析與容器的啟動(dòng),比如 BeanPostProcessor, BeanFactoryAware, SmartInitializingSingleton, InitializingBean, DisposableBean 等
基于回調(diào)與 MethodHandler 實(shí)現(xiàn)消息體的封裝、參數(shù)解析以及方法調(diào)用;
??https://github.com/sucls/pulsar-starter.git??
如果你看過(guò) spring-kafka 的源代碼,那么你會(huì)發(fā)現(xiàn)所有代碼基本都是仿造其實(shí)現(xiàn)。一方面能夠閱讀 kafka client 在 spring 具體如何實(shí)現(xiàn);同時(shí)通過(guò)編寫自己的 spring starter 模塊,學(xué)習(xí) 整個(gè) starter 的實(shí)現(xiàn)過(guò)程。

我們?cè)谖⑿派?4小時(shí)期待你的聲音
解答本文疑問(wèn)/技術(shù)咨詢/運(yùn)營(yíng)咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流