温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

SpringCloud-Gateway如何整合Eureka路由转发

发布时间:2021-09-29 14:41:38 来源:亿速云 阅读:150 作者:柒染 栏目:编程语言

这篇文章将为大家详细讲解有关SpringCloud-Gateway如何整合Eureka路由转发,文章内容质量较高,因此小编分享给大家做个参考,希望大家阅读完这篇文章后对相关知识有一定的了解。

创建 Gateway 项目

创建一个 Spring Boot 的 Maven 项目,增加 Spring Cloud Gateway 的依赖,代码如下所示。

<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version><relativePath /></parent><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency></dependencies>

启动类就按 Spring Boot 的方式即可,无须添加额外的注解。代码如下所示。

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

路由转发示例

下面来实现一个最简单的转发功能——基于 Path 的匹配转发功能。

Gateway 的路由配置对 yml 文件支持比较好,我们在 resources 下建一个 application.yml 的文件,内容如下:

server:
  port: 2001spring:
  cloud:gateway:  routes:- id: path_routeuri: http://c.biancheng.netpredicates:
  - Path=/spring_cloud

当你访问 http://localhost:2001/spring_cloud 的时候就会转发到 http://c.biancheng.net/spring_cloud。

如果我们要支持多级 Path,配置方式跟 Zuul 中一样,在后面加上两个*号即可,比如:

- id: path_route2uri: http://c.biancheng.netpredicates:
  - Path=/spring_cloud/**

这样一来,上面的配置就可以支持多级 Path,比如访问 http://localhost:2001/spring_cloud/view/1 的时候就会转发到 http://c.biancheng.net/spring_cloud/view/1。

整合 Eureka 路由

添加 Eureka Client 的依赖,代码如下所示。

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

配置基于 Eureka 的路由:

- id: user-serviceuri: lb://user-servicepredicates:
  - Path=/user-service/**

uri 以lb://开头(lb 代表从注册中心获取服务),后面接的就是你需要转发到的服务名称,这个服务名称必须跟 Eureka 中的对应,否则会找不到服务,错误代码如下:

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for user-service1

整合 Eureka 的默认路由

Zuul 默认会为所有服务都进行转发操作,我们只需要在访问路径上指定要访问的服务即可,通过这种方式就不用为每个服务都去配置转发规则,当新加了服务的时候,不用去配置路由规则和重启网关。

在 Spring Cloud Gateway 中当然也有这样的功能,通过配置即可开启,配置如下:

spring:
  cloud:gateway:  discovery:locator:  enabled: true

开启之后我们就可以通过地址去访问服务了,格式如下:

http://网关地址/服务名称(大写)/**http://localhost:2001/USER-SERVICE/user/get?id=1

这个大写的名称还是有很大的影响,如果我们从 Zuul 升级到 Spring Cloud Gateway 的话意味着请求地址有改变,或者重新配置每个服务的路由地址,通过源码笔者发现可以做到兼容处理,再增加一个配置即可:

spring:
  cloud:gateway:  discovery:locator:  lowerCaseServiceId: true

配置完成之后我们就可以通过小写的服务名称进行访问了,如下所示:

http://网关地址/服务名称(小写)/**http://localhost:2001/user-service/user/get?id=1

注意:开启小写服务名称后大写的服务名称就不能使用,两者只能选其一。

配置源码在 org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties 类中,代码所示。

@ConfigurationProperties("spring.cloud.gateway.discovery.locator")public class DiscoveryLocatorProperties {/**
     * 服务名称小写配置, 默认为false
     *
     */private boolean lowerCaseServiceId = false;
}

关于SpringCloud-Gateway如何整合Eureka路由转发就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI