是什么
Zuul包含了对请求路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。而过滤功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。Zuul和Eureka进行整合,将zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过zuul跳转后获得。
注意:zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
基本配置
1. 创建maven工程micoservicecloud-zuul-gateway-9527
2. 配置其pom.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>micoservicecloud</artifactId>
<groupId>com.minn.springcloud</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>micoservicecloud-zuul-gateway-9527</artifactId>
<dependencies>
<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>com.minn.springcloud</groupId>
<artifactId>micoservicecloud-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>springloaded</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
</dependencies>
</project>
3. 配置其application.yml
server:
port: 9527
spring:
application:
name: micoservicecloud-zuul-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.com
prefer-ip-address: true
info:
app.name: minn-microservicecloud
company.name: www.minn.com
build.artifactId: $project.artifactId$
build.version: $project.version$
4. 创建启动类
package com.minn.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class Zuul_App {
public static void main(String[] args) {
SpringApplication.run(Zuul_App.class,args);
}
}
5. 启动三个eureka集群,一个服务提供类micoservicecloud-provider-dept-8001,一个路由。
6. 在本地hosts文件添加:
localhost myzuul.com
7. 启用路由
http://myzuul.com:9527/microservicecloud-dept/dept/get/1
访问映射规则
统一更改路由访问路径:
zuul:
routes:
mydept.serviceId: microservicecloud-dept #http://myzuul.com:9527/microservicecloud-dept/dept/get/1
mydept.path: /mydept/** #将上面的路径中microservicecloud-dept变为mydept
ignored-services: "*" #屏蔽http://myzuul.com:9527/microservicecloud-dept这个访问路径,"*" 代表多个微服务名
prefix: /minn #统一访问路径:http://myzuul.com:9527/minn/mydept/dept/get/1