admin管理员组

文章数量:1530517

作者:jiangzz 电话:15652034180 微信:jiangzz_wx 微信公众账号:jiangzz_wy

路由是微服务架构不可或缺的一部分。例如/可能映射到您的Web应用程序,/api/users映射到用户服务,/api/shop映射到商店服务。 Zuul是Netflix的基于JVM的路由器和服务器端负载均衡器。

快速入门

pom引入zuul依赖

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

配置application.properties

server.port=8888

zuul.routes.users.path=/users/**
zuul.routes.users.url=http://localhost:8080

management.endpoints.web.exposure.include=*

启动入口类添加@EnableZuulProxy注解

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

用户可以访问http://localhost:8888/users/manager/user/8系统底层会将用户请求转发给后台http://localhost:8080/manager/user/8服务。

实现服务负载均衡

修改pom.xml文件添加Eureka依赖

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.5.RELEASE</version>
</parent>

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>Greenwich.SR1</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>


    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
    </dependency>

</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

修改application.properties

server.port=8888

zuul.routes.users.path=/users/**
zuul.routes.users.service-id=USER-SERVICE

management.endpoints.web.exposure.include=*

# 配置Eureka
eureka.instance.appname=ZUUL-EUREKA
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true
eureka.client.healthcheck.enabled=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/

此时,用户可以尝试多启动几个USER-SERVICE的实例,测试zuul的负载均衡效果。

Cookies和Sensitive Headers

默认情况下Zuul会尝试去除HTTP请求头和cookies中一些信息,这样就导致后台系统获取不到某些请求头,用户可以关闭

server.port=8888

zuul.routes.users.path=/users/**
zuul.routes.users.service-id=USER-SERVICE
zuul.routes.users.sensitive-headers=
management.endpoints.web.exposure.include=*

# 配置Eureka
eureka.instance.appname=ZUUL-EUREKA
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true
eureka.client.healthcheck.enabled=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/

集成熔断failBack

Zuul中给定路径的电路跳闸时,您可以通过创建FallbackProvider类型的bean来提供回退响应。

@Component
public class ZuulFallbackProvider implements FallbackProvider {
    @Override
    public String getRoute() {
        return "*";//表示对所有的Service提供failback
    }

    @Override
    public ClientHttpResponse fallbackResponse(String route, Throwable cause) {
        System.out.println(cause.getClass());
        if (cause instanceof HystrixTimeoutException) {
            return response(HttpStatus.GATEWAY_TIMEOUT);
        } else {
            return response(HttpStatus.INTERNAL_SERVER_ERROR);
        }

    }
    private ClientHttpResponse response(final HttpStatus status) {
        return new ClientHttpResponse() {
            @Override
            public HttpStatus getStatusCode() throws IOException {
                return status;
            }

            @Override
            public int getRawStatusCode() throws IOException {
                return status.value();
            }

            @Override
            public String getStatusText() throws IOException {
                return status.getReasonPhrase();
            }

            @Override
            public void close() {
            }

            @Override
            public InputStream getBody() throws IOException {
                return new ByteArrayInputStream("er ops!!服务器忙!".getBytes());
            }

            @Override
            public HttpHeaders getHeaders() {
                HttpHeaders headers = new HttpHeaders();
                headers.setContentType(MediaType.APPLICATION_JSON);
                return headers;
            }
        };
    }
}

ribbon重试

添加pom依赖

<dependency>
    <groupId>org.springframework.retry</groupId>
    <artifactId>spring-retry</artifactId>
</dependency> 

application.properties配置服务

server.port=8888

zuul.routes.users.path=/users/**
zuul.routes.users.service-id=USER-SERVICE
zuul.routes.users.strip-prefix=false

hystrixmand.USER-SERVICE.execution.isolation.strategy=THREAD # 设置线程池隔离
hystrixmand.USER-SERVICE.execution.isolation.thread.timeoutInMilliseconds=10000 # 设置hystrix超时

zuul.retryable=true # 开启重试策略
USER-SERVICE.ribbon.ConnectTimeout=100 # 设置连接超时时间
USER-SERVICE.ribbon.ReadTimeout=500 # 设置响应时间
USER-SERVICE.ribbon.MaxAutoRetriesNextServer=1 # 如果连接失败,尝试其他机器的次数
USER-SERVICE.ribbon.MaxAutoRetries=2 # 如果本次失败尝试次数

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

# 配置Eureka
eureka.instance.appname=ZUUL-EUREKA
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true
eureka.client.healthcheck.enabled=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/

Hystrix限流

修改application.properties

server.port=8888

zuul.routes.users.path=/users/**
zuul.routes.users.service-id=USER-SERVICE
zuul.routes.users.strip-prefix=false

hystrixmand.USER-SERVICE.execution.isolation.strategy=THREAD
hystrixmand.USER-SERVICE.execution.isolation.thread.timeoutInMilliseconds=10000


hystrix.threadpool.default.coreSize=10 # 设置线程池
hystrix.threadpool.default.maxQueueSize=10 # 设置最大队列
hystrix.threadpool.default.queueSizeRejectionThreshold=10 # 设置最大队列限制

zuul.retryable=true
USER-SERVICE.ribbon.ConnectTimeout=100
USER-SERVICE.ribbon.ReadTimeout=500
USER-SERVICE.ribbon.MaxAutoRetriesNextServer=1
USER-SERVICE.ribbon.MaxAutoRetries=2

management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always
# 配置Eureka
eureka.instance.appname=ZUUL-EUREKA
eureka.instance.instance-id=001
eureka.instance.prefer-ip-address=true
eureka.instance.lease-expiration-duration-in-seconds=20
eureka.instance.lease-renewal-interval-in-seconds=10

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=true
eureka.client.healthcheck.enabled=true
eureka.client.service-url.defaultZone=http://jiangzz:123456@localhost:8761/eureka/

更多精彩内容关注

本文标签: 过滤器路由器章节Zuul