1.Zuul服务网关

  作用:路由转发和过滤,将请求转发到微服务或拦截请求。Zuul默认集成了负载均衡功能。

2.Zuul实现路由

  a.新建springboot项目,依赖选择 Eureka Discovery 、Web 以及 Zuul。

  b.pom文件关键依赖

    <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.3.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-zuul</artifactId>
</dependency> ...... </dependencies> ......

  

  c.application.yml文件

spring:
application:
name: zuul-server
server:
port: 8090 eureka:
client:
service-url:
defaultZone: http://localhost:8081/eureka/ zuul:
routes:
#自定义名称,建议使用微服务名称
eurekaClientConsumer:
#路径
path: /ribbon/**
#微服务名称
serviceId: eureka-client-consumer
#自定义名称,建议使用微服务名称
eurekaClientFeign:
#路径
path: /feign/**
#微服务名称
serviceId: eureka-client-feign

  d.启动类添加注解 @EnableEurekaClient 和 @EnableZuulProxy

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

3.Zuul实现过滤

  a.创建拦截器

@Component
public class TokenFilter extends ZuulFilter { /**
* 表示过滤类型(pre表示路由之前; routing表示路由当中;post表示路由之后;error表示路由发生错误)
*/
@Override
public String filterType() {
return "pre";
} /**
* 表示执行时序(0的优先级最高)
*/
@Override
public int filterOrder() {
return 0;
} /**
* 是否需要执行过滤
*/
@Override
public boolean shouldFilter() {
return true;
} @Override
public Object run() throws ZuulException {
System.out.println("[TokenFilter]进入过滤器");
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String token = request.getParameter("token");
if(StringUtils.isEmpty(token)) {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode(401);
return null;
}
return null;
}
}

微服务框架——SpringCloud(三)的更多相关文章

  1. 微服务框架——SpringCloud

    1.SpringCloud微服务框架 a.概念:SpringCloud是基于SpringBoot的微服务框架 b.五大神兽:Eureka(服务发现).Ribbon(客服端负载均衡).Hystrix(断 ...

  2. 微服务框架SpringCloud(Dalston版)学习 (一):Eureka服务注册与发现

    eureka-server eureka服务端,提供服务的注册与发现,类似于zookeeper 新建spring-boot工程,pom依赖: <dependency> <groupI ...

  3. 微服务框架SpringCloud与Dubbo

    #v1.0.0# 1.背景 Dubbo,是阿里巴巴服务化治理的核心框架,并被广泛应用于阿里巴巴集团的各成员站点.阿里巴巴近几年对开源社区的贡献不论在国内还是国外都是引人注目的,比如:JStorm捐赠给 ...

  4. 微服务框架——SpringCloud(二)

    1.Feign声明式服务调用(负载均衡+熔断器) a.概念:Feign是一个声明式的Web Service客户端,它的目的就是让Web Service调用更加简单.Feign整合了Ribbon和Hys ...

  5. 微服务框架——SpringCloud(四)

    1.Spring Cloud Config 分布式配置 a.Config服务器 ①新建springboot项目,依赖选择Config Server ②pom文件关键依赖 <parent> ...

  6. 转载 (三)surging 微服务框架使用系列之我的第一个服务(审计日志)

    (三)surging 微服务框架使用系列之我的第一个服务(审计日志)   前言:前面准备了那么久的准备工作,现在终于可以开始构建我们自己的服务了.这篇博客就让我们一起构建自己的第一个服务---审计日志 ...

  7. java框架之SpringCloud(1)-微服务及SpringCloud介绍

    微服务概述 是什么 业界大牛 Martin Fowler 这样描述微服务: 参考[微服务(Microservices)-微服务原作者Martin Flower博客翻译]. 下面是关于上述博客中的部分重 ...

  8. 基于Spring-Cloud的微服务框架设计

    基于Spring-Cloud的微服务框架设计 先进行大的整体的框架整理,然后在针对每一项进行具体的详细介绍

  9. 微服务框架Dubbo与Springcloud的区别

    微服务框架Dubbo与Springcloud的区别 微服务主要的优势如下: 1.降低复杂度 将原来偶合在一起的复杂业务拆分为单个服务,规避了原本复杂度无止境的积累.每一个微服务专注于单一功能,并通过定 ...

随机推荐

  1. MySQL学习笔记(七)使用AutoMySQLBackup工具自动备份MySQL数据库

    1.下载 wget https://nchc.dl.sourceforge.net/project/automysqlbackup/AutoMySQLBackup/AutoMySQLBackup%20 ...

  2. MySql存储过程 CURSOR循环

    游标 游标(Cursor)是处理数据的一种方法,为了查看或者处理结果集中的数据,游标提供了在结果集中一次一行或者多行前进或向后浏览数据的能力. 使用步骤 声明一个游标: declare 游标名称 CU ...

  3. windows 7 命令修改IP地址

    netsh interface ip set address "本地连接"  static 172.17.15.97 255.255.255.0 172.17.12.1

  4. 用SQL表达交并差操作

    交-并-差的处理 SQL语言:并运算UNION,交运算INTERSECT,差运算EXCEPT 基本语法形式: 子查询{UNION [ALL] | INTERSECT [ALL] | EXPECT [A ...

  5. MyRolan (快速启动小工具)

    类似 Rolan的快速启动工具. 启动后隐藏,当鼠标移至左上角时,窗口显示,点击项目可运行程序. GitHub地址: MyRolan . #if defined(UNICODE) && ...

  6. VS 测试printf 多参数 输出 i++ 和++i 结果

    代码如截图: 总结: printf 多参数中有运算时 是从右到左执行的: i++ 和 ++i 优先级是大于 赋值 =运算的: i++ 和++i 是平级的: i++ 先用在算,++i 先算在用: 从右往 ...

  7. Pytorch学习笔记(一)Numpy SciPy MatPlotlib Tutorial

    英文原文链接:http://cs231n.github.io/python-numpy-tutorial/ Numpy Numpy是Python中科学计算的核心库.它提供了一个高性能的多维数组对象,以 ...

  8. Windows如何上传代码到Github

    1.首先得安装git客户端 进入官网:https://git-scm.com/ ,点击右侧下载windows版本的软件包,然后双击安装就可以了. 安装完成之后,在开始菜单可以看到,此时,在想上传的文件 ...

  9. kettle使用记录

    1.linux系统抽取数据 export KETTLE_HOME=/home/oracle/Kettle/pdi-ce-6.1.0.1-196/data-integration export JAVA ...

  10. Tableau 之一 连接数据源

    导入数据源 与各类数据源建立连接关系,是使用tableau探索分析数据的第一步,本节内容包括: 数据源类型 连接数据源 数据源类型 打开tableau,可以在左侧窗口看到连接选项,目前tableau可 ...