mardi 5 février 2019

Better design approach for invoking multiple main class from single microservice

We are using micro-service(MS) architecture in company. There is one DB MS which gets JSON wrapped in Response class and it sends it to another MS OutputFormatter which formats JSON being sent in a different way based on business type(as of now there are 2 but it is supposed to grow a lot in future) defined in one of the fields in Response class. This MS returns same class's new object with formatted response back to DB MS which then finally persists it in DB. Right now I have created:

  • 2 Main class one for each business type say Main1 and Main2.
  • One interface OutputService which has implementors for each business type say Bus1OutputService and Bus1OutputService
  • Separate YML config file linked through main classes. I am running both classes on different port.
  • One controller say RequestController that has end point to be triggered from DB MS and it receives Response object as a RequestBody parameter

My question is around controller only. I have used autowiring as following:

@RestController
public class RequestController {

    @Autowired
    @Qualifier("Bus1OutputService")
    OutputService oService1;

    @Autowired
    @Qualifier("Bus2OutputService")
    OutputService oService2;

    @PostMapping("/generateOutput")
    public Response generateOutput(@RequestBody Response requestJSON) {
        String bType = requestJSON.getMetadata().get("BUSINESS_TYPE");

        Response formattedOResponse = null;
        switch (bType){
            case "Bus1":
                formattedOResponse = oService1.generateOutputJSON(requestJSON);
                break;
            case "Bus2":
                formattedOResponse = oService2.generateOutputJSON(requestJSON);
                break;
        }
        return formattedOResponse;
    }
}

If I run both main classes then 2 spring boot applications will be booted. All business logic is only in 1/2 service classes only.

In future, if new business type comes then we need to make changes in controller and create new main class, YML and service class accordingly for new business type but it won't affect 2 already running spring boot application for existing 2 business types. Is it a good design? We all developers signed up for this design but I am still researching on better approaches and wanted to ask the community also :)

I could have created different MS for each business type and runs it individually. But, maintaining on bit bucket, single click deployment and all would be hassle as application grows.

Aucun commentaire:

Enregistrer un commentaire