1. 创建工程 getway-server

2. 添加 pom 依赖:

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

3. 添加启动类

4. 添加配置文件:

#端口
server:port: 8080#服务名称
spring:application:name: getway-servicecloud:gateway:routes:- id: product-serviceuri: lb://product-service     #根据服务名称从注册中心拉取服务请求路径predicates:- Path=/product/**

id:我们自定义的路由 ID,保持唯一
uri:目标服务地址
predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默
认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)

5. 启动 getway 服务报错如下:

这个错误是因为 getway 内部是通过 netty + webflux 实现,webflux 实现和 springmvc 冲突

解决办法:将父工程配置的如下依赖添加到需要的工程中:

<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId>
</dependency>

对应在 product 和 order 服务中添加依赖,再次启动 getway 服务就成功了。

访问 http://localhost:8080/product/1

重写转发路径

在 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: getway-servicecloud:gateway:routes:- id: product-service
#        uri: http://127.0.0.1:9001
        uri: lb://product-service     #根据服务名称从注册中心拉取服务请求路径
        predicates:
#          - Path=/product/**
        - Path=/product-service/**filters:- RewritePath=/product-service/(?<segment>.*), /$\{segment}#根据服务名称配置路由转发
    discovery:locator:enabled: true   #开启服务名称自动转发
        lower-case-service-id: true   #名称小写形式

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

网关限流

1.pom 文件添加如下依赖:

<!--redis的依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!--监控依赖-->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId>
</dependency> 2.修改配置文件
server:port: 8080 #端口
spring:application:name: gateway-service #服务名称
  redis:host: localhostpool: 6379database: 0cloud: #配置SpringCloudGateway的路由
    gateway:routes:- id: order-serviceuri: lb://order-servicepredicates:- Path=/order-service/**filters:- RewritePath=/order-service/(?<segment>.*), /$\{segment}- id: product-serviceuri: lb://product-servicepredicates:- Path=/product-service/**filters:- name: RequestRateLimiterargs:
#            # 使用SpEL从容器中获取对象
            key-resolver: '#{@pathKeyResolver}'
#            # 令牌桶每秒填充平均速率
            redis-rate-limiter.replenishRate: 1
#            # 令牌桶的上限
            redis-rate-limiter.burstCapacity: 3- RewritePath=/product-service/(?<segment>.*), /$\{segment}

3. 新建 KeyResolverConfiguration 类

@Configuration
public class KeyResolverConfiguration {/**
     * 请求路径的限流规则
     * @return
     */
    @Beanpublic KeyResolver pathKeyResolver() {return new KeyResolver(){public Mono<String> resolve(ServerWebExchange exchange) {return Mono.just(exchange.getRequest().getPath().toString());}};}
}

访问 http://localhost:8080/product-service/product/1

如果连续刷新请求就会出现如下界面

通过 reids 的 MONITOR 可以监听 redis 的执行过程。这时候 Redis 中会有对应的数据

大括号中就是我们的限流 Key, 这边是 IP,本地的就是 localhost
timestamp: 存储的是当前时间的秒数,也就是 System.currentTimeMillis() / 1000 或者
Instant.now().getEpochSecond()
tokens: 存储的是当前这秒钟的对应的可用的令牌数量

根据参数限流  

1. 在 KeyResolverConfiguration 类中添加如下代码

/**
 * 请求参数限流
 * @return
 */
@Bean
public KeyResolver userKeyResolver() {return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId")//exchange.getRequest().getHeaders().getFirst("X-Forwarded-For") 基于请求ip的限流);
}

2. 修改配置文件:

访问同上,如果访问频率过高也是出现同样的效果

访问地址: http://localhost:8080/product-service/product/1?userId=1

需要带上参数。

spring getway的配置相关推荐

  1. 玩转Spring Cloud之配置中心(config server config client)

    玩转Spring Cloud之配置中心(config server &config client)  本文内容导航: 一.搭建配置服务中心(config server) 1.1.git方式 1 ...

  2. 如何让Spring Boot 的配置 “动” 起来?

    前言 对于微服务而言配置本地化是个很大的鸡肋,不可能每次需要改个配置都要重新把服务重新启动一遍,因此最终的解决方案都是将配置外部化,托管在一个平台上达到不用重启服务即可一次修改多处生效的目的. 但是对 ...

  3. spring外部化配置

    例如 1 <bean id="dataSource" 2 3 class="....." 4 5 p:username="aa" 6 ...

  4. Spring多数据源配置和使用

    Spring多数据源配置和使用 1.配置信息 <!--==============================bpt_mobdb数据库配置========================== ...

  5. Spring Boot 属性配置和使用

    spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot 系列 Spring Boot 入门 S ...

  6. Spring定时任务的配置

    spring的定时任务配置分为三个步骤:  1.定义任务  2.任务执行策略配置  3.启动任务 1.定义任务 <!--要定时执行的方法--> <bean id="test ...

  7. springboot原生mysql写法_【Rainbond最佳实践】Spring Boot框架配置MySQL

    Rainbond开源软件介绍: Rainbond是国内首个开源的生产级无服务器PaaS. 深度整合基于Kubernetes的容器管理.多类型CI/CD应用构建与交付.多数据中心的资源管理等技术,提供云 ...

  8. spring mvc mysql配置_spring mvc配置数据库连接

    ACM 配置中心实战:Spring + MyBatis + Druid + ACM 很多基于 Spring MVC 框架的 Web 开发中,Spring + MyBatis + Druid 是一个黄金 ...

  9. spring boot自动配置

    首先,一般的java Web项目需要很多配置,web配置(web.xml).spring配置(默认叫applicationContext.xml),非常繁琐 而spring-boot-starter是 ...

最新文章

  1. mysql 数据库异常大_Mysql连接数据库异常汇总【必收藏】
  2. android ListView详解
  3. IT餐馆—第二十五回 结对
  4. oc73--NSArray使用
  5. 转贴:Google提供网站恶意软件侦测服务
  6. LeetCode-116. 填充每个节点的下一个右侧节点指针
  7. 【Elasticsearch】所有可用 Qbox 插件的概述:第二部分
  8. linux 安卓svn,linux安装svn
  9. 在Win10中通过Google Chrome运行安卓应用
  10. 使用idea练习springmvc时,出现404错误总结
  11. gcc/g++ 如何支持c11/c++11标准编译
  12. 手机 机器人 谢超_大咖云集 长三角智造峰会演讲嘉宾名单曝光
  13. 对称正定矩阵的Cholesky分解
  14. JPEG文件中的EXIF(上)
  15. Shell脚本之正则表达式详解
  16. 讯为4412蜂鸣器驱动实现
  17. Eclipse所有快捷键
  18. 手机端html5自动调用touch事件,移动端中touch事件的详解
  19. Excel处理 中文转拼音缩写
  20. AngularJS动态加载Controller

热门文章

  1. 【攻防世界 level2】
  2. 输入一行字符,分别统计其中大写英文字母,小写英文字母,空格,数字,和其他字符的个数。
  3. 数码相机摄影修复技术DxO PureRAW中文
  4. Android中级面筋:开发2年的程序员如何短期突击面试?跟着这几步去准备,大厂也不远了
  5. 虚拟机(VMware Workstation或Hyper-V)装ghost版系统提示“ntldr is missing Press Ctrl+Alt+del to Resta
  6. 麦克风声源定位原理_一种利用麦克风阵列进行声源定位的方法与流程
  7. 装逼利器:QQ号转换成16进制登陆--用了这么长时间QQ竟然不知道
  8. bzoj 1646 bfs
  9. 我的世界——scratch——种子
  10. 3款最伟大的联机游戏