Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ packages/core/* @bajtos @raymondfeng @kjdelisle
packages/example-getting-started/* @bajtos @kjdelisle
packages/example-hello-world/* @b-admike
packages/example-log-extension/* @virkt25
packages/example-microservices/* @raymondfeng @virkt25 @kjdelisle
packages/example-rpc-server/* @kjdelisle
packages/metadata/* @raymondfeng
packages/openapi-spec/* @bajtos @jannyHou
Expand Down
5 changes: 4 additions & 1 deletion lerna.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
{
"lerna": "2.5.1",
"packages": ["packages/*"],
"packages": [
"packages/*",
"packages/example-microservices/services/*"
],
"command": {
"publish": {
"conventionalCommits": true
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"build:full": "npm run clean:lerna && npm run bootstrap && npm run build && npm run mocha && npm run lint",
"pretest": "npm run clean && npm run build:current",
"test": "node packages/build/bin/run-nyc npm run mocha",
"mocha": "node packages/build/bin/run-mocha \"packages/*/DIST/test/**/*.js\" \"packages/cli/test/*.js\"",
"mocha": "node packages/build/bin/run-mocha \"packages/*/DIST/test/**/*.js\" \"packages/cli/test/*.js\" && node packages/example-microservices/bin/test",
"posttest": "npm run lint"
},
"config": {
Expand Down
1 change: 1 addition & 0 deletions packages/cli/generators/example/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ const EXAMPLES = {
'An application and tutorial on how to build with LoopBack 4.',
'hello-world': 'A simple hello-world Application using LoopBack 4',
'log-extension': 'An example extension project for LoopBack 4',
'microservices': 'How to build scalable microservices using LoopBack',
'rpc-server': 'A basic RPC server using a made-up protocol.',
};
Object.freeze(EXAMPLES);
Expand Down
25 changes: 25 additions & 0 deletions packages/example-microservices/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Copyright (c) IBM Corp. 2018. All Rights Reserved.
Node module: @loopback/example-microservices
This project is licensed under the MIT License, full text below.

--------

MIT license

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
57 changes: 57 additions & 0 deletions packages/example-microservices/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
# loopback-next-example

[![Gitter](https://img.shields.io/gitter/room/nwjs/nw.js.svg)](https://gitter.im/strongloop/loopback)

How to build scalable microservices using LoopBack.next.

> What's the difference between LoopBack.next and the current version of
> Loopback? See [LoopBack 3 vs LoopBack 4](https://github.com/strongloop/loopback-next/wiki/FAQ#loopback-3-vs-loopback-4).

## Installation

Make sure you have the following installed:

- [Node.js](https://nodejs.org/en/download/) at v6.x or greater

1. Install the new loopback CLI toolkit.
```
npm i -g @loopback/cli
```

2. Download the "microservices" example.
```
lb4 example microservices
```

3. Switch to the directory and install dependencies.
```
cd loopback-example-microservices && npm install
```

## Basic use

```shell
# start all microservices
npm start

# perform GET request to retrieve account summary data
curl localhost:3000/account/summary?accountNumber=CHK52321122 # or npm test

# perform GET request to retrieve account data
curl localhost:3001/accounts?accountNumber=CHK52321122

# stop all microservices
npm stop
```

> Helper scripts for the above commands are in [`/bin`](https://github.com/strongloop/loopback-next-example/tree/master/bin)
directory.

# Contributing

- [Guidelines](https://github.com/strongloop/loopback-next/wiki/Contributing)
- [Join the team](https://github.com/strongloop/loopback-next/wiki/Contributing#join-the-team)

# License

MIT
3 changes: 3 additions & 0 deletions packages/example-microservices/bin/get-account
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
curl -s "http://localhost:3001/accounts?accountNumber=CHK52321122" | jq .

2 changes: 2 additions & 0 deletions packages/example-microservices/bin/get-account-summary
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/usr/bin/env bash
curl -s "http://localhost:3000/account/summary?accountNumber=CHK52321122" | jq .
53 changes: 53 additions & 0 deletions packages/example-microservices/bin/install.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env node
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const fs = require('fs');
const path = require('path');
const spawn = require('child_process').spawn;

const servicesRoot = path.resolve(__dirname, '..', 'services');
const services = fs.readdirSync(servicesRoot);

if (process.env.LERNA_ROOT_PATH) {
console.log(
'**Lerna was detected, skipping "npm install" in individual services**'
);
process.exit(0);
}

let p = Promise.resolve();
for (const s of services) {
p = p.then(() => {
return execNpmInstall(`services/${s}`);
});
}

p.catch(err => {
console.error(err);
process.exit(1);
});

function execNpmInstall(cwd) {
console.log(`\n=== Running "npm install" in ${cwd} ===\n`);
return new Promise((resolve, reject) => {
const child = spawn('npm', ['install'], {
cwd: cwd,
stdio: 'inherit',
// On Windows, `npm` is not an executable filea
// we have to execute it via shell
shell: true,
});
child.once('error', err => reject(err));
child.once('exit', (code, signal) => {
if (code || signal)
reject(`npm install failed: exit code ${code} signal ${signal}`);
else
resolve();
});
});
}
21 changes: 21 additions & 0 deletions packages/example-microservices/bin/start
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/usr/bin/env bash
./bin/stop

echo "Starting microservices..."
cd services/account
ts-node index.ts lb-next-account-micro-serv &
echo "Started Account microservice, PID: $!"
cd ../customer && \
ts-node index.ts lb-next-customer-micro-serv &
echo "Started Customer microservice, PID: $!"
cd ../transaction && \
ts-node index.ts lb-next-transaction-micro-serv &
echo "Started Transaction microservice, PID: $!"
cd ../facade && \
ts-node index.ts lb-next-facade-micro-serv &
echo "Started Facade microservice, PID: $!"

sleep 5

echo 'All microservices started successfully.'
echo 'To test the application, run "npm test".'
3 changes: 3 additions & 0 deletions packages/example-microservices/bin/stop
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env bash
ps -ef | grep "[l]b-next" | awk '{print $2}' | xargs kill -9
echo "All microservices stopped successfully."
56 changes: 56 additions & 0 deletions packages/example-microservices/bin/test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#!/usr/bin/env node
// Copyright IBM Corp. 2018. All Rights Reserved.
// Node module: @loopback/example-microservices
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

'use strict';

const Promise = require('bluebird');
const exec = require('child_process').execSync;
const spawn = require('child_process').spawn;
const fs = Promise.promisifyAll(require('fs'));
const path = require('path');

const cmd = path.resolve(__dirname, '..', 'node_modules', '.bin', '_mocha');
const args = ['--compilers', 'ts:ts-node/register,tsx:ts-node/register'];

const services = path.resolve(__dirname, '..', 'services');
return fs
.readdirAsync(services)
.then(folders => {
return Promise.each(folders, f => {
const dir = path.resolve(services, f);
return fs
.readdirAsync(dir)
.then(subfolders => {
if (subfolders.indexOf('test') > -1) {
return new Promise((resolve, reject) => {
console.log('RUN TESTS - %s:', f);
const testArgs = args.push(path.resolve(dir, 'test/**/*test.ts'));
const test = spawn(cmd, args, {stdio: 'inherit'});
test.on('close', code => {
if (code) {
return reject(code);
} else {
console.log('TEST SUCCESS - %s', f);
return resolve();
}
});
});
} else {
console.log('No "test" folder was found in %s', f);
return Promise.resolve();
}
})
.catch(code => {
return Promise.reject(`TESTS FAILED - ${f}, exit code ${code}`);
});
}).then(() => {
console.log('TESTS COMPLETE');
});
})
.catch(err => {
console.log(err);
process.exit(1);
});
40 changes: 40 additions & 0 deletions packages/example-microservices/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"name": "@loopback/example-microservices",
"version": "4.0.0-alpha.0",
"description": "How to build scalable microservices using LoopBack",
"private": true,
"main": "facade/index.js",
"scripts": {
"postinstall": "node bin/install.js",
"restart": "npm run stop && npm run start",
"start": "bin/start",
"stop": "bin/stop",
"test": "npm run mocha",
"mocha": "node bin/test.js"
},
"engines": {
"node": ">=6"
},
"keywords": [
"loopback-next",
"example"
],
"author": "IBM",
"license": "MIT",
"repository": {
"type": "git",
"url": "git+https://github.com/strongloop/loopback-next-example.git"
},
"bugs": {
"url": "https://github.com/strongloop/loopback-next-example/issues"
},
"homepage": "https://github.com/strongloop/loopback-next-example#readme",
"dependencies": {
"bluebird": "^3.5.0",
"ts-node": "^3.1.0",
"typescript": "^2.4.1"
},
"devDependencies": {
"mocha": "^4.0.0"
}
}
Loading