是什么
Zuul包含了对请求路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。而过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。Zuul和Eureka进行整合,将zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过zuul跳转后获得。
注意:zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
基本配置
1. 创建maven工程micoservicecloud-zuul-gateway-9527
2. 配置其pom.xml文件
1<?xml version="1.0" encoding="UTF-8"?> 2<project xmlns="http://maven.apache.org/POM/4.0.0" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 5 <parent> 6 <artifactId>micoservicecloud</artifactId> 7 <groupId>com.minn.springcloud</groupId> 8 <version>1.0-SNAPSHOT</version> 9 </parent> 10 <modelVersion>4.0.0</modelVersion> 11 12 <artifactId>micoservicecloud-zuul-gateway-9527</artifactId> 13 14 <dependencies> 15 <dependency> 16 <groupId>org.springframework.boot</groupId> 17 <artifactId>spring-boot-starter-actuator</artifactId> 18 </dependency> 19 <dependency> 20 <groupId>org.springframework.cloud</groupId> 21 <artifactId>spring-cloud-starter-hystrix</artifactId> 22 </dependency> 23 <dependency> 24 <groupId>org.springframework.cloud</groupId> 25 <artifactId>spring-cloud-starter-config</artifactId> 26 </dependency> 27 <dependency> 28 <groupId>com.minn.springcloud</groupId> 29 <artifactId>micoservicecloud-api</artifactId> 30 <version>${project.version}</version> 31 </dependency> 32 <dependency> 33 <groupId>org.springframework.cloud</groupId> 34 <artifactId>spring-cloud-starter-eureka</artifactId> 35 </dependency> 36 <dependency> 37 <groupId>org.springframework.cloud</groupId> 38 <artifactId>spring-cloud-starter-zuul</artifactId> 39 </dependency> 40 <dependency> 41 <groupId>org.springframework.boot</groupId> 42 <artifactId>spring-boot-starter-web</artifactId> 43 </dependency> 44 <dependency> 45 <groupId>org.springframework</groupId> 46 <artifactId>springloaded</artifactId> 47 </dependency> 48 <dependency> 49 <groupId>org.springframework.boot</groupId> 50 <artifactId>spring-boot-devtools</artifactId> 51 </dependency> 52 <dependency> 53 <groupId>org.springframework.boot</groupId> 54 <artifactId>spring-boot-starter-jetty</artifactId> 55 </dependency> 56 </dependencies> 57</project>
3. 配置其application.yml
1server: 2 port: 9527 3 4spring: 5 application: 6 name: micoservicecloud-zuul-gateway 7eureka: 8 client: 9 service-url: 10 defaultZone: http://eureka7001.com:7001/eureka,http://eureka7002.com:7002/eureka,http://eureka7003.com:7003/eureka 11 instance: 12 instance-id: gateway.com 13 prefer-ip-address: true 14 15info: 16 app.name: minn-microservicecloud 17 company.name: www.minn.com 18 build.artifactId: $project.artifactId$ 19 build.version: $project.version$
4. 创建启动类
1package com.minn.springcloud; 2 3import org.springframework.boot.SpringApplication; 4import org.springframework.boot.autoconfigure.SpringBootApplication; 5import org.springframework.cloud.netflix.zuul.EnableZuulProxy; 6 7@SpringBootApplication 8@EnableZuulProxy 9public class Zuul_App { 10 public static void main(String[] args) { 11 SpringApplication.run(Zuul_App.class,args); 12 } 13}
5. 启动三个eureka集群,一个服务提供类micoservicecloud-provider-dept-8001,一个路由。
6. 在本地hosts文件添加:
localhost myzuul.com
7. 启用路由
http://myzuul.com:9527/microservicecloud-dept/dept/get/1
访问映射规则
统一更改路由访问路径:
1zuul: 2 routes: 3 mydept.serviceId: microservicecloud-dept #http://myzuul.com:9527/microservicecloud-dept/dept/get/1 4 mydept.path: /mydept/** #将上面的路径中microservicecloud-dept变为mydept 5 ignored-services: "*" #屏蔽http://myzuul.com:9527/microservicecloud-dept这个访问路径,"*" 代表多个微服务名 6 prefix: /minn #统一访问路径:http://myzuul.com:9527/minn/mydept/dept/get/1