引入pulsar maven依赖
<dependency>
<groupId>org.apache.pulsar</groupId>
<artifactId>pulsar-client</artifactId>
<version>2.10.0</version>
</dependency>
生产者
import lombok.extern.slf4j.Slf4j;
import org.apache.pulsar.client.api.MessageId;
import org.apache.pulsar.client.api.Producer;
import org.apache.pulsar.client.api.PulsarClient;
import org.apache.pulsar.client.api.PulsarClientException;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* @author 向振华
* @date 2022/10/11 10:05
*/
@Slf4j
@Component
public class PulsarProducer {
// Pulsar服务serviceURL
private String serviceUrl = "pulsar://192.168.0.1:6650";
// topic
private String topic = "xzhtest";
// PulsarClient
private PulsarClient client;
// 生产者
private Producer<byte[]> producer = null;
@PostConstruct
public void initPulsarProducer() throws PulsarClientException {
// 一个进程一个PulsarClient,一个PulsarClient下可以创建多个生产和消费者
client = PulsarClient.builder()
.serviceUrl(serviceUrl)
.build();
// 创建生产者producer
producer = client.newProducer(