前言
案例的都是循序渐进的。案例的顺序:
- 微服务讲诉->
- 父工程maven的module建立和rest风格的请求->
- eureka的学习、三大组件,服务注册与消费->
- eureka的集群操作 ->
- ribbon的负载均衡。
- feign的负载均衡
- Hystrix断路器
- Zuul路由网关
- SpringCloud Config分布式配置中心
一、 Zuul概述
1. 是什么
Zuul包含了对 请求的路由和过滤 两个最主要的功能。
其中路由功能负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础而过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.Zuul和Eureka进行整合,将Zuul自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意:Zuul服务最终还是会注册进Eureka;
提供=代理+路由+过滤三大功能
2. 能干嘛
- 路由
- 过滤
3. 官网
https://github.com/Netflix/zuul/wiki/Getting-Started
二、 路由基本配置
1. 案例讲解:新建Module模块microservicecloud-zuul-gateway-9527
2. POM
添加新依赖::zuul路由网关
<?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.atguigu.springcloud</groupId>
<version>0.0.1-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>
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: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$
4. Hosts修改
- 位置:
- 添加映射
5. 主启动类
添加新注解:@EnableZuulProxy
package com.atguigu.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);
}
}
6. 启动
- 三个eureka 集群
- 一个服务器提供类microservicecloud-provide-dept-8001
- 一个路由
7. 测试
三、 路由访问映射规则1. 工程microservicecloud-zuul-gateway-9527
继续还是以microservicecloud-zuul-gateway-9527 这个工程为例。
2. 代理名称
1) YML
before
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
zuul:
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
after
http://myzuul.com:9527/mydept/dept/get/1
2) 此时问题
路由访问OK
http://myzuul.com:9527/mydept/dept/get/1
原路径访问OK
http://myzuul.com:9527/microservicecloud-dept/dept/get/2
3. 原真实服务名忽略
1) YML
zuul:
ignored-services: microservicecloud-dept
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
2) 单个具体,多个可以用"*"
zuul:
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
4. 设置统一公共前缀
1) YML
zuul:
prefix: /atguigu
ignored-services: "*"
routes:
mydept.serviceId: microservicecloud-dept
mydept.path: /mydept/**
http://myzuul.com:9527/atguigu/mydept/dept/get/1
5. 最后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: /atguigu # 请求URI前带上前缀片段
ignored-services: "*" # 忽略某个URI片段
routes:
mydept.serviceId: microservicecloud-dept # 服务
mydept.path: /mydept/** # 请求URI片段
info:
app.name: atguigu-microcloud
company.name: www.atguigu.com
build.artifactId: $project.artifactId$
build.version: $project.version$