Spring Cloud (10): Service Gateway Zuul (routing) "version Finchley" Posted in 2018-04-23 | updated on 2018-05-09 |
With the introduction of several core components in Spring Cloud, we can already build a brief microservices architecture, perhaps like this:
We implemented service registries and service registration and discovery using the Eureka in spring Cloud Netflix, while service consumption and balanced workloads were realized through the Ribbon or feign, and the application multi-environment was realized through spring cloud Config External configuration and version management. To make the service cluster more robust, use the hystrix-break mechanism to avoid the spread of failures caused by individual service anomalies in the microservices architecture. It seems that a microservices framework has been completed.
We're still thinking less about how external applications can access a wide variety of microservices in-house. In microservices architectures, back-end services are often not directly open to the caller, but are routed to the appropriate service via an API gateway based on the requested URL. When the API gateway is added, a wall is created between the third-party caller and the service provider, which directly controls the caller's communication and distributes the request evenly to the backend server.
Why API Gateway is required
1. Simplify the complexity of client calls
The number of instances of back-end services in the MicroServices architecture mode is generally dynamic, and it is difficult for clients to discover the access address information of dynamically changing service instances. Therefore, in order to simplify the invocation logic of the front end in a microservices-based project, the API gateway is often introduced as a lightweight gateway, and the API gateway implements the relevant authentication logic to simplify the complexity of calls between internal services.
2. Data clipping and aggregation
In general, different clients are inconsistent with the need for data to be displayed, such as on a mobile phone or Web side, or in a low-latency network environment or a high-latency network environment.
Therefore, in order to optimize the client experience, API Gateway can tailor the common response data to suit the needs of different clients. You can also aggregate multiple API call logic to reduce the number of requests from clients and optimize the client user experience
3. Multi-channel support
Of course we can also provide different API Gateway for different channels and clients, the use of this mode by another familiar way called backend for front-end, in backend for front-end mode, we can for different customer Create their own BFF, to learn more about BFF can refer to this article: Pattern:backends for Frontends
4, the legacy system of micro-service transformation
Micro-service transformation is usually due to the existence of more or less problems in the system, such as technical debt, code quality, maintainability, extensibility and so on. The API Gateway model is also suitable for this kind of legacy system transformation, through the transformation of micro-service to gradually realize the problems in the original system repair, so as to improve the original business response to improve. By introducing the abstraction layer, you gradually replace the old implementation with the new implementation.
In the spring cloud system, Spring Cloud Zuul is an API Gateway that provides load balancing, reverse proxy, and privilege authentication.
The Zuul we're talking about is Zuul 1, actually Netflix has released Zuul 2, but spring doesn't seem to be integrating Zuul 2 into the spring cloud ecosystem because it makes a spring cloud Gatew Ay (presumably because the Zuul 21 jump ticket before can wait for it). We'll talk about the high-performance gateway of Spring Cloud Gateway later.
Spring Cloud Zuul
Spring Cloud Zuul Routing is an integral part of the microservices architecture, providing dynamic routing, monitoring, resiliency, security, and other edge services. Zuul is a load balancer based on the JVM Routing and service side of Netflix.
Here's a code to see how Zuul works
Preparatory work
Before building a service gateway, let's prepare the microservices inside the gateway to directly use the content that was written earlier, such as:
After launching the Eureka, producer, and consumer instances, all the preparation is ready, let's try using Spring Cloud Zuul to implement the capabilities of the Service gateway.
Start by creating a basic Spring Boot project named: Api-gateway.
POM Configuration
The pom.xml
following dependencies are introduced in
Copy
1 2 3 4 5 6 7 8
|
<Dependency> <Groupid>org.springframework.cloud</GROUPID> < artifactid>spring-cloud-starter-netflix-zuul</ Artifactid> </DEPENDENCY> <DEPENDENCY> <groupid>org.springframework.cloud</ GROUPID> <artifactid> Spring-cloud-starter-netflix-eureka-client</ARTIFACTID> </DEPENDENCY> |
Configuration file
Add the service name, port number, and Eureka registry address to the configuration file application.yml:
Copy
1 2 3 4 5 6 7 8 9
|
Spring Application: Api-gateway Server 14000 Eureka Client Service-url: http://localhost:7000/eureka/
|
Start class
Using @EnableZuulProxy
annotations to turn on Zuul features
Copy
1 2 3 4 5 6 7 8 |
@EnableZuulProxy @SpringBootApplication apigatewayapplication {
Main(string[] args) { Springapplication.run (Apigatewayapplication.class, args); } }
|
Here, a Spring Cloud-based Zuul service Gateway has been built. Starting the app, a default service gateway is built, and you can see the service in Eureka.
Test
Since Spring Cloud Zuul has the default service routing capability after consolidating Eureka, the api-gateway
service Gateway will discover the two services that we have started and, when the app we build here starts and registers with Eureka, producer
consumer
Zuul will create two routing rules. Each routing rule contains two parts, a matching rule for an external request, and a service ID for the route. In the case of the current example, Zuul creates the following two routing rules:
- The request rules that are forwarded to
producer
the service are:/producer/**
- The request rules that are forwarded to
consumer
the service are:/consumer/**
Finally, we can 14000
verify the correctness of the above routes by accessing the port's service gateway:
- For example, Access: HTTP://LOCALHOST:14000/CONSUMER/HELLO/WINDMT, the interface on which the request will eventually be routed
consumer
/hello
.
Related reading
Spring Cloud (i): Overview of service governance Technologies
Spring Cloud (ii): Service Registration and Discovery Eureka
Spring Cloud (iii): service delivery and invocation Eureka
Spring Cloud (iv): Service-tolerant protection hystrix
Spring Cloud (v): Hystrix Monitor Panel
Spring Cloud (vi): Hystrix monitoring Data Aggregation Turbine
Spring Cloud (vii): Configuration Center (Git vs. dynamic refresh)
Spring Cloud (eight): Configuration Center (service and high availability)
Spring Cloud (ix): Configuration Center (message bus)
Spring Cloud (10): Service Gateway Zuul (routing)
Spring Cloud (11): Service Gateway Zuul (filter)
Spring Cloud (12): Distributed Link Tracking (Sleuth and Zipkin)
Sample code: GitHub
Reference
Spring Cloud-router and Filter:zuul
Springcloud (10): Service Gateway Zuul Junior Chapter
Spring Cloud builds a microservices architecture: Service Gateway (Basic) "Version Dalston"
- this article Yibo
- This article link: https://windmt.com/2018/04/23/spring-cloud-10-zuul-router/
- Copyright Notice: All articles in this blog are subject to the CC BY-NC-SA 4.0 license Agreement except for special statements. Reprint please specify the source!
Spring Cloud (10): Service Gateway Zuul (routing) version Finchley