Spring Cloud整合Gateway(网关)

一:Spring Cloud Gateway 相关概念

1:官网地址:https://spring.io/projects/spring-cloud-gateway

2:API网关是一个服务器,是系统的唯一入口。从面向对象设计的角度看,它与外观模式类似。API网关封装了系统内部架构,为每个客户端提供一个定制的API。它可能还具有其它职责,如身份验证、监控、负载均衡、缓存、请求分片与管理、静态响应处理。API网关方式的核心要点是,所有的客户端和消费端都通过统一的网关接入微服务,在网关层处理所有的非业务功能。通常,网关也是提供REST/HTTP的访问API。

3:Spring Cloud Gateway核心概念

(1)Route(路由)

Route(路由)是网关最基础的部分,由ID,目标URL,断言,Filter过滤器组成,当请求到达网关时,由 Gateway Handler Mapping 通过断言进行路由匹配(Mapping),当断言为真时,匹配到路由。

(2)Predicate(断言)

Predicates是Java8中的断言函数。Spring Cloud Gateway中的Predicates断言函数输入类型是Spring5.0框架中的ServerWebExchange。Spring Cloud Gateway中的Predicates断言函数允许开发者去定义匹配来自于http request中的任何信息,比如请求头和参数等。

(3)Filter过滤器。

Spring cloud gateway中的filter分为两种类型的Filter,分别是Gateway Filter和Global Filter。过滤器Filter将会对请求和响应进行修改处理

 二:正文:

1:创建网关工程:

2:工程pom.xml文件(版本由父工程管理)
 

<dependencies>

        <dependency>

            <groupId>com.gx</groupId>

            <artifactId>common_utils</artifactId>

            <version>0.0.1-SNAPSHOT</version>

        </dependency>

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>

        </dependency>


        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-gateway</artifactId>

        </dependency>


        <!--gson-->

        <dependency>

            <groupId>com.google.code.gson</groupId>

            <artifactId>gson</artifactId>

        </dependency>


        <!--服务调用-->

        <dependency>

            <groupId>org.springframework.cloud</groupId>

            <artifactId>spring-cloud-starter-openfeign</artifactId>

        </dependency>

    </dependencies>

 3、编写application.properties配置文件

# 服务端口

server.port=8808

# 服务名

spring.application.name=service-gateway


# nacos服务地址

spring.cloud.nacos.discovery.server-addr=127.0.0.1:8848


#使用服务发现路由

spring.cloud.gateway.discovery.locator.enabled=true


#配置服务
#设置路由id

spring.cloud.gateway.routes[0].id=gateway-acl

#设置路由的uri   lb://nacos注册服务名称

spring.cloud.gateway.routes[0].uri=lb://gateway-acl

#设置路由断言,代理servicerId为auth-service的/auth/路径

spring.cloud.gateway.routes[0].predicates= Path=/*/acl/**

 

yml文件写法:

server:

  port: 8808

    nacos:

      discovery:

        server-addr: 127.0.0.1:8848

spring:

  application:

  cloud:

    gateway:

      discovery:

        locator:

          enabled: true

      routes:

      - id: gateway-acl

        uri: lb://gateway-acl

        predicates:

        - Path=/*/acl/** # 路径匹配


创建一个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);

    }

}

配置启动类在Nacos中注册网关服务

@EnableDiscoveryClient注解注册Nacos

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

 注意:使用网关时,前端连接的端口号需要修改为网关的端口号,

链接请求网关时会自动匹配网关中配置的相关服务进行路径的匹配,从而触发真正的请求返回数据

这里使用的前端模板为vue-admin

module.exports = merge(prodEnv, {

  NODE_ENV: '"development"',

  //网关开放的端口

  BASE_API: '"http://localhost:8808"',

})

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值