Microservices communication JDBC SQL VS REST [closed]

有些话、适合烂在心里 提交于 2019-12-11 08:57:15

问题


which is the best way between these two approches that allow two microservices to exchange data

1- Via Rest call.

2- Each microsevice expose its related data as a database's view ,so that it can be reached by other microservices using Spring JDBC template or JPA.

Notice that each microservice has its own (private) tables in the same database schema.

Thanks,


回答1:


I would say that from a domain driven design perspective (and microservices could be considered as domains), other domains should not know anything about how your data is stored/structured (Bounded Context). Therefore I would vote for REST. Another point would be, what if your table/view structure changes? this would cause breaking changes in other microservices. With REST you can change the underlying code of your routes without bothering your consumer. Direct Database queries would be needed if you have to use stored procedures (or other database related performance tweaks) for better performance.




回答2:


The world is imperfect, but in a perfect world your microservices should rarely (if ever) communicate directly with each other. One microservice having knowledge of another inherently couples them more tightly than is preferable for this distributed architecture. This coupling affects CI/CD, reduces fault tolerance and leaks domain information outside each service.

In our system, the only microservice accessed by (nearly) all others is the Authorization service so that if required, each microservice can validate the credentials it receives for a specific requested action. All other communication that occurs between services is asynchronous and passed along over our Integration Bus (RabbitMQ in our case).

Between the two options you presented, REST is probably better because it adds at least some abstraction between the services, but you might consider taking a hard look at your modeling to see if you can reduce dependencies between services to eliminate the need. Decent (though old) article on Auth0 here about dependencies and here is a good (long) talk about this issue from a Spring Data project lead.




回答3:


It depends.

The REST API provides I looser coupling between the two services and is usually the preferred way.

The database view would make sense if you have many queries and the performance calling the REST API could be an issue.




回答4:


I prefer REST in order to exchange data across different microservices, probably you can adapt something similar in your application.However I wrote this in Java you can write piece of code for any language of your choice.

Hope this helps.

public final class MicroServiceDelegator {

    private MicroServiceDelegator() {
    }

    /**
     * Calls the concerned microservice and gets the response JSON.
     * 
     * @param request       the request object containing the request information
     * @param microserviceUri       the URI to make a request to
     * @param requestMethod specifies either GET or POST request method
     * @return JSON string response from the micro service
     * @throws Exception

     */
    public static final String callMicroService(final HttpServletRequest request, final String microserviceUri, final RequestMethod requestMethod)
            throws Exception {

      String responseBody;


    // Logic to identify type of request GET/POST
    // Build any HttpClient
    // Execute the Client

    // Build responseBody 

    // Convert response to JSON

        return responseBody;
    }
  }

This can be used in any microservice, for example

@ResponseBody
    @RequestMapping(method = RequestMethod.POST, path = "/all", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String getAllProducts(final HttpServletRequest request, final HttpServletResponse response) {

        String responseString = "";
        try {
            responseString = MicroServiceDelegator.callMicroService(request,
                "http://products-microservice" + "/all-products",
                RequestMethod.POST);

        } catch (Exception e) {
            log.warn(e)
        }
        return responseString;
    }


来源:https://stackoverflow.com/questions/53934850/microservices-communication-jdbc-sql-vs-rest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!