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