springcloud 服务网关之 zuul 实战使用(八)

Zuul 是分布式 springcloud 项目的流量入口,理论上所有进入到微服务系统的请求都要经过 zuul 来过滤和路由,权限校验、路由到第三方接口等。

1、服务端jar包引入

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency>

2、启动类配置:

@SpringBootApplication
@EnableZuulProxy
//@EnableZuulServer
public class MicroZuulApplication {
    public static void main(String[] args) {
        SpringApplication.run(MicroZuulApplication.class,args);
    }

    @Bean
    @RefreshScope
    @ConfigurationProperties("zuul")
    @Primary
    public ZuulProperties zuulProperties() {
        return new ZuulProperties();
    }
}

3、配置详解

配置拦截的 url 和该 url 路由的服务

# 使用路径方式匹配路由规则。
# 参数key结构: zuul.routes.customName.path=xxx
# 用于配置路径匹配规则。
# 其中customName自定义。通常使用要调用的服务名称,方便后期管理
# 可使用的通配符有: * ** ?
# ? 单个字符
# * 任意多个字符,不包含多级路径
# ** 任意多个字符,包含多级路径
zuul.routes.micro-web.path=/web/**

# 参数key结构: zuul.routes.customName.url=xxx
# url用于配置符合path的请求路径路由到的服务地址。
#zuul.routes.micro-order.url=http://localhost:8080/

# key结构 : zuul.routes.customName.serviceId=xxx
# serviceId用于配置符合path的请求路径路由到的服务名称。
zuul.routes.micro-web.serviceId=micro-web-no

4、启动相关eureka、micro-web-no客户端和服务端,访问 http://localhost:7070/actuator/routes 查找服务列表

web就代表micro-web-no服务:http://localhost:7070/web/user/queryUser 就路由到 micro-web-no服务的user/queryUser接口里。

到这里网关效果演示成功。

5、路由动态刷新:

路由动态刷新其实就是分布式配置中心的思路,就是路由配置规则我们可以在项目启动后, 
通过修改远程 GitHub 上的配置,让配置在运行时生效,不需要重启机器。所以就需要加入 
配置中心的配置。引入jar 包,配置中心动态修改配置,网关这里也能实时刷新:

<dependency>
    <groupId>org.springframework.cloud</groupId>
    <artifactId>spring-cloud-starter-config</artifactId>
    <version>LATEST</version>
</dependency>
@Bean
@RefreshScope//实时刷新
@ConfigurationProperties("zuul")//和zull相关的配置
@Primary
public ZuulProperties zuulProperties() {
    return new ZuulProperties();
}

6、zull过滤器功能:

Zuul 大部分功能都是通过过滤器来实现的,Zuul 定义了 4 种标准的过滤器类型,这些过滤器 
类型对应于请求的典型生命周期。 
a、pre: 这种过滤器在请求被路由之前调用。可利用这种过滤器实现身份验证、在集群中选 
择请求的微服务,记录调试信息等。 
b、routing: 这种过滤器将请求路由到微服务。这种过滤器用于构建发送给微服务的请求,并 
使用 apache httpclient 或 netflix ribbon 请求微服务。 
c、post: 这种过滤器在路由到微服务以后执行。这种过滤器可用来为响应添加标准的 http 
header、收集统计信息和指标、将响应从微服务发送给客户端等。 
e、error: 在其他阶段发送错误时执行该过滤器。执行流程如图:

7、更多的zuul配置,大家可以根据具体业务场景选择使用:

spring.application.name=nandao-zuul
server.port=7070
#zuul网关是必须要配合注册中心使用,因为zuul要去eureka去服务列表
eureka.client.serviceUrl.defaultZone=http://admin:admin@localhost:8763/eureka/

# 使用路径方式匹配路由规则。
# 参数key结构: zuul.routes.customName.path=xxx
# 用于配置路径匹配规则。
# 其中customName自定义。通常使用要调用的服务名称,方便后期管理
# 可使用的通配符有: * ** ?
# ? 单个字符
# * 任意多个字符,不包含多级路径
# ** 任意多个字符,包含多级路径
zuul.routes.micro-web.path=/web/**
#
## 参数key结构: zuul.routes.customName.url=xxx
## url用于配置符合path的请求路径路由到的服务地址。
##zuul.routes.micro-order.url=http://localhost:8080/
#
## key结构 : zuul.routes.customName.serviceId=xxx
## serviceId用于配置符合path的请求路径路由到的服务名称。
zuul.routes.micro-web.serviceId=micro-web-no

#zuul.routes.micro-web1.path=/web/path/**
#zuul.routes.micro-web1.serviceId=micro-web-no
# ignored service id pattern
# 配置不被zuul管理的服务列表。多个服务名称使用逗号','分隔。
# 配置的服务将不被zuul代理。
#zuul.ignored-services=eureka-application-service

# 此方式相当于给所有新发现的服务默认排除zuul网关访问方式,只有配置了路由网关的服务才可以通过zuul网关访问
# 通配方式配置排除列表,即不走zuul 网关的列表
zuul.ignored-services=*
#
## 通配方式配置排除网关代理路径。所有符合ignored-patterns的请求路径都不被zuul网关代理。
zuul.ignored-patterns=/**/local/**

# prefix URL pattern 前缀路由匹配
# 配置请求路径前缀,所有基于此前缀的请求都由zuul网关提供代理。
#zuul.prefix=/api
#
management.endpoints.web.exposure.include=*
#
## http://localhost:6060/actuator/hystrix.stream
##针对某个服务传输指定的headers信息 ,默认是过滤掉 Cookie,Set-Cookie,Authorization 这三个信息的
##这里置空就是不要过滤掉这三个
#zuul.routes.micro-web.sensitive-headers=
#
##指定全局的headers传输,对所有路由的微服务
##zuul.sensitive-headers=Cookie,Set-Cookie,Authorization
#
##添加host头信息,标识最初的服务端请求地址
zuul.add-host-header=true
#
##默认添加  X-Forwarded-*头域
#zuul.add-proxy-headers=true

#路由到本地服务
zuul.routes.zuul-server.path=/local/**
zuul.routes.zuul-server.url=forward:/local

#路由带固定ip 服务
zuul.routes.blog.path=/blog/**
zuul.routes.blog.url=http://localhost:8083/
#
##全局关闭重试
#zuul.retryable=false
##关闭该服务的重试
zuul.routes.micro-web.retryable=false

#zuul网关插件包含ribbon和hystrix 功能
#ribbon.ConnectTimeout=2000
#ribbon.ReadTimeout=10000
#hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds=100000

8、zuul网关的实战使用就到这里,下篇我们介绍springcloud  admin管理页面使用!敬请期待!

猜你喜欢

转载自blog.csdn.net/nandao158/article/details/108189703