Spring Cloud Gateway 整合Eureka路由转发
网关负责转发工作,那么它需要知道后端的服务信息,今天我们来学习下Spring Cloud Gateway 整合Eureka的操作,实现服务转发功能。
在之前的基础上添加eureka-client的依赖:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
下面就是配置具体的转发规则了,这边需要注意下的是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/
uri以lb://开头(lb代表从注册中心获取服务),后面接的就是你需要转发到的服务名称,这个服务名称必须跟eureka中的对应,否则会找不到服务,错误如下:
org.springframework.cloud.gateway.support.NotFoundException: Unable to find instance for fsh-house1
如果引入了spring-cloud-starter-netflix-eureka-client包,但你不想整合Eureka,也可以通过下面的配置关闭:
eureka.client.enabled=false
说完了直接配置路由的方式,我们来说说不配置的方式也能转发,有用过Zuul的同学肯定都知道,Zuul默认会为所有服务都进行转发操作,只需要在访问路径上指定要访问的服务即可,通过这种方式就不用为每个服务都去配置转发规则,当新加了服务的时候,不用去配置路由规则和重启网关。
在Spring Cloud Gateway中当然也有这样的功能,只需要通过配置即可开启,配置如下:
spring.cloud.gateway.discovery.locator.enabled=true
开启之后我们就可以通过地址去访问服务了,格式如下:
http://网关地址/服务名称(大写)/** http://localhost:8084/FSH-HOUSE/house/1
这个大写的名称还是有很大的影响,如果我们从Zull升级到Spring Cloud Gateway的话意味着请求地址有改变,或者重新配置每个服务的路由地址,通过源码我发现可以做到兼容处理,再增加一个配置即可:
spring.cloud.gateway.discovery.locator.lowerCaseServiceId=true
配置完成之后我们就可以通过小写的服务名称进行访问了,如下:
http://网关地址/服务名称(小写)/** http://localhost:8084/fsh-house/house/1
配置源码: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<PredicateDefinition> predicates = new ArrayList<>(); private List<FilterDefinition> filters = new ArrayList<>(); }
文章源码参考地址:
https://github.com/yinjihuan/spring-cloud/tree/master/fangjia-gateway
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
Maven远程仓库的各种配置
1.远程仓库的配置 在平时的开发中,我们往往不会使用默认的中央仓库,默认的中央仓库访问的速度比较慢,访问的人或许很多,有时候也无法满足我们项目的需求,可能项目需要的某些构件中央仓库中是没有的,而在其他远程仓库中有,如JBoss Maven仓库。这时,可以在pom.xml中配置该仓库,代码如下: 1 <!-- 配置远程仓库 --> 2 <repositories> 3 <repository> 4 <id>jboss</id> 5 <name>JBoss Repository</name> 6 <url>http://repository.jboss.com/maven2/</url> 7 <releases> 8 <enabled>true</enabled> 9 <updatePolicy>daily</updatePolicy&...
- 下一篇
spring整合hibernate
个人感觉比较好的spring 整合hibernate的方式 一、添加依赖 <dependency> <groupId>org.springframework</groupId> <artifactId>spring-orm</artifactId> </dependency> <!--hibernate--> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-core</artifactId> </dependency> <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-ehcache</artifactId> </dependency> 二、在spring的core配置文件中配置 <?...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
-
Docker使用Oracle官方镜像安装(12C,18C,19C)
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8编译安装MySQL8.0.19
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2配置默认Tomcat设置,开启更多高级功能
- MySQL8.0.19开启GTID主从同步CentOS8
- CentOS7,8上快速安装Gitea,搭建Git服务器
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果
推荐阅读
最新文章
- SpringBoot2全家桶,快速入门学习开发网站教程
- SpringBoot2更换Tomcat为Jetty,小型站点的福音
- Springboot2将连接池hikari替换为druid,体验最强大的数据库连接池
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- MySQL8.0.19开启GTID主从同步CentOS8
- Docker快速安装Oracle11G,搭建oracle11g学习环境
- SpringBoot2整合Redis,开启缓存,提高访问速度
- Windows10,CentOS7,CentOS8安装Nodejs环境
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2编写第一个Controller,响应你的http请求并返回结果