1、Zuul介绍
Zuul包含了对请求的路由和过滤两个最主要的功能:其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。Zuul服务最终还是会注册进Eureka。 提供=代理+路由+过滤三大功能
2、基本路由配置
1、新建Module模块microservicecloud-zuul-gateway-9527
2、pom文件
<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">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>cn.jiatp.springcloud</groupId>
<artifactId>microservicecloud</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<artifactId>microservicecloud-zuul-gateway-9527</artifactId>
<dependencies>
<!--通用 -->
<dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<groupId>cn.jiatp.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>
<version>1.2.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!--zuul路由网关 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>
<!--注册到eureka,这是一个客户端-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<!--springboot2.x之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-netflix-hystrix</artifactId>
</dependency>
</dependencies>
</project>
3、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
info:
app.name: jiatp-microservicecloud
company.name: www.jiatp.club
build.artifactId: $project.artifactId$
build.version: $project.version$
4、修改host文件
127.0.0.1 myzuul.com
5、主启动类
package cn.jiatp.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) {
// TODO Auto-generated method stub
SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
}
}
6、启动测试:
分别启动三个eureka集群、一个服务提供类microservicecloud-provider-dept-8001、microservicecloud-zuul-gateway-9527
浏览器中测试:http://myzuul.com:9527/microservicecloud-dept/dept/get/2
3、路由访问映射规则
修改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
info:
app.name: jiatp-microservicecloud
company.name: www.jiatp.club
build.artifactId: $project.artifactId$
build.version: $project.version$
# 路由访问映射规则
zuul:
prefix: /jiatp #设置统一公共前缀
ignored-services: microservicecloud-dept # 多个可用 * 代替
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
路由访问映射从:http://myzuul.com:9527/microservicecloud-dept/dept/get/2---->http://myzuul.com:9527/mydept/dept/get/2(原真实服务名忽略)---->http://myzuul.com:9527/jiatp/mydept/dept/get/2(设置统一公共前缀)