草庐IT

SpringCloud之Gateway网关路由转发route、断言predicates

普通网友 2023-04-12 原文

官方文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/

1、gateway-ha 提供服务

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.yzm</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>gateway-ha</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>gateway-ha</name>
    <description>Demo project for Spring Boot</description>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class GatewayHaApplication {

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

}


server:
  port: 8021

spring:
  application:
    name: gateway-ha

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8088/eureka/

# 启动熔断降级
feign:
  hystrix:
    enabled: true

Controller

@RestController
public class HaController {

    @Resource
    private HaFeign haFeign;

    @Value("${server.port}")
    private String port;

    @GetMapping("hello")
    public String hello(@RequestParam String name) {
        return "ha," + name + " ! " + "访问端口号:" + port;
    }

    @GetMapping("ha")
    public String ha(@RequestParam String name) {
        return haFeign.callHi(name);
    }
}

Service

@FeignClient(value = "gateway-hi", fallback = HaBack.class)
public interface HaFeign {

    @GetMapping("hello")
    String callHi(@RequestParam String name);
}


@Service
public class HaBack implements HaFeign {
    @Override
    public String callHi(String name) {
        return "gateway-hi 服务挂了";
    }
}

2、gateway-hi 提供服务

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.yzm</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>gateway-hi</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>gateway-hi</name>
    <description>Demo project for Spring Boot</description>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>


@EnableFeignClients
@EnableEurekaClient
@SpringBootApplication
public class GatewayHiApplication {

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

}


server:
  port: 8022

spring:
  application:
    name: gateway-hi

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8088/eureka/

feign:
  hystrix:
    enabled: true

Controller

@RestController
public class HiController {

    @Resource
    private HiFeign hiFeign;

    @Value("${server.port}")
    private String port;

    @GetMapping("hello")
    public String hello(@RequestParam String name) {
        return "hi," + name + " ! " + "访问端口号:" + port;
    }

    @GetMapping("hi")
    public String hi(@RequestParam String name) {
        return hiFeign.callHa(name);
    }

}

Service

@FeignClient(value = "gateway-ha", fallback = HiBack.class)
public interface HiFeign {

    @GetMapping("hello")
    String callHa(@RequestParam String name);
}


@Service
public class HiBack implements HiFeign {
    @Override
    public String callHa(String name) {
        return "gateway-ha 服务挂了";
    }
}

3、gateway-route 网关服务-路由转发

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>com.yzm</groupId>
        <artifactId>springcloud</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <relativePath>../pom.xml</relativePath>
    </parent>

    <artifactId>gateway-route</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>
    <name>gateway-route</name>
    <description>Demo project for Spring Boot</description>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <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>


@EnableEurekaClient
@SpringBootApplication
public class GatewayRouteApplication {

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

application.yml

server:
  port: 8023

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8088/eureka/

spring:
  application:
    name: gateway-route
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          # serviceId是否小写
          lowerCaseServiceId: true

通过gateway实现路由转发有两种方式:

方式一:通过代码配置类实现

@Configuration
public class RouteConfig {

    @Bean
    public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
        return builder.routes()
                .route("to_gateway_ha", r -> r
                        .path("/ha/**")
                        .filters(f -> f.
                                stripPrefix(1)
                        )
                        .uri("http://localhost:8021/")
                )
                .route(r -> r
                        .path("/gw-ha/**", "/gw-a/**")
                        .filters(f -> f.
                                stripPrefix(1)
                        )
                        .uri("lb://gateway-ha")
                )
                .build();
    }

}

to_gateway_ha:路由ID,具备全局唯一性,可以不指定,缺省值UUID。
stripPrefix(1):去掉前缀,1表示个数,如路由前请求/a/b/c/d,去掉1个前缀后请求/b/c/d再转发
uri():可以指定详细地址(不能负载),也可以指定服务ID

依次启动eureka、ha、hi和route服务

访问
http://localhost:8023/gateway-ha/helloname=yzm
http://localhost:8023/gateway-hi/helloname=yzm

这是网关的默认路由规则

访问
http://localhost:8023/gw-a/helloname=yzm
http://localhost:8023/gw-ha/helloname=yzm
http://localhost:8023/ha/helloname=yzm

这是自定义的路由规则

方式二:通过配置文件yml实现

spring:
  application:
    name: gateway-route
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        - id: gw_ha
          uri: http://localhost:8021/
          predicates:
            - Path=/hhha/*
          filters:
            - StripPrefix=1
        - id: to_gateway_hi
          uri: lb://gateway-hi
          predicates:
            - Path=/hi/*
          filters:
            - StripPrefix=1

重启route服务
访问
http://localhost:8023/hhha/helloname=yzm
http://localhost:8023/hi/helloname=yzm

路由转发简单实现完成!

4、predicate 断言,即路由条件

Path 路径匹配

      routes:
        - id: path_route
          uri: lb://gateway-ha
          predicates:
            - Path=/ha

Method 请求方式

	routes:
        - id: method_route
          uri: lb://gateway-ha
          predicates:
            - Method=POST

Before 某时间点之前

	routes:
        - id: time_route
          uri: lb://gateway-ha
          predicates:
            - Before=2022-03-10T16:00:22.432+08:00[Asia/Shanghai] #在该时区前发生

After 某时间点之后

	routes:
        - id: time_route
          uri: lb://gateway-ha
          predicates:
            - After=2022-03-10T15:00:22.432+08:00[Asia/Shanghai] #在该时区后发生

Between 两时间点之间

	routes:
        - id: time_route
          uri: lb://gateway-ha
          predicates:
            - Between=2022-01-10T15:00:22.432+08:00[Asia/Shanghai],2022-10-10T16:00:22.432+08:00[Asia/Shanghai] #在两个时区内发生

Header 请求头

	routes:
        - id: header_route
          uri: lb://gateway-ha
          predicates:
            - Header=id,001 #携带header信息,相当于键值对,id为key,001为value
            - Header=id,d+ #携带header信息,相当于键值对,id为key,d+为value,是一个正则表达式,表达为正数

Cookie

	routes:
        - id: cookie_route
          uri: lb://gateway-ha
          predicates:
            - Cookie=username,yzm #携带cookies信息,相当于键值对,username为key,yzm为value
            - Cookie=username,d+ #携带cookies信息,相当于键值对,username为key,d+为value

Host 域名

	routes:
        - id: host_route
          uri: lb://gateway-ha
          predicates:
            - Host=**.baidu.com,**.another.com

Query 查询条件

	routes:
        - id: query_route
          uri: lb://gateway-ha
          predicates:
            - Query=number,123 #带查询条件,第一个是查询参数名称,第二个是可选的值,有参数名为number且值=123
            - Query=number #带查询条件,有参数名为number,值无所谓
            - Query=number,d+ 

RemoteAddr 远程地址

	routes:
        - id: raddr_route
          uri: lb://gateway-ha
          predicates:
            - RemoteAddr=192.168.1.1/24

server:
  port: 8024

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8088/eureka/

spring:
  application:
    name: gateway-predicate
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lowerCaseServiceId: true
      routes:
        #Path
        - id: path_route
          uri: lb://gateway-ha
          predicates:
            - Path=/ha
        #Method
        - id: method_route
          uri: lb://gateway-ha
          predicates:
            - Method=POST
        #Header
        - id: header_route
          uri: lb://gateway-ha
          predicates:
            - Header=id,d+ #携带header信息,相当于键值对,id为key,d+为value,是一个正则表达式,表达为正数
        #Cookie
        - id: cookie_route
          uri: lb://gateway-ha
          predicates:
            - Cookie=username,d+ #携带cookies信息,相当于键值对,username为key,d+为value
        #Host
        - id: host_route
          uri: lb://gateway-ha
          predicates:
            - Host=**.baidu.com,**.another.com
        #Query
        - id: query_route
          uri: lb://gateway-ha
          predicates:
            - Query=number,d+
        #Time
#        - id: time_route
#          uri: lb://gateway-ha
#          predicates:
#            - After=2020-10-30T15:00:22.432+08:00[Asia/Shanghai] #在该时区后发生
#            - Before=2020-10-30T16:00:22.432+08:00[Asia/Shanghai] #在该时区前发生
#            - Between=2020-10-30T15:00:22.432+08:00[Asia/Shanghai],2020-10-30T16:00:22.432+08:00[Asia/Shanghai] #在两个时区内发生

在gateway-ha服务中添加接口

    @PostMapping("post")
    public String post() {
        System.out.println("post");
        return "post";
    }

    @GetMapping("time")
    public String time() {
        System.out.println("time");
        return "time";
    }

    @GetMapping("/header")
    public String header(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            System.out.println(name + " = " + request.getHeader(name));
        }
        return "header";
    }

    @GetMapping("/cookie")
    public String cookie(HttpServletRequest request) {
        Cookie[] cookies = request.getCookies();
        for (Cookie cookie : cookies) {
            System.out.println(cookie.getName() + " = " + cookie.getValue());
        }
        return "cookie";
    }

    @GetMapping("/host")
    public String host(HttpServletRequest request) {
        Enumeration<String> headerNames = request.getHeaderNames();
        while (headerNames.hasMoreElements()) {
            String name = headerNames.nextElement();
            System.out.println(name + " = " + request.getHeader(name));
        }
        return "host";
    }

    @GetMapping("/query")
    public String query(Integer number) {
        System.out.println(number);
        return "query";
    }

依次启动eureka、ha、predicates服务

使用curl访问
curl -XPOST localhost:8024/post

curl localhost:8024/header -H “id:111”

curl localhost:8024/cookie --cookie “username=222”

curl localhost:8024/host -H “host:a.another.com”

curl localhost:8024/querynumber=333

相关链接

首页
上一篇:Dashboard服务监控
下一篇:Gateway过滤器Filter

有关SpringCloud之Gateway网关路由转发route、断言predicates的更多相关文章

  1. ruby-on-rails - Rails 3 中的多个路由文件 - 2

    Rails2.3可以选择随时使用RouteSet#add_configuration_file添加更多路由。是否可以在Rails3项目中做同样的事情? 最佳答案 在config/application.rb中:config.paths.config.routes在Rails3.2(也可能是Rails3.1)中,使用:config.paths["config/routes"] 关于ruby-on-rails-Rails3中的多个路由文件,我们在StackOverflow上找到一个类似的问题

  2. ruby - rails 3 redirect_to 将参数传递给命名路由 - 2

    我没有找到太多关于如何执行此操作的信息,尽管有很多关于如何使用像这样的redirect_to将参数传递给重定向的建议:action=>'something',:controller=>'something'在我的应用程序中,我在路由文件中有以下内容match'profile'=>'User#show'我的表演Action是这样的defshow@user=User.find(params[:user])@title=@user.first_nameend重定向发生在同一个用户Controller中,就像这样defregister@title="Registration"@user=Use

  3. ruby-on-rails - Rails 3,嵌套资源,没有路由匹配 [PUT] - 2

    我真的为这个而疯狂。我一直在搜索答案并尝试我找到的所有内容,包括相关问题和stackoverflow上的答案,但仍然无法正常工作。我正在使用嵌套资源,但无法使表单正常工作。我总是遇到错误,例如没有路线匹配[PUT]"/galleries/1/photos"表格在这里:/galleries/1/photos/1/edit路线.rbresources:galleriesdoresources:photosendresources:galleriesresources:photos照片Controller.rbdefnew@gallery=Gallery.find(params[:galle

  4. ruby-on-rails - Rails - 从命名路由中提取 HTTP 动词 - 2

    Rails中有没有一种方法可以提取与路由关联的HTTP动词?例如,给定这样的路线:将“users”匹配到:“users#show”,通过:[:get,:post]我能实现这样的目标吗?users_path.respond_to?(:get)(显然#respond_to不是正确的方法)我最接近的是通过执行以下操作,但它似乎并不令人满意。Rails.application.routes.routes.named_routes["users"].constraints[:request_method]#=>/^GET$/对于上下文,我有一个设置cookie然后执行redirect_to:ba

  5. ruby-on-rails - 如何在 Rails 中设置路由的默认格式? - 2

    路由有如下代码:resources:orders,only:[:create],defaults:{format:'json'}resources:users,only:[:create,:update],defaults:{format:'json'}resources:delivery_types,only:[:index],defaults:{format:'json'}resources:time_corrections,only:[:index],defaults:{format:'json'}是否可以使用1个字符串为所有资源设置默认格式,每行不带“默认值”散列?谢谢。

  6. ruby-on-rails - 优雅的 Rails : multiple routes, 相同的 Controller Action - 2

    让多条路线去同一条路的最优雅的方式是什么ControllerAction?我有:get'dashboard',to:'dashboard#index'get'dashboard/pending',to:'dashboard#index'get'dashboard/live',to:'dashboard#index'get'dashboard/sold',to:'dashboard#index'这很丑陋。有什么“更优雅”的建议吗?一个类轮的奖励积分。 最佳答案 为什么不只有一个路由和一个Controller操作,并根据传递给它的参数来

  7. ruby-on-rails - 与 ActiveMerchant 一起使用的最佳支付网关是什么? - 2

    我需要使用ActiveMerchant库在我们的一个Rails应用程序中设置支付解决方案。尽管这个问题非常主观,但人们对主要网关(BrainTree、Authorize.net等)的体验如何?它必须:处理定期付款。有能力记入个人帐户。能够取消付款。有办法存储用户的付款详细信息(例如Authotize.netsCIM)。干杯 最佳答案 ActiveMerchant很棒,但在过去一年左右的时间里,我在使用它时发现了一些问题。首先,虽然某些网关可能会得到“支持”——但并非所有功能都包含在内。查看功能矩阵以确保完全支持您选择的网关-http

  8. ruby-on-rails - ruby rails : How do you prefix named routes? - 2

    我希望生成一个链接,该链接的前缀附加到命名路由本身。像这样显示路径“/old/recipes”:recipes_path(:prefix=>"old/")#thecorrectwayshouldshow"/old/recipes"我不想动routes.rb文件,而是修改带前缀的命名路由。这可能吗?您将如何正确地做到这一点?编辑:我正在使用Rails3。添加可选前缀的原因是我也想使用普通的recipes_path。所以我想同时使用“/recipes”和“/old/recipes”。 最佳答案 如果您不想触及路由文件,您将会遇到很多麻烦

  9. ruby - cucumber 的路由问题 - 2

    我正在使用rails3和cucumber,除了这个小问题,一切都很顺利GivenIamonthe"editautomobile"pageNoroutematches{:controller=>"automobiles",:action=>"edit"}(ActionController::RoutingError)现在路径在paths.rb中设置为edit_automobile_path在routes.rb中我有汽车作为资源,我搭建了它所以请告诉我我遗漏了什么,清楚地定义了路线并且匹配,因为我运行了rake路线并看到了路线。请指出正确的方向 最佳答案

  10. ruby - Rails 路由 : Giving default values for path helpers - 2

    有什么方法可以为url/path助手提供默认值吗?我有一个可选范围环绕我的所有路线:#config/routes.rbFoo::Application.routes.drawdoscope"(:current_brand)",:constraints=>{:current_brand=>/(foo)|(bar)/}do#...allotherroutesgohereendend我希望用户能够使用这些URL访问网站:/foo/some-place/bar/some-place/some-place为了方便起见,我在我的ApplicationController中设置了一个@current

随机推荐