SpringCloudAlibaba - 整合 Gateway 实现对微服务的反向代理

时间:2021-09-29
本文章向大家介绍SpringCloudAlibaba - 整合 Gateway 实现对微服务的反向代理,主要包括SpringCloudAlibaba - 整合 Gateway 实现对微服务的反向代理使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

前言

Spring Cloud GatewaySpringCloud生态的第二代网关(第一代是Zuul),基于NettyReactor以及WebFlux构建


环境

Spring Cloud Hoxton.SR9 + Spring Cloud Alibaba 2.2.6.RELEASE


为什么要使用网关

  • 简化登录认证,网关统一认证再转发到其它微服务
  • 网关方式对外暴露的永远是一个域名,客户端配置简单

Gateway的优点

  • 性能强劲,是第一代网关Zuul 1.x的1.6倍
  • 内置了很多实用功能,如转发、监控、限流等
  • 易扩展

Gateway 核心概念

名称 解释 作用
Route 路由 一个转发规则,包含ID、目标URL、Predicate集合一级Filter集合
Predicate 谓词,也称断言 控制请求是否走转发规则的条件
Filter 过滤器 修改请求以及响应,可以为路由添加业务逻辑

Gateway 整合实例

网关 core-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 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.3.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.coisini</groupId>
    <artifactId>core-gateway</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>core-gateway</name>
    <description>Gateway project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Hoxton.SR9</spring-cloud.version>
        <spring-cloud-alibaba.version>2.2.6.RELEASE</spring-cloud-alibaba.version>
    </properties>
    <dependencies>
        <!-- gateway -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

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

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <!-- 整合spring cloud -->
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
            <!-- 整合spring cloud alibaba -->
            <dependency>
                <groupId>com.alibaba.cloud</groupId>
                <artifactId>spring-cloud-alibaba-dependencies</artifactId>
                <version>${spring-cloud-alibaba.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>
  • application.yml
spring:
  application:
    name: core-gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          # 让gateway通过服务发现组件找到其它的服务
          enabled: true

server:
  port: 8040

# Actuator
management:
  endpoints:
    web:
      exposure:
        include: "*"
  endpoint:
    health:
      show-details: always

用户中心 user-center

  • TestController.java
@RestController
@Slf4j
public class TestController {

    @GetMapping("/test/{name}")
    public String test(@PathVariable String name) {
        log.info("请求...");
        return "hello " + name;
    }

}

测试

  • 启动网关,访问Nacos控制台
  • 测试接口访问


Gateway的转发规律:访问${GATEWAY_URL}/{微服务X}/xx 会转发到微服务X 的 /xx 路径


- End -
白嫖有风险
点赞加收藏
以上为本篇文章的主要内容,希望大家多提意见,如果喜欢记得点个推荐哦
作者:Maggieq8324
本文版权归作者和博客园共有,欢迎转载,转载时保留原作者和文章地址即可。

原文地址:https://www.cnblogs.com/maggieq8324/p/15353837.html