Spring Cloud Gateway 整合Eureka路由转发

https://blog.csdn.net/u010889990/article/details/81231736

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/u010889990/article/details/81231736

前面我们对Spring Cloud Gateway进行了一个入门的学习,具体文章可以查看《Spring Cloud Gateway 网关尝鲜》进行学习。

网关负责转发工作,那么它需要知道后端的服务信息,今天我们来学习下Spring Cloud Gateway 整合Eureka的操作,实现服务转发功能。

在之前的基础上添加eureka-client的依赖:

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

   
   
  • 1
  • 2
  • 3
  • 4

下面就是配置具体的转发规则了,这边需要注意下的是uri的配置:

server:
  port: 8084
spring:
  cloud:
    gateway:
      routes:
      - id: fsh-house
        uri: lb://fsh-house
        predicates:
        - Path=/house/**

application:
name: fangjia-gateway

eureka:
instance:
prefer-ip-address: true
client:
service-url:
defaultZone: http://yinjihuan:123456@master:8761/eureka/

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20

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

org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for fsh-house1

 
 
  • 1

如果引入了spring-cloud-starter-netflix-eureka-client包,但你不想整合Eureka,也可以通过下面的配置关闭:

eureka.client.enabled=false

 
 
  • 1

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

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

spring.cloud.gateway.discovery.locator.enabled=true

 
 
  • 1

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

http://网关地址/服务名称(大写)/**

http://localhost:8084/FSH-HOUSE/house/1

  • 1
  • 2
  • 3

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

spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true

 
 
  • 1

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

http://网关地址/服务名称(小写)/**

http://localhost:8084/fsh-house/house/1

  • 1
  • 2
  • 3

配置源码:org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties

@ConfigurationProperties("spring.cloud.gateway.discovery.locator")
public class DiscoveryLocatorProperties {
/** Flag that enables DiscoveryClient gateway integration */
private boolean enabled = false;

/**
 * The prefix for the routeId, defaults to discoveryClient.getClass().getSimpleName() + "_".
 * Service Id will be appended to create the routeId.
 */
private String routeIdPrefix;

/**
 * SpEL expression that will evaluate whether to include a service in gateway integration or not,
 * defaults to: true
 */
private String includeExpression = "true";

/** SpEL expression that create the uri for each route, defaults to: 'lb://'+serviceId */
private String urlExpression = "'lb://'+serviceId";

/**
 * Option to lower case serviceId in predicates and filters, defaults to false.
 * Useful with eureka when it automatically uppercases serviceId.
 * so MYSERIVCE, would match /myservice/**
 */
private boolean lowerCaseServiceId = false;

private List&lt;PredicateDefinition&gt; predicates = new ArrayList&lt;&gt;();

private List&lt;FilterDefinition&gt; filters = new ArrayList&lt;&gt;();

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

文章源码参考地址:
https://github.com/yinjihuan/spring-cloud/tree/master/fangjia-gateway

欢迎加入我的知识星球,一起交流技术,免费学习猿天地的课程(http://cxytiandi.com/course)

PS:目前星球中正在星主的带领下组队学习Spring Cloud,等你哦!

微信扫码加入猿天地知识星球

猿天地

                                </div>
            <link href="https://csdnimg.cn/release/phoenix/mdeditor/markdown_views-095d4a0b23.css" rel="stylesheet">
                </div>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hello_world!

你的鼓励将是我创作的最大动力!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值