SpringCloud Alibaba【三】Gateway

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

前言

在工作中遇到一种情况,一个父项目中有两个子项目。实际使用时,需要外网可以访问,宝信软件只能将一个端口号发布在外网上,所以需要运用网关技术,通过一个端口号访问两个项目。
之前已经试用nacos搭建了注册中心

新建gateway子项目

pom.xml

导入依赖时注意SpringCloudAlibaba与gateway依赖的版本是否对应,否则启动时会报错。

<?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">
    <parent>
        <artifactId>testmaven32springcloud</artifactId>
        <groupId>com.hzx</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>gateway-project</artifactId>

    <dependencies>
        <!-- 此依赖已经在父项目pom中导入
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
            <version>2.2.7.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <version>2.2.6.RELEASE</version>
        </dependency>
    </dependencies>
</project>

配置文件

将gateway服务注册到nacos中

server:
  port: 8901
spring:
  cloud:
    nacos:
      discovery:
        server-addr: http://192.168.0.248:8848
        namespace: 42d5d19a-8564-4da2-8b97-7e4eac1c53a3
    gateway:
      discovery:
        locator:
          enabled: true
      globalcors:
        cors-configurations:
          '[/**]':  # 允许跨域访问的资源
            allowedOrigins: "*" #跨域允许来源
            allowedMethods: "*"
            allowedHeaders: "*"
      default-filters:
        - DedupeResponseHeader=Vary Access-Control-Allow-Origin Access-Control-Allow-Credentials, RETAIN_FIRST
  application:
    name: nacos-gateway

配置文件中:spring.cloud.gateway.globalcors.cors-configurations是专门配置跨域的。也可以用配置类的方法(但我试过后没有成功。。。)需注意配置类中UrlBasedCorsConfigurationSource的包名。

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;
import org.springframework.web.util.pattern.PathPatternParser;

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsWebFilter(){
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*"); //允许的method
        config.addAllowedOrigin("*"); //允许的来源
        config.addAllowedHeader("*"); //允许的请求头参数

        //允许访问的资源
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**",config);

        return new CorsWebFilter(source);
    }
}

启动类

在启动类中需要加上注解:@EnableDiscoveryClient

@SpringBootApplication
@EnableDiscoveryClient
public class StartGatewayApplication {
    public static void main(String[] args) throws Exception {
        SpringApplication.run(StartGatewayApplication.class, args);
    }
}

以上就是gateway相关的代码,启动成功后就可以使用了。
我在学习的时候没想到这么简单。

访问接口方式

http://ip:网关端口/nacos中注册的服务名称/controller层路径

具体内容见测试部分

测试

上一篇讲nacos的文章中,我创建了两个子项目,分别是:nacos-provider-project、nacos-consumer-project,连同gateway项目启动后,在nacos可以看到注册的服务。
SpringCloud Alibaba【三】Gateway,spring cloud,gateway,spring
在nacos-provider-project项目添加接口

    @RequestMapping(value = "/send/provider/{msg}",method = RequestMethod.GET)
    public String sendMessageProvider(@PathVariable String msg){
        return "调用生产者端接口,向生产者发送消息:"+msg;
    }

在nacos-consumer-project项目添加接口

    @RequestMapping(value = "/send/consumer/{msg}",method = RequestMethod.GET)
    public String sendMessageConsumer(@PathVariable String msg){
        return "调用消费者端接口,向消费者发送消息:"+msg;
    }

通过接口文档测试上面的两个接口
首先是直接通过项目本身的端口号访问接口。其中8081和8091分别是两个项目的端口号。
SpringCloud Alibaba【三】Gateway,spring cloud,gateway,spring
SpringCloud Alibaba【三】Gateway,spring cloud,gateway,spring
然后通过网关端口分别访问两个接口。其中8901为gateway项目端口号,nacos-provider与nacos-consumer分别为两个项目在nacos注册中心的服务名称。
SpringCloud Alibaba【三】Gateway,spring cloud,gateway,spring
SpringCloud Alibaba【三】Gateway,spring cloud,gateway,spring

拓展

本文中使用的是gateway默认配置网关的方法,开发者还可以自定义配置路由,也可以不通过注册在nacos中的服务名就能访问接口,但这两种方法目前本人还不需要,所以文章中没有写出。文章来源地址https://www.toymoban.com/news/detail-730791.html

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

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

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

相关文章

  • 【springcloud 微服务】Spring Cloud 微服务网关Gateway使用详解

    目录 一、微服务网关简介 1.1 网关的作用 1.2 常用网关 1.2.1 传统网关 1.2.2 云原生网关

    2023年04月16日
    浏览(39)
  • 【SpringCloud】11、Spring Cloud Gateway使用Sentinel实现服务限流

    1、关于 Sentinel Sentinel 是阿里巴巴开源的一个流量防卫防护组件,可以为微服务架构提供强大的流量防卫能力,包括流量控制、熔断降级等功能。Spring Cloud Gateway 与 Sentinel 结合,可以实现强大的限流功能。 Sentinel 具有以下特性: 丰富的应用场景:Sentinel 承接了阿里巴巴近

    2024年02月01日
    浏览(43)
  • SpringCloud - Spring Cloud 之 Gateway网关,Route路由,Predicate 谓词/断言,Filter 过滤器(十三)

    阅读本文前可先参考 ​​​​​​SpringCloud - Spring Cloud根/父项目,开发准备(二)_MinggeQingchun的博客-CSDN博客 SpringCloud - Spring Cloud 之 Gateway网关(十三)_MinggeQingchun的博客-CSDN博客 Web 有三大组件(监听器 过滤器 servlet),Spring Cloud GateWay 最主要的功能就是路由转发,而在定义

    2024年02月14日
    浏览(55)
  • SpringCloud Alibaba【三】Gateway

    在工作中遇到一种情况,一个父项目中有两个子项目。实际使用时,需要外网可以访问,宝信软件只能将一个端口号发布在外网上,所以需要运用网关技术,通过一个端口号访问两个项目。 之前已经试用nacos搭建了注册中心 pom.xml 导入依赖时注意SpringCloudAlibaba与gateway依赖的版

    2024年02月07日
    浏览(28)
  • SpringCloud Alibaba Sentinel整合GateWay

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

    2024年02月16日
    浏览(31)
  • SpringCloud Alibaba入门7之引入服务网关Gateway

    我们需要在客户端和服务端之间加一个统一的入口,来作为请求的统一接入,而在微服务的体系中,承担这个角色的就是网关。我们只需要将网关的机器IP配置到DNS,或者接入负载,那么客户端的服务最终通过我们的网关,再转发到对应的服务端服务。 一、创建网关服务  1.引

    2024年02月11日
    浏览(36)
  • springcloud Alibaba中gateway和sentinel联合使用

    看到这个文章相信你有一定的sentinel和gateway基础了吧。 官网的gateway和sentinel联合使用有些过时了,于是有了这个哈哈,给你看看官网的: 才sentinel1.6,现在都几了啊,所以有些过时。 下面开始讲解: 首先我们总的回顾一下,sentinel就是需要运行一个jar包,开启dashbord页面,来

    2024年01月17日
    浏览(34)
  • 【SpringCloud Alibaba】Nacos Config配置管理与Gateway 网关

    目录 一、Config 远程配置 1.1 config 介绍 1.2 bootstrap.yml 配置文件 二、Gateway 网关 2.1 gateway 介绍 2.2 gateway 使用 2.2.1 方式一 2.2.2 方式二(动态路由)         微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中会出现大 量的服务。

    2024年02月02日
    浏览(41)
  • springcloud-alibaba (04)Gateway与Nacos结合使用

    🎉欢迎来到这里,今天我将为大家介绍如何将Spring Cloud Gateway和Nacos结合使用,实现一个高效稳定的服务网关!在微服务架构中,API网关是必不可少的一部分,它提供了路由请求、负载均衡、安全认证和限流等功能。Spring Cloud Gateway是基于Spring Framework、Spring Boot和Project Reacto

    2024年02月15日
    浏览(28)
  • SpringCloud Alibaba Sentinel 与 SpringCloud Gateway 的限流有什么差别?(三种限流算法原理分析)

    目录 一、Sentinel 与 Gateway 的限流有什么差别? 1.1、前置知识 - 四种常见的限流算法 1.1.1、Tips 1.1.2、计数器算法 1)固定窗口计数器算法 2)滑动窗口计数器算法 1.1.3、令牌桶算法 1.1.4、漏桶算法 1.2、解决问题 1.1.1、Tips 限流, 就是指对服务器请求量做限制,避免因为突发的

    2024年01月25日
    浏览(36)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包