gRpc server on node js, deploy to azure web app service CANCELLED error

I have created a gRPC server in my Node.js app alongside my Express.js server

const server = getServer();
server.bindAsync(
`0.0.0.0:${process.env.HTTP20_ONLY_PORT}`,
grpc.ServerCredentials.createInsecure(),
(err, port) =\> {
if (err) {
console.error(err);
return;
}
console.log(`gRPC server started on port ${port}`);
server.start();
}
);

const getServer = () =\> {
const server = new grpc.Server();
server.addService(dataCalculatorPackage.DataCalculatorService.service, {
PerformanceDataByPlanAndForcesIds:
getPerformanceDataByPlanIdAndForceIdByCall,
} as DataCalculatorServiceHandlers);
return server;
};

It appears that the gRPC server is up and running.

I enabled the HTTP version to 2 and set the HTTP 2.0 Proxy to gRPC only. I added the HTTP20_ONLY_PORT to the environment variables.

For my gRPC client, I am using Nest.js, and I establish the connection with the Azure service URL.

const packageDef = loadSync(path.resolve(\__dirname, PROTO_FILE), {
keepCase: true,
longs: String,
enums: String,
defaults: true,
oneofs: true,
});
const grpcObj = loadPackageDefinition(packageDef) as unknown as ProtoGrpcType;

this.grpcClient = new grpcObj.dataCalculatorPackage.DataCalculatorService(
`azurewebsite.url.com`,
credentials.createInsecure(),
);

However, when I run the request, I receive the following cancellation error:

[ERROR] 29 - 02/19/2024, 10:34:35 AM ERROR [ExceptionsHandler] 1 CANCELLED: Call cancelled 2024-02-19T10:34:35.863451309Z: [ERROR] Error: 1 CANCELLED: Call cancelled ...
I have also tried running the same images in my Docker, and it seems to work well. If anyone has any ideas, I would appreciate it.

Thanks!

Upvotes: 0

Views: 252

Answers (1)

Sampath
Sampath

Reputation: 3591

The "CANCELLED" error you're encountering when deploying your gRPC server on Node.js to Azure Web App Service due to Server-Side Issues and Configuration Mismatches

The code below communicates with the server via gRPC, while also having an Express.js server handling HTTP requests.

I followed Configure gRPC on App Service - Azure App Service.

  • Code taken from git.

grpc_server.js

var PROTO_PATH = __dirname + '/protos/helloworld.proto';

var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true
    });
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

var portgRPC = process.env.portHttp2 || 8585

function sayHello(call, callback) {
    callback(null, { message: 'Hello ' + call.request.name });
}

function main() {
    var server = new grpc.Server();
    server.addService(hello_proto.Greeter.service, { sayHello: sayHello });
    server.bindAsync(`0.0.0.0:${portgRPC}`, grpc.ServerCredentials.createInsecure(), () => {
        console.log(`gRPC server listening on ${portgRPC}`)
        server.start();
    });
}

main();

grpc_client.js

var PROTO_PATH = __dirname + '/protos/helloworld.proto';

var parseArgs = require('minimist');
var grpc = require('@grpc/grpc-js');
var protoLoader = require('@grpc/proto-loader');
var packageDefinition = protoLoader.loadSync(
    PROTO_PATH,
    {
        keepCase: true,
        longs: String,
        enums: String,
        defaults: true,
        oneofs: true
    });
var hello_proto = grpc.loadPackageDefinition(packageDefinition).helloworld;

function main() {
    var argv = parseArgs(process.argv.slice(2), {
        string: 'target'
    });
    var target;
    if (argv.target) {
        target = argv.target;
    } else {
        target = 'localhost:8585'
        // target = '<your-app-name>.azurewebsites.net'
    }
    var client = new hello_proto.Greeter(target,
        grpc.credentials.createInsecure());
    var user;
    if (argv._.length > 0) {
        user = argv._[0];
    } else {
        user = 'world';
    }![enter image description here](https://i.imgur.com/sJVkGys.png)
    client.sayHello({ name: user }, function (err, response) {
        console.log('Greeting:', response.message);
    });
}

main();





enter image description here

Web App Configuration:

  • Navigate to your web app's Configuration under Settings.

  • Click on the General Settings tab and scroll down to Platform settings.

  • Set the HTTP version to 2.0.

  • Enable the HTTP 2.0 Proxy.

  • Add an application setting named HTTP20_ONLY_PORT with the value 8282.

  • Update the target variable in app.js to your web app's URL (e.g., <your-app-name>.azurewebsites.net).

Output:

enter image description here

Upvotes: 0

Related Questions