一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - Java教程 - 詳解Spring Cloud Hystrix斷路器實現(xiàn)容錯和降級

詳解Spring Cloud Hystrix斷路器實現(xiàn)容錯和降級

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

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

簡介

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

基礎(chǔ)環(huán)境

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

git:項目源碼

添加產(chǎn)品服務(wù)

在intellij中創(chuàng)建一個新的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>

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

在src/main/resources下創(chuàng)建bootstrap.yml文件,添加如下內(nèi)容:

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

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

?
1
2
server:
 port: 8081

此配置指定了產(chǎn)品服務(wù)的端口為8081。接著創(chuàng)建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自動把本服務(wù)注冊到eureka。最后創(chuàng)建cn.zxuqian.controllers.productcontroller控制器,提供/products api,返回示例數(shù)據(jù):

?
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客戶端

打開我們之前創(chuàng)建的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標(biāo)記的方法將告訴如何初始化此類對象,比如本例中就是使用resttemplatebuilder來創(chuàng)建一個resttemplate的對象,這個稍后在使用斷路器的service中用到。

創(chuàng)建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 "夾克,毛衣";
  }
}

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

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

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

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

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

最后創(chuàng)建一個控制器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路徑提供數(shù)據(jù)。

測試

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

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 免费特黄一区二区三区视频一 | 亚洲天堂导航 | 日本一在线中文字幕天堂 | 久久久久嫩草影院精品 | 国产亚洲精品美女久久久 | 午夜亚洲视频 | 亚裔maricahaseaⅴ | 日韩精品免费一区二区三区 | 成年人免费观看的视频 | 97自拍视频在线观看 | 爽好舒服快想要免费看 | 亚洲国产成人综合 | 色碰视频| 视频二区 素人 欧美 日韩 | 精品午夜视频 | 欧美老肥妇bbb | 欧美日韩一区二区三区久久 | 亚洲AV久久久久久久无码 | 肥胖女性大bbbbbb视频女厕 | 色噜噜亚洲男人的天堂www | 国产精品视频久久 | 99国产小视频 | 国产精品久久免费观看 | 日韩在线视频在线 | 日本大学生xxxxx69泡妞 | 太大了轻点阿受不了小说h 四色6677最新永久网站 | 日韩欧美国产免费看清风阁 | 草草视频免费观看 | 国产精品俺来也在线观看了 | 日韩成人av在线 | 亚洲精品123区在线观看 | 人人精品久久 | 国产毛片在线观看 | 99久久精品国产一区二区 | 成人夜视频寂寞在线观看 | 日本无遮挡拍拍拍凤凰 | 99在线视频观看 | 性派对videofreeparty| 99久久一香蕉国产线看观看 | 精品视频网站 | 奇米影视999 |