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. Vitamio与FFmpeg、LGPL、GPL的关系

    转自:http://sun.sanniang.me/2014/04/26/the-relationship-vitamio-with-ffmepg-lgp-gpl Vitamio 使用了 FFmpeg ...

  2. 《转》python学习--基础上

    学习的python本来想自己总结,但是发现了一篇不错的大牛的博客,拿来主义,,又被我实践了 关于前两篇如果总结的不详细,因此把他人的转载过来 http://www.cnblogs.com/BeginM ...

  3. HTTP/2笔记之连接建立

    前言 HTTP/2协议在TCP连接之初进行协商通信,只有协商成功,才会涉及到后续的请求-响应等具体的业务型数据交换. HTTP版本标识符 h2,基于TLS之上构建的HTTP/2,作为ALPN的标识符, ...

  4. su命令cannot set groups: Operation not permitted的解决方法

    版权声明:本文由曾倩倩原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/103 来源:腾云阁 https://www.qclo ...

  5. {sharepoint} SetPermission

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Microsof ...

  6. 问答项目---账号密码异步校验后进行PHP校验

    在做登陆的时候,通过异步校验后还需要通过PHP来校验账号和密码的正确性. PHP校验账号密码: public function login(){ if(!IS_POST){echo "页面不 ...

  7. Firefox不能加载TinyMCE的问题

    不知Firefox的哪个版本开始,经常不能正常加载TinyMCE. 经过分析发现,出问题时总是停留在加载advhr插件(tiny_mce/plugins/advhr/editor_plugin.js) ...

  8. C++ Websites

    C++ Websites C++ 推荐网站 1.cprogramming.com 2.cppreference.com 3.cplusplus.com 4.Boost C++ Library

  9. docker 2375 vulnerability and self-signatuer certifications

    Docker暴露2375端口,引起安全漏洞 今天有小伙伴发现Docker暴露出2375端口,引起了安全漏洞.我现在给大家介绍整个事情的来龙去脉,并告诉小伙伴们,怎么修复这个漏洞. 为了实现集群管理,D ...

  10. Java字符编码问题

    今天研究了一下,记录下来 中间用的是redis,可以使用任意其他的io替代,一样的 Test1 String s1 = "我要测试"; String s2 = "I wa ...