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

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?

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:
By statically defining the addresses in gateway microservice's configuration;
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.

Related

What to put as discoverable services when using Eureka?

I am following a course about microservice architecture using spring, covering netflix's eureka.
The clientui serves webpages and calls the 3 microservices when needed.
The config-server serves configuration for the 3 microservices from a git repo.
Of course the 3 microservices are registered as eureka clients.
My questions are :
should the config server and also be registered as an eureka client, or is there no benefit in doing so?
what about the clientui (which is the web entry point) ? can it be registered as an eureka client in order to benefit from load balancing system and if yes, how then should the app be accessed by clients?
About your first question :- Yes you can register config server as eureka client. Benefit of this will be that in terms of service management it will give you a single point of visibility of all the services. Also later if you try to expand your app in terms of distributed architecture and say you implement an api gateway like zuul, it will be easy for you to setup a fallback config server say if one config server goes down requests can be routed to other config server and so on.
About your second question :- Honestly speaking , I didn't understand it very well in first place. I have never seen any ui service registering to eureka so I am not very sure about this. Still if you have more doubts about it , you can let me know like is it a angular ui or is it a http based client or what.

Spring cloud gateway route with multiple instances and sticky session

I'm pretty much new to spring cloud gateway. I have configured routing with two different apps with 'path'. Now, I need some help/docs on
1. How to route to different instance of an app from spring cloud gateway?
2. How to enable sticky session?
My apps are not using spring boot/eureka. I do see that I can use lb://service-name if i'm using any discovery client (unfortunately that is not my case).
Thanks in advance.
IMHO:
How to route to different instance of an app from spring cloud gateway?
Routing to different instances is the basic job of what spring cloud gateway is doing. Spring cloud gateway implements the pattern named client side routing along with the ease of service discovery. So if you are not using any discovery server or your apps are not registered with any discovery server, you loose the dynamic discovery and routing feature, BUT still you can specify your server list (refer to Netflix Ribbon). Until then you can think about your routing strategy.
How to enable sticky session?
I suppose that's one requirement of your routing strategy - implement sticky session because you are not using shared session store. According to my limited knowledge of spring cloud gateway, sticky seems not being supported out of box. But it could be customized with a Filter, see shipped LoadBalancerClientFilter for reference.
Good luck!

Should we use api gateway(such as zuul) between microservices?

There is no doubt that API gateway should be the edge server to outside world.We are wondering that should we use API gateway in the communications between the microservices?
You can definitely use API gateway lets say for that matter (netflix -zuul) for inter-service calls, only thing of concern for you would be,
what happens when you start versioning your services, assuming you'll be using eureka as a naming server from which zuul gateway will fetch all registered services, but now in your case zuul will get two instances of your service (version previous and verison next) and ribbon will load balance the requests between the two, this point is already thoughtfully covered in
How to route in between microservices using Spring Cloud & Netflix OSS
Basically if you are familiar with BlueGreen Deployment model, implementing that would be a problem, surely there are proper workarounds for that as in defining/registering some metadata along with your previous and latest versions which would later be picked by ribbon client to route accordingly

Creating a Client/Service in Netflix Eureka

With regards to the Netflix Eureka Service Registry, I have setup the Eureka Netflix Server using Tomcat 8.0.35 successfully. I have also got the basic Example Service & Client to communicate with each other. I'm still new to Eureka and the management decision is to use Eureka with Spring as several new applications are written around the Spring framework.
I have been following the wiki in order to understand how the communication works (But with little to no success with registering services).. Eureka Github Wiki.
My question is: Do I need to create my own Eureka Service & Client in order to maintain a registry of about 50 cloud instances? (If so, can you please point me in the right direction).
I have hands on experience with Consul/RESTfull API, and have implemented Consul in production (using php, and qbit); however the Netflix Eureka Registry look's as though I need to learn eureka/spring + client/server java programming? I'm still getting used to the following terms.
Eureka Server (I successfully got this working using Tomcat8/JDK1.8 + the eureka.war)
Eureka Service (Some kind of stand-alone RESTfull service that queries the Eureka-Server and listens for client requests)
Eureka Client (Java snippet to be embedded into the Java servlets/jsp)?
Zuul (A type of routing/load-balancing app - similar to HaProxy ?)
Ribbon (A type of routing/load-balancing app - similar to HaProxy ?)
I would like to get the Service & Client configured for mostly non-AWS cloud instances. The Eureka Wiki is not very helpful when it comes to creating a working eureka service & client. Any help to point me in the right direction to implement a Eureka based RESTfull system would be helpful.
I suggest you to read this documentation about spring-cloud : http://cloud.spring.io/spring-cloud-static/spring-cloud.html
It should be a good start to setup a few simple spring-boot/spring-cloud services and start to use advanced tools like zuul/ribbon/hystrix ...
There is a simple example of distributed system using Spring Boot and Spring Cloud Netflix.
This project contains the following microservices:
requestor and responder which communicates via REST/HTTP
gateway microservice - Spring Boot app + Zuul
discovery microservice - Spring Boot app + Eureka
Moreover, requestor microservice uses Hystrix library.
As you can see, Spring Cloud provides a really good wrapper for Netflix solutions. As a result, you can start quickly with minimal configuration.

How to do service discovery for Spring Boot REST endpints

If I have multiple Spring Boot embedded tomcat containers and each can have service endpoints like
http://localhost:8080/employeeSelfService/getDetails
http://localhost:8081/employeeSelfService/getDetails
How can do load balancing using 2 micro services such that clients can hit any of the URL's mentioned based on some load balancing startegy
One option thats come to my mind is to use NetFlix Curator (or) have a apache webserver acting as reverse proxy but with apache, when you create new instances of your services, you will have have an entry of that service as a member in httpd.conf
Does Spring Boot provides any service discovery and load balancing mechanism ?
Spring Boot does not provide this feature, as it is already usually provided by a reverse proxy such as apache/nginx running in front of the Spring Boot server.
See here for an example here how the commercial version of nginx provides the functionality of dynamically scaling and reducing the upstream nodes.
So in this case it's for the dynamic instance, in this case the Spring Boot process to signal it's presence/unregister itself to the upstream server at initialization/shutdown.
See here how to do so in the case of nginx, this procedure will be different from server to server.
Arguably it's not really an application's role to manage its own load-balancing, and Spring Boot focuses on the implementation of an application (or service, equivalently). We have been thinking about whether we could provide features in Spring (Boot or otherwise) to make it easy to write your own load-balancer, or service registry app, but even then I don't think that was what the question was really about (or was it?).
If I interpret the question, and the example use case, literally, I would say that the most natural answer is an out-of-the-box reverse proxy solution (as the other answers pointed out). I also note that such a reverse proxy is an essential and natural part of a PaaS solution, so if you need it to "just work" and don't want to know about the details, PaaS would be a natural path (e.g. see cloudfoundry as an example of such a solution that I happen to have worked on).
Indeed Spring Boot has not inherit support for load-balancing. Just to add to the list of available solutions for load-balancing, here are the instructions to configure an Apache for load-balancing.

Resources