Spring Cloud 整合 GateWay

本文详细介绍了如何在微服务架构中集成SpringCloudGateway,包括添加相关Maven依赖、配置服务发现、整合Nacos和knife4j,以及与springBootAdmin的集成。通过实例展示了关键步骤,以帮助开发者构建高效安全的微服务网关并解决常见问题。
摘要由CSDN通过智能技术生成


前言

在数字化时代,微服务架构已成为企业构建复杂、可扩展和灵活应用程序的首选方案。随着微服务数量的不断增加,如何有效地管理和协调这些服务之间的通信变得至关重要。在这个背景下,API网关作为微服务架构的关键组件,承担着统一接入点、安全控制、流量管理和监控等重任。

Spring Cloud Gateway,作为Spring Cloud生态系统的一部分,提供了一个功能强大且灵活的API网关实现。它不仅集成了WebFlux响应式编程模型,还提供了丰富的路由、过滤和监控功能,使得开发者能够轻松地构建高效、安全的微服务网关。

本文旨在探讨如何集成Spring Cloud Gateway到现有的微服务架构中,并详细介绍其配置、使用以及最佳实践。我们将从Spring Cloud Gateway的基本概念开始,逐步深入到其核心组件和功能,并通过实例展示如何将其集成到实际项目中。

通过本文的学习,读者将能够掌握Spring Cloud Gateway的集成方法,了解如何在微服务架构中发挥其最大效用。同时,我们也将分享一些在使用Spring Cloud Gateway过程中遇到的常见问题及其解决方案,帮助读者更好地应对实际开发中的挑战。

无论你是微服务架构的新手还是经验丰富的开发者,本文都将为你提供有价值的信息和实践经验,助力你构建更强大、更安全的微服务应用程序。让我们一起走进Spring Cloud Gateway的世界,探索它在微服务架构中的无限可能。

步骤

集成Spring Cloud GateWay

引入相关maven依赖

		<dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--因为底层使用了Ribbon作为负载均衡,依赖中没有加入相关的组件,所以不可以进行正确的分发-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-loadbalancer</artifactId>
        </dependency>
        <!--就解决Spring Cloud LoadBalancer is currently working with the default cache. You can switch to using Caffeine cache' warning-->
        <dependency>
            <groupId>com.github.ben-manes.caffeine</groupId>
            <artifactId>caffeine</artifactId>
        </dependency>

添加相关配置

server:
  port: 8011  #端口配置
spring:
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true #启用DiscoveryClient网关集成的标志,可以实现服务的发现
          lower-case-service-id: true
        routes:
          - id: single-admin
            uri: lb://single-admin
            predicates:
              - Path=/single-admin/**

整合nacos

引入相关maven依赖

        <!--SpringCloud2020及以后的版本默认不启用 bootstrap 配置,我们需要在pom里面显式地引入:-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-bootstrap</artifactId>
        </dependency>
		<!--Spring Cloud Starter Alibaba Nacos Discovery 依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <!--Spring Cloud Alibaba Config 依赖-->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>

添加相关配置

spring:
  profiles:
    active: @profiles.active@
  application:
    name: @artifactId@
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
        namespace: @profiles.active@
      config:
        server-addr: 127.0.0.1:8848
        namespace: @profiles.active@
        file-extension: yaml

整合knife4j

引入相关maven依赖

		<dependency>
            <groupId>com.github.xiaoymin</groupId>
            <artifactId>knife4j-gateway-spring-boot-starter</artifactId>
            <version>4.4.0</version>
        </dependency>

添加相关配置

knife4j:
  gateway:
    enabled: true
    # 指定服务发现的模式聚合微服务文档,并且是默认`default`分组
    strategy: discover
    discover:
      enabled: true
      # 指定版本号(Swagger2|OpenAPI3)
      version : openapi3
      # 需要排除的微服务(eg:网关服务)
      excluded-services:
        - single-gateway

整合springBootAdmin

引入相关maven依赖

		<!--监控-->
        <dependency>
            <groupId>de.codecentric</groupId>
            <artifactId>spring-boot-admin-starter-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

添加相关配置

spring:
  boot:
    admin:
      client:
        url: http://localhost:9000
        username: admin
        password: admin
management:
  endpoints:
    logfile:
      external_file: log
    web:
      exposure:
        include: '*'

总结

完成上诉步骤我们就可以启动网关服务了


  • 29
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
项目说明 该项目是一个典型的由Spring Cloud管理的微服务项目,主要包括如下模块 micro-service-cloud─────────────────顶层项目 ├──cloud-service-core───────────────基础核心模块 ├──cloud-service-tools──────────────全局通用工具类 ├──cloud-service-reids──────────────Redis二次封装 ├──cloud-eureka-server──────────────服务注册中心[8761] ├──cloud-turbine-server─────────────断路器聚合监控[8769] ├──cloud-zipkin-server──────────────链路追踪监控[9411] ├──cloud-zuul-server────────────────第一代服务网关(Zuul)[8080] ├──cloud-gateway-server─────────────第二代服务网关(Gateway)[8080] ├──cloud-modules-app────────────────App微服务模块 ├───────modules-app-user────────────App用户服务模块[努力更新中] ├───────modules-app-doctor──────────App医生服务模块[努力更新中] ├──cloud-modules-service────────────微服务通用服务模块 ├───────mongodb-file-service────────Mongodb文件服务模块[11010] ├───────redis-delay-service─────────延迟消费服务模块[11020] ├──cloud-modules-web────────────────Web微服务模块 ├───────modules-web-security────────Web医生服务模块[12010] ├───────modules-web-user────────────Web用户服务模块[12020] ├──cloud-modules-wechat─────────────Wechat微服务模块 ├───────modules-wechat-user─────────Wechat用户服务模块[努力更新中] └───────modules-wechat-doctor───────Wechat医生服务模块[努力更新中] 修改日志 修改日志 修改人 修改日期 版本计划 V1.0 刘岗强 2019-01-07 项目初始化 V1.1 刘岗强 待定 新增自动问答 项目介绍 基于Spring Cloud Finchley SR2 Spring Boot 2.0.7的最新版本。 核心基础项目内实现类自定义的权限注解,配合RBAC权限模型+拦截器即可实现权限的控制,具体的参考项目中的实现。同时也封装了一些顶层类和结果集等。 注册中心实现高可用配置,详情见eureka的one、two、three三个配置文件,摘要如下。 ------------------------------------------配置节点一---------------------------------------------- server: port: 8761 spring: application: name: cloud-eureka-server eureka: instance: hostname: cloud.server.one prefer-ip-address: true instance-id: ${spring.cloud.client.ip-address}:${server.port}:${spring.application.name} client: healthcheck: enabled: true register-with-eureka: false fetch-registry: false service-url: defaultZone: http://cloud.server.two:8762/eureka/,http://cloud.server.three:8763/eureka/ ------------------------------------------配置节点二----------------------------------------------
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

刘凌枫羽

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

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

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

打赏作者

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

抵扣说明:

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

余额充值