Move Node healthchecks to gRPC
This commit is contained in:
parent
3e90b73464
commit
e4d681ee65
8 changed files with 47 additions and 23 deletions
|
@ -30,13 +30,11 @@ spec:
|
||||||
- name: grpc
|
- name: grpc
|
||||||
containerPort: 7000
|
containerPort: 7000
|
||||||
readinessProbe:
|
readinessProbe:
|
||||||
periodSeconds: 5
|
exec:
|
||||||
tcpSocket:
|
command: ["/bin/grpc_health_probe", "-addr=:7000"]
|
||||||
port: 7000
|
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
periodSeconds: 5
|
exec:
|
||||||
tcpSocket:
|
command: ["/bin/grpc_health_probe", "-addr=:7000"]
|
||||||
port: 7000
|
|
||||||
resources:
|
resources:
|
||||||
requests:
|
requests:
|
||||||
cpu: 100m
|
cpu: 100m
|
||||||
|
|
|
@ -29,13 +29,11 @@ spec:
|
||||||
ports:
|
ports:
|
||||||
- containerPort: 50051
|
- containerPort: 50051
|
||||||
readinessProbe:
|
readinessProbe:
|
||||||
periodSeconds: 5
|
exec:
|
||||||
tcpSocket:
|
command: ["/bin/grpc_health_probe", "-addr=:50051"]
|
||||||
port: 50051
|
|
||||||
livenessProbe:
|
livenessProbe:
|
||||||
periodSeconds: 5
|
exec:
|
||||||
tcpSocket:
|
command: ["/bin/grpc_health_probe", "-addr=:50051"]
|
||||||
port: 50051
|
|
||||||
resources:
|
resources:
|
||||||
requests:
|
requests:
|
||||||
cpu: 100m
|
cpu: 100m
|
||||||
|
|
|
@ -120,6 +120,7 @@ message Address {
|
||||||
service CurrencyService {
|
service CurrencyService {
|
||||||
rpc GetSupportedCurrencies(Empty) returns (GetSupportedCurrenciesResponse) {}
|
rpc GetSupportedCurrencies(Empty) returns (GetSupportedCurrenciesResponse) {}
|
||||||
rpc Convert(CurrencyConversionRequest) returns (Money) {}
|
rpc Convert(CurrencyConversionRequest) returns (Money) {}
|
||||||
|
rpc Check(Empty) returns (Empty) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Represents an amount of money with its currency type.
|
// Represents an amount of money with its currency type.
|
||||||
|
@ -156,6 +157,7 @@ message CurrencyConversionRequest {
|
||||||
|
|
||||||
service PaymentService {
|
service PaymentService {
|
||||||
rpc Charge(ChargeRequest) returns (ChargeResponse) {}
|
rpc Charge(ChargeRequest) returns (ChargeResponse) {}
|
||||||
|
rpc Check(Empty) returns (Empty) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
message CreditCardInfo {
|
message CreditCardInfo {
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
FROM gcr.io/microservices-demo-app/grpc-health-probe:1.0 AS probe
|
||||||
FROM node:8
|
FROM node:8
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
COPY package*.json ./
|
COPY package*.json ./
|
||||||
RUN npm install --only=production
|
RUN npm install --only=production
|
||||||
COPY . .
|
COPY . .
|
||||||
|
COPY --from=probe /bin/grpc_health_probe /bin/grpc_health_probe
|
||||||
EXPOSE 7000
|
EXPOSE 7000
|
||||||
CMD [ "node", "server.js" ]
|
CMD [ "node", "server.js" ]
|
||||||
|
|
|
@ -49,10 +49,18 @@ client.getSupportedCurrencies({}, (err, response) => {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
client.convert(request, function (err, response) {
|
client.convert(request, (err, response) => {
|
||||||
if (err) {
|
if (err) {
|
||||||
console.error(`Error in convert: ${err}`);
|
console.error(`Error in convert: ${err}`);
|
||||||
} else {
|
} else {
|
||||||
console.log(`Convert: ${_moneyToString(request.from)} to ${_moneyToString(response)}`);
|
console.log(`Convert: ${_moneyToString(request.from)} to ${_moneyToString(response)}`);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
client.check({}, (err, response) => {
|
||||||
|
if (err) {
|
||||||
|
console.log(`Error in check: ${err}`);
|
||||||
|
} else {
|
||||||
|
console.log(`Health check successful!`);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
|
@ -16,17 +16,17 @@
|
||||||
|
|
||||||
require('@google-cloud/profiler').start({
|
require('@google-cloud/profiler').start({
|
||||||
serviceContext: {
|
serviceContext: {
|
||||||
service: 'currencyservice',
|
service: 'currencyservice',
|
||||||
version: '1.0.0'
|
version: '1.0.0'
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
require('@google-cloud/trace-agent').start();
|
require('@google-cloud/trace-agent').start();
|
||||||
require('@google-cloud/debug-agent').start({
|
require('@google-cloud/debug-agent').start({
|
||||||
serviceContext: {
|
serviceContext: {
|
||||||
service: 'currencyservice',
|
service: 'currencyservice',
|
||||||
version: 'VERSION'
|
version: 'VERSION'
|
||||||
}
|
}
|
||||||
})
|
});
|
||||||
|
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const grpc = require('grpc');
|
const grpc = require('grpc');
|
||||||
|
@ -116,8 +116,8 @@ function convert (call, callback) {
|
||||||
nanos: euros.nanos * data[request.to_code]
|
nanos: euros.nanos * data[request.to_code]
|
||||||
});
|
});
|
||||||
|
|
||||||
result.units = Math.floor(result.units)
|
result.units = Math.floor(result.units);
|
||||||
result.nanos = Math.floor(result.nanos)
|
result.nanos = Math.floor(result.nanos);
|
||||||
result.currency_code = request.to_code;
|
result.currency_code = request.to_code;
|
||||||
|
|
||||||
console.log(`conversion request successful`);
|
console.log(`conversion request successful`);
|
||||||
|
@ -130,6 +130,13 @@ function convert (call, callback) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Endpoint for health checks
|
||||||
|
*/
|
||||||
|
function check (call, callback) {
|
||||||
|
callback(null);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Starts an RPC server that receives requests for the
|
* Starts an RPC server that receives requests for the
|
||||||
* CurrencyConverter service at the sample server port
|
* CurrencyConverter service at the sample server port
|
||||||
|
@ -137,7 +144,7 @@ function convert (call, callback) {
|
||||||
function main () {
|
function main () {
|
||||||
console.log(`Starting gRPC server on port ${PORT}...`);
|
console.log(`Starting gRPC server on port ${PORT}...`);
|
||||||
const server = new grpc.Server();
|
const server = new grpc.Server();
|
||||||
server.addService(shopProto.CurrencyService.service, {getSupportedCurrencies, convert});
|
server.addService(shopProto.CurrencyService.service, {getSupportedCurrencies, convert, check});
|
||||||
server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure());
|
server.bind(`0.0.0.0:${PORT}`, grpc.ServerCredentials.createInsecure());
|
||||||
server.start();
|
server.start();
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,3 +1,5 @@
|
||||||
|
FROM gcr.io/microservices-demo-app/grpc-health-probe:1.0 AS probe
|
||||||
|
|
||||||
FROM node:8
|
FROM node:8
|
||||||
|
|
||||||
WORKDIR /usr/src/app
|
WORKDIR /usr/src/app
|
||||||
|
@ -8,6 +10,8 @@ RUN npm install --only=production
|
||||||
|
|
||||||
COPY . .
|
COPY . .
|
||||||
|
|
||||||
|
COPY --from=probe /bin/grpc_health_probe /bin/grpc_health_probe
|
||||||
|
|
||||||
EXPOSE 50051
|
EXPOSE 50051
|
||||||
|
|
||||||
CMD [ "node", "index.js" ]
|
CMD [ "node", "index.js" ]
|
||||||
|
|
|
@ -41,6 +41,10 @@ class HipsterShopServer {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static CheckHandler(call, callback) {
|
||||||
|
callback(null);
|
||||||
|
}
|
||||||
|
|
||||||
listen() {
|
listen() {
|
||||||
this.server.bind(`0.0.0.0:${this.port}`, grpc.ServerCredentials.createInsecure());
|
this.server.bind(`0.0.0.0:${this.port}`, grpc.ServerCredentials.createInsecure());
|
||||||
console.log(`PaymentService grpc server listening on ${this.port}`);
|
console.log(`PaymentService grpc server listening on ${this.port}`);
|
||||||
|
@ -56,7 +60,7 @@ class HipsterShopServer {
|
||||||
enums: String,
|
enums: String,
|
||||||
defaults: true,
|
defaults: true,
|
||||||
oneofs: true,
|
oneofs: true,
|
||||||
},
|
}
|
||||||
);
|
);
|
||||||
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
|
const protoDescriptor = grpc.loadPackageDefinition(packageDefinition);
|
||||||
const hipsterShopPackage = protoDescriptor.hipstershop;
|
const hipsterShopPackage = protoDescriptor.hipstershop;
|
||||||
|
@ -69,7 +73,8 @@ class HipsterShopServer {
|
||||||
service,
|
service,
|
||||||
{
|
{
|
||||||
charge: HipsterShopServer.ChargeServiceHandler.bind(this),
|
charge: HipsterShopServer.ChargeServiceHandler.bind(this),
|
||||||
},
|
check: HipsterShopServer.CheckHandler.bind(this)
|
||||||
|
}
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue