SpringCloud组件五之Zuul-网关(G版本)

墨蓝 2022-04-24 04:00 245阅读 0赞

一.Zuul简介

Zuul是Netflix开源的微服务网关,可以和Eureka、Ribbon、Hystrix等组件配合使用,Spring Cloud对Zuul进行了整合与增强,Zuul的主要功能是路由转发和过滤器。

二.项目现状

一个注册中心(registry),服务提供微服务(provider),消费者(ribbon),消费者(feign)。
在这里插入图片描述

三.注册网关服务

  • POM文件

    <?xml version=”1.0” encoding=”UTF-8”?>


    4.0.0

    org.springframework.boot
    spring-boot-starter-parent
    2.1.4.RELEASE


    cn.lll
    zuul
    0.0.1-SNAPSHOT
    zuul
    Demo project for Spring Boot


    1.8
    Greenwich.SR1




    org.projectlombok
    lombok
    1.18.4


    org.springframework.boot
    spring-boot-starter-web


    org.springframework.cloud
    spring-cloud-starter-netflix-eureka-client


    org.springframework.cloud
    spring-cloud-starter-netflix-zuul



    org.springframework.boot
    spring-boot-starter-test
    test






    org.springframework.cloud
    spring-cloud-dependencies
    ${ spring-cloud.version}
    pom
    import







    org.springframework.boot
    spring-boot-maven-plugin




  • yml配置文件

    server:
    port: 8084

    spring:
    application:

    1. name: zuul-server

    eureka:
    client:

    1. service-url:
    2. defaultZone: http://127.0.0.1:10010/eureka
  • 启动类添加注解

    @SpringBootApplication
    @EnableEurekaClient
    public class ZuulApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(ZuulApplication.class, args);
    3. }

    }

四.开启网关服务

  • 启动类上添加注解

    @SpringBootApplication
    @EnableEurekaClient
    @EnableZuulProxy
    public class ZuulApplication {

    1. public static void main(String[] args) {
    2. SpringApplication.run(ZuulApplication.class, args);
    3. }

    }

  • 配置文件添加配置
    我们的目的在于:127.0.0.1:8084/feign/** 转发到 feign-server服务,以下的两种配置都可以

    zuul:
    routes:

    1. FEIGN-SERVER: /feign/** api-a: path: /ribbon/** serviceId: RIBBON-SERVER

五.过滤器

  • 编写自己的Filter类继承ZuulFilter,实现其中的抽象方法,并实例化实体类注入IOC容器。

    @Component
    @Slf4j
    public class MyFilter extends ZuulFilter {

    1. /** * filterType表示返回一个字符串代表过滤器的类型 * pre:路由之前 * routing:路由之时 * post: 路由之后 * error:发送错误调用 * @return */
    2. @Override
    3. public String filterType() {
    4. return "pre";
    5. }
    6. /** * 过滤的顺序 * @return */
    7. @Override
    8. public int filterOrder() {
    9. return 0;
    10. }
    11. /** * 是否要过滤,可添加逻辑 * @return */
    12. @Override
    13. public boolean shouldFilter() {
    14. return true;
    15. }
    16. /** * 过滤器的具体逻辑 * @return * @throws ZuulException */
    17. @Override
    18. public Object run() throws ZuulException {
    19. log.info("网关的过滤器正在执行!!!");
    20. return null;
    21. }

    }

发表评论

表情:
评论列表 (有 0 条评论,245人围观)

还没有评论,来说两句吧...

相关阅读

    相关 springcloud学习zuul

    uul网关有路由和过滤器等作用 简单使用路由部分功能: 路由功能:使用户不直接访问服务,而是访问网关,网关在去eureka上拉去服务然后比对用户请求是要请求哪个服务,...

    相关 springcloud zuul

    网关可以对请求进行过滤拦截,转发等,这种跟过滤器不一样,它可以拦截入口,而不是单独一个服务。 业务场景,会员服务和订单服务,用户必须登录才能调用相关接口(即含有userTok

    相关 SpringCloud路由zuul

    在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现、服务消费、负载均衡、断路器、智能路由、配置管理等,由这几个基础组件相互协作,共同组建了一个简单的微服务系统。一个