Spring Cloud Alibaba 使用Gateway作为服务网关

为什么使用Gateway

从没有网关的角度来看后台如有N个服务,那么前端则需要对接N个服务;只要后台修改IP或者端口等任何信息那么前端也需要修改。
当服务对接了网关后前端只需要统一调用网关入口即可,具体调用那个服务,IP地址或者端口号是多少前端无需关注由网关处理。

搭建网关服务

创建普通SpringBoot工程(spring-cloud-alibaba-gateway)该工程当前用于搭建微服务网关
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>com.gitee</groupId>
        <artifactId>spring-cloud-alibaba-version-parent</artifactId>
        <version>0.0.1</version>
    </parent>
    <groupId>com.gitee.example.gateway</groupId>
    <artifactId>spring-cloud-alibaba-gateway</artifactId>
    <version>0.0.1</version>
    <packaging>jar</packaging>
    <dependencies>
        <!-- 使用 bootstrap -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
        <!-- openfeign 服务发现调用 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <!-- gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- 负载均衡 -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>
        <!-- webflux -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>
        <!-- nacos -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
        <!-- utils -->
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>${hutool.version}</version>
        </dependency>
    </dependencies>

</project>

application.yaml

spring:
  application:
    name: gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
        metadata:
          version: 0.0.1
          appname: ${spring.application.name}
        #指定命名空间 对应dev环境
        namespace: '7e3699fa-09eb-4d47-8967-60f6c98da94a'
        #指定分组 案例组
        group: 'EXAMPLE-GROUP'
        #指定集群环境 华南
        cluster-name: 'HuaNan'
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: service-member
          uri: lb://service-member
          predicates:
            - Path=/member/**
          filters:
            - StripPrefix=1
        - id: service-user
          uri: lb://service-user
          predicates:
            - Path=/user/**
          filters:
            - StripPrefix=1

启动类定义: GatewayApplication.java

/**
 * gateway 网关
 *
 * @author wentao.wu
 */
@EnableDiscoveryClient // 开启服务发现
@SpringBootApplication // SpringBoot启动
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}

先后启动用户服务与会员服务:spring-cloud-alibaba-service-member,spring-cloud-alibaba-service-user,启动完成后启动网关服务: spring-cloud-alibaba-gateway
** 网关访问测试 **
访问用户服务: http://localhost/user/hello/say/userGateway,该接口地址:http://localhost/user这一段会路由到用户服务里面去。
所以实际访问的是用户服务的hello/say/userGateway。

{
"code": "1",
"msg": "你好啊,userGateway!",
"errorCode": null,
"errorMsg": null,
"result": null,
"ok": true
}

源码代码存放地址#

gitee: https://gitee.com/SimpleWu/spring-cloud-alibaba-example.git
cnblogs: https://www.cnblogs.com/SimpleWu
持续更新目录:https://www.cnblogs.com/SimpleWu/p/15476427.html

posted @ 2022-09-06 11:05  日落西风又在吹  阅读(137)  评论(0编辑  收藏  举报