SpringCloud之Gateway和Zuul的区别

文章目录

  • 前言
  • 一、zuul?
    • 1.引入库
    • 2.yml配置
  • 二、GateWay?
    • 1.引入库
    • 2.yml配置
  • 总结


前言

SpringCloud之Gateway和Zuul的区别

一、zuul?

网关配置

  1. 统一路口:所有的数据接口统一管理和维护,保障了外部和内部的隔离,安全等。
  2. 路径校验:可以对请求的路径进行过滤,防止接口中的非法入侵。
  3. 动态路由:可以将请求分发给不同的服务器中。

记得在主启动类上添加 @EnableZuulProxy 相关注解

1.引入库

代码如下(示例):

<properties>
     <java.version>1.8</java.version>
     <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
 </properties>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>${spring-cloud.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>com.hh</groupId>
            <artifactId>user-service-api</artifactId>
            <version>0.0.1-SNAPSHOT</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-sleuth -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-sleuth</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-sleuth-zipkin -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-sleuth-zipkin</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-starter-netflix-zuul -->
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
        <!-- actuator监控信息完善 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
    </dependencies>

2.yml配置

代码如下(示例):

server:
  port: 10086

spring:
  application:
    name: user-service-zuul


eureka:
  instance:
    prefer-ip-address: true
    ip-address: 127.0.0.1
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka:7000/eureka,http://eureka01:7001/eureka,http://eureka02:7002/eureka

zuul:
  routes:
    zuul-path:
      path: /zuul-path/**
      serviceId: USER-SERVICE-PROVIDE
    zuul-ribbon-path:
      path: /zuul-ribbon-path/**
      serviceId: USER-SERVICE-CONSUME

http://localhos:10086/zuul-path/findUserInfo/1
切记请求的路径不同。很容易会出现问题。例:zuul-path请求的路径过滤下来,拼接到请求的路径后面,zuul-path后面才是真正的接口路径(和gateway不同)。

二、GateWay?

1.引入库

<properties>
        <java.version>1.8</java.version>
        <spring-cloud.version>Greenwich.SR1</spring-cloud.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-gateway</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.apache.tomcat.embed</groupId>
                    <artifactId>tomcat-embed-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.cloud</groupId>
                <artifactId>spring-cloud-dependencies</artifactId>
                <version>${spring-cloud.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

2.yml配置

server:
  port: 7070

spring:
  application:
    name: user-service-gateway
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
          lower-case-service-id: true
      routes:
        - id: way
          uri: lb://user-service-provide
          #          uri: http://localhost:9002
          predicates:
            - Path=/** # 请求路径匹配所有的路径,和zuul网关的路径过滤不同(切记)
#            - Path=/findUserInfo      后面放接口路径,切记不能自定义路径然后进行匹配。和zuul不同,zuul可以自定义匹配
#            - Method=GET


eureka:
  client:
    register-with-eureka: true
    service-url:
      defaultZone: http://eureka:7000/eureka,http://eureka01:7001/eureka,http://eureka02:7002/eureka

请求路径: http://localhos:10086/findUserInfo/1 请求的路径不能自定义添加,只能放接口中的路径,否则会路由失败。提示无法找到网页等。


总结

无论你选择做什么,追求完美的程度决定你成就的高度。

你可能感兴趣的:(java)