Search code examples
spring-bootmicroservices

which is the best API gateway for micro services using spring?


I am trying to build a simple application with microservices architecture. Below are the details about 3 microservices I have created.

1] Customer.
       database: mongodb
       server  : embeded tomcat server.
       port    : 8081
2] vendor.
       database: mongodb
       server  : embeded tomcat server.
       port    : 8082
3] product.
       database: mongodb
       server  : embeded tomcat server.
       port    : 8083

All the 3 micros runs on an embeded tomcat server. Now I want to create a common gateway for all these micros [API gateway]. which help me to route my request based on the request I get for example:- for example if I get a request of http://hostname:port_of_gateway/customer. on reading this I need to route the request tom my customer micro and fetch its response and send it back to client. Which of the spring tool I can use to achieve this?


Solution

  • Because your requirements are quite simple you can implement such a gateway by yourself. Here's an example.

    But if you really want to use some Spring solution you can try to use Spring Cloud Netflix which is a part of Spring Cloud umbrella project. It includes router and filter features which in turn based on Netflix Zuul gateway service.

    Note that this is not a complete standalone application but a library. Therefore you still should create another microservice that would act as API gateway in your application. To make it a gateway you should just add @EnableZuulProxy annotation to the same class that has @SrpingBootApplication annotation. You can find a very good example here.

    Please also note that you should somehow inform the gateway about your microservices' addresses for redirection. It can be done in two general ways:

    1. By statically defining the addresses in gateway microservice's configuration;
    2. By applying service discovery pattern in conjunction with e.g. Netflix Eureka service registry.

    The 1st approach is easy and straightforward but is not very well for large number of microservices and/or when microservices' locations can change dynamically (e.g. due to auto-scaling).
    The 2nd approach requires additional component - service registry - and needs modification of other microservices (to let them register themselves in the registry). This is quite more complicated approach but is the only possible in case of complex architecture. Simple yet expressive example can be found in the same article.

    UPDATE (January'19)

    As of December 2018 the Spring Cloud team announced that almost all Netflix components in Spring Cloud (except Eureka) entered maintenance mode. It means that for the next year they won't receive any feature updates (only bugs and security fixes).
    There are replacements for all the affected components, including Netflix Zuul aforementioned above. So please consider using Spring Cloud Gateway instead of it in new projects.