zoukankan      html  css  js  c++  java
  • 1.java连接pulsar服务

    是什么

    Pulsar 是一个用于服务器到服务器的消息系统,具有多租户、高性能等优势。详见

    安装

    本文主练习怎么用,本地搭建了一个单机版,无非就是wget、tar、start这些命令,详见

    Java客户端

    1.引入GAV

            <!-- https://mvnrepository.com/artifact/org.apache.pulsar/pulsar-client -->
            <dependency>
                <groupId>org.apache.pulsar</groupId>
                <artifactId>pulsar-client</artifactId>
                <version>2.8.0</version>
            </dependency>
    

    2.创建配置项

    用于连接Pulsar等配置

    • Yml配置

      pulsar:
        url: 10.20.30.228:6650
      #  url: 10.20.30.228:6650,10.20.30.228:6651 #集群配置
      
    • 新增配置类

      package com.project.pulsar.conf;
      
      import org.apache.pulsar.client.api.PulsarClient;
      import org.apache.pulsar.client.api.PulsarClientException;
      import org.springframework.beans.factory.annotation.Value;
      import org.springframework.context.annotation.Bean;
      import org.springframework.context.annotation.Configuration;
      
      @Configuration
      public class PulsarConf {
          @Value("${pulsar.url}")
          String url;
      
          @Bean
          public PulsarClient pulsarFactory(){
              PulsarClient client = null;
              try {
                  client = PulsarClient.builder()
                          .serviceUrl("pulsar://"+url)
                          .build();
              } catch (PulsarClientException e) {
              }
              return client;
          }
      }
      

    3.验证测试

    ​ 通过简单生产-消费测试配置是否正常

    • 创建BaseController

      注意,subscriptionName要保证唯一

      package com.project.pulsar.base;
      
      
      import com.project.pulsar.conf.PulsarConf;
      import org.apache.pulsar.client.api.*;
      import org.springframework.beans.factory.annotation.Autowired;
      import org.springframework.context.annotation.Bean;
      import org.springframework.web.bind.annotation.GetMapping;
      import org.springframework.web.bind.annotation.RestController;
      
      import java.util.HashMap;
      import java.util.Map;
      
      @RestController
      public class BaseController {
          @Autowired
          PulsarConf pulsarConf;
      
          /**
           * 生产消息
           * @param msg
           * @throws PulsarClientException
           */
          @GetMapping("/base/sendMsg")
          public MessageId sendMsg(String msg) throws PulsarClientException {
              PulsarClient pulsarFactory = pulsarConf.pulsarFactory();
      
              Producer<byte[]> producer1 = pulsarFactory.newProducer()
                      .topic("my-topic")
      
                      .create();
              // 然后你就可以发送消息到指定的broker 和topic上:
              return producer1.send(msg.getBytes());
          }
      
          /**
           * 手动执行获取消息
           * @throws PulsarClientException
           */
          @GetMapping("/base/comsumer")
          public void comsumerByArtificial() throws PulsarClientException {
              PulsarClient pulsarFactory = pulsarConf.pulsarFactory();
              Consumer<byte[]> consumer = pulsarFactory.newConsumer()
                      .topic("my-topic")
                      .subscriptionName("my-subscription")
                      .subscribe();
              Message<byte[]> receive = consumer.receive();
              System.out.println(new String(receive.getData()));
              consumer.acknowledge(receive);//确认消息被消费
              consumer.close();
          }
      
          /**
           * 自动监听消费消息
           * @throws PulsarClientException
           */
          @Bean
          public void comsumerByListener() throws PulsarClientException {
              MessageListener myMessageListener = (consumer, msg) -> {
                  try {
                      System.out.println("Message received: " + new String(msg.getData()));
                      consumer.acknowledge(msg);
                  } catch (Exception e) {
                      consumer.negativeAcknowledge(msg);
                  }
              };
              PulsarClient pulsarFactory = pulsarConf.pulsarFactory();
              pulsarFactory.newConsumer()
                      .topic("my-topic")
                      .subscriptionName("my-subscriptionByListener")
                      .messageListener(myMessageListener)
                      .subscribe();
          }
      
      
      }
      
      
    • 生产消息

      [127.0.0.1:9999/base/sendMsg?msg=Hello RB](http://127.0.0.1:9999/base/sendMsg?msg=Hello RB)

    • 消费消息

      • 在生产后,如果采用监听模式,会自动消费
      • 在生产后,如果采用手动模式,执行127.0.0.1:9999/base/comsumer会被消费,如队列中无消费,则会阻塞等待

    其他及代码下载

    • topic不用显式创建,当消息发送或消费者建立连接时,如未创建会自动创建
    • 代码见此Base包下
    1.所写技术都是我工作中用到的
    2.所写技术都是自己从项目中提取的
    3.所有配置搭建流程都经过2到3遍的测试
    4.因都是工作中使用的技术,所以不确定是否有转载的,如果有,请及时通知
  • 相关阅读:
    iOS 网易新闻用到的框架
    iOS homekit使用说明
    iOS 如何在整个屏幕中都能实现滑动返回的效果
    iOS 如何设置导航的滑动返回手势, 和系统饿一样
    iOS 7 tabbar 透明的问题
    iOS 使用xib后获取view的frame出错的问题
    Masonry 当需要把某个控件进行隐藏的时候有警告的解决方案
    java 中多线程之间的通讯之生产者和消费者 (多个线程之间的通讯)
    java 中多线程之间的通讯之等待唤醒机制
    linux时间同步chrony介绍
  • 原文地址:https://www.cnblogs.com/rb2010/p/15314682.html
Copyright © 2011-2022 走看看