This mono repository contains a sample of micro-services architecture built on top of gRPC protocol and TypeScript node.js applications
Install protoc
for generating definitions based on .proto
files
brew install protobuf
protoc --version # Ensure compiler version is 3+
Prepare environment
yarn install
yarn lerna bootstrap
Build common packages, so we're able to use it for our services
yarn lerna run build
var all = require('@common/go-grpc')
var testServerHost = 'localhost:50051'
// var testServerHost = 'ecb-provider:50051'
var c3 = new all.ecbProvider.EcbProviderClient( testServerHost, all.createInsecure());
var r3 = await c3.GetRates(new all.currencyProvider.GetRatesRequest())
r3.toObject()
// inside converter container
var all = require('@common/go-grpc')
var testServerHost = '0.0.0.0:50052'
var c2 = new all.currencyConverter.CurrencyConverterClient( testServerHost, all.createInsecure());
var r2 = await c2.Convert(new all.currencyConverter.ConvertRequest({ sellAmount: 100, sellCurrency: 'USD', buyCurrency: 'GBP' }));
r2.toObject()
- For example, we want to create a new
logger
library. - Create a folder under
./packages/common/
path. For simplicity, just copy an existing lib and rename it.
mkdir ./packages/common/logger
- Go to the folder in the terminal
cd ./packages/common/logger
- Install dependencies
yarn install
- Make sure to define appropriate name in the package.json file:
"name": "@common/logger",
Let's follow a rule all common libraries have a prefix @common/
5. Create our library in a src/index.js
export const debug = (message: string) => console.debug(message);
export const info = (message: string) => console.info(message);
export const error = (message: string) => console.error(message);
export default { debug, info, error };
- Make sure it builds successfully withing a command:
yarn build
- Let's connect our newly created library somewhere in the existing service:
yarn lerna add @common/logger --scope=@grpc/ecb-provider
- The final step, we need to use the library inside ecb-provider service.
Let's amend file
./src/index.ts
:
import logger from '@common/logger';
logger.debug('service has started');
- Re-build currency-converter to ensure the is not issues
yarn build
Yay! 🎉 It works!
- For example, we want to create a new
crypto-compare-provider
service, which is another currency rate provider returning cryptocurrencies. - Create a folder under
./packages/services/grpc/crypto-compare-provider
path. For simplicity, just copy an existingecb-provider
and rename it.
mkdir ./packages/services/grpc/crypto-compare-provider
- Go to the folder in the terminal
cd ./packages/services/grpc/crypto-compare-provider
- Install dependencies
yarn install
- Make sure to define appropriate name in the package.json file:
"name": "@grpc/crypto-compare-provider",
Let's follow a rule - all grpc services have a prefix @grpc/
.
5. Create a service method file packages/services/grpc/crypto-provider/src/services/getRates.ts
import { currencyProvider } from '@common/go-grpc';
export default async (
_: currencyProvider.GetRatesRequest,
): Promise<currencyProvider.GetRatesResponse> => {
return new currencyProvider.GetRatesResponse({
rates: [],
baseCurrency: 'USD',
});
};
- So next we need to use this method inside server.ts
import { Server, LoadProtoOptions, currencyProvider } from '@common/go-grpc';
import getRates from './services/getRates';
const { PORT = 50051 } = process.env;
const protoOptions: LoadProtoOptions = {
path: `${__dirname}/../../../../../proto/crypto-compare-provider.proto`,
// this value should be equvalent to the one defined in *.proto file as "package cryptoCompareProvider;"
package: 'cryptoCompareProvider',
// this value should be equvalent to the one defined in *.proto file as "service CryptoCompareProvider"
service: 'CryptoCompareProvider',
};
const server = new Server(`0.0.0.0:${PORT}`, protoOptions);
server
.addService<currencyProvider.GetRatesRequest,
Promise<currencyProvider.GetRatesResponse>>('GetRates', getRates);
export default server;
- Make sure it builds successfully withing a command:
yarn build
- Start the service with the command:
yarn start