With Eureka service registration discovery, Hystrix circuit breaker, Ribbon service call load balancing, and spring cloud config cluster configuration center, it seems that a microservice framework is complete, last but not least, a service gateway is indispensable.
Spring Cloud Zuul routing is an integral part of the microservice architecture, providing edge services such as dynamic routing, monitoring, resilience, security, etc. Zuul is a load balancer based on JVM routing and server side produced by Netflix.
Introduction to Zuul
Zuul plays the role of "intelligent gateway" throughout the Spring Cloud microservices framework. On the one hand, Zuul is an access gateway, which plays the role of a reverse proxy, and is the only entrance for external consumers to request internal services. On the other hand, Zuul also has filtering functions. By injecting filter rules at runtime, it can realize user authentication, dynamic routing, grayscale release, A/B testing, load current limiting and other functions.
Most of Zuul's functions are completed through filtering functions. Zuul can provide four standard types of filtering, as shown in the figure below:
1) Pre: Filtering rules work before routing. You can use the "Pre" filter to achieve user authentication, record request logs, etc.;
2) Routing: Filtering rules take effect when routing. Dynamic routing, grayscale release, A/B testing, load current limiting, etc. can be used to achieve dynamic routing, grayscale release, and more.
3) Post: The filtering rule takes effect after routing. The "Post" filter can be used to collect statistics and metrics, write the corresponding microservices to the Http response and return it to the service consumer;
4) Error: It will happen when an error occurs during the routing process of the filtering rule. You can use the Error filter to record the error log and perform secondary processing of the error, etc.
Pass messages between filters with RequestContext. The content stored in RequestContext includes routing target address, error information, request information, response information, etc. Zuul's filtering rules can also be written in a JVM-based language, including Java, Python, Groovy, etc.
1. Zuul example
Based on the previous demo, we will demonstrate the zuul gateway service
1. Create a gateway class
@EnableZuulProxy @SpringCloudApplication //Integrate @SpringBootApplication, @EnableDiscoveryClient, @EnableCircuitBreaker public class ZuulApplication { public static void main(String[] args) { new SpringApplicationBuilder(ZuulApplication.class).web(true).run(args); } }2. Add properties configuration file
spring.application.name=api-gateway server.port=5555 zuul.routes.api-a.path=/api-a/** zuul.routes.api-a.serviceId=compute-service zuul.routes.api-b.path=/api-b/** zuul.routes.api-b.serviceId=compute-service eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
Also point to the eureka service registration center address, api-a.serviceId, b-serviceId point to the service provider name
3. Access effect
Originally, the compute-service service instance was directly accessed through the http://COMPUTE-SERVICE/add?a=10&b=20 link. Now, you can directly access the compute-service service by localhost: 5555/api-a/add?a=1&b=2 gateway address. Similarly, the zuul gateway also provides service load balancing function, sending all requests to the service service instance.
2. What is a gateway? Why do you need to use a gateway?
Through the above figure, the API interface is directly exposed to the service caller without a gateway. When the number of callers increases, different business callers are different, and custom access rights, verification and other logics are bound to be added. After adding the API gateway, a wall is created between the third-party caller and the service provider. This wall directly communicates with the caller for permission control, and then the request is balanced to the backend server. Just as there is no need to directly access the add method of the compute-service, the request is passed to the service instance through the API-a/add link. Zuul is an API gateway that provides load balancing, reverse proxy, and permission authentication.
Similar to Nginx adding a protection wall at the front end of the application service, zuul's load balancing is for distributing requests to a service or a service instance in the cluster. The ribbon introduced earlier also focuses on service load functions, which is aimed at the service consumers distributing call requests to a specific service provision instance. Both are load balancing, which is actually carried out at different levels of the system.
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.