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. Oracle ORA-08104报错处理方法及注意事项

    [环境介绍] 系统环境:IBM P740 8205-E6C (AIX) + 11.2.0.3.0 Oracle RAC [背景介绍] 故障描述:数据库表空间超过90%,无法进行扩容表空间,需要业务侧清 ...

  2. 一张图11招学会Python网络黑客

    全部学起来: 第一招:搭建Python防范环境 第二招:扫描漏洞 第三招:暴力破解的秘密 第四招:防SQL注入 第五招:防命令注入 第六招:看清文件上传木马 第七招:看清Web攻击 第八招:利用Pyt ...

  3. JAVA通过http访问其他服务端API

    项目要实现这么一个功能,用户通过点击按钮,通过axios来访问python的API(算法,java不好做)得到一个结果存储到数据库并且返回到页面. 但是python不是在tomcat上面运行的,所以不 ...

  4. mysql常用操作(一)

    [数据库设计的三大范式]1.第一范式(1NF):数据表中的每一列,必须是不可拆分的最小单元.也就是确保每一列的原子性. 例如:userInfo:'山东省烟台市 18865518189' 应拆分成 us ...

  5. luogu P5324 [BJOI2019]删数

    传送门 不如先考虑暴力,能删的序列首先有\(1,2,3...n\),还有就是升序排序后从后往前放数,第\(i\)位要么放\(i\),要么放\(i+1\)位置的数,例如\(1,2,4,4,5,6,9,9 ...

  6. window C/C++ 简单的IDE编译器

    C-Free 官网链接: http://www.programarts.com/cfree_ch/download.htm

  7. 贪吃蛇游戏——C语言双向链表实现

    采用了双向链表结点来模拟蛇身结点: 通过C语言光标控制函数来打印地图.蛇身和食物: /************************** *************************** 贪吃 ...

  8. [转载]在termux上安装Kali Linux

    最近在手机上下了个Termux,然后想装个kali,就找到了这篇文章. 不过其中的命令有一处错误(在我进行配置的时候报错了): 命令应该是 ./atilo install kali

  9. tomcat启动后产生的日志

    产生的日志  相当于 定时选取webapps里面搜索有没有超时的session,然后将超时的session关掉. 每一个webapp 都是独立的 一个application对应一个context,se ...

  10. vscode中vue代码颜色插件

    vue提示插件[Vscode]       编者寄语:vscode的确是前端开发中很好的工具,安装颜色插件,从视觉上是美的享受.曾经的我遇到了vscode代码全是灰色,黑色的困惑,后来整理找到方法,整 ...