demo(五) zuul(1)构建网关

在之前feign的基础上,

 新建zuul项目,zuul有两个重要应用:构建网关、请求过滤。这里先看下构建网关

(1)pom:这里引入eureka依赖是给服务路由使用的,传统路由不需要添加这个依赖

<?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>com.demo.zuul</groupId>
    <artifactId>myspringcloud-zuul-demo</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <!-- springBoot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.9.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-eureka-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zuul</artifactId>
        </dependency>
    </dependencies>

    <properties>
        <spring-cloud.version>Edgware.RELEASE</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>

    <build>
        <finalName>my-zuul</finalName>
        <plugins>
            <plugin>
                <artifactId>maven-release-plugin</artifactId>

            </plugin>
            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <version>2.2.1</version>
                <configuration>
                    <descriptors>
                        <descriptor>src/main/assemble/package.xml</descriptor>
                    </descriptors>
                </configuration>
                <executions>
                    <execution>
                        <id>make-assembly</id>
                        <phase>package</phase>
                        <goals>
                            <goal>single</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <version>2.4</version>
                <configuration>
                    <excludes>
                        <exclude>**/*.properties</exclude>
                        <exclude>**/*.xml</exclude>
                        <exclude>**/*.html</exclude>
                        <exclude>config</exclude>
                    </excludes>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.5.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

(2)配置文件

server.port=5555
server.context-path=/zuulDemo

(3)启动类

package com.demo.zuul;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;

@SpringBootApplication
@EnableZuulProxy
public class ZuulApplication {
    public static void main(String args[]){
        SpringApplication.run(ZuulApplication.class,args);
    }
}

 一、路由类型:分传统路由、服务路由、自定义路由三种

 1、传统路由:不需要依赖eureka

 (1) 单实例配置:下面配置feign的网关路由:

zuul.routes.feignDemo.path = /feignDemo/**
zuul.routes.feignDemo.url = http://localhost:4444/feignDemo/

如访问localhost:5555/zuulDemo/feignDemo/test/getAllUsers,因符合/feignDemo/**规则,

则会自动转发到localhost:4444/feignDemo/test/getAllUsers上,

 (2) 多实例配置:这种配置方式同后面的服务路由有些相似,使用了serviceId指向服务

#传统路由 多实例配置
zuul.routes.a-service.path = /a/**
zuul.routes.a-service.serviceId = my-feign
ribbon.eureka.enabled = false
my-feign.ribbon.listOfServers =http://localhost:4444/feignDemo/,http://localhost:4445/feignDemo/

  ①ribbon.eureka.enabled:由于传统路由并没有整合eureka,所以设置为false,否则配置的serviceId获取不到对应实例的清单。

  ②<serviceId>.ribbon.listOfServers:相当于在该应用内部手动维护了服务与实例的对应关系。

这是zuul传统的路由方式,不管是单实例还是多实例,都需要手动通过url或者serviceId来指定请求表达式的具体映射。如果有多个feign服务,仍然需要耗费时间来维护各个路由的path和url的关系;而多实例集群模式url是写死的,维护起来很麻烦。由此出现了面向服务的路由。

2、面向服务的路由:

  (1)手动配置:让路由的path不再映射到具体的url,而是指向服务,至于具体的url由eureka的服务发现机制去自动维护。由此需要将zuul也注册到注册中心

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.application.name=my-zuul

启动后,刷新eureka注册中心,可以看到zuul组件也注册成功了。

 修改路由配置代码,此时完整的配置文件为:

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.application.name=my-zuul

server.port=5555
server.context-path=/zuulDemo

#访问地址为​ localhost:5555/zuulDemo/a/feignDemo/test/getAllUsers​;如果设置的为/feignDemo/**,访问需要再加一层feignDemo,即http://localhost:5555/zuulDemo/feignDemo/feignDemo/test/getAllUsers
zuul.routes.a-service.path = /a/**
zuul.routes.a-service.serviceId = my-feign
#假设还有其他服务
#zuul.routes.b-service.path = /b/**
#zuul.routes.b-service.serviceId = xxx

访问 localhost:5555/zuulDemo/a/feignDemo/test/getAllUsers,因符合/a/**规则,会根据配置自动转发到对应的服务上,

  (2)服务路由的默认配置:zuul中引入eureka后,如果不手动配置路由规则,zuul默认以eureka中其他微服务的spring.application.name作为路径前缀来创建服务路由。如zuul的以下配置文件并未添加手动路由配置

eureka.client.serviceUrl.defaultZone=http://localhost:1111/eureka/
spring.application.name=my-zuul

server.port=5555
server.context-path=/zuulDemo

 现有eureka服务spring.application.name=my-service、feign服务my-feign,

直接访问feign地址为localhost:4444/feignDemo/test/getAllUsers

通过网关访问地址为http://localhost:5555/zuulDemo/my-feign/feignDemo/test/getAllUsers

   

再如直接访问eureka地址为localhost:2222/myService/user/getAllUsers

通过网关访问该服务接口地址为http://localhost:5555/zuulDemo/my-service/myService/user/getAllUsers

(3)屏蔽访问:因zuul可以自动创建服务路由,如果某些路由不希望被访问到,即不希望被自动创建路由,可以通过zuul.ignored-servies参数来设置。如果设置zuul.ignored-servies = *则zuul对所有的服务都不会自动创建路由规则。

3、自定义路由映射规则: 

二、网关表达式:网关(传统/服务)路由路径表达式path采用了Ant风格定义,通配符有?(匹配任意单个字符)、*(批量任意数量的字符)、**(匹配任意数量的字符,支持多级目录)三种。

 实际访问时匹配的是通配符的内容,如my-feign服务设置路由为

zuul.routes.a-service.path = /feignDemo/**
zuul.routes.a-service.serviceId = my-feign

 则访问localhost:4444/feignDemo/test/getAllUsers需要再加一层feignDemo,也即

localhost:5555/zuulDemo/feignDemo/feignDemo/test/getAllUsers

三、本地跳转: 

四、动态路由:将API网关的配置文件通过spring cloud config连接的GIt仓库存储和管理。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

w_t_y_y

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值