springcloud16---zuul-filter

Posted 672530440

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了springcloud16---zuul-filter相关的知识,希望对你有一定的参考价值。

package com.itmuch.cloud;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;

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

  @Bean
  public PreZuulFilter preZuulFilter() {
    return new PreZuulFilter();
  }
}
package com.itmuch.cloud;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;

//http://localhost:8040/routes  :   {"/microservice-provider-user/**":"microservice-provider-user"}

//http://localhost:8040/microservice-provider-user/simple/1 通过zuul访问user微服务并且使用过滤器
public class PreZuulFilter extends ZuulFilter {
  private static final Logger LOGGER = LoggerFactory.getLogger(PreZuulFilter.class);

  @Override   //事先判断是否要使用这个过滤器
  public boolean shouldFilter() {
    return true;
  }

  @Override
  public Object run() {  //过滤器的逻辑
    HttpServletRequest request = RequestContext.getCurrentContext().getRequest();
    String host = request.getRemoteHost();
    PreZuulFilter.LOGGER.info("请求的host:{}", host);
    return null;
  }

  @Override  //过滤器类型
  public String filterType() {
    return "pre";
  }

  @Override  //执行顺序,越大越靠后
  public int filterOrder() {
    return 1;
  }

}
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
    
    
hystrix.command.default.execution.isolation.thread.timeoutInMilliseconds: 60000
ribbon:
  ConnectTimeout: 3000
  ReadTimeout: 60000
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  ignoredServices: microservice-consumer-movie-ribbon-with-hystrix
  routes:
    microservice-provider-user: /user/**
    
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  routes:
    abc:
      path: /user-path/**
      serviceId: microservice-provider-user
    
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  routes:
    abc:
      path: /user-url/**
      url: http://192.168.85.1:7900/
    
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  routes:
    abc:
      path: /user-url/**
      service-id: microservice-provider-user
ribbon:
  eureka:
    enabled: false
microservice-provider-user:     # 这边是ribbon要请求的微服务的serviceId
  ribbon:
    listOfServers: http://localhost:7900,http://localhost:7901
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  prefix: /api
  strip-prefix: false
logging:
  level:
    com.netflix: DEBUG
spring:
  application:
    name: microservice-gateway-zuul
server:
  port: 8040
eureka:
  client:
    service-url:
      defaultZone: http://user:password123@localhost:8761/eureka
  instance:
    prefer-ip-address: true
zuul:
  prefix: /simple
  strip-prefix: false
logging:
  level:
    com.netflix: debug
<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>com.itmuch.cloud</groupId>
        <artifactId>microservice-spring-cloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </parent>

    <artifactId>microservice-gateway-zuul-filter</artifactId>
    <packaging>jar</packaging>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <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>
    </dependencies>

</project>

 

以上是关于springcloud16---zuul-filter的主要内容,如果未能解决你的问题,请参考以下文章

springcloud01-SpringCloud 概述

SpringCloud系列SpringCloud概述及微服务技术栈的使用

springcloud2020版本使用zuul和nacos服务发现

springcloud情操陶冶-初识springcloud

SpringCloud学习系列-SpringCloud

springcloud nacos 跨服务调用失败