springcloud-Api网关服务Zuul
springcloud项目例子:链接:https://pan.baidu.com/s/1O1PKrdvrq5c8sQUb7dQ5Pg 密码:ynir
1.由来:
如果我的微服务中有很多个独立服务都要对外提供服务,那么对于开发人员或者运维人员来说,他要如何去管理这些接口?特别是当项目非常大非常庞杂的情况下要如何管理?2.权限管理也是一个老生常谈的问题,在微服务中,一个独立的系统被拆分成很多个独立的模块,为了确保安全;
2.功能:作为一个更智能应用服务器,类似于微服务构架系统门面,所有外部访问都经过api网关,有他来实现请求路由、负载均衡、权限验证等功能
一:创建服务
1.pom
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.</modelVersion>
<groupId>springcloud</groupId>
<artifactId>springcloud-Zuul-api-gate</artifactId>
<version>0.0.-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5..RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<project.build.sourceEncoding>UTF-</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<spring-cloud.version>Dalston.SR3</spring-cloud.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-dependencies</artifactId>
<version>${spring-cloud.version}</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
</project>
2.入口类
@SpringBootApplication
@EnableZuulProxy //开启Zuul的API网关服务功能
public class ApiGatewayApplication { public static void main(String[] args) {
SpringApplication.run(ApiGatewayApplication.class, args);
}
}
3.配置路由
# 基础信息配置
spring.application.name=api-gateway
server.port=
# 路由规则配置
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=feign-consumer # API网关也将作为一个服务注册到eureka-server上
eureka.client.service-url.defaultZone=http://localhost:1111/eureka/
我们在这里配置了路由规则所有符合/api-a/**的请求都将被转发到feign-consumer服务上,至于feign-consumer服务的地址到底是什么则由eureka-server去分析,我们这里只需要写上服务名即可。以上面的配置为例,如果我请求http://localhost:2006/api-a/hello1接口则相当于请求http://localhost:2005/hello1(我这里feign-consumer的地址为http://localhost:2005),我们在路由规则中配置的api-a是路由的名字,可以任意定义,但是一组path和serviceId映射关系的路由名要相同
4.测试
依次启动我们的eureka-server、provider和feign-consumer,然后访问如下地址http://localhost:2006/api-a/hello,即可访问对应服务
二:配置过滤
自定义过滤器继承ZuulFilter
public class PermisFilter extends ZuulFilter {
@Override
public String filterType() {
return "pre";
} @Override
public int filterOrder() {
return ;
} @Override
public boolean shouldFilter() {
return true;
} @Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
HttpServletRequest request = ctx.getRequest();
String login = request.getParameter("login");
if (login == null) {
ctx.setSendZuulResponse(false);
ctx.setResponseStatusCode();
ctx.addZuulResponseHeader("content-type","text/html;charset=utf-8");
ctx.setResponseBody("非法访问");
}
return null;
}
}
解析
.filterType方法的返回值为过滤器的类型,过滤器的类型决定了过滤器在哪个生命周期执行,pre表示在路由之前执行过滤器,其他可选值还有post、error、route和static,当然也可以自定义。
.filterOrder方法表示过滤器的执行顺序,当过滤器很多时,这个方法会有意义。
.shouldFilter方法用来判断过滤器是否执行,true表示执行,false表示不执行,在实际开发中,我们可以根据当前请求地址来决定要不要对该地址进行过滤,这里我直接返回true。
.run方法则表示过滤的具体逻辑,假设请求地址中携带了login参数的话,则认为是合法请求,否则就是非法请求,如果是非法请求的话,首先设置ctx.setSendZuulResponse(false);表示不对该请求进行路由,然后设置响应码和响应值。这个run方法的返回值在当前版本(Dalston.SR3)中暂时没有任何意义,可以返回任意值。
配置过滤器Bean,在入口类中配置相关Bean
此时,如果我们访问http://localhost:2006/api-a/hello,显示非法访问,此时,如果我们访问http://localhost:2006/api-a/hello?login=123通过过滤器就能访问!
@Bean
PermisFilter permisFilter() {
return new PermisFilter();
}
三:总结
Zuul,API网关作为系统统一入口,将微服务内部细节都屏蔽掉了,自动维护服务实例,实现负载均衡转发,通过他能提供统一的权限验证机制!,使服务本身只需要关注业务逻辑!
四:路由配置细节
1.配置路由规则可替换:zuul.routes后面跟着的是服务名,服务名后面跟着的是路径规则,这种配置方式显然更简单
zuul.routes.api-a.path=/api-a/**
zuul.routes.api-a.serviceId=feign-consumer
可替换为
zuul.routes.feign-consumer=/api-a/**
2.一般生产者的服务是不对外提供服务的,此时
zuul.ignored-services=hello-service
可去除这个服务接口,浏览器访问返回404
3.使用yml可指定路由匹配顺序
spring:
application:
name: api-gateway
server:
port:
zuul:
routes:
feign-consumer-hello:
path: /feign-consumer/hello/**
serviceId: feign-consumer-hello
feign-consumer:
path: /feign-consumer/**
serviceId: feign-consumer
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka/
4.网关本身的接口配置
zuul:
prefix: /myapi
ignored-patterns: /**/hello/**
routes:
local:
path: /local/**
url: forward:/local
5.在这里配置全局,特定服务的ribbon,hytrix
#全局关闭hytrix重试机制
zuul:
retryable: false #关闭莫个服务hytrix重试机制
zuul:
routes:
feign-consumer:
retryable: false
五:配置异常处理
可创建一个Componet来处理异常信息
@Component
public class MyErrorAttribute extends DefaultErrorAttributes {
@Override
public Map<String, Object> getErrorAttributes(RequestAttributes requestAttributes, boolean includeStackTrace) {
Map<String, Object> result = super.getErrorAttributes(requestAttributes, includeStackTrace);
result.put("status", );
result.put("error", "error");
result.put("exception", "exception");
result.put("message", "message");
return result;
}
}
springcloud-Api网关服务Zuul的更多相关文章
- springcloud中的API网关服务Zuul
到目前为止,我们Spring Cloud中的内容已经介绍了很多了,Ribbon.Hystrix.Feign这些知识点大家都耳熟能详了,我们在前文也提到过微服务就是把一个大的项目拆分成很多小的独立模块, ...
- springCloud学习05之api网关服务zuul过滤器filter
前面学习了zuul的反向代理.负载均衡.fallback回退.这张学习写过滤器filter,做java web开发的对filter都不陌生,那就是客户端(如浏览器)发起请求的时候,都先经过过滤器fil ...
- SpringCloud开发学习总结(八)—— API网关服务Zuul(一)
大多数情况下,为了保证对外服务的安全性,我们在服务端实现的为服务接口时往往都会有一定的权限校验机制,比如对用户登录状态的校验等:同时为了防止客户端在发起请求时被篡改等安全方面的考虑,还会有一些签名校验 ...
- 第七章 API网关服务:Spring Cloud Zuul
API网关是一个更为智能的应用服务器, 它的定义类似于面向对象设计模式中的Facade模式, 它的存在就像是整个微服务架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤.它除了要实现 ...
- spring cloud 入门系列六:使用Zuul 实现API网关服务
通过前面几次的分享,我们了解了微服务架构的几个核心设施,通过这些组件我们可以搭建简单的微服务架构系统.比如通过Spring Cloud Eureka搭建高可用的服务注册中心并实现服务的注册和发现: 通 ...
- Spring Cloud Zuul 1(API 网关服务)
API网关是一个更为智能的应用服务器,它的存在就像是整个微服架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤. 它实现的功能包括:请求路由.负载均衡.校验过滤等功能. Spring ...
- API网关服务:Spring Cloud Zuul
最近在学习Spring Cloud的知识,现将API网关服务:Spring Cloud Zuul 的相关知识笔记整理如下.[采用 oneNote格式排版]
- API网关服务Zuul-Spring Cloud学习第五天(非原创)
文章大纲 一.Zuul是什么二.Zuul的基本实现三.路由配置细节四.异常处理细节五.项目源码与参考资料下载六.参考文章 一.Zuul是什么 到目前为止,我们Spring Cloud中的内容已 ...
- Spring Cloud API网关服务 5.2
为什么需要API网关 通过前面内容的学习,我们已经可以构建一个简单的微服务架构系统.这个系统可以使用Spring Boot实现微服务的开发,使用Spring Cloud Eureka实现注册中心以及服 ...
随机推荐
- OKhttp3
针对上一博文订单调用用户使用默认数据交互方式,下面介绍下使用 Okhttp3网络数据交换方式. 1.订单启动类变化 package com.tycoon.orderService; import or ...
- ios关于数据的存储
本文转载至http://blog.csdn.net/chen505358119/article/details/9278539 这里我总结了一下数据的存储方式,一是保存在沙盒里 ...
- JSF -> 导航(Navigation)
在使用jsf框架时,肯定会用到faces-config.xml. 而其中就会出现很多的Navigation项. 其实这些Navigation就是一些页面跳转的东西. 以下内容来自http://blog ...
- grafana-----Annotation
注释提供了一个方法,在丰富的活动中做个标志点.当你鼠标移到这个注释上,你可以得到title,tags,和text information的事件. Queries 注释事件获得是通过一个注释查询.打开d ...
- 为什么要提倡"Design Pattern"呢? 开闭原则 系统设计时,注意对扩展开放,对修改闭合。
[亲身经历] 无规矩不成方圆 设计模式 - 搜狗百科 https://baike.sogou.com/v123729.htm?fromTitle=设计模式 为什么要提倡"Design Pat ...
- redis add 'vm.overcommit_memory = 1' to /etc/sysctl.conf
w root@well:/etc# vim sysctl.conf #kernel.domainname = example.com # # /etc/sysctl.conf - Configurat ...
- Python函数参数默认值的陷阱和原理深究(转)
add by zhj: 在Python文档中清楚的说明了默认参数是怎么工作的,如下 "Default parameter values are evaluated when the func ...
- 我的Android进阶之旅------>修改Android签名证书keystore的密码、别名alias以及别名密码
转载于:http://blog.k-res.net/archives/1229.html 和 http://blog.k-res.net/archives/1671.html ADT允许自定义调试用 ...
- document write & close
在载入页面后,浏览器输出流自动关闭.在此之后,任何一个对当前页面进行操作的document.write()方法将打开—个新的输出流,它将清除当前页面内容. 必须确保调用document.close() ...
- Linux用户相关文件之用户信息文件
1.文件地址: /etc/pssswd -rw-r--r--. 1 root root 936 10月 6 12:50 /etc/passwd 2.文件内容: xiaol_1:x:501:501::/ ...