7、Spring -Cloud-路由网管Spring Cloud Zuul
7.1、为什么需要Zuul
Zuul 作为路由网关组件,在微服务架构中有着非常重要的作用:
7.2、Zuul的工作原理
请求的生命周期图:
7.3、案例
7.3.1、搭建 Zuul 服务
pom文件:
<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> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

spring.application.name=feign
server.port=
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
#开启Hystrix的功能
feign.hystrix.enabled=true
在下方配置文件的使用:
spring.application.name=hystric
server.port=
eureka.client.service-url.defaultZone=http://localhost:8762/eureka/
#开启Hystrix的功能
feign.hystrix.enabled=true
在下方配置文件的使用:
新建工程的配置类:
@EnableZuulProxy
@EnableEurekaClient
@SpringBootApplication
public class EurekaZuulClientApplication { public static void main(String[] args) {
SpringApplication.run(EurekaZuulClientApplication.class, args);
}
}
eureka:
client:
service-url:
defaultZone: http://localhost:8762/eureka/ server:
port:
spring:
application:
name: zuul zuul:
routes:
#自定义的
ribbonapi:
#路径
path: /ribbonapi/**
#引入的application(上述需要使用到的工程名)
serviceid: hystric feignapi:
path: /feignapi/**
serviceid: feign
zuul:
routes:
ribbonapi:
path: /ribbonapi/**
serviceid: hystric
url: http://localhost:8089
7.3.2、Zuul 上配置 API 接口的版本号
7.3.3、Zuul 上配置熔断器
package com.cr.eurekazuulclient.zull; import org.slf4j.LoggerFactory;
import org.springframework.cloud.netflix.zuul.filters.route.FallbackProvider;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.stereotype.Component; import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Logger; @Component
public class MyFallbackProvider implements FallbackProvider { @Override
public String getRoute() {
// 表明是为哪个微服务提供回退,*表示为所有微服务提供回退
return "hystric";
} public ClientHttpResponse fallbackResponse(){
return new ClientHttpResponse() {
@Override
public HttpStatus getStatusCode() throws IOException {
return HttpStatus.OK;
} @Override
public int getRawStatusCode() throws IOException {
return ;
} @Override
public String getStatusText() throws IOException {
return "OK";
} @Override
public void close() { } @Override
public InputStream getBody() throws IOException {
return new ByteArrayInputStream("The service is unavailable.".getBytes());
} @Override
public HttpHeaders getHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
return headers;
}
};
} @Override
public ClientHttpResponse fallbackResponse(String route, Throwable cause) { if (cause != null && cause.getCause() != null){
String reason = cause.getCause().getMessage();
System.out.println("exception:" + reason);
}
return fallbackResponse();
}
}
重启hystrix服务:
7.3.4、在Zuul中使用过滤器
过滤器的类型:
package com.cr.eurekazuulclient.zull; import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component; import javax.servlet.http.HttpServletRequest;
import java.util.logging.Logger; @Component
public class MyZuulFilter extends ZuulFilter { //private static Logger log= (Logger) LoggerFactory.getLogger(MyZuulFilter.class);
@Override
public String filterType() {
return "pre";
} @Override
public int filterOrder() {
return ;
} @Override
public boolean shouldFilter() {
return true;
} @Override
public Object run() throws ZuulException {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
Object accessToken = request.getParameter("token"); if (accessToken == null){
System.out.println("token is empty");
//log.warning("token is empty");
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode();
try {
ctx.getResponse().getWriter().write("token is empty");
}catch (Exception e){
return null;
}
}
return null;
}
}
此时请求:
此时加上参数token:
7.3.5、Zuul常见的使用方式
7、Spring -Cloud-路由网管Spring Cloud Zuul的更多相关文章
- spring boot 2.0.3+spring cloud (Finchley)5、路由网关Spring Cloud Zuul
Zuul作为微服务系统的网关组件,用于构建边界服务,致力于动态路由.过滤.监控.弹性伸缩和安全. 为什么需要Zuul Zuul.Ribbon以及Eureka结合可以实现智能路由和负载均衡的功能:网关将 ...
- spring cloud 路由
Spring Cloud Feign:用于微服务之间,只映射内网ip Spring Cloud Gateway:用于服务端,对外开放的接口,对外统一访问gateway映射的ip 是这样吗? 但是这样权 ...
- 一起来学spring Cloud | 第一章:spring Cloud 与Spring Boot
目前大家都在说微服务,其实微服务不是一个名字,是一个架构的概念,大家现在使用的基于RPC框架(dubbo.thrift等)架构其实也能算作一种微服务架构. 目前越来越多的公司开始使用微服务架构,所以在 ...
- Spring Cloud(六)服务网关 zuul 快速入门
服务网关是微服务架构中一个不可或缺的部分.通过服务网关统一向外系统提供REST API的过程中,除了具备服务路由.均衡负载功能之外,它还具备了权限控制等功能.Spring Cloud Netflix中 ...
- (2)java Spring Cloud+Spring boot+mybatis企业快速开发架构之SpringCloud-Spring Cloud是什么?Spring Cloud版本介绍
Spring Cloud 是一系列框架的有序集合.它利用 Spring Boot 的开发便利性,巧妙地简化了分布式系统基础设施的开发,如服务注册.服务发现.配置中心.消息总线.负载均衡.断路器.数 ...
- spring cloud 入门系列六:使用Zuul 实现API网关服务
通过前面几次的分享,我们了解了微服务架构的几个核心设施,通过这些组件我们可以搭建简单的微服务架构系统.比如通过Spring Cloud Eureka搭建高可用的服务注册中心并实现服务的注册和发现: 通 ...
- 微服务领域是不是要变天了?Spring Cloud Alibaba正式入驻Spring Cloud官方孵化器!
引言 微服务这个词的热度自它出现以后,就一直是高烧不退,而微服务之所以这么火,其实和近几年互联网的创业氛围是分不开的. 与传统行业不同,互联网企业有一个特点,那就是市场扩张速度非常之快,可能也就是几天 ...
- spring boot 2.0.3+spring cloud (Finchley)6、配置中心Spring Cloud Config
https://www.cnblogs.com/cralor/p/9239976.html Spring Cloud Config 是用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持, ...
- spring cloud (一):大话 Spring Cloud
转自:http://www.ityouknow.com/springcloud/2017/05/01/simple-springcloud.html 研究了一段时间Spring Boot了准备向Spr ...
随机推荐
- HDU 1874(简单最短路) (大优化)
优先队列那里用greater会报错 http://acm.hdu.edu.cn/showproblem.php?pid=1874 /* 使用pair代替结构 */ #include <iostr ...
- jenkins学习笔记
Jenkins 是一款流行的开源持续集成(Continuous Integration)工具,广泛用于项目开发,具有自动化构建.测试和部署等功能.本系列博客以 windows 10 环境为例 1 安装 ...
- cf1056B. Divide Candies(数论 剩余系)
题意 题目链接 求满足\(i^2 + j^2 \% M = 0\)的数对\((i, j)\)的个数,\(1 \leqslant i, j \leqslant 10^9, M \leqslant 100 ...
- int占几个字节?
class Program19 { static void Main(string[] args) { // true,或false Console.WriteLine("bool占用:&q ...
- CC150相关问题
18.9 动态计算中位数 利用两个堆:一个最大堆,存放小于中位数的值:一个最小堆,存放大于中位数的值. 则两个堆的堆顶即为数组中最中间的两个数. 在插入新元素的时候,我们只要维护两个堆, 使其堆中元素 ...
- Visual Staudio 2015 打开指定文件,定位到指定文件目录下
Visual Staudio 2015 项目定位文件位置 每次使用的Visual Staudio 2015 写代码的时候总是打开了.cs文件或xaml文件时, 还要手动去找该 文件位置,每次都要翻好大 ...
- 词法分析器Lexer
词法分析 In computer science, lexical analysis, lexing or tokenization is the process of converting a se ...
- time random sys 模块
time模块 顾名思义就是时间模块 我们在之前就用过一些时间模块 比如你想要让打印的时间延迟就time.sleep() 首先我们知道这是一个时间操作的模块 它可以分为三种模式:时间戳模式.格式化时间模 ...
- 二、Asp.Net Core WebAPI——OcelotDemo
项目源码OcelotDemo 基础知识在教程或者官网文档查看 Ocelot源码 基于.NET平台的Ocelot网关框架教程汇总 这篇文章不错. 这里我只写我想说的 项目结构 API1和API2是测试的 ...
- 辉光的UIView
辉光的UIView 辉光UIView使用了一个UIView的一个category,名为UIView+Glow,请自行到github上查找. 源码如下: // // RootViewController ...