Zuul包含了对请求的路由和过滤两个最主要的功能。git
路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础。github
过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础。web
Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中得到其余微服务的消息,也即之后的访问微服务都是经过Zuul跳转后得到。spring
注:Zuul服务最终仍是会注册进Eurekaapache
Zuul提供代理、路由、过滤三大功能。api
新建Module模块microservicecloud-zuul-gateway-9527maven
<!-- zuul路由网关 -->ide <dependency>spring-boot <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> |
<?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>microservicecloud</artifactId> <groupId>com.hosystem</groupId> <version>1.0-SNAPSHOT</version> </parent> <modelVersion>4.0.0</modelVersion>
<artifactId>microservicecloud-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.atguigu.springcloud</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>
</project> |
server: port: 9527
spring: application: name: microservicecloud-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: hosystem-microcloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
127.0.0.1 myzuul.com |
建立主启动类Zuul_9527_StartSpringCloudApp,并添加注解@EnableZuulProxy.
package com.hosystem.springcloud;
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication @EnableZuulProxy public class Zuul_9527_StartSpringCloudApp { public static void main(String[] args) { SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args); } } |
启动microservicecloud-eureka-700一、microservicecloud-eureka-700二、microservicecloud-eureka-7003
启动microservicecloud-provider-dept-8001
启动microservicecloud-zuul-gateway-9527
http://myzuul.com:9527/microservicecloud-dept/dept/get/2 |
修改部分:
zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** |
完整部分:
server: port: 9527
spring: application: name: microservicecloud-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
zuul: routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
info: app.name: hosystem-microcloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
访问效果
#以前访问的时候经过'microservicecloud-dept‘ http://myzuul.com:9527/microservicecloud-dept/dept/get/2
#以后访问的时候经过'mydept' |
出现的问题,经过'microservicecloud-dept‘或者'mydept'均可以访问。如何限制只容许'mydept'访问,而'microservicecloud-dept'访问失败呢?具体解决方法以下,
#解决单个的时候指定具体的servideId #若是想要解决多个的时候能够使用 "*" zuul: ignored-services: microservicecloud-dept routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** |
统一公共前缀
zuul: prefix: /hosystem ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/** |
完整的applicaiton.yml文件
server: port: 9527
spring: application: name: microservicecloud-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
zuul: prefix: /hosystem ignored-services: "*" routes: mydept.serviceId: microservicecloud-dept mydept.path: /mydept/**
info: app.name: hosystem-microcloud company.name: www.hosystem.com build.artifactId: $project.artifactId$ build.version: $project.version$ |
参考文档: