简介
Spring Cloud Gateway是Spring Cloud官方推出的第二代网关框架,与Zuul网关对比,两者相同的地方就是都是作为网关,处理前段的请求,可以进行路由到对应的服务或者url,也可以针对权限做过滤处理,也可以对其他服务响应的结果做处理,但相对于Zuul。Gateway性能和功能整体要好,且使用 Gateway 做跨域相比应用本身或是 Nginx 的好处是规则可以配置的更加灵活。
项目结构:
首先搭建一个服务注册中心eurekaserver,一个普通的服务gateway-service-hi,最后搭建一个gateway网关的服务。
服务注册中心eurekaserver配置大致相同,不做详细介绍了,服务gateway-service-hi也不做详细介绍,
搭建Gateway网关:
spring cloud的版本是 Greenwich.SR1
pom.xml文件添加依赖-
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
gateway有两种方式配置,一种是配置文件application的方式,一种是代码配置。
个人觉得配置文件application的方式更容易理解与方便配置
application.yml文件配置如下-
server:
port: 8888
spring:
cloud:
gateway:
locator:
#服务名小写开启
lowerCaseServiceId: true
routes:
- id: 163
#转发到的目标地址
uri: http://www.163.com/
predicates:
- Path=/foo/**
filters:
- StripPrefix=1
- id: gateway-service-hi
#转发到的目标服务,gateway-service-hi为服务名
uri: lb://gateway-service-hi
# 如果请求地址满足/gateway/hi/**,则转发到 gateway-service-hi 服务
predicates:
- Path=/gateway/**
filters:
- StripPrefix=1
# 是否可以通过其他服务的serviceId来转发到具体的服务实例。默认为enabled:false
# 为enabled:true,自动创建路由,路由访问方式:http://Gateway_HOST:Gateway_PORT/大写的serviceId/**,其中微服务应用名默认大写访问
discovery:
locator:
enabled: true
application:
name: service-gateway
logging:
level:
org.springframework.cloud.gateway: trace
org.springframework.http.server.reactive: debug
org.springframework.web.reactive: debug
reactor.ipc.netty: debug
eureka:
client:
service-url:
defaultZone: http://localhost:8761/eureka/
注意:如果 id、uri、predicates、 filters未像上面所展示的一样对齐,就会出错…
启始类配置如下所示:
package com.sinosoft.gataway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@SpringBootApplication
@EnableEurekaClient
public class GatawayApplication {
public static void main(String[] args) {
SpringApplication.run(GatawayApplication.class, args);
}
}
运行项目
依次运行:eurekaserver、gateway-service-hi、gateway
访问:http://localhost:8888/gateway/hi
运行之后结果图如下:
接下来就是个人的验证:
在这里贴出其控制器的代码,方便理解。
gateway-service-hi 控制器代码如下-
package com.sinosoft.gatewayservicehi.control;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GatewayServiceControl {
@Value("${spring.application.name}")
String name;
@Value("${server.port}")
String port;
@RequestMapping("/hi")
public String sayHi(){
return "Hi, I am from "+name+" hi port:"+port;
}
@RequestMapping("/hello")
public String sayHello(){
return "Hi, I am from "+name+" hello port:"+port;
}
}
-Path = /gateway/hi/**的值进行测试验证
- id: gateway-service-hi
#转发到的目标服务,gateway-service-hi为服务名
uri: lb://gateway-service-hi
# 如果请求地址满足/gateway/hi/**,则转发到 gateway-service-hi 服务
predicates:
- Path=/gateway/hi/**
filters:
- StripPrefix=1
-Path = /gateway/**的值进行测试验证
- id: gateway-service-hi
#转发到的目标服务,gateway-service-hi为服务名
uri: lb://gateway-service-hi
# 如果请求地址满足/gateway/**,则转发到 gateway-service-hi 服务
predicates:
- Path=/gateway/**
一系列的结果截图可以看出,gateway网关中的配置- Path=/gateway/**
其实是从localhost:8888后进行匹配,如果满足条件,只是替换请求中与- Path值/gateway/*相匹配的部分gateway,从而将请求转发到目标服务中
如果有多层匹配,如/gateway/hi/**,也只是替换gateway部分,hi部分就是服务暴露出来的接口。