微服务架构 - 网关 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中起到保护微服务的作用,不会让发生的异常无 ...
随机推荐
- 简介SoftICE是Compuware NuMega公司1的产品,是目前公认最好的系统级调试工具
简介SoftICE是Compuware NuMega公司1的产品,是目前公认最好的系统级调试工具
- RabbitMQ实战(三)-高级特性
0 相关源码 1 你将学到 如何保证消息百分百投递成功 幂等性 如何避免海量订单生成时消息的重复消费 Confirm确认消息.Return返回消息 自定义消费者 消息的ACK与重回队列 限流 TTL ...
- Ubuntu通过修改配置文件进行网络配置
Ubuntu系统进行网络配置有的时候用图形界面不起作用,这种情况下可以直接修改某些启动脚本或配置文件 Ubuntu系统进行网络配置涉及到几个配置文件1./etc/network/interfaces ...
- HttpServlet cannot be resolved to a type 解决办法
刚开始学习Servlet,在Eclipse中新建了一个Servlet,不过页面上报错: Httpservlet cannot be resolved to a type,显然是Eclipse找不到相应 ...
- springBoot-mongodb
作者:纯洁的微笑出处:http://www.ityouknow.com/ 版权归作者所有,转载请注明出处 mongodb是最早热门非关系数据库的之一,使用也比较普遍,一般会用做离线数据分析来使用,放到 ...
- jQuery通过id和name获取值的区别
$(#'id').函数 $("input[name='name']")
- idea中写servlet时报错--关于405错误
将super方法注释掉 原因:super是调用了此类继承父类doget和dopost方法的, 如果此类中没有这个方法,就会报错The specified HTTP method is not allo ...
- 终极CRUD-2-用lombok提高开发效率
目录 1 lom介绍与基本使用 2 lombok 注意点 2.1 lombok自动生成方法可以混合自己写的方法 2.2 尽量不要使用@Data 2.3 属性不要使用基本类型 2.4 小心@ToStri ...
- Spring WebClient vs. RestTemplate
1. 简介 本教程中,我们将对比 Spring 的两种 Web 客户端实现 -- RestTemplate 和 Spring 5 中全新的 Reactive 替代方案 WebClient. 2. 阻塞 ...
- 从windows10迁移到Linux Deepin
如题, 这几天从windows系统迁移到deepin的linux系统花了很多时间, 以致最近都没时间来博客园.现在将这几天的成果分享出来, 顺便也做个记录.先不多说, 上一张新系统界面. 其实在装de ...