Zuul的主要功能是路由和过滤器。路由功能是微服务的一部分,zuul实现了负载均衡。

1.1

新建模块zuul

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<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">
<parent>
<artifactId>spring-cloud</artifactId>
<groupId>com.feng</groupId>
<version>0.0.1</version>
</parent>
<modelVersion>4.0.0</modelVersion> <artifactId>zuul</artifactId> <dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
</dependencies> </project>

1.2

application.yml:

spring:
application:
name: zuul server:
port: 8020 eureka:
client:
service-url:
defaultZone: http://localhost:8010/eureka/ zuul:
routes:
api-a:
path: /service-a/**
serviceId: service-a
api-b:
path: /service-b/**
serviceId: service-b
sensitive-headers: #设置忽略的头信息,设置为空能解决会话保持问题
add-host-header: true #设为true才能保持Host头信息处理正确

上面配置说明把/service-a/开头的所有请求都转发给service-a服务执行,把/service-b/开头的所有请求都转发给service-b服务执行

1.3

ZuulApplication,使用EnableZuulProxy注解开启zuul功能,并且注册了一个zuul的过滤器

/**
* @author fengzp
* @date 17/4/27
* @email fengzp@gzyitop.com
* @company 广州易站通计算机科技有限公司
*/
@SpringCloudApplication //这个注解整合了@SpringBootApplication、@EnableDiscoveryClient、@EnableCircuitBreake
@EnableZuulProxy
public class ZuulApplication { public static void main(String[] args) {
SpringApplication.run(ZuulApplication.class, args);
} @Bean
public MyZuulFilter getZuulFilter(){
return new MyZuulFilter();
}
}

MyZuulFilter,过滤器加了个判断id是否为空,空则返回401错误码

public class MyZuulFilter extends ZuulFilter {

    private static Logger log = LoggerFactory.getLogger(MyZuulFilter.class);

    /**
* 返回过滤器类型
* @return
* pre:可以在请求被路由之前调用
* routing:在路由请求时候被调用
* post:在routing和error过滤器之后被调用
* error:处理请求时发生错误时被调用
*/
@Override
public String filterType() {
return "pre";
} /**
* 通过int值来定义过滤器的执行顺序
*/
@Override
public int filterOrder() {
return 0;
} /**
* 判断过滤器是否执行
*/
@Override
public boolean shouldFilter() {
return true;
} /**
* 过滤器的具体逻辑
* ctx.setSendZuulResponse(false)令zuul不允许请求,
* ctx.setResponseStatusCode(401)设置了其返回的错误码
* ctx.setResponseBody(body)编辑返回body内容
* @return
*/
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext(); HttpServletRequest request = ctx.getRequest(); log.info(String.format("%s request to %s", request.getMethod(), request.getRequestURL().toString()));
Object accessToken = request.getParameter("id");
if(accessToken == null) {
log.warn("id is null");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
} log.info("pass!");
return null;
} }

1.4

把之前负载的service-b的spring.application.name改为service-b, 运行项目,分别打开http://localhost:8020/service-a/hi?id=zuul和http://localhost:8020/service-b/hi?id=zuul



spring cloud学习(四) 动态路由的更多相关文章

  1. Spring Cloud Gateway的动态路由怎样做?集成Nacos实现很简单

    一.说明 网关的核心概念就是路由配置和路由规则,而作为所有请求流量的入口,在实际生产环境中为了保证高可靠和高可用,是尽量要避免重启的,所以实现动态路由是非常有必要的:本文主要介绍 Spring Clo ...

  2. Spring Cloud Zuul的动态路由怎样做?集成Nacos实现很简单

    一.说明 网关的核心概念就是路由配置和路由规则,而作为所有请求流量的入口,在实际生产环境中为了保证高可靠和高可用,是尽量要避免重启的,所以实现动态路由是非常有必要的:本文主要介绍实现的思路,并且以Na ...

  3. spring cloud学习(四) Fegin 的使用

    Feign 的使用 什么是Feign? Feign : Declarative REST clients. Feign 是一个声明web服务客户端,这便得编写web服务客户端更容易,使用Feign 创 ...

  4. Spring Cloud Gateway之动态路由(数据库版)

    1.实现动态路由的关键是RouteDefinitionRepository接口,该接口存在一个默认实现(InMemoryRouteDefinitionRepository) 通过名字我们应该也知道该实 ...

  5. Spring Cloud 学习 (四) Hystrix & Hystrix Dashboard & Turbine

    在复杂的分布式系统中,可能有几十个服务相互依赖,这些服务由于某些原因,例如机房的不可靠性.网络服务商的不可靠性等,导致某个服务不可用 . 如果系统不隔离该不可用的服务,可能会导致整个系统不可用.Hys ...

  6. Spring Cloud Zuul实现动态路由

    1.添加依赖 2.启动类上添加注解 3.配置文件 zuul.ignored-services配置需要忽略的服务,多个用逗号分隔 注释zuul.ignored-services 前: 注释zuul.ig ...

  7. Spring Cloud Alibaba | Nacos动态网关路由

    Spring Cloud Alibaba | Gateway基于Nacos动态网关路由 本篇实战所使用Spring有关版本: SpringBoot:2.1.7.RELEASE Spring Cloud ...

  8. Spring Cloud(四):服务容错保护 Hystrix【Finchley 版】

    Spring Cloud(四):服务容错保护 Hystrix[Finchley 版]  发表于 2018-04-15 |  更新于 2018-05-07 |  分布式系统中经常会出现某个基础服务不可用 ...

  9. spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号)

    spring cloud: zuul(四): 正则表达式匹配其他微服务(给其他微服务加版本号) 比如我原来有,spring-boot-user微服务,后台进行迭代更新,另外其了一个微服务: sprin ...

随机推荐

  1. 使用Simple MvvmToolkit开发Android和iOS程序

    详情见:Android and iOS Development with Simple MVVM Toolkit? Yes you can! :http://blog.tonysneed.com/20 ...

  2. Python学习杂记

    Python中关键字yield有什么作用? 首先得理解generators,而理解generators前还要理解iterables: 你可以用在for...in...语句中的都是可迭代的:比如list ...

  3. 2018.12.12 codeforces 931E. Game with String(概率dp)

    传送门 感觉这题难点在读懂题. 题目简述:给你一个字符串s,设将其向左平移k个单位之后的字符串为t,现在告诉你t的第一个字符,然后你可以另外得知t的任意一个字符,求用最优策略猜对k的概率. 解析: 预 ...

  4. 容器监控告警方案(cAdvisor + nodeExporter + alertmanager + prometheus +grafana)

    一.prometheus基本架构 Prometheus 是一套开源的系统监控报警框架.它启发于 Google 的 borgmon 监控系统,由工作在 SoundCloud 的 google 前员工在 ...

  5. if结构和逻辑运算符

    一 :if选择结构 语法结构: 01.单个if if(表达式){ 如果满足表达式 则执行的代码 } 02.if(表达式) else if(表达式){ 如果满足表达式 则执行的代码 }else{ 不满足 ...

  6. 【设计模式】Javascript设计模式——状态模式(行为型)

    注:这个模式是非常聪明的,很有点数学中组合的意思,现在,来看下这个模式是怎么个思想. 问题提出:假如某个操作有三种可能,分别为1,2,3,还可能是组合,比如先执行1,再执行2或者先执行2再执行3或者1 ...

  7. $.contains(a,b)

    jQuery.contains()函数用于判断指定元素内是否包含另一个元素. 简而言之,该函数用于判断另一个DOM元素是否是指定DOM元素的后代. 该函数属于全局jQuery对象. 语法 jQuery ...

  8. 2.2.2synchronized同步代码块的使用

    当两个并发线程访问同一个对象object中的synchronized(this)同步代码块时,一段时间内只能有一个线程执行,另一个线程必须等待期执行完才能执行. package com.cky.bea ...

  9. Ng第八课:神经网络表述(Neural Networks: Representation)

    8.1  非线性假设 8.2  神经元和大脑 8.3  模型表示 1 8.4  模型表示 2 8.5  特征和直观理解 1 8.6  样本和直观理解 II 8.7  多类分类 8.1  非线性假设 无 ...

  10. 20171126-handler消息机制理解

    1.handler消息机制的理解 http://www.jianshu.com/p/8343a39b8a2c?s_q_s_h_a_r_e_1MTAzNTIwODAxNTExNTg5NTkwMzE0Nz ...