springcloud gateway搭建实例

gateway官方文档:https://docs.spring.io/spring-cloud-gateway/docs/current/reference/html/#route-cors-configuration

搭建一个简单gateway网管实例

gateway运作过程

浏览器-》nigix负载均衡->gateway网关节点群(匹配断言-》找到对应routMapper->找到对应的路由-》执行pre-filters-》微服务-》执行post-filters)-》返回给浏览器

所需依赖
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
</dependency>
<!--将服务注册进注册中心 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
</dependencies>
yml配置

 

server:
port: 8601
eureka:
instance:
hostname: gatawayModel
prefer-ip-address: true
client:
service-url:
defaultZone: http://localhost:8301/eureka/
fetch-registry: true #是否向注册服务中心获取其他服务
register-with-eureka: true #是否注册到euraka服务中
# defaultZone:http://${eureka.instance.hostname}:${server.port}/eureka/


spring:
application:
name: gatawayModel
cloud:
gateway:
routes:
- id: FEIGNCLIENT-SERVICE
#lb是负载均衡 lb://注册的服务名
uri: lb://FEIGNCLIENT-SERVICE
predicates: #路由断言 路由断言path中的第一个词不能和服务名相同 相同会被去除掉
- Path=/feignClientService/**
filters:
- StripPrefix=1 #表示在当前匹配路径中去掉第一个前缀
        - id: eurekaClient-userService
uri: lb://eurekaClient-userService
predicates: #路由断言
- Path=/userService/**
filters:
- StripPrefix=1 #表示在当前匹配路径中去掉第一个前缀

(坑:开始没有加下面这个,导致一直访问不到路由
filters:
- StripPrefix=1 #表示在当前匹配路径中去掉第一个前缀


启动类

 大功告成

测试:

8601是gataway网关的端口 

/feignClientService  是配置的断言匹配规则 只有断言匹配规则为true才会去寻找路由

/feignService1/testRibbonProvider   具体的服务controller rest服务路径

上诉接口内容是打印一段字符串到控制台

 

userService有两个节点,因此路由负载均衡到两个节点上 分别是8401和8404服务

 

 

posted on   丶柚子  阅读(37)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
历史上的今天:
2018-06-16 oracle 导入导出数据
< 2025年3月 >
23 24 25 26 27 28 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 1 2 3 4 5

导航

统计

点击右上角即可分享
微信分享提示