Gateway的使用

gateway是什么?

gateway是springboot提供的一种网关系统,用于用前言判断的方法去判断他们的路径,从而调用相应路径的方法。

它与nginx的作用相似,不过nginx是需要在外部启动,在使用方面并没有Gateway来的方便。

接下来,介绍一下如何在springboot中进行使用?

首先我们需要创建一个模块,Gateway模块

第二步我们当然要添加依赖。

因为我们的 Gateway需要在nacos注册,所以我们有两个依赖需要加入。

 

 

    <!--加入Gateway网关-->
        <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-gateway</artifactId>
    </dependency>

    <!-- 服务注册 -->
    <dependency>
        <groupId>com.alibaba.cloud</groupId>
        <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
    </dependency>

第三部,我们需要在这个模块中的引导类的yml中的加入我们的配置

server:
  port: 9000
spring:
  application:
    name: service_gateway
  cloud:
    nacos:
      discovery:
        server-addr: localhost:8848
    gateway:
      discovery:
        locator:
          enabled: true

      routes: # 网关路由配置
         - id: service-hosp # 路由 id,自定义,只要唯一即可
           uri: lb://service-hosp #负载均衡
           predicates:
             - Path=/*/hosp/** #选择路径 以/user/..开头,路径断言#
         - id: service-cmn
           uri: lb://service-cmn
           predicates:
             - Path=/*/cmn/**

这里的写法如果有什么不明白可以去看我的另外一个博客微服务开发-------------搭建网关,断言工厂以及GatewayFilter路由过滤器

第四步我们需要创建引导类。

@SpringBootApplication(exclude= {DataSourceAutoConfiguration.class})
public class ServerGatewayApplication {
    public static void main(String[] args) {
       SpringApplication.run(ServerGatewayApplication.class,args);
    }
}

 这里的(exclude= {DataSourceAutoConfiguration.class})

它的用处是在于,如果出现在启动时会显示没有连接数据库时就加入它可以断开需要连接数据库的请求。这里的其他启动类与我们之前写的项目的启动类基本相同。

写到这里我们的Gateway的网关模块就写好了。

这里有几个问题需要介绍一下。

1.我在开发时遇到了启动时,一直爆错错误的情况是我的maven中的netty文件一直爆错。

经过我的疯狂找错,我发现了问题所在。

由于Spring Boot,Spring Cloud Gateway都带有spring-boot-starter-web依赖,但是前者使用Tomcat,后者使用Netty,会导致冲突,项目无法启动(这是我在一个同仁那里得到的解释)所以在父文件中注意千万不要把spring-boot-dependencies依赖加入。特别要注意。

2.另外一个就是跨域的问题,因为我们在使用时,一开始都是在我们的controller类中加我们

@CrossOrign的注解,它是用于跨域的问题,跨域指的就是我们的每个服务的端口号不一样当要同时使用时我们需要同时调用。

所以我们在我们的gateway模块中最好注入我们的一个新的config配置类进行全局的跨域,接下来我为大家写一个配置类,用于覆盖全局的跨域。

@Configuration
public class CorsConfig {

    @Bean
    public CorsWebFilter corsFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedMethod("*");
        config.addAllowedOrigin("*");
        config.addAllowedHeader("*");
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(new PathPatternParser());
        source.registerCorsConfiguration("/**", config);
        return new CorsWebFilter(source);
    }
}

到此我们的服务就完成了,快试一下你的前端是否能成功的启动吧,遇到什么问题可以在评论区讨论。

  • 6
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud Gateway是一个基于Spring 5.0、Spring Boot 2.0和Project Reactor等技术开发的API网关,它的作用是保护、增强和控制对于API服务的访问。 在使用Spring Cloud Gateway时,首先需要搭建网关项目。在网关项目的配置文件(application.yml)中,我们可以配置多个路由(routes),每个路由都有一个自定义的id和目标服务的URI。通过断言(predicates)来确定路由的条件,例如路径(path)。 例如,我们可以使用如下配置来创建一个路由到订单服务(Spring Cloud Order Service Provider)的路径"/order/**",并将请求转发到具体的订单服务实例。同样地,我们可以创建另一个路由到用户服务(Spring Cloud User Service Consumer)的路径"/user/**"。 除了路由配置,我们同样需要配置Eureka注册中心的地址,以便网关能够获取服务列表。可以通过配置eureka.client.service-url来指定Eureka Server的地址。 另外,我们还可以为网关配置多个profile,通过不同的端口启动不同的实例。例如可以配置两个profile(g1和g2),分别在8020和8021端口启动网关实例。 通过以上步骤,我们就可以成功使用Spring Cloud Gateway来管理和控制API服务的访问了。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [SpringCloud之Gateway使用篇](https://blog.csdn.net/yuanshangshenghuo/article/details/107101640)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* [SpringCloud gateway使用](https://blog.csdn.net/m0_37044584/article/details/115302516)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值