zuul路由网关
1、概述
Zuul包含了对请求的路由和过滤的主要功能:
-
路由功能:
主要负责将外部请求转发到具体的微服务上,是实现外部访问入口的基础 -
过滤功能:
负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础
Zuul与Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的信息,也即以后的微服务访问都是通过Zuul跳转后获得。
三大功能:代理、路由、过滤
2、路由基本配置
- 新建springcloud-study-zuul-gateway-9527模块
- pom文件
<?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>springcloud-study</artifactId>
<groupId>com.blj</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>springcloud-study-zuul-gateway-9527</artifactId>
<dependencies>
<dependency>
<groupId>com.blj</groupId>
<artifactId>springcloud-study-api</artifactId>
<version>${project.version}</version>
</dependency>
<!--zuul依赖-->
<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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<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.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>
- yml文件
server:
port: 9527
spring:
application:
name: springcloud-study-zuul-gateway
eureka:
instance:
instance-id: gateway-9572
prefer-ip-address: true
client:
service-url:
# 单机版配置defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka #设置与eureka server 交互的地址查询服务和注册服务都需要依赖的地址
#集群配置
defaultZone: http://localhost:7001/eureka/,http://localhost:7004/eureka/,http://localhost:7001/eureka/
info:
app.name: springcloud-study-zuul-gateway
company.name: www.blj.com
build.artifactId: ${project.artifactId}
build.version: ${project.version}
- hosts文件修改: 127.0.0.1 myzuul.com
- 主启动类
package com.blj.springcloud;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
@SpringBootApplication
@EnableZuulProxy
public class ZuulGatewayApp {
public static void main(String[] args) {
SpringApplication.run(ZuulGatewayApp.class,args);
}
}
- 测试
不启用路由:http://localhost:8001/dept/get/1
启用路由:http://myzuul.com:9527/STUDY-SPRINGCLOUD-DEPT/dept/get/1
路由访问映射规则
- 修改yml,增加如下配置
zuul:
routes:
mydept.serviceId: STUDY-SPRINGCLOUD-DEPT
mydept.path: /mydept/*
配置后,由:http://myzuul.com:9527/STUDY-SPRINGCLOUD-DEPT/dept/get/1
可变为:http://myzuul.com:9527/mydept/dept/get/1
- 隐藏真实微服务名称
zuul:
routes:
mydept.serviceId: STUDY-SPRINGCLOUD-DEPT
mydept.path: /mydept/*
#多个用"*" ignored-services: "*"
ignored-services: STUDY-SPRINGCLOUD-DEPT
- 设置统一公共前缀
zuul:
routes:
mydept.serviceId: STUDY-SPRINGCLOUD-DEPT
mydept.path: /mydept/*
#多个用"*" ignored-services: "*"
ignored-services: STUDY-SPRINGCLOUD-DEPT
#公共前缀
prefix: /springcloud
配置统一公共前缀后,由:http://myzuul.com:9527/mydept/dept/get/1
可变为:http://myzuul.com:9527/springcloud/mydept/dept/get/1