Introduction of Zuul
Zuul is a load balancer based on JVM routing and server, which provides dynamic routing, monitoring, resilience, security and other edge services on cloud platform. Source of articles Javaa B2B2C Imitating Taobao Electronic Mall System
Routing function: equivalent to the reverse proxy function of nginx.
For example: / You may need to map to your web application, / api/users to user services, / api/shop to mall services. B2B2C E-Commerce Platform Source Code 10387474626 Needing JAVA Spring Cloud Large Enterprise Distributed Micro-Service Cloud Construction
II. NEW PROJECTS
1. Adding dependency packages
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-zuul</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-eureka</artifactId> </dependency>
2. Configuration file
server server.port=9876 spring spring.application.name=spring-cloud-gateway eureka #eureka.client.serviceUrl.defaultZone=http://localhost:8761/eureka/ eureka.client.serviceUrl.defaultZone=http://roncoo:123456@localhost:8761/eureka/ info custom info.build.name=@project.name@ info.build.description=@project.description@ info.build.groupId=@project.groupId@ info.build.artifact=@project.artifactId@ info.build.version=@project.version@ eureka.instance.status-page-url-path=/info eureka.instance.instanceId=${spring.application.name}:${random.value} eureka.instance.prefer-ip-address=true #Set the registration time of pull service, default 60s eureka.client.registry-fetch-interval-seconds=30 #Specify renewal frequency, default is 30s eureka.instance.lease-renewal-interval-in-seconds=15 #Set expiration culling time, default 90s eureka.instance.lease-expiration-duration-in-seconds=45
3. Main Class
package com.roncoo.education; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @EnableZuulProxy @SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } }
4. Start access:
localhost:7778/api/user/1
localhost:8886/feign/user/1
Using zuul
localhost:9876/spring-cloud-provider/api/user/1
localhost:9876/spring-cloud-consumer2/feign/user/1
Note: zull can call either the provider or the consumer. There are no fixed rules, which can meet the business needs. In many cases, the provider will also call another provider (maybe more than one), and it is recommended that the number of calls between the three applications should not exceed.
For example: an order-placing api service, when the user orders, it will call the order service. In the order service, it will call the user service to operate the user information. It will also call the integral service to perform the integral operation. It may also have a log service to log, so that the order can be placed eventually.