概念
官网:github官网
在微服务架构中,Zuul就是网关的守门员,保安
Zuul包含了对请求的路由和过滤两个最主要的功能:
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
Zuul服务最终还是会注册进Eureka
提供=代理+路由+过滤三大功能
使用
1.新建工程democloud-zuul-gateway-9527
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>demo1</artifactId>
<groupId>com.yan</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>democloud-zuul-gateway-9527</artifactId>
<dependencies>
<!-- zuul路由网关 -->
<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>
<!-- actuator监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- hystrix容错-->
<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.yan</groupId>
<artifactId>demo-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>
</project>
2.新建主启动类,添加注解@EnableZuulProxy
package com.yan.cloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* @Author: 闫明辉
* @Description:
* @Date: Create in 20:36 2020/3/9
*/
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {
public static void main(String[] args) {
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class,args);
}
}
3.初步yml
server:
port: 9527
spring:
application:
name: democloud-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-9527.com
prefer-ip-address: true
info:
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: ${project.artifactId}$
build.version: ${project.version}$
4.修改hosts文件
加入以下映射
127.0.0.1 myzuul.com
基本配置完成
测试
1.启动测试euerka
出现两个微服务注册进来就ok
2.启动provider-8001和democloud-zuul-gateway-9527
不用路由测试 http://localhost:8001/dept/get/1
使用路由测试 http://myzuul.com:9527/democloud-dept/dept/get/1
3.此时发现问题,路由访问过于麻烦
yml可加入以下配置
zuul:
routes:
mydept.serviceId: democloud-dept
mydept.path: /mydept/**
可替代真实微服务名字
访问测试 http://myzuul.com:9527/mydept/dept/get/1
此时发现访问真实微服务名字 也可以
4.yml加入以下配置 可指定微服务不自动默认配置路由
单个微服务
zuul:
ignored-services: demo-dept
routes:
mydept.serviceId: demoecloud-dept
mydept.path: /mydept/**
多个微服务
zuul:
ignored-services: "*"
routes:
mydept.serviceId: democloud-dept
mydept.path: /mydept/**
真实微服务访问测试 http://myzuul.com:9527/democloud-dept/dept/get/1
真实微服务名字不可以访问
替代的名字访问测试 http://myzuul.com:9527/mydept/dept/get/1
可以正常访问
5.设置统一公共前缀,过滤其他无用请求
prefix 前缀
yml添加以下配置
zuul:
prefix: /atguigu
ignored-services: "*"
routes:
mydept.serviceId: democloud-dept
mydept.path: /mydept/**
统一前缀访问测试 http://myzuul.com:9527/ymh/mydept/dept/get/1
原来的路径访问测试:http://myzuul.com:9527/mydept/dept/get/1
不可被访问,过滤成功
参考资料:尚硅谷周阳Spring Cloud讲解