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. STDIN_FILENO和stdin

    title: STDIN_FILENO和stdin date: 2019/03/15 22:03:22 toc: true --- STDIN_FILENO和stdin https://blog.cs ...

  2. neufz

    ~~~~1--1--5--2020/4/23 08:50:30|1--1--5--2020/4/23 08:50:30|1--1--5--2020/4/23 08:50:30|1--1--5--202 ...

  3. java ,用公司框架写的显示列表 Table控件

    <!DOCTYPE html> <html lang="en" xmlns:th="http://www.thymeleaf.org" lan ...

  4. docker安装elasticsearch

    docker search elasticsearch 选择一个版本,拉取镜像 docker pull elasticsearch: 查看镜像 docker images 通过镜像,启动一个容器,并将 ...

  5. API设计中响应数据格式用json的优点

    通常我们再设计api返回时,都使用json格式返回,相比xml,他又有什么优点呢? 更直观易懂 占用空间更小 能与JavaScript跟好的兼容.js通过eval()进行Json读取. 支持多种语言. ...

  6. BLSTM的训练算法、解码算法以及模型的改进

    摘要 BLSTM解码时,解码器需要等待整个音频到达后才开始解码,因为时间反方向的前向传播需要末尾的历史信息.BLSTM这一延时问题使其不适用与实时语音识别.context-sensitive-chun ...

  7. Eclipse 设置生成带有属性注释的getter/setter方法

    1.  在开发中,一般类注释常注释在字段上面.但eclipse工具自动生成的getter和setter方法是没有注释的,而且还需要自己手动添加,这样比较麻烦.下面介绍如何通过修改eclipse的jar ...

  8. Request method 'POST' not supported解决办法

    (1)考虑拦截器是否将该链接拦截

  9. pythonのdjango Form简单应用。

    Form表单有两种应用场景: 1.生成HTML标签. 2.验证输入内容. 如果我们在django程序中使用form时,需要在views中导入form模块 from django import form ...

  10. 利用C# 窗体设计 写一个抽奖游戏

    老师布置了一个任务,要求我们做一个抽奖游戏,以下是我个人制作的一个作品与写项目的过程. 我们用到了8个pictureBox控件和一个button,设置好大小,并且编排成一个九宫个形状 添加窗体的背景图 ...