SpringCloud 服务网关路由规则的坑及解决

一、场景简述

笔者最近用到SpringCloud 服务网关的时候,进行服务网关的路由测试,发现无法路由自己设置的规则,测试的时候如下

通过错误排查发现,原来是路由规则写错了!

路由规则如下(错误)

#端口
server:
  port: 8080
spring:
  #该配置文件中的配置,对应的服务名称是wc-gateway
  application:
    name: wc-gateway
  profiles:
    active: dev
#服务网关配置
zuul:
  host:
    connect-timeout-millis: 60000
    socket-timeout-millis: 60000
    #路由规则
    routes:
      api:
        path: /api/user/**
        serviceId: wc-client-user

二、解决方案

只需要将routes及以下的属性左移,与host相等级别即可

修改后的路由规则

#端口
server:
  port: 8080
spring:
  #该配置文件中的配置,对应的服务名称是wc-gateway
  application:
    name: wc-gateway
  profiles:
    active: dev
#服务网关配置
zuul:
  host:
    connect-timeout-millis: 60000
    socket-timeout-millis: 60000
  #路由规则
  routes:
    api:
      path: /api/user/**
      serviceId: wc-client-user

好了,问题解决,我们重启应用测试,测试结果和预期一样。

SpringCloud 进阶之Zuul(路由网关)

1. Zuul(路由网关)

Zuul 包含了对请求的路由和过滤两个最主要的功能;

路由功能:负责将外部请求转发到具体的微服务实例上,是实现外部访问统一入口的基础;

过滤功能:负责对请求的处理过程进行干预,是实现请求校验,服务聚合等功能的基础;

Zuul 服务最终还是会注册进Eureka;

1.1 路由基本配置

新建 microservicecloud-zuul-gateway-9527

// pom.xml
<!-- zuul 路由网关 -->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-zuul</artifactId>
</dependency>
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-eureka</artifactId>
</dependency>
// application.yml
server:
  port: 9527
spring:
  application:
    name: microservicecloud-zuul-gateway
eureka:
  client:
    service-url:
      defaultZone: http://eureka7001.com:7001/eureka/,http://eureka7002.com:7002/eureka/,http://eureka7003.com:7003/eureka/
instance:
  instance-id: gateway-9527.com
  prefer-ip-address: true
  info:
    app.name: noodles-microcloud
    company.name: www.google.com
    build.artifactId: $project.artifactId$
    build.version: $project.version$
// hosts 修改: 127.0.0.1   myzuul.com
// 主启动类
@SpringBootApplication
@EnableZuulProxy
public class Zuul_9527_StartSpringCloudApp {
	public static void main(String[] args) {
		SpringApplication.run(Zuul_9527_StartSpringCloudApp.class, args);
	}
}
// 启动
// 三个Eureka集群
// microservicecloud-provider-dept-8001
// 路由
// 测试访问:
// 不用路由: http://localhost:8001/dept/get/1
// 使用路由: http://myzuul.com:9527/microservicecloud-dept/dept/get/1

1.2 Zuul 路由访问映射规则

// microservicecloud-zuul-gateway-9527
// 修改 application.yml
zuul:
  ignored-services: microservicecloud-dept      # 将原有路由关闭
  routes:
    prefix: /test       # 设置统一公共前缀, 访问地址:http://myzuul.com:9527/test/mydept/dept/get/1
    mydept.serviceId: microservicecloud-dept
    mydept.path: /mydept/**
// 修改之前,访问地址: http://myzuul.com:9527/microservicecloud-dept/dept/get/1
// 修改之后,访问地址: http://myzuul.com:9527/mydept/dept/get/1

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 引入SpringCloud-gateway报错的解决方案

    1.问题描述 在我引入SpringCloud-gateway,运行时报错如下: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'routeDefinitionRouteLocator' defined in class path resource [org/springframework/cloud/gateway/config/GatewayAuto

  • SpringCloud 如何提取公共配置

    SpringCloud 提取公共配置 在开发微服务项目时,通常会有很多服务,此时会用配置中心来管理这些服务的配置,但有些服务可能会有相同的配置,比如数据源配置,eureka server注册中心地址配置,actuator开放端口配置等,很多的服务都需要,如果每个服务都写一份这样相同的配置,服务一多,也挺麻烦的,并且如果要换一个数据库或注册中心,每个服务都得改,很麻烦,所以就需要将这些公共的配置提取出来,放到公共的配置文件中,而这些服务去引用这些配置即可. 原本服务的配置文件可能是这样的: spr

  • 使用SpringCloudApiGateway之支持Cors跨域请求

    问题背景 公司的项目需要前后端分离,vue+java,这时候就需要支持Cors跨域请求了.最近对zuul进行升级,假如说zuul是1.0的话,api gateway就是2.0的网关,支持ws等,基于NIO,各方面还是强大的. 解决方案 新建一个Configuration类即可 import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.gateway.dis

  • SpringCloud gateway跨域配置的操作

    gateway跨域配置 gateway允许跨域的配置和zuul的不一样,记录一下. 版本 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.6.RELEASE</version> <relativePath/> <!--

  • 解决springcloud-gateway限流遇到的问题

    场景: 最近在研究spring cloud组件gateway 限流的实现,看官网spring cloud操作下去,决定采用redis的方式去实现这个限流,因为系统架构是分布式的,方便以后的迭代升级,所以就用redis了,之后就出现了下面的异常!!! 异常信息: java.lang.IllegalArgumentException: Unable to find GateWayFilterFactory with name RequestRateLimiter 我的开发环境是离线的状态,maven

  • 浅谈springcloud gateway 连接保活问题

    项目中使用了springcloud gateway作为网关,上游与负载均衡服务器连接. 近期通过监控系统观察,发现网关与上游负载均衡服务器保持的TCP连接有300+,初步怀疑是调用方未释放连接 用如下方法进行分析: 1)周期性采集当前建立的连接及端口数据 首先是每隔10分钟连续采集2两个小时,发现在两个小时之内新出现的端口不到12个,再逐步缩短采样周期,到最后每秒采集一次,分析发现每秒种建立一个连接,同时关闭一个连接,当仍存在300+连接,这些连接对应的端口称为不活跃端口,记录下这300+不活跃

  • Java之Springcloud Gateway内置路由案例讲解

    Spring Cloud Gateway路由匹配是Spring WebFlux基础功能的一部分,在Spring Cloud Gateway中内置了很多路由断言工厂类.不同的断言工厂类针对HTTP请求的不同属性.多个断言工厂类可以使用逻辑"and"进行组合使用. 4.1 After Route Predicate Factory        这个Predicate工厂的实现类是AfterRoutePredicateFactory,使用一个时间参数,如果当前请求的时间在配置的赶时间之后,

  • springboot集成springCloud中gateway时启动报错的解决

    在项目中引入springcloud中的gateway时报以下错误 Description: Parameter 0 of method modifyRequestBodyGatewayFilterFactory in org.springframework.cloud.gateway.config.GatewayAutoConfiguration required a bean of type 'org.springframework.http.codec.ServerCodecConfigur

  • SpringCloud 服务网关路由规则的坑及解决

    一.场景简述 笔者最近用到SpringCloud 服务网关的时候,进行服务网关的路由测试,发现无法路由自己设置的规则,测试的时候如下 通过错误排查发现,原来是路由规则写错了! 路由规则如下(错误) #端口 server: port: 8080 spring: #该配置文件中的配置,对应的服务名称是wc-gateway application: name: wc-gateway profiles: active: dev #服务网关配置 zuul: host: connect-timeout-mi

  • SpringCloud服务网关Gateway的使用教程详解

    目录 Gateway 什么是Gateway 什么是api网关 网关的三个核心概念 路由(Route) 断言(Predicate) 过滤(Filter) gateway的工作流程 如何使用Gateway gateway路由转发 使用配置文件 使用代码配置 路由实现负载均衡 gateway九种断言 gateway过滤修改 Gateway 什么是Gateway   由于Netflix的zuul发生问题,spring公司自己研发了一套网关框架Gateway用于取代zuul的使用.什么是gateway呢?

  • Spring Cloud Zuul路由规则动态更新解析

    这篇文章主要介绍了Spring Cloud Zuul路由规则动态更新解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 背景 Spring Cloud Zuul 作为微服务的网关,请求经过zuul路由到内部的各个service,由于存在着新增/修改/删除服务的路由规则的需求,zuul的路由规则的动态变更功能 提供了 无须重启zuul网关,即可实时更新,现有如下几种方式: 一.基于refresh + config-server事件动态刷新 (1)

  • SpringCloud超详细讲解微服务网关Gateway

    目录 前言 微服务网关GateWay介绍 GateWay特性介绍 Gateway 中的相关术语 Gateway实战 1.创建项目gateway 2.创建启动类 3.新增配置文件 4.编程方式实现路由 5.启动验证 总结 前言 上一篇:微服务网关Zuul 上文中,我们介绍了微服务网关Zuul,Zuul 是 Netflix 公司开源的产品,被称为第一代网关,也是 Spring Cloud 前几个版本默认使用的一款提供动态路由微服务网关组件,但是随着 Netflix 公司一系列的停更事件,在最新的 S

  • SpringCloud超详细讲解微服务网关Zuul

    目录 网关的作用 Spring Cloud 网关组件Zuul介绍 Zuul网关实战 1.创建服务 2.创建配置文件 3.创建Zuul过滤器 4.编写启动类 5.启动验证 总结 网关的作用 微服务架构中,服务实例的地址可能经常会发生变化,所以我们不能直接将服务的地址暴露出来.如果每一个微服务都直接暴露接口,会导致一系列的问题,比如调用过于复杂,涉及到账户.权限不能统一处理等.另外基于高内聚低耦合的设计准则来讲,我们也应该将内部系统和外部系统做切割. 因此,这时就需要有一个独立的组件来处理外部的请求

  • 详解Spring Cloud Gateway基于服务发现的默认路由规则

    1.Spring Gateway概述 1.1 什么是Spring Cloud Gateway Spring Cloud Gateway是Spring官方基于Spring 5.0,Spring Boot 2.0和Project Reactor等技术开发的网关,Spring Cloud Gateway旨在为微服务架构提供一种简单而有效的统一的API路由管理方式.Spring Cloud Gateway作为Spring Cloud生态系中的网关,目标是替代Netflix ZUUL,其不仅提供统一的路由

  • springcloud gateway自定义断言规则详解,以后缀结尾进行路由

    目录 springcloud gateway自定义断言规则,后缀结尾进行路由 1.新建一个路由断言工厂ExtCheckRoutePredicateFactory 2.修改gateway配置 3.修改gateway源码,将自定义断言类加到系统 predicates里 Gateway自定义路由断言工厂类 application.yml文件 路由断言工厂配置类 springcloud gateway自定义断言规则,后缀结尾进行路由 因工作需要,需要使用springcloud gateway ,以.ht

  • SpringCloud gateway+zookeeper实现网关路由的详细搭建

    目录 准备工作 网关搭建 准备工作 需要两个项目去实现路由demo1为springboot项目用于接入网关,测试网关连通性gateway为网关路由项目 网关搭建 1.电脑安装好zookeeper,并且正常运行服务Zookeeper官网 2.创建一个spring cloud gateway项目,并引入zookeeper功能 pom文件配置 <dependencies> <dependency> <groupId>org.springframework.cloud</

  • Spring Cloud 服务网关Zuul的实现

    服务网关的要素 稳定性 安全性 性能,并发性 扩展性 Spring Cloud Zuul - 路由+过滤器 - 核心是一系列的过滤器 Zuul路由配置 management: security: enabled: false // 权限设置 zuul: routes: # myProduct: // 这个名称可以随便填 # path: /myProduct/** # serviceId: product # sensitiveHeader: //敏感头过滤 # 简洁写法 product: /my

  • 详解SpringCloud新一代网关Gateway

    一.概述简介 1.1.简介 SpringCloud Gateway作为Spring Cloud生态系统中的网关,目标是替代Zuul,在Spring Cloud 2.0以上版本中,没有对新版本的Zuul 2.0以上最新高性能版本进行集成,仍然还是使用的Zuul 1.x非Reactor模式的老版本.而为了提升网关的性能,SpringCloud Gateway是基于WebFlux框架实现的,而WebFlux框架底层则使用了高性能的Reactor模式通信框架Netty. Spring Cloud Gat

随机推荐