JamesMessinger / swagger-server

No longer maintained. Please use https://github.com/BigstickCarpet/swagger-express-middleware
MIT License
149 stars 47 forks source link

does swagger-server support microservices defined through individual swagger specs? #19

Open phani1kumar opened 8 years ago

phani1kumar commented 8 years ago

Hi,

Thank you for a wonderful API. I am currently developing microservices using swagger 2.0 spec: meaning, I've one complete swagger spec for each of my microservice. Say, I've

  1. doctor microservice to deal with doctor CRUD operations
  2. patient microservice to deal with patient CRUD operations
  3. appointment microservice to deal with appointment CRUD operations

Now, is it possible to load all these three YAML files (swagger 2.0 specs) into one server running at a specific port (say 8000)?

Thanks and Regards, Phani

JamesMessinger commented 8 years ago

Yeah, there are a couple ways you can do that.

  1. Use Node's cluster feature, which lets you partition your app across multiple sub-processes. Each process can run a separate instance of Swagger Server, but they can all share the same port number. This is fault-tolerant and gives you true multi-threading (normally Node apps are single threaded)
  2. Use Express's mounting feature, which lets you mount compose an Express app of multiple sub-apps. Multiple instances of Swagger Server can be mounted as sub-apps to a parent Express app. This doesn't give you the fault-tolerance and multi-threading benefits of option #1 though.
phani1kumar commented 8 years ago

Thank you very much @BigstickCarpet for your suggestions. In the approach 1, is it that, all the microservices use the same datastore? or they use independent datastores! My question is can I setup the dummy data once for all microservices or should I set it up independently?

JamesMessinger commented 8 years ago

Each Node cluster runs in a separate process, so there is no shared memory. So if you're using the MemoryDataStore, then you'll need to maintain separate data for each cluster. If you're using the FileDataStore, then all clusters can share the same data store. Although, if your server is handling many simultaneous requests, then you could run into locking issues, where multiple clusters are trying to read/write to the same JSON file at the same time.

phani1kumar commented 8 years ago

Thank you very much for your quick answer @BigstickCarpet. I'll try the above two options and check how I could achieve my use case.

feikiss commented 8 years ago

@BigstickCarpet Actually, I have tried the #1 solution but failed... I've also tried to do it with the sample in the Node's cluster feature. The following is my codes:

// Set the DEBUG environment variable to enable debug output
process.env.DEBUG = 'swagger:*';
process.chdir(__dirname);

// Create a Swagger Server app from the PetStore.yaml file
var swaggerServer = require('swagger-server');
var arr = ["../swagger.json","../swagger-petstore.json"]
var cluster = require('cluster');

if (cluster.isMaster) {
    console.log('[master] ' + "start master...");

    for (var i = 0; i < arr.length; i++) {
        cluster.fork({"index":i});
    }

    cluster.on('listening', function (worker, address) {
        console.log('[master] ' + 'listening: worker' + worker.id + ',pid:' + worker.process.pid + ', Address:' + address.address + ":" + address.port);
    });
    cluster.on('fork',function (worker){
        // timeouts[worker.id] = setTimeout(errorMsg, 2000);

    });

} else if (cluster.isWorker) {
    var id = cluster.worker.id;
    var content = arr[id-1]

    console.log('[worker] ' + "start worker ..." + id+", index:"+cluster.worker.index+", content:"+content);
    var app = swaggerServer(content);

// Start listening on port 8000
    app.listen(8001, function() {
        console.log('The Swagger Pet Store is now running at http://localhost:8000');
    })
}

In this case, neither of the two swagger files can work... Is there anything wrong I configured?

raphodn commented 6 years ago

on the subject of reading of multiple swagger files, I can share my implementation.

The idea is to write a root swagger file, list all the paths you want, and use $ref to reference your child swagger files and their logic.

swagger: "2.0"
info:
  description: ""
  version: "1.0"
paths:
  /login:
    $ref: authenticationSwagger.json#/paths/~1login
  /users/info:
    $ref: userSwagger.json#/paths/~1users~1info
  /books:
    $ref: bookSwagger.json#/paths/~1books