zuul routing gateway
Zuul includes two main functions of request routing and filtering:
The routing function is responsible for forwarding the external request to the specific microservice instance, which is the basis of realizing the unified access to the external access, while the filter function is responsible for intervening the request processing process, which is the basis of realizing the request verification, service aggregation and other functions. Zuul and Eureka integrate, register zuul itself as an application under Eureka service governance, and obtain from Eureka at the same time The messages of other microservices, that is, the access to microservices in the future, are obtained through zuul jump.
Note: Zuul service will eventually register with Eureka
Provide three functions of = agent + routing + filtering
Official: https://github.com/Netflix/zuul/wiki/Getting-Started
Basic configuration
To create a new Module microservicecloud-zuul-gateway-9527:
Introduce dependency:
<dependencies> <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> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-hystrix</artifactId> </dependency> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-config</artifactId> </dependency> <dependency> <groupId>pers.zhang</groupId> <artifactId>microservicecloud-api</artifactId> <version>${project.version}</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>springloaded</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> </dependency> </dependencies>
application.yml
server: port: 9527 spring: application: name: microservicecloud-auul-gateway eureka: client: service-url: defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka instance: instance-id: gateway-9527.com prefer-ip-address: true info: app.name: zhang-microcloud company.name: pers.zhang build.artifactId: $project.artifactId$ build.version: $project.version$
hosts modification:
127.0.0.1 myzuul.com
Main startup class:
@SpringBootApplication @EnableZuulProxy public class Zuul_9527_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args); } }
Test:
- Start three eureka clusters
- Start a service provider class microservicecloud-provider-dept-8001
- Startup routing
No routing:
http://localhost:8001/dept/get/2
Use routing:
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
Route access mapping rules
Agent Name:
Modify the configuration of microservicecloud-zuul-gateway-9527:
zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
Route to OK: http://myzuul.com:9527/mydept/dept/get/1
Go to OK: http://myzul.com: 9527/microservicecloud-dept/dept/get/2
Original real service name ignored:
zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** ignored-services: microservicecloud-dept # Ignore real service name
Route to OK: http://myzuul.com:9527/mydept/dept/get/1
Original path access not available: http://myzuul.com:9527/microservicecloud-dept/dept/get/2
Single specific, multiple can use "*":
zuul: ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
Set uniform public prefix:
zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** ignored-services: "*" # Ignore real service name prefix: /test
Visit: http://myzul.com: 9527 / test / mydept / dept / get / 1