您现在的位置是:首页 > 学无止境 > Spring Cloud

spring cloud alibaba(7)基础-Spring Cloud Gateway网关

贾继涛 2019-03-11 2135人围观

相关文章

 spring cloud alibaba(1)基础注册中心nacos

 spring cloud alibaba(2)基础-服务提供者

 spring cloud alibaba(3)基础-创建消费者

 spring cloud alibaba(4)基础-创建消费者(feign)

 spring cloud alibaba(5)基础-熔断器防止雪崩sentinel

 spring cloud alibaba(6)基础-Sentinel 控制台搭建

 spring cloud alibaba(7)基础-Spring Cloud Gateway网关

 spring cloud alibaba(8)基础-geteway路由全局过滤

 spring cloud alibaba(9)基础-nacos云配置服务-nacos-config

 spring cloud alibaba(10)基础-Nacos Config多环境配置

项目工程结构

png

  1. hello-spring-cloud-gateway 及pom.xml

<?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>com.jjt</groupId>
        <artifactId>hello-spring-cloud-alibaba-dependencies</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../hello-spring-cloud-alibaba-dependencies/pom.xml</relativePath>
    </parent>
    <artifactId>hello-spring-cloud-gateway</artifactId>
    <packaging>jar</packaging>
    <name>hello-spring-cloud-gateway</name>
    <url>http://www.jiajitao.cn</url>
    <inceptionYear>2019-Now</inceptionYear>
    <dependencies>
        <!-- Spring Boot Begin -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- Spring Boot End -->
        <!-- Spring Cloud Begin -->
        <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-alibaba-sentinel</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
        </dependency>
        <!-- Spring Cloud End -->
        <!-- Commons Begin -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
        </dependency>
        <!--
        特别注意
        Spring Cloud Gateway 不使用 Web 作为服务器,而是 使用 WebFlux 作为服务器,
         Gateway 项目已经依赖了 starter-webflux,所以这里 千万不要依赖 starter-web
        由于过滤器等功能依然需要 Servlet 支持,故这里还需要依赖
        javax.servlet:javax.servlet-api
        # Application
        -->
        <!-- Commons Begin -->
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <mainClass>com.jjt.hello.spring.cloud.gateway.GatewayApplication</mainClass>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>


2.springbootapplication

package com.jjt.hello.spring.cloud.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableFeignClients
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class,args);
    }
}


3.application.yaml

spring:
  application:
    # 应用名称
    name: spring-gateway
  cloud:
    # 使用 Naoos 作为服务注册发现
    nacos:
      discovery:
        server-addr: 192.168.102.130:8848
    # 使用 Sentinel 作为熔断器
    #sentinel:
    #  transport:
    #    port: 8721
    #    dashboard: localhost:8080
    # 路由网关配置
    gateway:
      # 设置与服务注册发现组件结合,这样可以采用服务名的路由策略
      discovery:
        locator:
          enabled: true
      # 配置路由规则
      routes:
      # 采用自定义路由 ID(有固定用法,不同的 id 有不同的功能,详见:https://cloud.spring.io/spring-cloud-gateway/2.0.x/single/spring-cloud-gateway.html#gateway-route-filters)
      - id: NACOS-CONSUMER
        # 采用 LoadBalanceClient 方式请求,以 lb:// 开头,后面的是注册在 Nacos 上的服务名
        # http://127.0.0.1:9000/nacos-consumer/c/app
        uri: lb://nacos-consumer
        # Predicate 翻译过来是“谓词”的意思,必须,主要作用是匹配用户的请求,有很多种用法
        predicates:
        # Method 方法谓词,这里是匹配 GET 和 POST 请求
        #http://127.0.0.1:9000/nacos-consumer-feign/c/hi
        - Method=GET,POST
      - id: NACOS-CONSUMER-FEIGN
        uri: lb://nacos-consumer-feign
        predicates:
        - Method=GET,POST
server:
  port: 9000
# 目前无效
feign:
  sentinel:
    enabled: true
# 目前无效
management:
  endpoints:
    web:
      exposure:
        include: "*"
# 配置日志级别,方别调试
logging:
  level:
    org.springframework.cloud.gateway: debug



注意:请求方式是 http://Geteway的IP:路由网关Port/服务名/**

http://127.0.0.1:9000/nacos-consumer/c/app 

http://127.0.0.1:9000/nacos-consumer-feign/c/hi


源码地址

 https://gitee.com/jiajitao/Spring-Cloud-Ali


您的支持