ルータとフィルタ:Zuul(10節)

著作権:著者の許可なく商用目的のために許可されていませんhttps://blog.csdn.net/weixin_38231448/article/details/91040252

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

ルーティングはマイクロサービスアーキテクチャの不可欠な部分です。たとえば、/Webアプリケーションにマッピングすることができ/api/users、利用者のサービスにマッピングされている/api/shop店舗にサービスをマッピングします。ZuulはNetflixのJVMベースのルータやサーバロードバランサです。

はじめに

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ファイルを変更

<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の負荷分散効果のいくつかのインスタンス以上を起動しようとすることができます。

クッキー和敏感ヘッダ

Zuulはにつながるバックオフィスシステムは、特定のリクエストヘッダ未満を取得し、ユーザーがオフにすることができるので、情報の一部では、デフォルトのHTTPのリクエストヘッダとクッキーを削除しようとします

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/

統合されたヒューズフェイルバック

回路は、与えられたパスをトリップしたときZuulは、あなたは、BeanのFallbackProviderタイプを作成することによって、フォールバック応答を提供することができます。

@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;
            }
        };
    }
}

リボンの再試行

ポンポン依存を追加

<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

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

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/

よりエキサイティングなコンテンツフォーカス

マイクロチャネルパブリックアカウント

おすすめ

転載: blog.csdn.net/weixin_38231448/article/details/91040252