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

 2019-11-23 11:15  阅读(1375)
文章分类:Spring Cloud

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)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> Spring Cloud Zuul(五)路由器和过滤器

相关推荐