当前位置:网站首页>Gateway路由的配置方式
Gateway路由的配置方式
2022-07-31 01:29:00 【万里长江雪】
路由的配置方式
路由是网关配置的基本组成模块,和Zuul的路由配置模块类似。一个Route模块由一个 ID,一个目标 URI,一组断言和一组过滤器定义。如果断言为真,则路由匹配,目标URI会被访问。
基础路由配置方式
如果请求的目标地址,是单个的URI资源路径,配置文件实例如下。
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: service1
uri: https://blog.csdn.net
predicates:
- Path=/csdn
各字段含义如下。
- id:我们自定义的路由 ID,保持唯一
- uri:目标服务地址
- predicates:路由条件,Predicate 接受一个输入参数,返回一个布尔值结果。该接口包含多种默认方法来将 Predicate 组合成其他复杂的逻辑(比如:与,或,非)。
上面这段配置的意思是,配置了一个 id 为 url-proxy-1的URI代理规则,路由的规则为,当访问地址http://localhost:8080/csdn时,会路由到地址https://blog.csdn.net。
基于代码的路由配置方式
转发功能同样可以通过代码来实现,我们可以在启动类 GateWayApplication 中添加方法 customRouteLocator() 来定制转发规则。
@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayApplication.class, args);
}
@Bean
public RouteLocator customRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route("path_route", r -> r.path("/csdn")
.uri("https://blog.csdn.net"))
.build();
}
}
和注册中心相结合的路由配置方式
在uri的schema协议部分为自定义的lb:类型,表示从微服务注册中心(如Eureka)订阅服务,并且通过负载均衡进行服务的路由。代码如下。
server:
port: 8080
spring:
application:
name: api-gateway
cloud:
gateway:
routes:
- id: service1
# uri: http://127.0.0.1:9001
uri: lb://cloud-payment-service
predicates:
- Path=/payment/**
eureka:
client:
service-url:
defaultZone: http://127.0.0.1:9004/eureka
注:这里cloud-payment-service是提前注册好的服务,且开放两个端口9000,9001供伦循使用。
注册中心相结合的路由配置方式,与单个URI的路由配置,区别其实很小,仅仅在于URI的schema协议不同。单个URI的地址的schema协议,一般为http或者https协议。启动多个支付微服务,会发现端口9000,9001轮流出现。
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
- ShardingSphere's unsharded table configuration combat (6)
- 聚簇索引和非聚簇索引到底有什么区别
- Jetpack Compose学习(8)——State及remeber
- typescript15- (specify both parameter and return value types)
- 1.非类型模板参数 2.模板的特化 3.继承讲解
- 设置浏览器滚动条样式
- Huawei's "genius boy" Zhihui Jun has made a new work, creating a "customized" smart keyboard from scratch
- 1782. Count the number of point pairs Double pointer
- ShardingSphere's public table combat (7)
- Why use high-defense CDN when financial, government and enterprises are attacked?
猜你喜欢
随机推荐
调度中心xxl-Job
pycharm重命名后无法运行(报错: can‘t open file......No such file or directory)
太阳能板最大面积 od js
Set the browser scrollbar style
Rocky/GNU之Zabbix部署(1)
tensorflow与GPU版本对应安装问题
网站频繁出现mysql等数据库连接失败等信息解决办法
MySQL (6)
1782. Count the number of point pairs Double pointer
蛮力法/邻接矩阵 广度优先 有向带权图 无向带权图
TiDB 操作实践 -- 备份与恢复
87. 把字符串转换成整数
Meta元宇宙部门第二季度亏损28亿 仍要继续押注?元宇宙发展尚未看到出路
This project is so geeky
Typescript14 - (type) of the specified parameters and return values alone
Ticmp - 更快的让应用从 MySQL 迁移到 TiDB
Google官方控件ShapeableImageView使用
typescript11-数据类型
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
射频器件的基本参数1








