服务网关 Spring Cloud Gateway 的应用

简介: 如何启动 Spring Cloud Gateway1、新建 Maven 工程,添加相关依赖 pom.xml4.0.0 com.


如何启动 Spring Cloud Gateway

1、新建 Maven 工程,添加相关依赖 pom.xml

4.0.0 com.anoyi core-gateway 0.0.1-SNAPSHOT core-gateway gateway for miroservice UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-gateway 2.0.0.RELEASE pom import org.springframework.cloud spring-cloud-starter-gateway org.springframework.boot spring-boot-maven-plugin

2、添加启动类 Application.java

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Configuration;@Configuration@SpringBootApplicationpublic 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;@SpringBootApplicationpublic 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 等等。仅通过简单的配置即可实现功能强大的网关服务。

欢迎工作一到五年的Java工程师朋友们加入Java架构开发:744677563

本群提供免费的学习指导 架构资料 以及免费的解答

不懂得问题都可以在本群提出来 之后还会有职业生涯规划以及面试指导

如何启动 Spring Cloud Gateway

1、新建 Maven 工程,添加相关依赖 pom.xml

4.0.0 com.anoyi core-gateway 0.0.1-SNAPSHOT core-gateway gateway for miroservice UTF-8 UTF-8 1.8 org.springframework.cloud spring-cloud-gateway 2.0.0.RELEASE pom import org.springframework.cloud spring-cloud-starter-gateway org.springframework.boot spring-boot-maven-plugin

2、添加启动类 Application.java

import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;import org.springframework.context.annotation.Configuration;@Configuration@SpringBootApplicationpublic 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;@SpringBootApplicationpublic 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 等等。仅通过简单的配置即可实现功能强大的网关服务。

欢迎工作一到五年的Java工程师朋友们加入Java架构开发:744677563

本群提供免费的学习指导 架构资料 以及免费的解答

不懂得问题都可以在本群提出来 之后还会有职业生涯规划以及面试指导

相关文章
|
6天前
|
Java API 微服务
【Spring Boot系列】通过OpenAPI规范构建微服务服务接口
【4月更文挑战第5天】通过OpenAPI接口构建Spring Boot服务RestAPI接口
|
1月前
|
负载均衡 Java API
Spring Cloud 面试题及答案整理,最新面试题
Spring Cloud 面试题及答案整理,最新面试题
138 1
|
27天前
|
安全 Java 数据安全/隐私保护
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
【深入浅出Spring原理及实战】「EL表达式开发系列」深入解析SpringEL表达式理论详解与实际应用
62 1
|
1月前
|
Java Nacos Sentinel
Spring Cloud Alibaba 面试题及答案整理,最新面试题
Spring Cloud Alibaba 面试题及答案整理,最新面试题
204 0
|
1月前
|
SpringCloudAlibaba Java 持续交付
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
【构建一套Spring Cloud项目的大概步骤】&【Springcloud Alibaba微服务分布式架构学习资料】
148 0
|
1天前
|
监控 Java 微服务
第八章 Spring Cloud 之 Hystrix
第八章 Spring Cloud 之 Hystrix
|
1天前
|
监控 Java API
第七章 Spring Cloud 之 GateWay
第七章 Spring Cloud 之 GateWay
|
1天前
|
消息中间件 Java Nacos
第三章 Spring Cloud简介
第三章 Spring Cloud简介
|
1天前
|
Java Nacos 开发者
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
Java从入门到精通:4.2.1学习新技术与框架——以Spring Boot和Spring Cloud Alibaba为例
|
1天前
|
Dubbo Java 应用服务中间件
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架
Java从入门到精通:3.2.2分布式与并发编程——了解分布式系统的基本概念,学习使用Dubbo、Spring Cloud等分布式框架