Spymemcached源码 第一节 初始化

首先看下spy memcache的使用demo

List<InetSocketAddress> serverAddress = new LinkedList<>();
serverAddress.add(new InetSocketAddress("127.0.0.1", 11211));
//构造者
ConnectionFactoryBuilder connectionFactoryBuilder = new ConnectionFactoryBuilder();
connectionFactoryBuilder.setOpTimeout(50);
connectionFactoryBuilder.setTimeoutExceptionThreshold(DEFAULT_MAX_TIMEOUTEXCEPTION_THRESHOLD);
connectionFactoryBuilder.setReadBufferSize(65535);

MemcachedClient client = new MemcachedClient(connectionFactoryBuilder.build(), serverAddress);

System.out.println(client.set("zhurui", 2, "zhurui").get());

首先设置ip、port,client可以连接多个memcache node。然后创建ConnectionFactoryBuilder,设置一些基础属性,然后buid,返回一个DefaultConnectionFactory,并实现了抽象方法。然后new MemcachedClient。

接下来看下MemcachedClient的构造方法:

public MemcachedClient(ConnectionFactory cf, List<InetSocketAddress> addrs)
    throws IOException {
    if (cf == null) {
      throw new NullPointerException("Connection factory required");
    }
    if (addrs == null) {
      throw new NullPointerException("Server list required");
    }
    if (addrs.isEmpty()) {
      throw new IllegalArgumentException("You must have at least one server to"
          + " connect to");
    }
    if (cf.getOperationTimeout() <= 0) {
      throw new IllegalArgumentException("Operation timeout must be positive.");
    }
    connFactory = cf;
    //初始化异步转换编码器,内部创建一个线程池
    tcService = new TranscodeService(cf.isDaemon());
    //返回默认的object-》byte array序列化qi器SerializingTranscoder
    transcoder = cf.getDefaultTranscoder();
    //返回AsciiOperationFactory
    opFact = cf.getOperationFactory();
    assert opFact != null : "Connection factory failed to make op factory";
    //创建MemcachedConnection,核心,待会分析
    mconn = cf.createConnection(addrs);
    assert mconn != null : "Connection factory failed to make a connection";
    operationTimeout = cf.getOperationTimeout();
    authDescriptor = cf.getAuthDescriptor();
    executorService = cf.getListenerExecutorService();
    if (authDescriptor != null) {
      addObserver(this);
    }
  }

下面看cf.createConnection(addrs)

public MemcachedConnection createConnection(List<InetSocketAddress> addrs)
    throws IOException {
    return new MemcachedConnection(getReadBufSize(), this, addrs,
        getInitialObservers(), getFailureMode(), getOperationFactory());
  }
  1. getFailureMode( )返回DEFAULT_FAILURE_MODE,即FailureMode.Redistribute,策略是当前节点处理失败可以转移其他的节点处理

  2. getOperationFactory( )返回AsciiOperationFactory对象

下面分析怎么构造MemcachedConnection对象的:

 /**
   * Construct a {@link MemcachedConnection}.
   *
   * @param bufSize the size of the buffer used for reading from the server.
   * @param f the factory that will provide an operation queue.
   * @param a the addresses of the servers to connect to.
   * @param obs the initial observers to add.
   * @param fm the failure mode to use.
   * @param opfactory the operation factory.
   * @throws IOException if a connection attempt fails early
   */
  public MemcachedConnection(final int bufSize, final ConnectionFactory f,
      final List<InetSocketAddress> a, final Collection<ConnectionObserver> obs,
      final FailureMode fm, final OperationFactory opfactory) throws IOException {
    connObservers.addAll(obs);
    //重连队列,key是过多久可以尝试重连的时间
    reconnectQueue = new TreeMap<Long, MemcachedNode>();
    //用来记录排队到节点的操作
    addedQueue = new ConcurrentLinkedQueue<MemcachedNode>();
    //请求失败策略
    failureMode = fm;
    //是否需要优化多个连续的get操作,默认false,不做优化处理
    shouldOptimize = f.shouldOptimize();
    //重连最大等待时间,这里是30s
    maxDelay = TimeUnit.SECONDS.toMillis(f.getMaxReconnectDelay());
    //clone,create operation工厂,这里是AsciiOperationFactory
    opFact = opfactory;
    //最大的连接超时导致异常的次数 996
    timeoutExceptionThreshold = f.getTimeoutExceptionThreshold();
    //选择器
    selector = Selector.open();
    //存放需要被重试的操作
    retryOps = Collections.synchronizedList(new ArrayList<Operation>());
    //存放需要被定时关闭的节点
    nodesToShutdown = new ConcurrentLinkedQueue<MemcachedNode>();
    //回调用的连接池
    listenerExecutorService = f.getListenerExecutorService();
    //从服务端读取数据的buffer size ,65535
    this.bufSize = bufSize;
    //创建 MemcachedNode的工厂
    this.connectionFactory = f;

    String verifyAlive = System.getProperty("net.spy.verifyAliveOnConnect");
    if(verifyAlive != null && verifyAlive.equals("true")) {
      verifyAliveOnConnect = true;
    } else {
      verifyAliveOnConnect = false;
    }

    wakeupDelay = Integer.parseInt( System.getProperty("net.spy.wakeupDelay",
      Integer.toString(DEFAULT_WAKEUP_DELAY)));
    //根据提供的地址创建memcache连接
    List<MemcachedNode> connections = createConnections(a);
    //创建locator,这边使用Native hash (String.hashCode())取模的方法进行多节点负载
    locator = f.createLocator(connections);

    metrics = f.getMetricCollector();
    metricType = f.enableMetrics();

    registerMetrics();

    setName("Memcached IO over " + this);
    setDaemon(f.isDaemon());
    //启动MemcachedConnection的run方法
    start();
  }

接下来看下怎么创建MemcachedConnection的

protected List<MemcachedNode> createConnections(
    final Collection<InetSocketAddress> addrs) throws IOException {
    List<MemcachedNode> connections = new ArrayList<MemcachedNode>(addrs.size());

    for (SocketAddress sa : addrs) {
      //打开一个SocketChannel
      SocketChannel ch = SocketChannel.open();
      //设置成非阻塞
      ch.configureBlocking(false);
      //创建一个memcachedNode,实现类是AsciiMemcachedNodeImpl,
      //内部初始化了读操作、写操作、连接操作队列,最大阻塞操等待作完成时间(10s),操作(50ms)、验证超时设置
      MemcachedNode qa = connectionFactory.createMemcachedNode(sa, ch, bufSize);
      qa.setConnection(this);
      int ops = 0;
      //设置NoDelay为true
      ch.socket().setTcpNoDelay(!connectionFactory.useNagleAlgorithm());

      try {
        //连接服务端,如果立即连接成功则返回true    
        if (ch.connect(sa)) {
          getLogger().info("Connected to %s immediately", qa);
          connected(qa);
        } else {
          getLogger().info("Added %s to connect queue", qa);
          ops = SelectionKey.OP_CONNECT;
        }
        //唤醒select()方法
        selector.wakeup();
        //注册CONNECT事件并把selection保存到node
        qa.setSk(ch.register(selector, ops, qa));
        assert ch.isConnected()
            || qa.getSk().interestOps() == SelectionKey.OP_CONNECT
            : "Not connected, and not wanting to connect";
      } catch (SocketException e) {
        getLogger().warn("Socket error on initial connect", e);
        queueReconnect(qa);
      }
      connections.add(qa);
    }

    return connections;
  }

转载于:https://my.oschina.net/u/913896/blog/797494

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
提供的源码资源涵盖了安卓应用、小程序、Python应用和Java应用等多个领域,每个领域都包含了丰富的实例和项目。这些源码都是基于各自平台的最新技术和标准编写,确保了在对应环境下能够无缝运行。同时,源码中配备了详细的注释和文档,帮助用户快速理解代码结构和实现逻辑。 适用人群: 这些源码资源特别适合大学生群体。无论你是计算机相关专业的学生,还是对其他领域编程感兴趣的学生,这些资源都能为你提供宝贵的学习和实践机会。通过学习和运行这些源码,你可以掌握各平台开发的基础知识,提升编程能力和项目实战经验。 使用场景及目标: 在学习阶段,你可以利用这些源码资源进行课程实践、课外项目或毕业设计。通过分析和运行源码,你将深入了解各平台开发的技术细节和最佳实践,逐步培养起自己的项目开发和问题解决能力。此外,在求职或创业过程中,具备跨平台开发能力的大学生将更具竞争力。 其他说明: 为了确保源码资源的可运行性和易用性,特别注意了以下几点:首先,每份源码都提供了详细的运行环境和依赖说明,确保用户能够轻松搭建起开发环境;其次,源码中的注释和文档都非常完善,方便用户快速上手和理解代码;最后,我会定期更新这些源码资源,以适应各平台技术的最新发展和市场需求。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值