所有代码都在github上:https://github.com/demonruin/cloud2020/tree/master
Zuul包含了对请求的路由和过滤两个最主要的功能:
- 路由功能负责将外部请求转发到具体的微服务实例上,实现外部访问统一入口的基础
- 过滤器功能则负责对请求的处理过程进行干预,是实现请求校验、服务聚合等功能的基础.
Zuul和Eureka进行整合,将ZuuI自身注册为Eureka服务治理下的应用,同时从Eureka中获得其他微服务的消息,也即以后的访问微服务都是通过Zuul跳转后获得。
注意: Zuul服务最终还是会注册进Eureka,提供=代理+路由+过滤三大功能
下面先构建zuul微服务吧
1、构建pom文件,添加上zuul组件,注意,这里演示zuul是在springboot 1.5.9.RELEASE 和springcloud的Dalston.SR1 环境下搭建的,所以需要格外注意,因为在springboot 2.2.2RELEASE 和 springcloud 的Hoxton.SR1的版本组合中已经用 gateway替换了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>king_cloud</artifactId>
<groupId>com.king</groupId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>cloud-gateway-zuul9527</artifactId>
<dependencies>
<!--zuul网关配置-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<!-- 将微服务provider侧注册进eureka -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</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.cloud</groupId>
<artifactId>spring-cloud-starter-config</artifactId>
</dependency>
<dependency><!-- 引入自己定义的api通用包,可以使用Dept部门Entity -->
<groupId>com.king</groupId>
<artifactId>king_cloud_api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</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>
2、构建主启动类,并添加启用网关注解@EnableZuulProxy
package com.king;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
/**
* created by king on 2020/4/21 5:34 下午
*/
@SpringBootApplication
@EnableZuulProxy
public class ZullMain9527 {
public static void main(String[] args) {
SpringApplication.run(ZullMain9527.class,args);
}
}
3、构建yml文件,将网关微服务也注册到注册中心,这个前面提到过,此时yml文件中并没有配置zuul的其他功能,请留意,等测试通过后,后面再补充上zuul的映射等配置
server:
port: 9527
spring:
application:
name: zuul-gateway-9527
eureka:
client: #客户端注册进eureka服务列表内
service-url:
# defaultZone: http://localhost:7001/eureka/
defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
instance-id: zuul-gateway9527 #自定义服务名称信息
prefer-ip-address: true #访问路径可以显示IP地址
info:
app.name: dept_provider
company.name: www.king.com
build.artifactId: ${project.artifactId}$
build.version: ${project.version}$
4、此时zuul微服务已经构建完毕,启动eureka注册中心,启动provider8001服务提供者服务,再启动zuul微服务,然后进行测试:
单独直接访问8001,测试通过,说明服务提供者没问题
这里测试通过zuul访问微服务名称来请求服务提供者和provider8001的时候一定要注意:微服务名称从eureka注册中心拷贝的是大写的,但是请求url的时候一定要改成小写,否则请求不成功,我也是坑了好久才注意这个问题。
到此,说明通过zuul路由请求服务提供者provider8001已经成功。zuul的简单路由配置已经实现,下面补充上zuul的路由访问映射规则
1、在yml中添加配置属性,具体属性功能已经做了注释
#beforehttp://localhost:9527/microservicecloud-dept/dept/get/2
zuul:
prefix: "/king" #给服务请求加统一前缀
ignored-services: "*" #忽略所有的微服务
# ignored-services: dept-provider #忽略单个微服务
routes:
mydept.serviceId: dept-provider #一定要注意区分大小写
mydept.path: /mydept/**
# afterhttp://localhost:9527/mydept/dept/get/1
2、测试请求通过结果