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的更多相关文章

  1. spring-cloud-Zuul学习(四)【中级】--自定义zuul Filter详解【重新定义spring cloud实践】

      实现自定义zuul Filter 方法很简单,只要继承ZuulFilter跟加入到spring IOC容器即可,zuulFilter是一个抽象类,里面包含以下方法需要我们实现: String fi ...

  2. Spring Cloud(七)服务网关 Zuul Filter 使用

    上一篇文章中,讲了Zuul 转发,动态路由,负载均衡,等等一些Zuul 的特性,这个一篇文章,讲Zuul Filter 使用,关于网关的作用,这里就不再次赘述了,重点是zuul的Filter ,我们可 ...

  3. zuul filter

    前言 过滤器是Zuul的核心组件,这篇文章我们来详细讨论Zuul的过滤器.下面话不多说,来看看详细的介绍吧. 过滤器类型与请求生命周期 Zuul大部分功能都是通过过滤器来实现的.Zuul中定义了四种标 ...

  4. Spring Cloud Zuul Filter 和熔断

    转一篇很不错的关于Spring Cloud Zuul 相关用法的文章,基本包含常用的一些场景,另外附上实际项目中的熔断.打印请求日志和登录验证的实例. 原文地址:https://www.cnblogs ...

  5. Spring Cloud Zuul网关 Filter、熔断、重试、高可用的使用方式。

    时间过的很快,写springcloud(十):服务网关zuul初级篇还在半年前,现在已经是2018年了,我们继续探讨Zuul更高级的使用方式. 上篇文章主要介绍了Zuul网关使用模式,以及自动转发机制 ...

  6. spring-cloud-Zuul学习(三)【中级篇】--Filter链 工作原理与Zuul原生Filter【重新定义spring cloud实践】

    这里开始记录zuul中级进阶内容.前面说过了,zuul主要是一层一层的Filter过滤器组成,并且Zuul的逻辑引擎与Filter可用其他基于JVM的语言编写,比如:Groovy. 工作原理 Zuul ...

  7. zuul过滤器filter 的编写

    通过上一节(zuul的各种配置)的学习,我们学会了zuul路由的各种配置,这一节我们来实现一下zuul的过滤器功能.那么为什么需要用到zuul的过滤器呢?我们知道zuul是我们实现外部系统统一访问的入 ...

  8. Spring REST 与 Zuul 代理

    http://www.baeldung.com/spring-rest-with-zuul-proxy 作者: Eugen Paraschiv 译者: http://oopsguy.com 1.概述 ...

  9. zuul入门(3)开发zuul的过滤器

    1.编写Zuul过滤器(Java&Groovy) 理解过滤器类型和请求生命周期后,我们来编写一个Zuul过滤器.编写Zuul的过滤器非常简单,我们只需继承抽象类ZuulFilter,然后实现几 ...

  10. Netflix网关zuul(1.x和2.x)全解析

    zuul 是netflix开源的一个API Gateway 服务器, 本质上是一个web servlet应用. Zuul可以通过加载动态过滤机制,从而实现以下各项功能: 验证与安全保障: 识别面向各类 ...

随机推荐

  1. PyQt4颜色对话框QColorDialog

    QColorDialog提供了用于显示颜色的对话框. #!/usr/bin/python # -*- coding: utf-8 -*- import sys from PyQt4 import Qt ...

  2. (转)淘淘商城系列——中文分析器IK-Analyzer的使用

    在Solr中默认是没有中文分析器的,需要手工配置,配置一个FieldType,在FieldType中指定使用的中文分析器.另外,Solr中的字段(即业务域)必须先定义后使用.下面我们先把中文分析器配好 ...

  3. 《转》python学习(5)--数据类型

    转自 http://www.cnblogs.com/BeginMan/archive/2013/06/08/3125876.html 一.标准类型函数 cmp():比较大小 str():转换为字符串 ...

  4. java三方---->dom4j解析xml数据

    Dom4j是一个易用的.开源的库,用于XML,XPath和XSLT.它应用于Java平台,采用了Java集合框架并完全支持DOM,SAX和JAXP.今天我们就开始Dom4j的学习. Dom4j的使用 ...

  5. Windows Phone 页面切换动画

    1.首先引用Microsoft.Phone.Toolkit 2.将App.xaml.cs 中的 RootFrame = new PhoneApplicationFrame(); 改成RootFrame ...

  6. Android通知栏的高度获取

    public static int getStatusBarHeight(Context context){ Class<?> c = null; Object obj = null; F ...

  7. ehcache加载配置文件ehcache.xml的源码

    package net.sf.ehcache.config; public final class ConfigurationFactory { public static Configuration ...

  8. Jenkins构建时提示maven版本问题

    在使用Jenkins进行项目构建的时候出现下面问题 [INFO] Scanning for projects... [WARNING] [WARNING] Some problems were enc ...

  9. centos7yum下载安装报:软件包与预期下载的不符。建议:运行 yum --enablerepo=updates clean metadata尝试其他镜像

    centos7yum下载安装报:软件包与预期下载的不符.建议:运行 yum --enablerepo=updates clean metadata尝试其他镜像. rm -f /var/lib/rpm/ ...

  10. Python爬虫基础(四)Requests库的使用

    requests文档 首先需要安装:pip install requests get请求 最基本的get: # -*- coding: utf-8 -*-import requests respons ...