微服务架构 - 网关 Spring Cloud Gateway

Spring Cloud Gateway 工作原理

客户端向 Spring Cloud Gateway 发出请求,如果请求与网关程序定义的路由匹配,则将其发送到网关 Web 处理程序,此处理程序运行特定的请求过滤器链。
过滤器之间用虚线分开的原因是过滤器可能会在发送代理请求之前或之后执行逻辑。所有 "pre" 过滤器逻辑先执行,然后执行代理请求,代理请求完成后,执行 "post" 过滤器逻辑。
如何启动 Spring Cloud Gateway
1、新建 Maven 工程,添加相关依赖 pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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.0</modelVersion>
<groupId>com.anoyi</groupId>
<artifactId>core-gateway</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>core-gateway</name>
<description>gateway for miroservice</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-gateway</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2、添加启动类 Application.java
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
@Configuration
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
3、启动 Application(和 Spring Boot 项目一样)
访问 http://localhost:8080/ 报错 404,同时日志输出:
2018-06-27 09:18:48.981 WARN 44156 --- [ctor-http-nio-2] .a.w.r.e.DefaultErrorWebExceptionHandler :
Failed to handle request [GET http://localhost:8080/]: Response status 404
配置服务的路由:配置文件方式
假设本地启动了另外两个 Spring Boot 服务,分别是 服务A( http://localhost:8081 )、服务B( http://localhost:8082 ),下面通过 Spring Cloud Gateway 来路由到这两个服务。
1、在 resources 路径下添加配置文件 application.yml
spring:
cloud:
gateway:
routes:
- id: host_route
uri: http://localhost:8081
predicates:
- Path=/a/**
filters:
- StripPrefix=1
- id: host_route
uri: http://localhost:8082
predicates:
- Path=/b/**
filters:
- StripPrefix=1
- id:固定,不同 id 对应不同的功能,可参考 官方文档
- uri:目标服务地址
- predicates:路由条件
- filters:过滤规则
2、重启 Gateway 服务
3、测试
访问 http://localhost:8080/a/ 路由到 服务A http://localhost:8081/
访问 http://localhost:8080/b/ 路由到 服务B http://localhost:8082/
其他地址,例如 http://localhost:8080/a/user/all 路由到 服务A http://localhost:8081/user/all
配置服务的路由:编码方式
实现如上服务路由,还可以通过编码的方式实现。
1、删除配置文件 application.yml
2、修改 Application.java, 添加自定义路由配置
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.factory.StripPrefixGatewayFilterFactory;
import org.springframework.cloud.gateway.route.RouteLocator;
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Application {
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
StripPrefixGatewayFilterFactory.Config config = new StripPrefixGatewayFilterFactory.Config();
config.setParts(1);
return builder.routes()
.route("host_route", r -> r.path("/a/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8081"))
.route("host_route", r -> r.path("/b/**").filters(f -> f.stripPrefix(1)).uri("http://localhost:8082"))
.build();
}
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
其他功能
http://cloud.spring.io/spring-cloud-gateway/single/spring-cloud-gateway.html
官方提供了大量的路由规则,比如Time、Host、Header 等等,同时也提供了大量的过滤器,比如AddRequestHeader、AddRequestParameter、AddResponseHeader 等等。仅通过简单的配置即可实现功能强大的网关服务。
微服务架构 - 网关 Spring Cloud Gateway的更多相关文章
- 微服务架构之spring cloud gateway
Spring Cloud Gateway是spring cloud中起着非常重要的作用,是终端调用服务的入口,同时也是项目中每个服务对外暴露的统一口径,我们可以在网关中实现路径映射.权限验证.负载均衡 ...
- Spring Cloud 微服务五:Spring cloud gateway限流
前言:在互联网应用中,特别是电商,高并发的场景非常多,比如:秒杀.抢购.双11等,在开始时间点会使流量爆发式地涌入,如果对网络流量不加控制很有可能造成后台实例资源耗尽.限流是指通过指定的策略削减流量, ...
- 微服务架构集大成者—Spring Cloud (转载)
软件是有生命的,你做出来的架构决定了这个软件它这一生是坎坷还是幸福. 本文不是讲解如何使用Spring Cloud的教程,而是探讨Spring Cloud是什么,以及它诞生的背景和意义. 1 背景 2 ...
- 微服务架构之spring cloud 介绍
在当前的软件开发行业中,尤其是互联网,微服务是非常炽热的一个词语,市面上已经有一些成型的微服务框架来帮助开发者简化开发工作量,但spring cloud 绝对占有一席之地,不管你是否为java开发,大 ...
- 微服务架构-选择Spring Cloud,放弃Dubbo
Spring Cloud 在国内中小型公司能用起来吗?从 2016 年初一直到现在,我们在这条路上已经走了一年多. 在使用 Spring Cloud 之前,我们对微服务实践是没有太多的体会和经验的.从 ...
- 微服务架构之spring cloud eureka
Spring Cloud Eureka是spring cloud的核心组件,负责服务治理功能,起到中心枢纽作用,其它组件都依赖eureka来获取服务,然后再根据项目需求实现自己的业务,eureka在整 ...
- 微服务架构之spring cloud zipkin
Spring Cloud Zipkin是微服务的链路跟踪组件,帮助详细了解一次request&response的总计时,及每个微服务的消耗时间.微服务名称.异常信息等等过程信息. (一) 版本 ...
- 微服务架构之spring cloud turbine
在前面介绍了spring cloud hystrix及其hystrix dashboard,但都是对单个项目的监控,对于一个为项目而言,必定有很多微服务,一个一个去看非常的不方便,如果有一个能集中熔断 ...
- 微服务架构之spring cloud hystrix&hystrix dashboard
在前面介绍spring cloud feign中我们已经使用过hystrix,只是没有介绍,spring cloud hystrix在spring cloud中起到保护微服务的作用,不会让发生的异常无 ...
随机推荐
- 简单函数编写_strcpy、_stroverchg、_strcmp
字符串复制函数 void _strcpy(char *tar, const char * res) { char *p = tar; while(assert(tar && res), ...
- mysql数据库建表分类字段--尽量少用字符串--原因探索
虽然一直都知道,类型 之类的字段 直接用字符窜会很方便,不过最好还是不要用字符串:但是也不是特别清楚为什么不要用,时间久了 就忍不住用一下字符窜试试,这一试 还挺好用的,吓得我 感觉探究了一下 为什么 ...
- 洛谷 P1463、POI2002、HAOI2007 反素数
题意: 求最小的$x\in[1,N]$,使得$x$为$g(x)$最大的数 中最小的一个. 分析: 1.$x$不会有超过$10$个不同质因子.理由:$2 \times 3\times 5...\time ...
- RabbitMQ 实践之在处理异步任务中的流程
一.背景: 我司的系统,用户可以创建任务,启动任务,但任务的运行需要很长的时间,所以采用消息队列的方式,后台异步处理. 这里所用到的是 RabbitMQ . 二.MQ 处理任务的流程 ① ② ③ ④ ...
- tar 命令详解(持续更新)
可以用man tar查看tar命令使用的权威解释 Main operation mode: -c: 建立压缩档案 -r:向压缩归档文件末尾追加文件 -t:查看内容 -u:更新原压缩包中的文件 -x:解 ...
- 《VR入门系列教程》之13---相机与立体渲染
相机.透视图.视口.投影 渲染好的场景都需要一个可以供用户查看的视图,我们通常在3D场景中用相机来提供这种需求.相机相对场景有位置和方向,就像我们生活中的相机一样,它也提供透视图查看方式,这种 ...
- go mod 无法自动下载依赖包的问题
go 11以后启用了go mod功能,用于管理依赖包. 当执行go mod init生成go.mod文件之后,golang在运行.编译项目的时候,都会检查依赖并下载依赖包. 在启动了go mod之后, ...
- Android的简述3
Activity的生命周期 Activity类中有许多onXXX形式的函数可以重载,比如onCreate,onStart,onStop,onPause,那么它们的调用顺序到底是如何的呢?下面就通过一个 ...
- java中代码的注释和快捷
添加必要的注释,对一个有责任心.有道德模范的前端必须具备的好习惯, 可以大大提高代码的可维护性.可读性. java代码注释快捷键:ctrl+shift+/首先熟悉一下html.css.js的注释的写法 ...
- Git/Github使用方法小记
今天把人间网的桌面客户端renjian-deck正式开源了,之前对javascript的了解其实非常的不够的,所以这一次的代码写的也是乱七八糟重用性及其低下,虽然我无数次的想把代码重新整理一下,不过还 ...