ericholiveira / studio-cluster

Auto cluster for Studio framework
23 stars 4 forks source link

Use with Redis #14

Closed sean-hill closed 8 years ago

sean-hill commented 8 years ago

Hey there,

I'm trying to use Redis because I'll eventually have this setup on multiple networks / machines. I'm trying to test locally with:

Studio.use(studioCluster({
    publisher:studioCluster.publisher.redis(6379,'127.0.0.1')
}));

I have my redis server running locally, and it is able to connect with my other projects. However when I call any studio service, it takes a few seconds, and then call my .catch() callback with an undefined err.

I was curious how to implement this correctly. Thanks!

ericholiveira commented 8 years ago

Are you running multiple process on the same machine? If doing so you will have to add an rpcPort to the object youre passing as arguments for studioCluster. Also on studio.publisher.redis function, the first argument is the rpcPort (default to 10120 on studio cluster) not the redis port, the second argument is the object thats going to be passed to redis client. Check this example:

var rpcPort = 10120;// THIS IS YOUR RPC PORT. needs one different for each process/machine
Studio.use(studioCluster({
    rpcPort:rpcPort,
    publisher:studioCluster.publisher.redis(rpcPort,'YOUR_REDIS_ADDRESS')
}));
/*Notice that im passing rpcPort twice, i know this is messy, and i plan to find a better way to avoid this duplication, but it is needed atm
*/

The second argument is directly passed to ioredis constructor, you can see the accepted options here

Let me know if this worked for you

ericholiveira commented 8 years ago

Also this is the oficial example of redis usage, (you can avoid the balance parameter on option, it just show the usage of a different balancer)

sean-hill commented 8 years ago

Perfect thanks, I digged into the source and realized that the first argument was the rpcPort not the redis port 😝 I'll give the new way a go and report back.

sean-hill commented 8 years ago

@ericholiveira for my first process these are my options:

{
  rpcPort: 10120,
  publisher: studioCluster.publisher.redis(10120, { port: 6379, host: '127.0.0.1' })
}

And for my second process:

{
  rpcPort: 10121,
  publisher: studioCluster.publisher.redis(10121, { port: 6379, host: '127.0.0.1' })
}

And I'm having the same issue. Redis is connecting successfully, however, the services are still not running appropriately and are calling my .catch() after about 5 seconds.

ericholiveira commented 8 years ago

Which versions are you running on node, Studio and Studio-cluster?

ericholiveira commented 8 years ago

Oh man... im sorry... i already know what happened ...

You need to open your ports 10120 and 10121 to internet. Redis publisher is designed to communicate through different networks... so all rpcPorts used needs to be open for external access.. even if youre communicating on the same machine.

If you're just trying to see if its going to work when you're running on several networks you can open your node_modules folder, locate studio-cluster, and change the following line https://github.com/ericholiveira/studio-cluster/blob/master/src/publisher/redis/server.js#L59

for

myIp = '127.0.0.1';

As i said you should change this only as a POC and then delete and re-install studio-cluster. Tomorrow i can publish a new version of studio-cluster adding a flag to run "only local" using redis publisher.

sean-hill commented 8 years ago

Got it! Cause that module is probably returning something weird like ::1 for my IP. Let me give it a shot.

sean-hill commented 8 years ago

Worked perfectly :) I'll update my local repo once we get that onlyLocal flag available.

ericholiveira commented 8 years ago

Cool... anyway its really important to have this flag for local development, and also it lets me create at least a basic test using redis publisher

ericholiveira commented 8 years ago

Just published version 0.2.2, now redis publisher accepts a third argument, which is an options object where you can add a getIp method. Use the example above to force only local:

cluster.publisher.redis(rpcPort, 'REDIS_IP',{getIp:function(){
        return '127.0.0.1';
    }})

I added getIp instead of a flag because this can also be used to communicate several machines in the same network (if for some weird reason the user dont want to use the broadcast publisher for doing so).

Im closing this issue because its already fixed