Spring Cloud Zuul(五)路由器和过滤器

扫码关注公众号:Java 技术驿站

发送:vip
将链接复制到本浏览器,永久解锁本站全部文章

【公众号:Java 技术驿站】 【加作者微信交流技术,拉技术群】
免费领取10G资料包与项目实战视频资料

Spring Cloud Zuul(五)路由器和过滤器

自我理解:zuul主要做一层网关,1.转发请求到具体服务
2.过滤一些敏感的词汇和简单的预防侵入措施

一、简单示例demo开始
gateway-service-zuul 的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>
        <groupId>cn.qx</groupId>
        <artifactId>spring-cloud-zuul-demo</artifactId>
        <version>0.0.1-SNAPSHOT</version>
        <packaging>jar</packaging>
        <name>spring-cloud-zuul-demo</name>
        <description>Demo project for Spring Boot</description>
        <parent>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-parent</artifactId>
            <version>2.0.0.RELEASE</version>
            <relativePath/> <!-- lookup parent from repository -->
        </parent>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
            <java.version>1.8</java.version>
            <spring-cloud.version>Finchley.M9</spring-cloud.version>
        </properties>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</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-netflix-zuul</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>

        <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>
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>

        <repositories>
            <repository>
                <id>spring-milestones</id>
                <name>Spring Milestones</name>
                <url>https://repo.spring.io/milestone</url>
                <snapshots>
                    <enabled>false</enabled>
                </snapshots>
            </repository>
        </repositories>
    </project>

gateway-service-zuul application.yml文件

    spring:
      application:
        name: gateway-service-zuul
    server:
      port: 8888
    zuul:
      routes:
        first:
          path: /it/**
          url: http://www.baidu.com/
    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8001/eureka/

在SpringCloudZuulDemoApplication启动文件中加入@EnableZuulProxy,和@EnableDiscoveryClient就可以完成在eureka上注册和Zuul功能

二、Zuul改进为分发请求连接到具体的服务提供者上去

  • 将请求指定转发到服务提供者上去
  • 修改application.yml文件
    spring:
      application:
        name: gateway-service-zuul
    server:
      port: 8888
    zuul:
      routes:
        first:
          path: /it/**
          url: http://www.ityouknow.com/
        second:
          path: /producer/**
          serviceId:  demo-clent

    eureka:
      client:
        service-url:
          defaultZone: http://localhost:8001/eureka/

服务提供者用之前的demo-clent的2个服务提供
– 测试
– 在浏览器输入http://localhost:8888/producer/producerHello?name=小明
– 结果:20191123100349\_1.png
20191123100349\_2.png
– 会发现自动做了负载均衡


来源:http://ddrv.cn/a/88268

赞(0) 打赏
版权归原创作者所有,任何形式的转载请联系博主:daming_90:Java 技术驿站 » Spring Cloud Zuul(五)路由器和过滤器

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏