What I want to achieve is:
registering a node.js service in my eureka-server in heroku.
So far i can register a regular eureka-client in my eureka-server in heroku, without problems. But i am getting really confused with the configuration when try with node.js service...
I thought eureka-js-client was the solution, no success so far...
Code below.
const express = require('express');
const path = require('path');
const port = process.env.PORT || 3090;
const app = express();
app.use(express.static(__dirname));
const bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.get('*', (req, res) => {
res.sendFile(path.resolve(__dirname, 'index.html'))
});
const Eureka = require('eureka-js-client').Eureka;
const eureka = new Eureka({
instance: {
app: 'sheila',
hostName: 'localhost',
ipAddr: '127.0.0.1',
statusPageUrl: 'http://localhost:5000',
healthCheckUrl: 'http://localhost:5000/health',
port: {
'$': 5000,
'#enabled': 'true',
},
vipAddress: 'myvip',
dataCenterInfo: {
'#Class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
},
eureka: {
host: 'localhost',
port: 8761,
servicePath: '/eureka/apps/',
},
});
eureka.logger.level('debug');
eureka.start(function(error){
console.log(error || 'complete');
});
// ------------------ Server Config --------------------------------------------
var server = app.listen(5000, function () {
var port = server.address().port;
console.log('Listening at %s', port);
});
First i've tried locally after running docker run -it -p 8761:8761 springcloud/eureka on my docker console. But i get this error:
Problem making eureka request { [Error: connect ECONNREFUSED 127.0.0.1:8761]
code: 'ECONNREFUSED',
errno: 'ECONNREFUSED',
syscall: 'connect',
address: '127.0.0.1',
port: 8761 }
if i run it as it is from a heroku service, It does not execute :( :(
I also tried by substituting the host for the url of my heroku eureka server, but then i get 401 or 404 errors. The eureka server requires a password which i added in my heroku client js .
Any ideas?
You need to include an instanceId which should be a unique identifier of this instance.
const eureka = new Eureka({
instance: {
instanceId:'sheila',
app: 'sheila',
hostName: 'localhost',
ipAddr: '127.0.0.1',
statusPageUrl: 'http://localhost:5000',
healthCheckUrl: 'http://localhost:5000/health',
port: {
'$': 5000,
'#enabled': 'true',
},
vipAddress: 'myvip',
dataCenterInfo: {
'#Class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
},
eureka: {
host: 'localhost',
port: 8761,
servicePath: '/eureka/apps/',
},
});
Related
I'm new to Node and I want my website, dacio.app, working with subdomains for my college projects using vhost.
However, I need to have it secured due to the requirement for .app domains, so I'm using greenlock-express to automate it.
Don't be frontin', yo! TLS SNI 'giphy.dacio.app' does not match 'Host:
potatoes.dacio.app'
I've tried using the vhost example in the repo, but it doesn't look like server-static supports express apps.
Any tips on how to get this working? I keep hearing about reverse proxies, but I'm not sure if it's worth the effort as I don't even know if it would work - would it help?
server.js
#!/usr/bin/env node
'use strict';
// DEPENDENCIES
const express = require('express');
const vhost = require('vhost');
const path = require('path');
const glx = require('greenlock-express');
// MIDDLEWARE
const app = express();
const giphyApp = require('../giphy-search');
const potatoesApp = require('../rotten-potatoes');
const portfolioApp = require('../dacio.app');
// ROUTES
app.use(vhost('giphy.dacio.app', giphyApp));
app.use(vhost('potatoes.dacio.app', potatoesApp));
app.use(portfolioApp);
// GREENLOCK for HTTPS
glx.create({
version: 'draft-11',
server: 'https://acme-v02.api.letsencrypt.org/directory',
email: 'dacioromero#gmail.com',
agreeTos: true,
approveDomains: [ 'dacio.app', 'giphy.dacio.app', 'potatoes.dacio.app' ],
configDir: '~/.config/acme/',
app: app,
communityMember: false
}).listen(80, 443);
I've switched to using redbird which seems to accomplish everything I was hoping to do.
const path = require('path')
const proxy = require('redbird')({
port: 80,
letsencrypt: {
path: path.join(__dirname, '/certs'),
port: 9999
},
ssl: {
http2: true,
port: 443
}
});
proxy.register('dacio.app', 'http://localhost:8080', {
ssl: {
letsencrypt: {
email: 'dacioromero#gmail.com',
production: true,
}
}
});
proxy.register('giphy.dacio.app', 'http://localhost:8081', {
ssl: {
letsencrypt: {
email: 'dacioromero#gmail.com',
production: true
}
}
})
proxy.register('potatoes.dacio.app', 'http://localhost:8082', {
ssl: {
letsencrypt: {
email: 'dacioromero#gmail.com',
production: true
}
}
});
I have 3 microservices in Spring:
Netflix Eureka
A Producer
A Consumer
And another microservice written in NodeJs with Eureka-js-client
node-microservice
Spring Eureka dashboard lists all of them
Until now everything looks OK, but the problem is when I try to read my node-microservice instance in eureka server. While I successfully find employee-producer instance in this way
List<ServiceInstance> instances=discoveryClient.getInstances("employee-producer");
ServiceInstance serviceInstance=instances.get(0);
I'm not able to find my node-microservice
List<ServiceInstance> instances=discoveryClient.getInstances("node-microservice");
ServiceInstance serviceInstance=instances.get(0);
From debug the result is
node-microservice
const Eureka = require('eureka-js-client').Eureka;
const express = require('express');
const server = express();
server.use(express.json());
server.use(function(req, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
});
server.listen(3001);
server.get('/', function (req, res) {
res.send("CIaooo");
});
// example configuration
const client = new Eureka({
// application instance information
instance: {
app: 'node-microservice',
hostName: 'localhost',
ipAddr: '127.0.0.1',
port: {
'$': 3001,
'#enabled': 'true',
},
vipAddress: 'jq.test.something.com',
statusPageUrl: 'http://localhost:3001/info',
dataCenterInfo: {
'#class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
}
},
eureka: {
// eureka server host / port
host: 'localhost',
port: 8761,
servicePath: '/eureka/apps/'
},
});
client.logger.level('debug');
client.start(function(error){
console.log(error || 'complete')});
Other strange thing is that from spring debug I can list services, where also node-microservice is listed
What's wrong with my code?
The problem is that in the instance object I didn't write instanceId (wasn't mentioned anywhere). I found this solution crawling the code and than in another project where there was also this field
instance: {
app: 'node-microservice',
instanceId: 'nodemicroservice',
hostName: 'localhost',
ipAddr: '127.0.0.1',
port: {
'$': 3001,
'#enabled': 'true',
},
vipAddress: 'nodemicroservice',
statusPageUrl: 'http://localhost:3001/info',
dataCenterInfo: {
'#class': 'com.netflix.appinfo.InstanceInfo$DefaultDataCenterInfo',
name: 'MyOwn',
},
registerWithEureka: true,
fetchRegistry: true
},
I am trying to host api and web servers on the same node code stack. I have used labels in order to apply configurations independently to each server but only one server works. Below is the code:
var hapi = require('hapi');
// server definition
var server = new hapi.Server();
var runningPort = process.env.PORT || 3000;
// setting up connection
server.connection({
host: '0.0.0.0',
port: runningPort,
labels: ['api']
});
server.connection({
host: '0.0.0.0',
port: runningPort,
labels: ['web']
});
var webServer = server.select('web');
var apiServer = server.select('api');
// registering view engine
webServer.views({
engines: { html: require('handlebars') },
relativeTo: __dirname,
path: './views',
layoutPath: './views/layout',
layout: 'default',
partialsPath: './views/partials'
});
// registering hapi auth cookie and application authentication
webServer.register(
{
register: require('hapi-auth-cookie')
},
function (err) {
if (err) {
throw err;
}
var cache = webServer.cache({ segment: 'sessions', expiresIn: 3 * 24 * 60 * 60 * 1000 });
webServer.app.cache = cache;
webServer.auth.strategy('session', 'cookie', true, {
password: 'secret',
cookie: 'sid-example',
redirectTo: '/account/login',
isSecure: false
});
});
// registrations for api server
apiServer.register(
{
register: require('lout')
},
function (err) {
if (err) {
throw err;
}
});
apiServer.register(require('hapi-auth-bearer-token'), function (err) {
apiServer.auth.strategy('simple', 'bearer-access-token', {
allowQueryToken: true, // optional, true by default
allowMultipleHeaders: false, // optional, false by default
accessTokenName: 'access_token', // optional, 'access_token' by default
validateFunc: function( token, callback ) {
// For convenience, the request object can be accessed
// from `this` within validateFunc.
var request = this;
// Use a real strategy here,
// comparing with a token from your database for example
if(token === "1234"){
//## user object to be looked up here
callback(null, true, { token: token })
} else {
callback(null, false, { token: token })
}
}
});
});
//To do something to request before they passed on to routes
apiServer.ext('onRequest', function (request, reply) {
//## we can get user object here off of the authToken
utils.log('info', 'apiCall', {method: request.method, path: request.path})
return reply.continue();
});
// register routes
webServer.route(webRoutes);
apiServer.route(apiRoutes);
server.start(function () {
console.log('Web servers running at: ', 'localhost:' + runningPort);
console.log('Api server running at: ', 'localhost:' + runningPort);
});
Currently only api routes work.
As mentioned by the commenters, you can't create 2 connections to the same port on the same network interface. This goes all the way back to the listen syscall giving a EADDRINUSE error if two sockets try to listen on the same port.
Creating two connections on separate ports or separate network interfaces is perfectly ok though:
server.connection({
host: '127.0.0.1',
port: 4000,
labels: ['api']
});
server.connection({
host: '127.0.0.1',
port: 4001,
labels: ['web']
});
As Matt already said, you can't run different connections on the same port. Make sure to apply different ports, like
server.connection({
host: 'localhost',
port: process.env.PORT || port
});
server.connection({
host: 'localhost',
port: process.env.PORT + 1 || port + 1
});
If you want to read more on running an API and your web server within the same hapi project, you might scan through those tutorials:
https://futurestud.io/tutorials/hapi-how-to-run-separate-frontend-and-backend-servers-within-one-project
https://futurestud.io/tutorials/hapi-how-to-use-server-labels
Hope that helps!
I'm trying to setup my own peerJS server following the readme on https://github.com/peers/peerjs-server#combining-with-existing-express-app
my code on server
port = process.env.PORT or 8080
http = require 'http'
express = require 'express'
app = express()
server = http.createServer app
app.use '/peerjs', ExpressPeerServer server, debug : on
server.listen port
server.listen 9000
my code on client
peer = new Peer
host : 'localhost'
port : 9000
secure : no
config :
iceServers : [ url : 'stun:stun.l.google.com:19302' ]
I get this error on client console
GET http://localhost:9000/peerjs/peerjs/id?ts=14150106969530.4679094860330224 net::ERR_CONNECTION_REFUSED
In case you are still looking for a way to establish your own peer server, then here is what I have done to make it work for me.
server.js
// initialize express
var express = require('express');
var app = express();
// create express peer server
var ExpressPeerServer = require('peer').ExpressPeerServer;
var options = {
debug: true
}
// create a http server instance to listen to request
var server = require('http').createServer(app);
// peerjs is the path that the peerjs server will be connected to.
app.use('/peerjs', ExpressPeerServer(server, options));
// Now listen to your ip and port.
server.listen(8878, "192.168.1.14");
Client side code
I guess, you should not have much problem in this, but if you are wondering what to put for certain parameters, then here is the initialization of the peer object:
var peer = new Peer({
host: '192.168.1.14',
port: 8878,
path: '/peerjs',
config: { 'iceServers': [
{ url: 'stun:stun01.sipphone.com' },
{ url: 'stun:stun.ekiga.net' },
{ url: 'stun:stunserver.org' },
{ url: 'stun:stun.softjoys.com' },
{ url: 'stun:stun.voiparound.com' },
{ url: 'stun:stun.voipbuster.com' },
{ url: 'stun:stun.voipstunt.com' },
{ url: 'stun:stun.voxgratia.org' },
{ url: 'stun:stun.xten.com' },
{
url: 'turn:192.158.29.39:3478?transport=udp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
},
{
url: 'turn:192.158.29.39:3478?transport=tcp',
credential: 'JZEOEt2V3Qb0y27GRntt2u2PAYA=',
username: '28224511:1379330808'
}
]
},
debug: 3
});
This should help you to establish the connection.
One have to set a path to the server on their client side like this:
var peer = new Peer('your_peer_id', {host: 'localhost', port: 9000, path: '/peerjs'});
(this path has to be the same with the routing path to your ExpressPeerServer)
I am trying to run the following script on OpenShift SSH terminal
var deployd = require('deployd');
var server = deployd({
port: process.env.PORT || 5000,
env: 'production',
db: {
host: 'ds030607.mongolab.com',
port: '30607',
name: 'kheapdata',
credentials: {
username: 'admin',
password: 'admin'
}
}
});
server.listen();
server.on('listening', function() {
console.log("Server is listening");
});
server.on('error', function(err) {
console.error(err);
process.nextTick(function() { // Give the server a chance to return an error
process.exit();
});
});
like this node production.js but geting this error
{ [Error: listen EACCES] code: 'EACCES', errno: 'EACCES', syscall: 'listen' }
Please me to resolve this issue.
here is the new script
//production.js
var deployd = require('deployd');
var server = deployd({
// port: process.env.PORT || 5000,
env: 'production',
db: { host: '127.5.233.2',
port: '27017',
name: 'test',
credentials: { username: 'admin', password: '4mA51PDYnL89' } } });
server.listen('8080','127.5.233.1');
server.on('listening', function() { console.log("Server is listening"); });
server.on('error', function(err) { console.error(err); process.nextTick(function() {
// Give the server a chance to return an error
process.exit();
});
});
It looks like you're using the wrong env variable port: process.env.PORT || 5000. Try sshing into your gear and run env to locate the right environment variable. Since it looks like you're trying to connect to mongo try running env | grep -i mongo in your gear.
Looks like you need to bind to process.env.OPENSHIFT_NODEJS_IP and process.env.OPENSHIFT_NODEJS_PORT, looks like your script is trying to bind to 0.0.0.0 which is not allowed, and possibly the wrong port also. Also looks like you are connecting to an outside mongodb service, so you won't have any mongo environment variables in your gear.