激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解Spring Cloud Hystrix斷路器實現容錯和降級

詳解Spring Cloud Hystrix斷路器實現容錯和降級

2021-04-26 14:16fengqiuzhihua Java教程

本篇文章主要介紹了詳解Spring Cloud Hystrix斷路器實現容錯和降級,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

簡介

spring cloud提供了hystrix容錯庫用以在服務不可用時,對配置了斷路器的方法實行降級策略,臨時調用備用方法。這篇文章將創建一個產品微服務,注冊到eureka服務注冊中心,然后我們使用web客戶端訪問/products api來獲取產品列表,當產品服務故障時,則調用本地備用方法,以降級但正常提供服務。

基礎環境

  1. jdk 1.8
  2. maven 3.3.9
  3. intellij 2018.1

git:項目源碼

添加產品服務

在intellij中創建一個新的maven項目,使用如下配置

  1. groupid: cn.zxuqian
  2. artifactid: productservice

然后在pom.xml中添加如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelversion>4.0.0</modelversion>
 
  <groupid>cn.zxuqian</groupid>
  <artifactid>productservice</artifactid>
  <version>1.0-snapshot</version>
  <parent>
    <groupid>org.springframework.boot</groupid>
    <artifactid>spring-boot-starter-parent</artifactid>
    <version>2.0.1.release</version>
    <relativepath/>
  </parent>
 
  <properties>
    <project.build.sourceencoding>utf-8</project.build.sourceencoding>
    <java.version>1.8</java.version>
  </properties>
 
  <dependencies>
    <dependency>
      <groupid>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>
    </dependency>
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-web</artifactid>
    </dependency>
 
    <dependency>
      <groupid>org.springframework.boot</groupid>
      <artifactid>spring-boot-starter-test</artifactid>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <dependencymanagement>
    <dependencies>
      <dependency>
        <groupid>org.springframework.cloud</groupid>
        <artifactid>spring-cloud-dependencies</artifactid>
        <version>finchley.m9</version>
        <type>pom</type>
        <scope>import</scope>
      </dependency>
    </dependencies>
  </dependencymanagement>
 
  <build>
    <plugins>
      <plugin>
        <groupid>org.springframework.boot</groupid>
        <artifactid>spring-boot-maven-plugin</artifactid>
      </plugin>
    </plugins>
  </build>
 
  <repositories>
    <repository>
      <id>spring-milestones</id>
      <name>spring milestones</name>
      <url>https://repo.spring.io/libs-milestone</url>
      <snapshots>
        <enabled>false</enabled>
      </snapshots>
    </repository>
  </repositories>
 
</project>

我們繼續使用了spring-cloud-starter-netflix-eureka-client以使產品服務自動注冊到eureka服務中。然后還使用了spring-cloud-starter-config讀取配置服務中心的配置文件。這個項目只是一個簡單的spring web項目。

在src/main/resources下創建bootstrap.yml文件,添加如下內容:

?
1
2
3
4
5
6
spring:
 application:
  name: product-service
 cloud:
  config:
   uri: http://localhost:8888

在配置中心的git倉庫中創建product-service.yml文件 添加如下配置并提交:

?
1
2
server:
 port: 8081

此配置指定了產品服務的端口為8081。接著創建application類,添加如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
package cn.zxuqian;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.cloud.client.discovery.enablediscoveryclient;
@enablediscoveryclient
@springbootapplication
public class application {
  public static void main(string[] args) {
    springapplication.run(application.class, args);
  }
}

@enablediscoveryclient注解將指示spring cloud自動把本服務注冊到eureka。最后創建cn.zxuqian.controllers.productcontroller控制器,提供/products api,返回示例數據:

?
1
2
3
4
5
6
7
8
9
10
11
12
package cn.zxuqian.controllers;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
 
@restcontroller
public class productcontroller {
 
  @requestmapping("/products")
  public string productlist() {
    return "外套,夾克,毛衣,t恤";
  }
}

配置web客戶端

打開我們之前創建的web項目,在pom.xml中新添hystrix依賴:

?
1
2
3
4
<dependency>
  <groupid>org.springframework.cloud</groupid>
  <artifactid>spring-cloud-starter-netflix-hystrix</artifactid>
</dependency>

然后更新application類的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package cn.zxuqian;
import org.springframework.boot.springapplication;
import org.springframework.boot.autoconfigure.springbootapplication;
import org.springframework.boot.web.client.resttemplatebuilder;
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
@springbootapplication
public class application {
  public static void main(string[] args) {
    springapplication.run(application.class, args);
  }
 
  @bean
  public resttemplate rest(resttemplatebuilder builder) {
    return builder.build();
  }
}

這里使用@enablecircuitbreaker來開啟斷路器功能,然后還添加了一個rest方法并使用@bean注解。這部分屬于spring依賴注入功能,使用@bean標記的方法將告訴如何初始化此類對象,比如本例中就是使用resttemplatebuilder來創建一個resttemplate的對象,這個稍后在使用斷路器的service中用到。

創建cn.zxuqian.service.productservice類,并添加如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package cn.zxuqian.services;
import com.netflix.hystrix.contrib.javanica.annotation.hystrixcommand;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.cloud.client.serviceinstance;
import org.springframework.cloud.client.discovery.discoveryclient;
import org.springframework.stereotype.service;
import org.springframework.web.client.resttemplate;
import java.util.list;
 
@service
public class productservice {
  private final resttemplate resttemplate;
  @autowired
  private discoveryclient discoveryclient;
  public productservice(resttemplate resttemplate) {
    this.resttemplate = resttemplate;
  }
 
  @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() + "/products", string.class);
    }
 
    return "";
  }
 
  public string backupproductlist() {
    return "夾克,毛衣";
  }
}

之所以要創建一個service類,是因為hystrix只能在標記為@service或@component的類中使用,這樣才能夠正常使用spring context所提供的api。這個以后深入spring時再作說明。

使用@hystrixcommand注解后,hystrix將監控被注解的方法即productlist(底層使用proxy包裝此方法以此實現監控),一旦此方法的錯誤累積到一定門檻的時候,就會啟動斷路器,后續所有調用productlist方法的請求都會失敗,而會臨時調用fallbackmethod指定的方法backupproductlist(),然后當服務恢復正常時,斷路器就會關閉。

我們還在此類中用了discoveryclient用以尋找產品服務的uri地址,使用產品服務的spring.application.name配置項的值,即product-service作為serviceid傳給discoveryclient.getinstances()方法,然后會返回一個list,因為目前我們只有一個產品服務啟動著,所以只需要取第一個實例的uri地址即可。

然后我們使用resttemplate來訪問產品服務的api,注意這里使用了spring的構造方法注入,即之前我們用@bean注解的方法會被用來初始化resttemplate變量,不需我們手動初始化。resttemplate類提供了getforobject()方法來訪問其它rest api并把結果包裝成對象的形式,第一個參數是要訪問的api的uri地址,第二參數為獲取的結果的類型,這里我們返回的是string,所以傳給他string.class。

backupproductlist()方法返回了降級后的產品列表信息。

最后創建一個控制器cn.zxuqian.controllers.productcontroller并添加如下代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
package cn.zxuqian.controllers;
import cn.zxuqian.services.productservice;
import org.springframework.beans.factory.annotation.autowired;
import org.springframework.web.bind.annotation.requestmapping;
import org.springframework.web.bind.annotation.restcontroller;
@restcontroller
public class productcontroller {
  @autowired
  private productservice productservice;
  @requestmapping("/products")
  public string productlist() {
    return productservice.productlist();
  }
}

 這里使用productservice為/products路徑提供數據。

測試

首先,我們使用spring-boot:run插件啟動配置中心服務,config-server,然后啟動eureka-server,再啟動product-service,最后啟動web客戶端,稍等片刻待eureka服務注冊成功之后訪問http://localhost:8080/products,正常的情況下會得到外套,夾克,毛衣,t恤結果,然后我們關閉product-service,之后再訪問同樣的路徑,會得到降級后的結果:夾克,毛衣

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/fengqiuzhihua/article/details/80199056

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 草莓福利社区在线 | 免费黄色在线电影 | 久久久久久久久久久久久国产精品 | 久久精品视频免费观看 | 毛片电影网址 | 亚洲亚色 | 午夜精品福利视频 | 特逼视频 | 久久精品无码一区二区日韩av | 天天夜碰日日摸日日澡性色av | 狠狠操天天射 | 一区二区三区在线观看免费视频 | 国产精品自拍99 | 法国性xxx精品hd专区 | 精品一区二区三区免费看 | 欧美一区中文字幕 | 国产91久久精品一区二区 | 久久久三级免费电影 | 在线a毛片免费视频观看 | jizzjizz中国人少妇中文 | 性欧美videos 另类喷潮 | www.com国产精品 | 一色一情 | 欧美成人一二区 | 国产精品一区在线观看 | 色综合激情 | www.com国产精品 | 欧美视频国产精品 | 国产精品视频一区二区三区四区五区 | 亚洲综合精品成人 | 天天操天天操天天操天天操天天操天天操 | 日韩在线观看视频一区二区三区 | 国产精选电影免费在线观看网站 | 久久777国产线看观看精品 | 国产一区二区视频网站 | 久章草影院 | 欧美成人免费电影 | 午夜精品久久久久久久99热浪潮 | 国产精品午夜性视频 | 欧美成人一区二区三区 | 国产女厕一区二区三区在线视 |