Gateway服务的搭建

发布时间 2023-10-08 22:39:17作者: ashet

微服务系统搭建大致顺序:

  • 建立父项目打包为pom上传到私服Maven
  • 子项目在pom.xml中继承父项目的(公共)依赖
  • 安装Nacos,所有服务都注册到Nacos
  • 搭建Gateway,Gateway是外部请求的统一入口
  • 安装Sentinel,Sentinel会监控经过Gateway的全部流量,实现流量监控与流量控制

至此,主要讲解Gateway服务的搭建。

新建一个project,修改pom.xml:

1、继承公共依赖

2、注册到Nacos(基于此才能实现路由转发request)

3、引入Gateway + Loadbalancer依赖

<?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">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.ashe</groupId>
        <artifactId>base</artifactId>
        <version>1.0.0</version>
    </parent>

    <artifactId>gateway</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-loadbalancer</artifactId>
        </dependency>

        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>

    </dependencies>

</project>

 

application.yml配置

server:
  port: 10082

spring:
  application:
    name: api-service
  cloud:
    nacos:
      discovery:
        server-addr: ip:8849  # Nacos 服务器地址
    gateway:
      discovery:
        locator:
          # 根据uri路径path中的服务名,识别request要访问的服务,根据Loadbalancer负载均衡选择具体的节点
          enabled: true # 自动识别并路由具体的服务(约定大于配置)
      --------------------------------------------------------------------------------------
      以下为手动实现路由转发的配置,二选一即可
      # 路由规则
      routes:
        - id: order_route # 订单路由标识
          uri: lb://order-service # 路由转发到Nacos的订单服务
          predicates:
            - Path=/order-service/** # 断言路径,断言成功则执行过滤器逻辑
          filters:
            - StripPrefix=1 # 剥除一级路径,即修改路径时,从path中去除order-service

 

建启动类(略)

 

跨域配置类

/**
 * 跨域配置
 */
@Configuration
public class CorsConf {

    @Bean
    public CorsWebFilter corsWebFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();

        CorsConfiguration corsConf = new CorsConfiguration ();
        // 允许跨域的请求头
        corsConf.addAllowedHeader("*");
        // 允许跨域的请求方式
        corsConf.addAllowedMethod("*");
        // 允许跨域的请求来源
        corsConf.addAllowedOrigin("*");
        // 为true时,允许跨域的来源不允许为*,必须指定明确的值,格式"[a,b,c]"
//        corsConf.setAllowCredentials(true);

        // 允许跨域所请求的资源
        source.registerCorsConfiguration("/**", corsConf);
        return new CorsWebFilter(source);

    }
}

 

最后启动服务,通过网关来请求order接口

 

通过网关入口访问order接口,测试通过,Gateway搭建成功。