本文介绍了Memcached无法连接到远程服务器memcached.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的API本地环境

node -v: v8.9.4
npm  -v:  5.6.0

Package 
memcached.js: "memcached": "^2.2.2"

我们有一个Node API,其中我们使用包memcached.js通过以下配置连接到Memcache服务器.

We have a Node API in which we are using package memcached.js to connect to Memcache server with below configurations.

MEMCACHED_CONFIG:
{
    MAX_VALUE: 1024,
    SERVER: "X.X.X.X",
    PORT: 11211,
    COMPLETE_PATH: "X.X.X.X:11211",
    CACHE_TIMEOUT: 3600,
    POOL_SIZE: 50,
    maxKeySize: 1024,
    timeout: 5000
}

因此 X.X.X.X 是运行Memcache服务器的远程服务器IP.并且我能够使用 c:/> telnet X.X.X.X 11211 之类的telnet命令从我的系统连接此 X.X.X.X 服务器,并且它可以正常工作.

So X.X.X.X is remote server IP where our Memcache server is running.and I am able to connect this X.X.X.X server from my system by using telnet command like c:/> telnet X.X.X.X 11211 and it works.

cacheUtility.js

var MEMCACHED_CONFIG= require('./MEMCACHED_CONFIG');
var Memcached = require('memcached');
Memcached.config.maxValue = MEMCACHED_CONFIG.MAX_VALUE;
Memcached.config.poolSize = MEMCACHED_CONFIG.POOL_SIZE;
Memcached.config.maxKeySize= MEMCACHED_CONFIG.maxKeySize;
Memcached.config.timeout=  MEMCACHED_CONFIG.timeout;

var memcached = new Memcached();
memcached.connect(MEMCACHED_CONFIG.COMPLETE_PATH, function( err, 
conn ){
   if( err ) {
     CONFIG.CONSOLE_MESSAGE("Cache Connect Error "+conn.server);
  }
 });

我们正在使用以上代码连接到Memcached服务器,并且您可以看到远程服务器IP来自MEMCACHED_CONFIG.

We are using above code to connect to Memcached Server and as you can see remote server IP is coming from MEMCACHED_CONFIG.

我的问题是,它始终尝试连接到127.0.0.1服务器,而不是传递远程Memcached服务器.因此,为了运行它,我必须在核心软件包的memcached.js文件中进行更改.

My issue is that it is always trying to connect to 127.0.0.1 server instead of passing Remote Memcached Server. So in order to run it, I have to make changes in the memcached.js file of the core package.

C:\ BitBucketProjects \ Licensor Server \ node_modules \ memcached \ lib \ memcached.js

C:\BitBucketProjects\Licensor Server\node_modules\memcached\lib\memcached.js

function Client (args, options) {
var servers = []
, weights = {}
, regular = 'localhost:11211'
 //, regular = 'X.X.X.X:11211'
, key;

我不想对核心软件包进行任何更改.为什么不连接到给定的服务器?

I don't want to make any change in core package.Why is it not connecting to the given server?

推荐答案

当您在与使用该服务器的服务器不同的计算机上安装了内存缓存服务器时,请务必提及服务器IP和选项,否则默认为本地主机.您可以看到,如果您查看客户端的"server"属性(使用NodeJs memcached客户端版本2.2.2):

When you have memcached server setup on a different machine than the server using it then always mention the server IP and options otherwise it defaults to localhost. You can see that if you view the "server" property of the client (using NodeJs memcached client version 2.2.2):

var Memcached = require('memcached');
var memcached = new Memcached();
console.log(memcached.server);

"memcache.connect"方法似乎存在一些问题,因为它不会覆盖localhost服务器.要使其正常工作,您必须在文档中提到的构造函数中提及memcached服务器的IP:

Seems to be some issue with the "memcache.connect" method as it does not override the localhost server. To make it work, you have to mention the IP of memcached server in the constructor as mentioned in the documentation:

var Memcached = require('memcached');
var memcached = new Memcached('192.168.10.10:11211');

现在,如果在主机上打开了11211端口,则应该可以连接到服务器而不会出现问题.如果不允许,您可以在Memcached主机上执行以下命令以打开端口:

Now you should be able to connect to the server without an issue if you have the 11211 port opened on the host. If not allowed, you can execute the following command on Memcached host to open the port:

$ sudo ufw allow 11211

为确保能够连接到memcached服务器,请使用以下命令:

To ensure you are able to connect to memcached server use following command:

telnet 192.168.10.10:11211

即使这样仍然无法正常工作,则您的服务器可能已经停止工作,因此您需要以服务或进程的形式启动服务器:

If even that does not work, your server might have stopped working so you need to start it either as a service or as a process:

从流程开始:

$ memcached -u memcached -d -m 30 -l 192.168.10.10 -p 11211

从服务开始:

$ sudo systemctl start memcached

OR

$ sudo service memcached start

仅供参考,对于可能不知道要暴露网络上Memcached服务器的用户,您可以像上面的命令或memcached配置文件中那样指定IP和端口.要提供默认配置,请在以下文件中查找"-l 127.0.0.1",并将回送地址替换为主机服务器的网络IP:

Just for reference for those who might not know, to expose memcached server on the network, you can either specify the IP and port like in the command above or in the memcached configuration file. To provide default configuration, look for "-l 127.0.0.1" in the following file and replace the loopback address with your host server's network IP:

$ sudo nano /etc/default/ufw

当然,以上命令仅在服务器上安装了memcached时才有效,如果未安装,请先运行以下命令进行安装:

Of course, above commands will work only if you have memcached installed on the server, if not installed then run the following to install it first:

$ sudo apt-get install memcached

希望对您有帮助.

这篇关于Memcached无法连接到远程服务器memcached.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 14:30