Routers and filters: Zuul (Section 10)

Copyright: without the author's permission is not allowed for any commercial purposes https://blog.csdn.net/weixin_38231448/article/details/91040252

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

Routing is an integral part of micro-services architecture. For example, /may be mapped to your Web application that /api/usersis mapped to user services, /api/shopmapping services to the store. Zuul is Netflix JVM-based routers and server load balancer.

Getting Started

pom dependent on the introduction of 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>

Configuration application.properties

server.port=8888

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

management.endpoints.web.exposure.include=*

Start import category Add @EnableZuulProxycomment

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

Users can access http://localhost:8888/users/manager/user/8the system underlying the user will forward the request to the backend http://localhost:8080/manager/user/8service.

To achieve load balancing service

Modify pom.xml file to add Eureka dependent

<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>

Modify 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/

At this point, the user can try to start more than a few instances of USER-SERVICE, load balancing effect of the test zuul.

Cookies和Sensitive Headers

Zuul will attempt to remove the default HTTP request headers and cookies in some of the information, thus leading to back-office systems get less than a certain request headers, users can turn off

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/

Integrated fuse failBack

Zuul when a circuit is tripped given path, you can provide a fallback response by creating FallbackProvider type of 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 Retry

Add pom-dependent

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

application.properties configuration services

server.port=8888

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

hystrix.command.USER-SERVICE.execution.isolation.strategy=THREAD # 设置线程池隔离
hystrix.command.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 limiting

Modify application.properties

server.port=8888

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

hystrix.command.USER-SERVICE.execution.isolation.strategy=THREAD
hystrix.command.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/

More exciting content focus

Micro-channel public account

Guess you like

Origin blog.csdn.net/weixin_38231448/article/details/91040252