Springboot: Ribbon and Zuul - spring-boot

Do we need to configure both Ribbon and Zuul for Loadbalancing the microservices.
From what I have seen Zuul is more of a conventional reverse-proxy loadbalancer and any request to services thro Zuul will automatically be loadbalanced and I do not have to configure Ribbon in individual microservices.
We will need Ribbon configured in the service only if we need to connect to a service that is not proxied by Zuul.
Is the understanding correct.

If you are using eureka for discovering your services, that are balanced through zuul, you don't need to configure ribbon in any way.
Say, you have a ServiceA that is discovered by eureka and you have a zuul route configured like this one
zuul:
routes:
example:
path: /example/**
serviceId: ServiceA
As soon as you have more than one instance of ServiceA running, zuul will start to load balance the load of all ServiceA with a round robin algorithm.

Related

Microservices - What is the exact difference between service discovery and Service Gateway?

I read the articles like : What is the difference between an API-Gateway and an Edge Service?, but still not clear on what's the exact difference between the service discovery (say Eureka) and Service Gateway (Zuul) ? Any pointers?
The Services gateway (a.k.a. API gateway) is receiving requests from the clients. It is the (single) service that the clients see. But it doesn't know by itself how to do what the clients want so it must forward the request to another service. It inspects the URL and extracts the name of the service that will actually serve the request, the backend service. That backend service may be located on a different machine. It may have a dynamic address or port, that may change from time to time, from different reasons (nodes fail, new nodes are added to the network etc).
So, the gateway doesn't know where to forward the request. This is the job of the Service-discovery service (Eureka). Eureka knows on which node is located and at which address and port every service is listening for requests. The gateway is asking Eureka where it can find the backend service (by sending it only its name). After Eureka is responding, the gateway is forwarding the request to that address.
The differences is more understandable if you focus in responsabilities of both Zuul and Eureka. As you can see below each one has specific tasks. Thats the main differences.
API Gateway
In Api Gateway we have to register services with its URL and matching patterns
like this:
id: order-service
uri: http://localhost:9003/
predicates:
- Path=/order/**
But the problem here is we can have multiple instances on different ports. Like this
uri: http://localhost:9003/
uri: http://localhost:9004/
uri: http://localhost:9005/
In this case, a gateway will fail to route to other instances.
Service Discovery
In case of service discovery, we will have to register client services to service discovery (which is another service). Like this
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://localhost:8761/eureka/
Now all clients will register to service discovery automatically so even if there are multiple instances service register will take care of it.
Service Gateway operates at Network layer 7 (HTTP) and Service discovery operates at Network Layer 4 to be precise.
Zuul works at Layer 7 and Eureka works at Layer 4
Please refer to Kong Gateway which provides a uniform solution

How to detect new REST calls dynamically as and when they are available?

I am creating an API gateway using Netflix Zuul and Netflix Eureka doing the part of service registration for service discovery for a microservices framework.
I am trying to support a use case where new micro-services may come up which expose REST calls, or existing micro-services may get upgraded to expose new REST calls with version as part of their URL etc. Eureka runs as a dedicated springboot application and Zuul runs as a dedicated application. The application.prop or application.yml file of Zuul is where i am putting config for zuul.routes for each micro-services
My zuul's application.yml looks something like this to support one microservice config.
zuul:
routes:
example:
path: /example/**
stripPrefix: false
serviceId: zuul-service
If i bring down zuul add another such snippet of config under zuul:routes:
and then bring up zuul again, i will start seeing the new endpoint. But i want that to happen dynamically. Like Eureka will dynamically detect any new micro-services that come up or go down. If zuul can get in sync with Eureka then all my rest endpoints can be exposed perfectly through my Zuul api gateway.
Any suggestions on what i may be missing or add any other open source third party to achieve this will be helpful.

Pick new routes when new micro-service registers with Eureka

I'm using Eureka Server where every microservice gets registered as soon it gets online.
Now I want to use Zuul to create a gateway proxy.
Zuul works if I provide routes manually:
zuul.routes.ms1.url=http://localhost:8901
zuul.routes.ms2.url=http://localhost:8902
ribbon.eureka.enabled=false
I want Zuul to auto pick routes from Eureka as soon new micro-services is up and registered with Eureka.
I tired adding #EnableAutoDiscovery to Zuul Initalization class
and setting ribbon.eureka.enabled=true after removing routes but nothing worked.
If you give eureka.client.serviceUrl.defaultZone in your properties in zuul service, zuul will take all services which is registered to eureka server.

How to get the service endpoint registered behind the Application name in Eureka Server?

I have some web services registered at Spring Boot Eureka Server. Is there any way I can check the endpoints registered at Eureka Server, with respect to all the application?
e.g: Eureka server
Service1 means Request will be routed to service endpoint: localhost:9080/service1
Service2 means Request will be routed to service endpoint: localhost:9088/service2
Service3 means Request will be routed to service endpoint: localhost:9085/service3
You can check the Eureka dashboard at http://host-name:8761 (Change the port if you're using a different one).
Secondly, Eureka doesn't route any request. It is just a service registry and keeps the records of the Microservices and its instances. You need an API Gateway (ZUUL) or similar to do the routing along with client side load balancers (ribbon, etc.).
You can configure the zuul endpoints in the application.yml (or properties) file like below to access your service endpoints.
zuul:
ignoredServices: "*"
routes:
service1:
path: /service1/**
service2:
path: /service2/**
If you want to access the service1, then your endpoint will be something like http://localhost:8765/service1/{custom-path}
Note: 8765 is the default zuul port. Change it accordingly.

Spring Cloud Zuul: how to add route without restart

I am writing microservices with Spring Cloud.
I'm also using Zuul as the API Gateway which routes some URLs to destination services which are not registered into Eureka.
When a new route needed to be added to Zuul, we often edit the application.yml file and then restart the Zuul server.
I was wondering is there a way to add new route into Zuul without restarting the Zuul server?
I found this link useful but it only works for services registed in Erueka.

Resources