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中整数类型后面的数字,比如int(11),11代表11个字节吗?

    原先对mysql不太理解,但也没有报错.但理解的不够深入.这次补上. 原来以为int(11)是指11个字节,int(10)就是10个字节.我错了. http://zhidao.baidu.com/li ...

  2. maven仓库有jar包还是报错怎么办?

    出现这种情况通常是jar不能自动下载 一.下载jar包外的其他文件,并放到仓库对应路径下: 点击View All,下载其他文件并放到仓库 二.这样仓库这个对应jar包的文件就齐全了.如果还是报错,请检 ...

  3. vue base64

    安装 cnpm install js-base64 --save 使用 let base64 = require('js-base64').Base64 base64.encode('要加密的内容') ...

  4. Nuxt.js笔记

    前置知识 SSR服务器渲染 Vue SSR(server side rendering)服务端渲染 和 Vue SPA(single page application)单页应用 Vue SSR-> ...

  5. CSS部分语法2

    <!-- 第1部分 尺寸与框模型 略 第2部分背景设置 2.1 背景颜色:background-color:value 2.2 背景图片:background-image body{ backg ...

  6. [C++]类成员返回语句 return *this 的理解

    经常会在类似 copy-assignment 的成员函数看到返回语句 return *this ,这类函数通常返回类型是所属类的引用. 类成员函数的隐式指针 class *this const 经过 ...

  7. jpa Auditor 自动赋值与自定义 @CreatedBy @LastModifiedBy @CreatedDate @LastModifiedDate

    在spring jpa audit 中,在字段或者方法上使用注解@CreatedDate.@CreatedBy.@LastModifiedDate.@LastModifiedBy,当进行实体插入或者更 ...

  8. django --视图装饰器

  9. mysql常用操作(一)

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

  10. Linux-Shell编程之判断文件类型

    前言 如需使用本博文源码或者撰写文章,请注明博文来源:https://www.cnblogs.com/johnnyzen/p/10534386.html,劳动所得,侵权必究. 题目 設計一個shell ...