当前位置:网站首页>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开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦
边栏推荐
猜你喜欢
斩获BAT、TMD技术专家Offer,我都经历了什么?
无线模块的参数介绍和选型要点
孩子的编程启蒙好伙伴,自己动手打造小世界,长毛象教育AI百变编程积木套件上手
typescript11 - data types
Teach you how to configure Jenkins automated email notifications
The Meta Metaverse Division lost 2.8 billion in the second quarter, still want to continue to bet?Metaverse development has yet to see a way out
Mysql: Invalid default value for TIMESTAMP
Typescript14 - (type) of the specified parameters and return values alone
倍增、DFS序
The sword refers to offer17---print the n digits from 1 to the largest
随机推荐
Responsive layout vs px/em/rem
【genius_platform软件平台开发】第七十四讲:window环境下的静态库和动态库的一些使用方法(VC环境)
【网络安全】文件上传靶场通关(1-11关)
prometheus 监控概述
VS warning LNK4099: No solution found for PDB
MySQL (6)
ROS Action communication
【952. Calculate the maximum component size according to the common factor】
Installation problem corresponding to tensorflow and GPU version
.NET 跨平台应用开发动手教程 |用 Uno Platform 构建一个 Kanban-style Todo App
1782. Count the number of point pairs Double pointer
小黑leetcode之旅:104. 二叉树的最大深度
勾股数元组 od js
程序员转正述职报告/总结
35. Reverse linked list
MySQL高级-六索引优化
16、注册中心-consul
C语言_结构体指针数组函数选票系统
《实战》基于情感词典的文本情感分析与LDA主题分析
分布式.分布式锁