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的更多相关文章

  1. springcloud中的API网关服务Zuul

    到目前为止,我们Spring Cloud中的内容已经介绍了很多了,Ribbon.Hystrix.Feign这些知识点大家都耳熟能详了,我们在前文也提到过微服务就是把一个大的项目拆分成很多小的独立模块, ...

  2. springCloud学习05之api网关服务zuul过滤器filter

    前面学习了zuul的反向代理.负载均衡.fallback回退.这张学习写过滤器filter,做java web开发的对filter都不陌生,那就是客户端(如浏览器)发起请求的时候,都先经过过滤器fil ...

  3. SpringCloud开发学习总结(八)—— API网关服务Zuul(一)

    大多数情况下,为了保证对外服务的安全性,我们在服务端实现的为服务接口时往往都会有一定的权限校验机制,比如对用户登录状态的校验等:同时为了防止客户端在发起请求时被篡改等安全方面的考虑,还会有一些签名校验 ...

  4. 第七章 API网关服务:Spring Cloud Zuul

    API网关是一个更为智能的应用服务器, 它的定义类似于面向对象设计模式中的Facade模式, 它的存在就像是整个微服务架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤.它除了要实现 ...

  5. spring cloud 入门系列六:使用Zuul 实现API网关服务

    通过前面几次的分享,我们了解了微服务架构的几个核心设施,通过这些组件我们可以搭建简单的微服务架构系统.比如通过Spring Cloud Eureka搭建高可用的服务注册中心并实现服务的注册和发现: 通 ...

  6. Spring Cloud Zuul 1(API 网关服务)

    API网关是一个更为智能的应用服务器,它的存在就像是整个微服架构系统的门面一样,所有的外部客户端访问都需要经过它来进行调度和过滤. 它实现的功能包括:请求路由.负载均衡.校验过滤等功能. Spring ...

  7. API网关服务:Spring Cloud Zuul

    最近在学习Spring Cloud的知识,现将API网关服务:Spring Cloud Zuul 的相关知识笔记整理如下.[采用 oneNote格式排版]

  8. API网关服务Zuul-Spring Cloud学习第五天(非原创)

    文章大纲 一.Zuul是什么二.Zuul的基本实现三.路由配置细节四.异常处理细节五.项目源码与参考资料下载六.参考文章   一.Zuul是什么   到目前为止,我们Spring Cloud中的内容已 ...

  9. Spring Cloud API网关服务 5.2

    为什么需要API网关 通过前面内容的学习,我们已经可以构建一个简单的微服务架构系统.这个系统可以使用Spring Boot实现微服务的开发,使用Spring Cloud Eureka实现注册中心以及服 ...

随机推荐

  1. Uncaught Error: artDialog: Document types require more than xhtml1.0

    这需要声明html文档,加上如下声明就不会报错了! <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  2. mysql的体系架构和存储引擎

    定义数据库和实例 数据库:物理操作系统的文件或其他形式文件类型的集合.在mysql数据库中,数据库文件可以是frm.MYD.MYI.ibd结尾的文件. 实例:MySQL数据库由后台线程以及一个共享内存 ...

  3. Linux I/O 进阶

    非阻塞I/O 阻塞I/O对应于低速的系统调用,可能会使进程永远阻塞.非阻塞I/O可以使我们发出open.read.write这样的I/O操作,并使这些操作不会永远阻塞.如果这种操作不能完成,则调用立即 ...

  4. 160818、CSS页面布局笔记

    居中布局   水平居中 父元素和子元素的宽度都未知 inline-block + text-ailgn .child{display:inline-block;} .parent{text-align ...

  5. 阻塞(sleep等等)区别 中断(interrupt)+ 中断的意义

    不客气地说,至少有一半人认为,线程的"中断"就是让线程停止.如果你也这么认为,那你对多线程编程还没有入门. 在java中,线程的中断(interrupt)只是改变了线程的中断状态, ...

  6. Jsp页面截取字符串

    用jstl标签: 首先页面中引入<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions ...

  7. HDU 3182 Hamburger Magi(状压dp)

    题目链接:pid=3182">http://acm.hdu.edu.cn/showproblem.php?pid=3182 Problem Description In the mys ...

  8. MC20模块教程目录

    MC20模块使用教程 为了阅读和修正方便,所有教程在线观看,请在有网络的环境下观看下面教程,谢谢! MC20模块教程在线目录 第一章:基础使用,使用电脑调试MC20的各项功能 1.1 使用电脑测试MC ...

  9. MVC4 中使用 Area 和 注意的地方

    在MVC项目中经常会使用到Area来分开不同的模块让项目结构更加的清晰. 步骤如下:  项目 –> 添加 -> 区域 (Area)  输入 Admin 添加成功后 Area包含:创建一个空 ...

  10. ubuntu搭建mib2c环境

    1.下载net-snmphttp://net-snmp.sourceforge.net/download.html例如,下载5.5版本2.进入下载目录,解压net-snmp压缩包#tar zxf ne ...