springboot通过配置区分并集成rocketmq还是kafka
时间: 2023-09-16 12:14:07 浏览: 146
Spring Boot 提供了对 Kafka 和 RocketMQ 的自动配置支持,可以通过简单的配置来集成它们。如果你想区分并集成 Kafka 和 RocketMQ,你需要按照以下步骤进行:
1. 添加 Kafka 和 RocketMQ 的依赖项:
```xml
<dependency>
<groupId>org.springframework.kafka</groupId>
<artifactId>spring-kafka</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.rocketmq</groupId>
<artifactId>rocketmq-spring-boot-starter</artifactId>
<version>2.0.3</version>
</dependency>
```
2. 配置 Kafka:
```yaml
spring:
kafka:
bootstrap-servers: localhost:9092
consumer:
group-id: my-group
auto-offset-reset: earliest
producer:
key-serializer: org.apache.kafka.common.serialization.StringSerializer
value-serializer: org.apache.kafka.common.serialization.StringSerializer
```
3. 配置 RocketMQ:
```yaml
spring:
rocketmq:
name-server: localhost:9876
producer:
group: my-group
retry-times-when-send-failed: 3
send-msg-timeout: 3000
consumer:
group: my-group
max-reconsume-times: 3
```
4. 创建 Kafka 和 RocketMQ 的 Producer 和 Consumer:
```java
@Configuration
public class KafkaConfig {
@Value("${spring.kafka.bootstrap-servers}")
private String bootstrapServers;
@Bean
public ProducerFactory<String, String> producerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
return new DefaultKafkaProducerFactory<>(props);
}
@Bean
public KafkaTemplate<String, String> kafkaTemplate() {
return new KafkaTemplate<>(producerFactory());
}
@Bean
public ConsumerFactory<String, String> consumerFactory() {
Map<String, Object> props = new HashMap<>();
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, bootstrapServers);
props.put(ConsumerConfig.GROUP_ID_CONFIG, "my-group");
props.put(ConsumerConfig.AUTO_OFFSET_RESET_CONFIG, "earliest");
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class);
return new DefaultKafkaConsumerFactory<>(props);
}
@Bean
public ConcurrentKafkaListenerContainerFactory<String, String> kafkaListenerContainerFactory() {
ConcurrentKafkaListenerContainerFactory<String, String> factory =
new ConcurrentKafkaListenerContainerFactory<>();
factory.setConsumerFactory(consumerFactory());
return factory;
}
}
@Configuration
public class RocketMQConfig {
@Value("${spring.rocketmq.name-server}")
private String nameServer;
@Bean
public RocketMQTemplate rocketMQTemplate() {
RocketMQTemplate rocketMQTemplate = new RocketMQTemplate();
rocketMQTemplate.setProducer(new DefaultMQProducer("my-group"));
rocketMQTemplate.setDefaultTopic("my-topic");
rocketMQTemplate.setSendTimeout(3000);
return rocketMQTemplate;
}
@Bean
public DefaultMQPushConsumer defaultMQPushConsumer() throws MQClientException {
DefaultMQPushConsumer consumer = new DefaultMQPushConsumer("my-group");
consumer.setNamesrvAddr(nameServer);
consumer.subscribe("my-topic", "*");
consumer.setMaxReconsumeTimes(3);
return consumer;
}
}
```
这样,你就能够区分并集成 Kafka 和 RocketMQ 了。在需要使用 Kafka 或 RocketMQ 的地方,可以注入相应的 Producer 和 Consumer,并使用它们来发送和接收消息。
阅读全文