【Spring Cloud Alibaba】Gateway 分布式网关

【Spring Cloud Alibaba】Gateway 分布式网关

1、Spring Cloud Gateway

分布式网关主要是统一调度各个服务之间的协调关系,如果说没有统一的网关,每个服务都给前端开发人员提供一个接口调用地址,这是一件非常变态的事,有了网关之后,就可以实现统一调度了,只需要给前端开发人员一个服务调用地址,调用不通的接口,即可完成服务分发与接口调用,是不是特别方便

下面我们通过实例来分析,Gateway 网关的实战作用,首先我们需要一个服务提供者,因为我们前面已经写了很多的服务了,我这里就挑选了两个,一个服务提供者,一个服务消费者,我们再建一个网关来统一处理测试网关的实战作用

下图就是网关调用消费者,消费者通过 OpenFeign 调用服务提供者

因为前面已经使用过了,这里就直接复用就可以了,并且两个都注册到了 Nacos

  • spring-cloud-alibaba-ribbon-consumer 服务消费者:https://gitee.com/tellsea/spring-cloud-alibaba-learn/tree/master/spring-cloud-alibaba-ribbon-consumer
  • spring-cloud-alibaba-ribbon-provider 服务提供者:https://gitee.com/tellsea/spring-cloud-alibaba-learn/tree/master/spring-cloud-alibaba-ribbon-provider

两个服务的构建
【Spring Cloud Alibaba】Ribbon 负载均衡器:https://blog.csdn.net/qq_38762237/article/details/122086619

2、构建网关服务

创建 spring-cloud-alibaba-gateway 模块,并增加 spring-cloud-starter-gateway 依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.1.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

这里有个细节点,不能再依赖中增加 web 模块的依赖,否则启动报错

修改 application.properties 配置文件

server.port=8085
spring.application.name=spring-cloud-alibaba-gateway
management.server.port=9095
management.endpoints.jmx.exposure.include=*
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# Nacos注册信息
spring.cloud.nacos.discovery.server-addr=localhost:8848
spring.cloud.nacos.discovery.username=nacos
spring.cloud.nacos.discovery.password=nacos
spring.cloud.nacos.discovery.namespace=sandbox-configuration

# gateway配置
spring.cloud.gateway.discovery.locator.enabled=true
spring.cloud.gateway.routes[0].id=spring-cloud-alibaba-ribbon-consumer
spring.cloud.gateway.routes[0].uri=lb://spring-cloud-alibaba-ribbon-consumer
spring.cloud.gateway.routes[0].predicates[0]=Path=/feign/**

就多了网关代理配置,可以配置多种形式的网关,启动类增加注解

@EnableDiscoveryClient

到这里,网关就配置完成了,是不是特别简单呀,接下来启动三个服务


三个服务的端口如下

  • spring-cloud-alibaba-gateway:8085
  • spring-cloud-alibaba-ribbon-consumer:8002
  • spring-cloud-alibaba-ribbon-provider:8000
直接访问

http://localhost:8002/feign/ribbon/message/Tellsea

返回内容

[端口:]8000,消息:Tellsea

说明消费者调用服务提供者没问题,接下来通过网关访问消费者

http://localhost:8085/feign/ribbon/message/Tellsea

同样返回消息

[端口:]8000,消息:Tellsea

OK,到此网关配置完成

微信公众号

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

Tellsea

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

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

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

打赏作者

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

抵扣说明:

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

余额充值