春の雲高度道路| 5:サービス・ゲートウェイ集積回路ブレーカ(zuul + hystrix)

序文

すでに、関連する回路ブレーカのコンポーネントと統合されたソリューションの役割について話しました前に、具体的資料を参照してください。サーキットブレーカー(hystrix):6 |春の雲の高度道路

だから、すべてのサービスのための事前のマイクロゲートウェイサービス、などなど、認証、許可、監査、ログ収集、統計、フロー制御を、ルーティングの重要な機能を持つ、私たちは、高い信頼性と安定性を確保しなければなりません。一方、前述の重要な機能を実現するために、それはまた、コール(残り、装う)リモート呼び出しを介して、他のサービスに依存することになります。

これにより、全体のサービス大面積の麻痺を引き起こすゲートウェイによって引き起こされるサービス利用できないサービスのダウンタイムに依存しないように、回路遮断器を統合する必要があります。

 

準備

zuulので、集積回路ブレーカも非常に簡単です、リボンを統合しました。複雑な支出記事春の雲アドバンスド・ロード| 14:サービスゲートウェイの再構築(FactoryBeanの、動的な構成、トークン解析、ユーザーのパス) xmall-AUTH、xmall-製品:プロジェクト全体の 、xmall-zuul。

 

変換zuul


依存変換

次のように春 - クラウド・スタータopenfeign、春・クラウド・スタータNetflixの-hystrix依存、POM変更されたファイルを追加します。

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
​
    <parent>
        <groupId>com.luas.cloud</groupId>
        <artifactId>java-boot-parent-2.1</artifactId>
        <version>1.0.0-SNAPSHOT</version>
        <relativePath>../../java-boot-parent-2.1</relativePath>
    </parent>
​
    <groupId>com.luas.xmall</groupId>
    <artifactId>xmall-zuul</artifactId>
    <version>1.0.0-SNAPSHOT</version>
​
    <name>xmall-zuul</name>
    <description>网关服务</description>
​
    <properties>
    </properties>
​
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-zuul</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-oauth2</artifactId>
        </dependency>
​
        <!-- nacos cloud -->
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
        </dependency>
​
        <dependency>
            <groupId>com.alibaba.cloud</groupId>
            <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
​
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>
​
​
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
​
</project>

 

クラスの変換を開始します

スタートクラスは、注釈追加された@EnableCircuitBreaker、@EnableFeignClientsオープンhystrix、装うのを

package com.luas.xmall.gateway;
​
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.openfeign.EnableFeignClients;
​
@EnableFeignClients
@EnableCircuitBreaker
@EnableZuulProxy
@SpringBootApplication
public class XmallZuulApplication {
​
    public static void main(String[] args) {
        SpringApplication.run(XmallZuulApplication.class, args);
    }
​
}

 

作成クライアント

心の便宜上、回路ブレーカのプロセスをシミュレートするために呼び出さzuulに直接装うクライアントxmall・製品・サービスを作成し、他のプロジェクトを、作成していません。

SkuServiceを作成します。

package com.luas.xmall.gateway.product.clients;
​
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
​
@FeignClient(name = "xmall-product", fallback = SkuFallbackService.class)
public interface SkuService {
​
    @RequestMapping(value = "/sku/{id}", method = RequestMethod.GET)
    Object info(@PathVariable("id") String skuId);
​
}

 

SkuFallbackServiceを作成します。

package com.luas.xmall.gateway.product.clients;
​
import cn.hutool.core.map.MapUtil;
import org.springframework.stereotype.Component;
​
@Component
public class SkuFallbackService implements SkuService {
​
    @Override
    public Object info(String skuId) {
        return MapUtil.builder()
                .put("skuId", "0000000")
                .put("name", "未知")
                .put("price", "99999")
                .put("port", "未知")
                .build();
    }
​
}

 

アナログ・コール

事前認証後のプレフィルターの種類、順序として、SkuFeignFilterを作成します。そのため、あなたは承認、アクセスSKUのインターフェイスを申請する必要があり、その後、フィルタはに実行することができます。

package com.luas.xmall.gateway.filter;
​
import com.luas.xmall.gateway.product.clients.SkuService;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.exception.ZuulException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
​
import java.util.Random;
​
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.PRE_TYPE;
​
@Component
public class SkuFeignFilter extends ZuulFilter {
​
    private Logger logger = LoggerFactory.getLogger(getClass());
​
    @Autowired
    private SkuService skuService;
​
    @Override
    public String filterType() {
        return PRE_TYPE;
    }
​
    @Override
    public int filterOrder() {
        return 1;
    }
​
    @Override
    public boolean shouldFilter() {
        return true;
    }
​
    @Override
    public Object run() throws ZuulException {
        Object skuInfo = this.skuService.info("" + new Random().nextInt(4));
​
        this.logger.info("sku info is {}", skuInfo);
​
        return null;
    }
}

 

TRANSFORMATION

装うhystrixスイッチの設定を追加します。

feign:
  hystrix:
    enabled: true

 

検証

ターンで開始し、それぞれxmall-AUTH、xmall-zuulエンジニアリング、ポート、8080,7777,5566-製品をxmall。

承認を申請。

SKUアクセス・インタフェースは、http:// localhostを:5566 /ゲートウェイ/製品/ SKU / 1122は、通常の訪問することができます。

ストップxmall製品・サービスは、アクセスSKUインタフェースは、再び、通常アクセスすることはできません。

コンソールを表示し、それが迅速な障害ポリシーを行った、デフォルトの情報に戻します。


記事はまた、言及していた前に、用hystrixブレーカーのトリガ条件20のフォルトは5秒以内に起こりますしたがって、20回インタフェース連続アクセスSKUは、ヒューズ・ポリシー・ブレーカが開かれたトリガました。

この時点で、コンソールは、回路ブレーカのオープンに関する情報が表示されます。

xmall物回収サービスの前に、要求がブロックされることはありません、後続の要求が失敗しても、大規模な障害が発生、状況の崩壊、全体ゲートウェイサービスと信頼性の高いサービスの安定性の効果的な保護を指示します。

 

ソース

githubの

https://github.com/liuminglei/SpringCloudLearning/tree/master/15/

gitee

https://gitee.com/xbd521/SpringCloudLearning/tree/master/15/

 

 

この記事は重版のための記事で、[ギャラクシー]オリジナルの建築家である明白なの著者とソースを明記してください。

マイクロチャンネルサーチ[建築家]ギャラクシーは、よりエキサイティングなコンテンツを見つけます。

 

 

公開された29元の記事 ウォンの賞賛1 ビュー2221

おすすめ

転載: blog.csdn.net/liuminglei1987/article/details/104226711