spring cloud学习笔记五 网关服务zuul
网关服务是指,客户端发送的请求不用直接访问特定的微服务接口,而且是经过网关服务的接口进行交互,网关服务再去到特定的微服务中进行调用。
网关服务的路由功能和Nginx的反向代理一样,所有的服务都先会来访问特定的服务器,然后通过这个服务器再去转发到指定的服务,这样对外界来说访问的是一个映射地址,真实的接口地址是不会暴露外界。
路由功能能够很好的帮助我们现在集群的流量,比如弃用超出限定值的请求和对请求量大的集群扩容。
除了路由功能,网关服务还为我们提供了过滤器功能,通过过滤器我们就可以鉴权验证。
一、spring cloud zuul路由配置
1.导入maven
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
<version>1.3.5.RELEASE</version>
</dependency>
2.在启动类上加上注解:@EnableZuulProxy ,表示这是一个网关服务
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
//这是一个网关服务
@EnableZuulProxy
public class ZuulDemoApplication { public static void main(String[] args) {
SpringApplication.run(ZuulDemoApplication.class, args);
}
}
3.配置application.properties文件,zuul中路由功能主要是通过配置application文件来操作,所以这个demo选用了properties文件格式,这是因为properties文件格式理解起来更容易一些。
spring.application.name=zuulDemo
server.port=8000 #这里的配置表示,访问/user直接重定向到eureka中的zuulFictitious这个serviceId,其中user是自定义的字段,但是这个字段必须唯一
zuul.routes.user.path=/user/**
zuul.routes.user.serviceId=zuulFictitious
二、spring cloud zuul过滤器
1.spring cloud zuul过滤器是创建一个实现类继承ZuulFilter这个类,这个类中有4个方法需要我们去实现:
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext; import javax.servlet.http.HttpServletRequest; public class ZuulDemoFilter extends ZuulFilter { //设置过滤器的类型,一共有4种
//pre:可以在请求被路由之前调用
//route:在路由请求时候被调用
//post:在route和error过滤器之后被调用
//error:处理请求时发生错误时被调用
@Override
public String filterType() {
return "pre";
} //设置滤器执行的顺序,依靠数值从小到大开始执行,0是最先执行的
@Override
public int filterOrder() {
return 0;
} //是否执行过滤器,true表示可以执行,这个可以通过代码逻辑来关闭过滤器
@Override
public boolean shouldFilter() {
return true;
} //过滤器的具体执行逻辑,如权限验证
@Override
public Object run() {
return null;
}
}
2.配置完成后,还需要将过滤器在主启动类中注入
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; @SpringBootApplication
//这是一个网关服务
@EnableZuulProxy
public class ZuulDemoApplication { public static void main(String[] args) {
SpringApplication.run(ZuulDemoApplication.class, args);
}
@Bean
public ZuulDemoFilter zuulDemoFilter (){
return new ZuulDemoFilter ();
}
}
spring cloud学习笔记五 网关服务zuul的更多相关文章
- spring cloud 学习之路由网关(zuul)
学习自方志朋的博客 http://blog.csdn.net/forezp/article/details/69939114 在微服务架构中,需要几个基础的服务治理组件,包括服务注册与发现.服务消费. ...
- Spring Cloud学习笔记之微服务架构
目录 什么是微服务 架构优点 架构的挑战 设计原则 什么是微服务 微服务构架方法是以开发一种小型服务的方式,来开发一个独立的应用系统的. 其中每个小型服务都运行在自己的进程中,并经常采 ...
- Spring Cloud学习笔记-009
API网关服务:Spring Cloud Zuul API网关是一个更为智能的应用服务器,它的定义类似于面向对象设计模式中的Façade模式,它的存在就像是整个微服务架构系统的门面一样,所有的外部客户 ...
- Spring cloud 学习笔记
前奏 1. 什么是微服务? 微服务化的核心就是将传统的一站式应用,根据业务拆分成一个一个的服务,彻底地去耦合,==每一个微服务提供单个业务功能的服务==,一个服务做一件事,从技术角度看就是一种 ...
- Spring Cloud学习笔记-006
服务容错保护:Spring Cloud Hystrix 在微服务架构中,我们将系统拆分成了很多服务单元,各单元的应用间通过服务注册与订阅的方式互相依赖.由于每个单元都在不同的进程中运行,依赖通过远程调 ...
- Spring Cloud学习笔记-002
搭建Spring Cloud注册中心:Eureka 服务注册:在服务治理框架中,通常都会构建一个注册中心,每个服务单元向注册中心登记自己提供的服务,将主机与端口号.版本号.通信协议等一些附加信息告诉注 ...
- Spring Cloud学习笔记-007
声明式服务调用:Spring Cloud Feign Feign基于Netflix Feign实现,整合了Spring Cloud Ribbon和Spring Cloud Hystrix,除了提供这两 ...
- Spring Cloud学习笔记-010
分布式配置中心:Spring Cloud Config Spring Cloud Config是Spring Cloud团队创建的一个全新的项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外 ...
- Spring Cloud学习笔记【九】配置中心Spring Cloud Config
Spring Cloud Config 是 Spring Cloud 团队创建的一个全新项目,用来为分布式系统中的基础设施和微服务应用提供集中化的外部配置支持,它分为服务端与客户端两个部分.其中服务端 ...
随机推荐
- python学习笔记(十一)处理json
json串就是字符串,json串里必须是双引号 d={'car':{'color':'red','price':100,'count':50}, '爱分叉':{'color':'red','price ...
- JLRoutes笔记
1.在info.plist中添加 <key>CFBundleURLTypes</key> <array> <dict> <key>CFBun ...
- 4412 使用usb摄像头拍照YUYV格式
一.内核设置 Linux内核中已经带有很完善的USB摄像头驱动,支持几乎所有的USB摄像头,我们只需要配置内核,选择上相应的Sensor型号即可. 配置内核,支持USB摄像头: Device Driv ...
- Seek the Name, Seek the Fame (poj2752
Seek the Name, Seek the Fame Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 14561 Ac ...
- LintCode之两两交换链表中的节点
题目描述: 我的思路: 由题目描述可知,题目是要求将第一个与第二个节点,第三个与第四节点....进行交换,而进行交换时只用将节点的值进行交换即可.需要注意的是:当链表为null或者当链表只有一个节点时 ...
- Cordova指令
安装 cordova: npm install -g cordova 创建应用程序 cordova create hello com.example.hello HelloWorld cordov ...
- Windows系统命令整理-Win10
硬件相关 显卡 显卡升级 - 我的电脑->属性->设备管理器->显示适配器->更新驱动程序 服务 telnet 安装:启用或关闭Windows 功能,勾选上“Telnet客户端 ...
- HeightCharts柱状图和饼状图
HTML: <div id="container1" style="height:350px; " ></div> <di ...
- Waiter.js
var Waiter = function() { var dfd = [], doneArr = [], failArr = [], slic ...
- Chrome开启多线程下载
Chrome多线程下载也和标签页预览一样属于Google测试中的功能,可通过在地址栏输入chrome://flags/,然后在搜索框中输入Parallel downloading,选择enabled, ...