導入
Spring Cloudは、Hystrix Fault-Tolerantライブラリを提供して、サービスが利用できないときに回路ブレーカーで構成された方法のダウングレード戦略を実装し、代替方法を一時的に呼び出します。この記事では、製品マイクロサービスを作成し、Eureka Service Registration Centerに登録し、Webクライアントアクセス/製品APIを使用して製品リストを取得します。製品サービスが失敗したときに、ローカルスタンバイ方法が格下げされますが、サービスを提供するためにサービスを提供します。
基本環境
Git:プロジェクトソースコード
製品サービスを追加します
次の構成を使用して、Intellijで新しいMavenプロジェクトを作成します
次に、pom.xmlに次のコードを追加します。
<?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 <http://www.w3.org/2001 xsi:schemalocation = "http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.0.sdsd"> <modelversion> 4.0.0 </modelversion> <artifactid> Productservice </artifactid> <version> 1.0-snapshot </version> <parent> <parent> <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter- parent </artifactid> <バージョン> 2.0.1 <project.build.sourceencoding> utf-8 </project.build.sourceEncoding> <java.version> 1.8 </java.version> </properties> <dependencies> <depthency> org.springframework.cloud </groupId> <artifactid> spring-cloud-starter-netflix-eureka-client </artifactid> </dependency> <dependency> <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-starter-config </artifactid> </depence> <artifactid> spring-boot-starter-web </artifactid> </dependency> <dependency> groupid> org.springframework.boot </groupid> <artifactid> spring-boot-starter-test </artifactid> <scope> test </scop <groupid> org.springframework.cloud </groupid> <artifactid> spring-cloud-dependencies </artifactid> <bersion> finchley.m9 </version> <type> pom </type> <scope> Import </scope> </dependency> </redenceancy> </depence <groupid> org.springframework.boot </groupid> <artifactid> spring-boot-maven-plugin </artifactid> </plugin> </glugins> </build> <id> spring-milestones </id> <name> spring mirestones </name> </name> <url> https://repo.spring.io/libs-milestone </url> <snapshots> <Enabled> false </enabled> </snapshots> </repository> </repository> </repositories> </project>
Spring-Cloud-Starter-Starter-Netflix-Eureka-Clientを使用して、製品とサービスをEurekaサービスに自動的に登録できるようにしました。次に、Spring-Cloud-Starter-Configを使用して、構成サービスセンターの構成ファイルを読み取ります。このプロジェクトは、単純なSpring Webプロジェクトです。
src/main/resourcesの下にbootstrap.ymlファイルを作成し、次のコンテンツを追加します。
春:アプリケーション:名前:製品サービスクラウド:config:uri:http:// localhost:8888
構成センターのGITリポジトリにProduct-Service.ymlファイルを作成し、次の構成を追加して送信します。
サーバー:ポート:8081
この構成は、製品サービスポートが8081であることを指定します。次に、アプリケーションクラスを作成し、次のコードを追加します。
パッケージcn.zxuqian; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springframework.cloud.cluent.client.discovery.enabledisclient;@enablediscoverycliccliccliccliccliccliccliccliant; void main(string [] args){springApplication.run(application.class、args); }}@enablediscoveryclientアノテーションは、Spring CloudにEurekaにこのサービスを自動的に登録するよう指示します。最後に、cn.zxuqian.controllers.productcontrollerコントローラーを作成し、 /製品APIを提供し、サンプルデータを返します。
パッケージcn.zxuqian.controllers; Import org.springframework.web.bind.annotation.RequestMapping; Import org.springframework.web.bind.annotation.restController; @RestControllollerPublic Class ProductController {@RequestMapping(@RequestMapting(upertion -coats)ed(/dectrylist)セーター、Tシャツ "; }}Webクライアントを構成します
以前に作成したWebプロジェクトを開き、pom.xmlに新しいHystrix依存関係を追加します。
<Dependency> groupId> org.springframework.cloud </groupId> <artifactid> spring-cloud-starter-netflix-hystrix </artifactid> </dependency>
次に、アプリケーションクラスのコードを更新します。
パッケージcn.zxuqian; Import org.springframework.boot.springApplication; Import org.springframework.boot.autoconfigure.springframework.boot.web.client.restemplatebuilder; Import; org.springframework.cloud.client.circuitbreaker.enablecircuitbreaker; Import org.springframework.cloud.client.discovery.enablediscoveryclient; Import org.springframework.context.annotation.bean; import org.springframework.web.client.resttemplate;@enablecircuitbreaker@enablediscoveryclient@springbootapplicationpublic class application {public static void main(string [] args){springApplication.run(Application.class、args); } @bean public rettemplate rest(rettemplatebuilder builder){return builder.build(); }}ここでは、 @enableCircuitBreakerを使用して回路ブレーカー機能を有効にし、RESTメソッドが追加され、@Beanアノテーションが使用されます。この部品は、スプリング依存関係噴射関数に属します。 @Beanタグを使用するメソッドは、そのようなオブジェクトを初期化する方法を教えてくれます。たとえば、この例では、RestTemplateBuilderを使用してRestTemplateオブジェクトを作成します。これは、回路ブレーカーを使用してサービスの後半で使用されます。
cn.zxuqian.service.productserviceクラスを作成し、次のコードを追加します。
パッケージcn.zxuqian.services; Import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand; Import org.springframework.beans.factory.annotation.autowired; Import org.spramework.clud.clud.serviceinstance; org.springframework.cloud.client.discovery.discoveryclient; Import org.springframework.stereotype.service; Import org.springframework.web.client.resttemplate; Import java.util.list; @servicepublic classervice {private restemplate; @Autowired Private DiscoveryClient DiscoveryClient; Public Productservice(RESTTEMPLATE RESTTEMPLATE){this.restTemplate = rettemplate; } @hystrixcommand(fallbackmethod = "backupproductlist")public string productlist(){list <serviceInstance> instances = this.discoveryclient.getInstances( "Product-Service"); if(instances!= null && instances.size()> 0){return this.resttemplate.getForObject(instances.get(0).geturi() + "/desction"、string.class); } 戻る ""; } public string backupproductlist(){return "jack、seater"; }}サービスクラスを作成する理由は、Hystrixが@Serviceまたは@Componentとマークされたクラスでのみ使用できるため、Springコンテキストで提供されるAPIを正常に使用できるようにするためです。これは、後で春に深く入ったときに説明されます。
@HyStrixCommandアノテーションを使用した後、Hystrixは注釈付きの方法、つまり製品リスト(基礎となる層がこの方法をラップして監視を実現します)を監視します。この方法のエラーが特定のしきい値に蓄積すると、回路ブレーカーが開始されます。 ProductListメソッドを呼び出すという後続のリクエストはすべて失敗し、FallbackMethodによって指定された方法は一時的にBackupproductlist()と呼ばれ、サービスが正常に戻ると、回路ブレーカーが閉じられます。
また、このクラスでは、DiscoveryClientを使用して製品サービスのURIアドレスを見つけ、Spring.Application.Name構成アイテムの値を使用します。つまり、製品サービスはDiscoveryClient.getInstances()メソッドに渡され、リストが返されます。製品サービスが1つしか開始されていないため、最初のインスタンスのURIアドレスを取得するだけです。
次に、RestTemplateを使用して、製品サービスのAPIにアクセスします。 Springのコンストラクターインジェクションはここで使用されていることに注意してください。つまり、@Beanで注釈を付けた方法を使用して、手動で初期化することなくRESTEMPLATE変数を初期化するために使用されます。 RESTTEMPLATEクラスは、他のREST APIにアクセスし、結果をオブジェクトフォームにラップするためのgetForObject()メソッドを提供します。最初のパラメーターは、アクセスするAPIのURIアドレスであり、2番目のパラメーターは得られた結果のタイプです。ここでは弦を返しますので、string.classに渡します。
backupproductlist()メソッドは、格下げされた製品リスト情報を返します。
最後に、コントローラーcn.zxuqian.controllers.productcontrollerを作成し、次のコードを追加します。
パッケージcn.zxuqian.controllers; import cn.zxuqian.services.productservice; import org.springframework.beans.factory.annotation.autowired; import org.springframework.web.bind.annotation.requestmapping; org.springframework.web.bind.annotation.restController; @RestControllerPublic Class ProductController {@Autowired Private ProductService Productservice; @RequestMapping( "/products")public string crowtlist(){return productervice.productlist(); }}ここで、Productserviceを使用して /Products Pathのデータを提供します。
テスト
まず、スプリングブート:プラグインを実行して構成センターサービス、構成サーバーを起動し、eureka-serverを起動し、製品サービスを開始し、最後にWebクライアントを起動します。 Eurekaサービスが正常に登録されるまでしばらく待って、http:// localhost:8080/製品にアクセスしてください。通常の状況では、ジャケット、ジャケット、セーター、Tシャツの結果が得られます。次に、製品サービスを閉じてから同じパスにアクセスすると、ジャケット、セーターのダウングレードの結果が得られます。
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。