优秀的编程知识分享平台

网站首页 > 技术文章 正文

36 微服务网关GateWay入门案例(微服务网关支持)

nanyue 2024-08-30 20:48:48 技术文章 4 ℃

入门案例

(1) 创建工程导入依赖

在项目中添加新的模块 shop_gateway_server ,并导入依赖

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>

注意SpringCloud Gateway使用的web框架为webflux,和SpringMVC不兼容。引入的限流组件是 hystrix。redis底层不再使用jedis,而是lettuce。

(2) 配置启动类

@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
        

(3) 编写配置文件

创建 application.yml 配置文件

server:
    port: 8080 #服务端口
spring:
    application:
        name: api-gateway #指定服务名
cloud:
    gateway:
        routes:
            - id: product-service
            uri: http://127.0.0.1:9002
            predicates:
            - Path=/product/**
  • id:我们自定义的路由 ID,保持唯一
  • uri:目标服务地址 predicates:路由条件,
  • Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默 认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
  • filters:过滤规则,暂时没用。上面这段配置的意思是,配置了一个 id 为 product-service的路由规则,当访问网关请求地址以 product 开头时,会自动转发到地址: http://127.0.0.1:9002/ 。配置完成启动项目即可在浏览器 访问进行测试,当我们访问地址 http://localhost:8080/product/1 时会展示页面展示如下:

路由规则

Spring Cloud Gateway 的功能很强大,前面我们只是使用了 predicates 进行了简单的条件匹配,其实 Spring Cloud Gataway 帮我们内置了很多 Predicates 功能。在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性实现了各种路由匹配规则,有通过 Header、请求参数等不同的条件来进行作为条件 匹配到对应的路由。

示例

#路由断言之后匹配
spring:
 cloud:
  gateway:
   routes:
    - id: after_route
    uri: https://xxxx.com
    #路由断言之前匹配
    predicates:
   - After=xxxxx
#路由断言之前匹配
spring:
 cloud:
  gateway:
   routes:
    - id: before_route
    uri: https://xxxxxx.com
    predicates:
   - Before=xxxxxxx
#路由断言之间
spring:
  cloud:
   gateway:
    routes:
      - id: between_route
     uri: https://xxxx.com
     predicates:
    - Between=xxxx,xxxx
#路由断言Cookie匹配,此predicate匹配给定名称(chocolate)和正则表达式(ch.p)
spring:
  cloud:
    gateway:
     routes:
      - id: cookie_route
      uri: https://xxxx.com
      predicates:
      - Cookie=chocolate, ch.p
#路由断言Header匹配,header名称匹配X-Request-Id,且正则表达式匹配\d+
spring:
   cloud:
    gateway:
      routes:
        - id: header_route
       uri: https://xxxx.com
       predicates:
      - Header=X-Request-Id, \d+
#路由断言匹配Host匹配,匹配下面Host主机列表,**代表可变参数
spring:
  cloud:
   gateway:
     routes:
      - id: host_route
     uri: https://xxxx.com
     predicates:
     - Host=**.somehost.org,**.anotherhost.org
#路由断言Method匹配,匹配的是请求的HTTP方法
spring:
    cloud:
      gateway:
       routes:
        - id: method_route
       uri: https://xxxx.com
       predicates:
       - Method=GET
#路由断言匹配,{segment}为可变参数
spring:
    cloud:
      gateway:
       routes:
         - id: host_route
         uri: https://xxxx.com
        predicates:
       - Path=/foo/{segment},/bar/{segment}
#路由断言Query匹配,将请求的参数param(baz)进行匹配,也可以进行regexp正则表达式匹配 (参数包含
foo,并且foo的值匹配ba.)
spring:
    cloud:
      gateway:
       routes:
        - id: query_route
       uri: https://xxxx.com
       predicates:
- Query=baz 或 Query=foo,ba.
#路由断言RemoteAddr匹配,将匹配192.168.1.1~192.168.1.254之间的ip地址,其中24为子网掩码位
数即255.255.255.0
spring:
     cloud:
      gateway:
        routes:
         - id: remoteaddr_route
         uri: https://example.org
         predicates:
         - RemoteAddr=192.168.1.1/24

动态路由

和zuul网关类似,在SpringCloud GateWay中也支持动态路由:即自动的从注册中心中获取服务列表并 访问。

(1)添加注册中心依赖

在工程的pom文件中添加注册中心的客户端依赖(这里以Eureka为例)

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

(2)配置动态路由

修改 application.yml 配置文件,添加eureka注册中心的相关配置,并修改访问映射的URL为服务名 称

server:
   port: 8080 #服务端口
spring:
   application:
     name: api-gateway #指定服务名
   cloud:
     gateway:
      routes:
       - id: product-service
       uri: lb://shop-service-product
       predicates:
      - Path=/product/**
eureka:
   client:
     serviceUrl:
      defaultZone: http://127.0.0.1:8761/eureka/
      registry-fetch-interval-seconds: 5 # 获取服务列表的周期:5s
  instance:
    preferIpAddress: true
    ip-address: 127.0.0.1

uri : uri以 lb: //开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称

重写转发路径

在SpringCloud Gateway中,路由转发是直接将匹配的路由path直接拼接到映射路径(URI)之后,那 么在微服务开发中往往没有那么便利。这里就可以通过RewritePath机制来进行路径重写。

(1) 案例改造

修改 application.yml ,将匹配路径改为 /product-service/**

重新启动网关,我们在浏览器访问http://127.0.0.1:8080/product-service/product/1,会抛出404。这 是由于路由转发规则默认转发到商品微服务( http://127.0.0.1:9002/productservice/product/1 )路径上,而商品微服务又没有 product-service 对应的映射配置。

(2) 添加RewritePath重写转发路径

修改 application.yml ,添加重写规则。

spring:
    application:
        name: api-gateway #指定服务名
    cloud:
      gateway:
      routes:
        - id: product-service
        uri: lb://shop-service-product
        predicates:
        - Path=/product-service/**
       filters:
        - RewritePath=/product-service/(?<segment>.*), /$\{segment}

通过RewritePath配置重写转发的url,将/product-service/(?.*),重写为{segment},然后转发到订单 微服务。比如在网页上请求http://localhost:8080/product-service/product,此时会将请求转发到htt p://127.0.0.1:9002/product/1( 值得注意的是在yml文档中 $ 要写成 $\ )

最近发表
标签列表