Spring Cloud Alibaba 整合gateway

北慕城南 提交于 2020-08-12 06:53:09

pom配置

    <dependencies>
        <!--gateway-->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!--Spring Cloud Alibaba-->
        <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-netflix-hystrix</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

    </dependencies>







yml文件配置

server:
  port: 8000
spring:
  application:
    name: gateway-server
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848/
    gateway:
      discovery:
        locator:
          enabled: true #开启从注册中心动态创建路由的功能,利用微服务名进行路由
      routes:
        - id: product-server #路由的ID,没有固定规则但要求唯一,建议配合服务名
          uri: lb://product-server   #匹配后提供服务的路由地址
          predicates:
            - Path=/product/**   #断言,路径相匹配的进行路由
          filters:
            - StripPrefix=1 #去除第一个级前缀

        - id: order-server
          uri: lb://order-server
          predicates:
            - Path=/order/**   #断言,路径相匹配的进行路由
          filters:
            - StripPrefix=1 #去除第一个级前缀



启动类

@EnableDiscoveryClient
@SpringBootApplication
public class GatewayApplication {

    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class,args);
    }
}

配置全局过滤器

@Component
@Slf4j
public class MyGlobalGatewayFilter implements GlobalFilter, Order {

    @Override
    public Class<? extends Annotation> annotationType() {
        return null;
    }

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        log.info("-----welcome to MyGlobalGatewatFilter------" + LocalDateTime.now().toString());
        String password = exchange.getRequest().getQueryParams().getFirst("password");
//        Assert.notNull(password, "password must not be null ! ");
        if (StringUtils.isBlank(password)) {
            throw new ResultException(1001, "password must not be null !");
        }
        String authorization = exchange.getRequest().getHeaders().getFirst("Authorization");
        if (StringUtils.isEmpty(authorization)) {
            log.info("Authorization is empty!!!");
            ServerHttpResponse response = exchange.getResponse();
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
        return chain.filter(exchange);
    }

    @Override
    public int value() {
        return 0;
    }
}

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!