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 ...
随机推荐
- android recycleView 简单使用二---分割线
转自:https://www.jianshu.com/p/b46a4ff7c10a RecyclerView没有像之前ListView提供divider属性,而是提供了方法 recyclerView. ...
- C# 批量 json 读取
// 方法一 //string test = "[{ 'CreateUser': 'CN=koujirou nishikawaOMHBK','CreateUserJ': '西川 公二郎'}, ...
- K:大数加法
相关介绍: 在java中,整数是有最大上限的.所谓大数是指超过整数最大上限的数,例如18 452 543 389 943 209 789 324 233和8 123 534 323 432 323 ...
- 在IIS中部署好WCF服务站点后,本机访问服务无问题,局域网中其他电脑访问不到
1.问题描述 在IIS中部署好WCF服务站点后,本机访问服务无问题,局域网中其他电脑访问不到. 2.解决方法 (1)控制面板 -> Windows防火墙 -> 高级设置 (2)属性 (3) ...
- MPU/SoC/Application Processor/Embedded OS
Everything has its principles and mechanisms which are designed by its creator and followed by its u ...
- C++ Knowledge series 1
Programming language evolves always along with Compiler's evolvement. 1. The C++ Object Model: Strou ...
- C#代码处理网页关于登录的code
作者:血饮狂龙链接:https://www.zhihu.com/question/49452639/answer/117294801来源:知乎著作权归作者所有,转载请联系作者获得授权. private ...
- PHP匿名函数(闭包)
匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数.最经常用作回调函数(callback)参数的值.当然,也有其它应用的情况. ...
- 使用Netty3或Netty4发布Http协议服务
现在是2018年1月11日18:12分,已经是下班时间了,小Alan今天给大家简单的介绍一下Netty,让大家以后在使用到Netty的时候能够有一定的了解和基础,这样深入学习Netty以及以后灵活应用 ...
- CPU纯软件半虚拟化技术
在2003年出现的Xen,使用了另外的一种半虚拟化的方案来解决x86架构下CPU的敏感指令问题.主要采用Hypercall技术.Guest OS的部分代码被改变,从而使Guest OS会将和特权指令相 ...