1. Concept
Zuul is a framework for providing dynamic routing, monitoring, resiliency, security and other edge services on cloud platforms. Zuul corresponds to the front door of all requests from the Web site backend of the device and Netflix streaming app. Another need to understand the concept is the reverse proxy and load balancing, reverse proxy: is the external network to request resources to the intranet, and the resources back to the client, is the opposite of the proxy; Load balancing is a multiple client request, and if there are multiple identical Serviceid, the gateway will be polled for access.
2. Build
2.1. Add dependencies
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.3.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<version>1.3.2.RELEASE</version>
</dependency>
2.2. Concrete Realization
@EnableZuulProxy
@SpringCloudApplication
public class Zuulapplication {
public static void Main (string[] args) {
New Springapplicationbuilder (Zuulapplication.class). Web (True). Run (args);
}
}
2.3. Configuration
Spring.application.name=api-gateway
server.port=9412
# routes to ServiceId here the side is bound by the ServiceId address, when the path after adding/xx-a/is to access service-a corresponding services.
zuul.routes.xx-a.path=/xx-a/**
Zuul.routes.xx-a.serviceid=service-a
zuul.routes.yy-b.path=/yy-b/**
Zuul.routes.yy-b.serviceid=service-b
eureka.client.serviceurl.defaultzone=http://localhost:8761/eureka/
2.4, new micro-service service-a and micro-service Service-b
You can inject @Autowired private discoveryclient client into the controller, and then use the client to get the host and port number
2.5, A, B service copy, modify the port, to start, and then request, you will find two requests a service, to obtain a different port number of the micro-service, thus achieving client load balancing.
3. Summary
Zuul simplifies the complexity of client calls, and load balancing reduces the pressure on a single server to distribute the request pressure to the same service.
Spring Cloud (vi): Integration of Zuul