springcloud+gateway初相识

本文介绍了如何在Spring Cloud微服务架构中使用Spring Cloud Gateway作为API网关,替代了之前的Zuul。展示了Gateway的基本配置,包括POM文件、application.properties设置以及启动类,并提供了通过Gateway自动路由到不同微服务的示例。此外,还提到了Eureka服务注册与发现的整合,以及如何通过配置文件定义路由规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在微服务架构中经常遇到的另一个组件,springcloud中的gateway网关,springboot在1.x版本中都是采用的zuul网关;zuul是netfix开发的一个网关组件,但在2.x版本中,zuul由于更新迭代的速度过慢,于是springcloud就自己推出了一个新的网关组件,那就是gateway。
gateway是在spring生态系统之上构建的API网关服务,基于Spring 5,Spring Boot2和Project Reactor等技术。gateway旨在提供一种简单而有效的方式来对API进行路由,以及提供一些强大的过滤器功能,例如:反向代理、熔断、限流、重试等。
在上篇文章的基础上,一共4个微服务工程,分别为eureka服务注册与发现、gateway网关(取代zuul网关)、consumer服务、provider服务
gateway实例
1依赖

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.5.7</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com</groupId>
    <artifactId>gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>gateway</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>2020.0.4</spring-cloud.version>
    </properties>
    <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>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>
    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

2application.properties


spring.application.name=gateway
server.port=9006

#使用IP注册
eureka.instance.prefer-ip-address=true
eureka.instance.instance-id=${spring.cloud.client.ip-address}:${server.port}
eureka.client.service-url.defaultZone=http://localhost:9001/eureka/

#开启网关
spring.cloud.gateway.enabled=true
#开启自动路由,以服务id建立路由,服务id默认大写
spring.cloud.gateway.discovery.locator.enabled=true
#服务id设置为小写
spring.cloud.gateway.discovery.locator.lower-case-service-id=true

3启动类

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }

}

测试
通过gateway自动路由到不同的微服务中,如


或如下配置

server:
  port: 9006
spring:
  application:
    name: gateway
  cloud:
    gateway:
      routes:
        - id: consumer
          uri: http://localhost:9004
          predicates:
            - Path=/rest1/**
        - id: provider
          uri: http://localhost:9005
          predicates:
            - Path=/rest2/**
eureka:
  client:
    service-url:
      defaultZone: http://localhost:9001/eureka/




已完成gateway入门的配置,还需深入学习应用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值