In the previous article, we introduced that Eureka is used for service registration and discovery, Feign supports service invocation and load balancing, Hystrix handles service fuses to prevent fault spread, and Spring Cloud Config service cluster configuration center seems to have completed a microservice framework. .
We still think about one less question, how do external applications access various internal microservices? In the microservice architecture, back-end services are often not directly open to the caller, but routed to the corresponding service through an API gateway according to the requested url. When the API gateway is added, a wall is created between the third-party caller and the service provider. This wall communicates directly with the caller for permission control, and then distributes requests to the background server in a balanced manner.
Why do you need API Gateway
1. Simplify the complexity of client calls
In the microservice architecture mode, the number of instances of the backend service is generally dynamic, and it is difficult for the client to find the access address information of the dynamically changed service instance. Therefore, in order to simplify the front-end calling logic in microservice-based projects, API Gateway is usually introduced as a lightweight gateway. At the same time, API Gateway will also implement related authentication logic to simplify the complexity of calling each other between internal services.
2. Data pruning and aggregation
Generally speaking, different clients have inconsistent data requirements for display, such as mobile phone or web, or in a low-latency network environment or a high-latency network environment.
Therefore, in order to optimize the user experience of the client, API Gateway can tailor the general response data to suit the needs of different clients. At the same time, multiple API call logics can be aggregated to reduce the number of client requests and optimize the client user experience.
3. Multi-channel support
Of course, we can also provide different API Gateways for different channels and clients. The use of this mode is called Backend for front-end by another well-known method. In the Backend for front-end mode, we can target different Clients create their BFFs separately. For more information on BFFs, please refer to this article: Pattern: Backends For Frontends
4. Microservice transformation of legacy systems
For systems, microservice transformation is usually due to more or less problems with the original system, such as technical debt, code quality, maintainability, scalability, and so on. The API Gateway model is also suitable for the transformation of this type of legacy system. Through the transformation of microservices, the problems in the original system are gradually repaired, thereby improving the responsiveness of the original business. Gradually replace old implementations with new implementations by introducing layers of abstraction.
In the Spring Cloud system, Spring Cloud Zuul is an API gateway that provides load balancing, reverse proxy, and authority authentication.
Spring Cloud Zuul
Spring Cloud Zuul routing is an integral part of the microservice architecture, providing edge services for dynamic routing, monitoring, resiliency, security, and more. Zuul is a JVM-based routing and server-side load balancer produced by Netflix.
Below we use the code to understand how Zuul works
Simple to use
1. Add dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency>
import spring-cloud-starter-zuul
package
2. Configuration file
spring.application.name=gateway-service-zuul server.port=8888 #The configuration here means that accessing /it /** redirects directly to http://www.ityouknow.com/** zuul.routes.baidu.path=/it/** zuul.routes.baidu.url=http://www.ityouknow.com/
3. Startup class
@SpringBootApplication @EnableZuulProxy public class GatewayServiceZuulApplication { public static void main(String[] args) { SpringApplication.run(GatewayServiceZuulApplication.class, args); } }
Startup classes are added @EnableZuulProxy
to support gateway routing.
The simplest zuul case in history is configured
4. Test
Start the gateway-service-zuul-simple
project, visit: in the browser, http://localhost:8888/it/spring-cloud
and see that the page returns: http://www.ityouknow.com/spring-cloud
the information of the page is as follows:
Let's take the sample code of the previous article as an spring-cloud-producer
example to test the redirection of the request and add in the configuration file:
zuul.routes.hello.path=/hello/** zuul.routes.hello.url=http://localhost:9000/
start spring-cloud-producer
, restart gateway-service-zuul-simple
, access: http://localhost:8888/hello/hello?name=%E5%B0%8F%E6%98%8E
, return:hello 小明,this is first messge
Indicates that the request for access gateway-service-zuul-simple
is automatically forwarded to spring-cloud-producer
, and the result is returned.
Servicing
There are limitations to implementing zull forwarding through url mapping. For example, each additional service needs to be configured with a content. In addition, if the back-end service is provided dynamically, this scheme cannot be used for configuration. In fact, when implementing the microservice architecture, the relationship between the service name and the service instance address already exists in the eureka server, so you only need to register Zuul to the eureka server to discover other services, and you can implement the serviceId mapping.
We illustrate with examples, and gateway-service-zuul-simple
transform on the basis of the above example project.
1. Add dependencies
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
Added spring-cloud-starter-eureka
package to add support for eureka.
2. Configuration file
The configuration is modified to:
spring.application.name=gateway-service-zuul server.port=8888 zuul.routes.api-a.path=/producer/** zuul.routes.api-a.serviceId=spring-cloud-producer eureka.client.serviceUrl.defaultZone=http://localhost:8000/eureka/
3. Test
Start spring-cloud-eureka
, spring-cloud-producer
, gateway-service-zuul-eureka
, access: http://localhost:8888/producer/hello?name=%E5%B0%8F%E6%98%8E
, return:hello 小明,this is first messge
Indicates that the request for access gateway-service-zuul-eureka
is automatically forwarded to spring-cloud-producer
, and the result is returned.
In order to better simulate the service cluster, we copied the spring-cloud-producer
project to change spring-cloud-producer-2
, modify the spring-cloud-producer-2
project port to 9001, and modify the controller code as follows:
@RestController public class HelloController { @RequestMapping("/hello") public String index(@RequestParam String name) { return "hello "+name+",this is two messge"; } }
After the modification is completed, start spring-cloud-producer-2
and restart gateway-service-zuul-eureka
. Test multiple accesses http://localhost:8888/producer/hello?name=%E5%B0%8F%E6%98%8E
and return in sequence:
hello 小明,this is first messge hello 小明,this is two messge hello 小明,this is first messge hello 小明,this is two messge ...
It means that the producer service was successfully called through zuul and the load was balanced.
Default routing rules for gateways
But if there are more than a dozen backend services, it is very troublesome to configure each one like this. Spring cloud zuul has already made the default configuration for us. By default, Zuul will proxy all microservices registered to Eureka Server, and Zuul's routing rules are as follows: http://ZUUL_HOST:ZUUL_PORT/微服务在Eureka上的serviceId/**
it will be forwarded to the microservice corresponding to serviceId.
We unregister gateway-service-zuul-eureka
the configuration of routing in the project:
#zuul.routes.api-a.path=/producer/** #zuul.routes.api-a.serviceId=spring-cloud-producer
After restarting, the access http://localhost:8888/spring-cloud-producer/hello?name=%E5%B0%8F%E6%98%8E
and test return results are the same as the above example, indicating that spirng cloud zuul has provided the forwarding function by default.
At this point, we have introduced the basic use of zuul. Regarding the more advanced use of zuul, we will introduce it in the next article.
refer to: