SpringBoot 整合 gateway

这篇具有很好参考价值的文章主要介绍了SpringBoot 整合 gateway。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1.添加依赖

1.1 在springboot项目中注意 pom文件配置<parent>节点,否则nacos依赖会出问题

<parent>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-parent</artifactId>
	<version>2.3.2.RELEASE</version>
</parent>

1.2 添加springcloud alibaba nacos 配置注册依赖

<!-- nacos-config配置中心依赖 -->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
	<version>2.2.3.RELEASE</version>
</dependency>
<!-- nacos-discovery注册中心依赖 -->
<dependency>
	<groupId>com.alibaba.cloud</groupId>
	<artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
	<version>2.2.3.RELEASE</version>
</dependency>

1.3 添加 spring-cloud-gateway依赖

<!--在springboot的pom文件中,该依赖已经集成了springMVC等web启动器,不需要再添加spring-boot-starter-web 依赖了-->
<dependency>
	<groupId>org.springframework.cloud</groupId>
	<artifactId>spring-cloud-starter-gateway</artifactId>
	<version>2.2.3.RELEASE</version>
</dependency>

2.配置

由于节点比较多使用properties个人觉得不是很好看,也不便维护,所以使用yml文件配置

2.1 配置application.properties

spring.application.name= springboot-gateway-service
spring.main.allowBeanDefinitionOverriding= true
spring.profiles.active= dev
server.port= 9992

2.2 配置application-dev.yml文件

spring:
  cloud:
    # nacos 配置
    nacos:
      # 服务注册配置
      discovery:
        server-addr: 127.0.0.1:8848

    # gateway 配置
    gateway:
      discovery:
        locator:
          #表明gateway开启服务注册和发现的功能,并且spring cloud gateway自动根据服务发现为每一个服务创建了一个router,这个router将以服务名开头的请求路径转发到对应的服务
          enabled: true
          #是将请求路径上的服务名配置为小写(因为服务注册的时候,向注册中心注册时将服务名转成大写的了
          lowerCaseServiceId: true
          #另一种写法
          #lower-case-service-id: true
      #路由配置
      routes:
        # nacos中的服务1(每个服务中可集群多个应用,可在nacos中配置与治理)
        - id: springboot-base-service
          uri: lb://springboot-base-service
          predicates:
            - Path=/springboot/**
          filters:
            # 校验
            # 去除一个前缀
            - StripPrefix=1

      #跨域设置
      globalcors:
        corsConfigurations:
          '[/**]':
            allowedOriginPatterns: "*"
            allowed-methods: "*"
            allowed-headers: "*"
            allow-credentials: true
            exposedHeaders: "Content-Disposition,Content-Type,Cache-Control"

# 安全配置
security:
  # 不校验白名单
  ignore:
    urls:
      - "/springboot/user/login"
      - "/springboot/user/register"
      - "/springboot/v2/api-docs"

# 暴露监控端点
management:
  endpoints:
    web:
      exposure:
        include: '*'

3.启动使用

在启动过程中,查看在nacos上注册的服务是否都被拉下来

3.1 springboot-base-service服务接口

springboot集成gateway,SpringBoot 相关,微服务 相关,spring boot,gateway,eureka

3.2 通过 gateway调用的springboot-base-service服务接口 

springboot集成gateway,SpringBoot 相关,微服务 相关,spring boot,gateway,eureka文章来源地址https://www.toymoban.com/news/detail-604858.html

到了这里,关于SpringBoot 整合 gateway的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • Springboot3.X整合Dubbo3.XSpringCloudAlibaba微服务 2022.0 + Springboot3.X 集成 Dubbo实现对外调用http内部调用RPC

    近期自己新开了一套SpringCloud Alibaba微服务项目,接口使用了对外HTTP,内部RPC的设计,具体点说就是外部用户或客户端通过Nginx访问到Gateway网关再分发到各个服务,内部各个服务之间统一使用Dubbo RPC进行通信。下面是Springboot3.x集成Dubbo的分享: 1. 需要的关键依赖 2. 启动程序入

    2024年02月15日
    浏览(26)
  • SpringBoot整合Gateway 的Demo(附源码)

    Gateway模块 Gateway 的父pom.xml Gateway 的pom.xml 添加注册中心和配置中心配置 添加路由配置信息(放在配置中心) 服务模块 添加一个service服务 配置注册中心和配置中心 配置中心的配置文件内容 效果 不通过gateway访问 通过gateway访问 本文由博客一文多发平台 OpenWrite 发布!

    2024年02月08日
    浏览(25)
  • 微服务整合Gateway网关

    ✅作者简介:大家好,我是Leo,热爱Java后端开发者,一个想要与大家共同进步的男人😉😉 🍎个人主页:Leo的博客 💞当前专栏: 微服务探索之旅 ✨特色专栏: MySQL学习 🥭本文内容:微服务整合Gateway网关 🖥️个人小站 :个人博客,欢迎大家访问 📚个人知识库: 知识库

    2024年02月07日
    浏览(31)
  • 09-微服务Sentinel整合GateWay

    在微服务系统中,网关提供了微服务系统的统一入口,所以我们在做限流的时候,肯定是要在网关层面做一个流量的控制,Sentinel 支持对 Spring Cloud Gateway、Zuul 等主流的 API Gateway 进行限流。 Sentinel 1.6.0 引入了 Sentinel API Gateway Adapter Common 模块,此模块中包含网关限流的规则和

    2024年01月24日
    浏览(27)
  • Springcloudalibaba整合es!实现相关性排序,集成完代码真香

    org.springframework.boot spring-boot-starter-data-elasticsearch 2.0.1.RELEASE com.querydsl querydsl-apt 4.2.2 com.querydsl querydsl-jpa 4.2.2 ma.glasnost.orika orika-core 1.5.2 相关性排序实现思路原理图: ============= 核心代码实现: kibana:查询json { “function_score” : { “query” : { “bool” : { “must” : [ { “multi_match”

    2024年04月22日
    浏览(25)
  • SpringCloud之Gateway整合Sentinel服务降级和限流

    1.下载Sentinel.jar可以图形界面配置限流和降级规则 地址:可能需要翻墙 下载jar文件 2.引入maven依赖 3.写个自动注入Resource的过滤器类(可以不写注解直接使用) 4.写配置文件 application.properties 5.cmd命令行启动jar文件访问localhost:18080页面,自己设置QPS java -jar -server.port=18080 sentinel-dash

    2024年02月07日
    浏览(32)
  • 【Spring Cloud Gateway】⑥SpringBoot3.x集成SpringDoc指南

    Spring Cloud Gateway 使用 Netty 作为嵌入式服务器,并基于响应式 Spring WebFlux 。做为微服务网关,多个微服务把 API 挂在 Gateway 上,如果查看某个 API 的 Swagger 还要去各个子微服务中去查看,就很不方便,如果能在 Gateway 上直接查看各个微服务的 API 文档,会方便很多,本文以截至

    2024年02月14日
    浏览(37)
  • Gateway服务集成Nacos2021.0.4错误解决

    gateway服务集成nacos,启动后报错: Caused by: com.alibaba.nacos.shaded.io.grpc.netty.shaded.io.netty.channel.AbstractChannel$AnnotatedConnectException: Connection refused: no further information: ; 版本: jdk:1.8 spring-boot:2.7.11 spring-cloud:2021.0.6 spring-cloud-alibaba:2021.0.4.0 单配置文件:application.yaml中配置 多配置文件:

    2024年02月13日
    浏览(27)
  • 【实现微服务集成satoken在网关gateway处统一鉴权】

    本文旨在使用开源轻量级 Java 权限认证框架sa-token+springcloud-gateway实现微服务在网关处统一鉴权。sa-token参考地址:https://sa-token.cc/doc.html#/ 项目按照业务分为三个板块,如图: api(也就是微服务中各种api接口,不涉及任何权限相关代码,只提供服务) auth(认证中心,实现登陆逻辑

    2024年02月10日
    浏览(33)
  • springboot集成Graphql相关问题汇总

    1、idea在debug运行时出现java.lang.NoClassDefFoundError:kotlin/collections/AbstractMutableMap 解决:禁用idea dubugger中kotlin coroutine agent 见:https://stackoverflow.com/questions/70796177/after-the-spring-boot-source-code-is-compiled-it-starts-to-report-an-error-in-d

    2024年02月12日
    浏览(24)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包