Hystrixのクラスタと監視タービンを
装う、Hystrixの統合サービスヒューズが完全にサービス低下をデカップリング
クラスタの後にタイムアウト設定
Hystrixのクラスタと監視タービンを
フロントダッシュボードのデモは、実際のプロジェクトはので、ここで、基本的にクラスターである、唯一のスタンドアロンのサービス監視でタービンを監視クラスタです。
タービンは、ダッシュボードに基づいています。
まず、前方クラスタを置きます。
そして microservice-学生プロバイダhystrix-で再従事 1005 ベースmicroservice-学生プロバイダhystrix- 1004 でプロジェクト
その後、コードと構成のコピー、およびいくつかの場所を変更します。
1、YML設定
--- サーバー: ポート:1004 コンテキストパス:/ 春: データソース: 種類:com.alibaba.druid.pool.DruidDataSource ドライバー・クラス名:はcom.mysql.jdbc.Driver URLます。jdbc:mysqlの:// localhostを:3306 /ブックuseUnicode =本当?&characterEncoding = UTF8 ユーザ名:ルート パスワード:123 JPA: 休止状態: DDL-自動:更新 ショー-SQLを:真の アプリケーション: 名前:microservice-学生の プロフィール:プロバイダhystrix-1004 ユーレカ: インスタンス: ホスト名を:localhostの 好む-IPアドレス:真 appnameの:microservice、学生 インスタンスID:microservice-学生:1004 クライアント: サービスURL: defaultZone:http://eureka2001.hmc.com:2001/eureka/,http://eureka2002.hmc.com:2002/eureka/,http:/ /eureka2003.hmc.com:2003/eureka/ 情報: のgroupId:com.hmc.testSpringcloud たartifactId:microservice-学生プロバイダhystrix-1004 バージョン:1.0-SNAPSHOT ユーザ名:http://hmc.com 電話:123456 hystrix: コマンド: デフォルト: 実行: アイソレーション: スレッド: timeoutInMilliseconds:1500 --- サーバー: ポート:1005 コンテキストパス:/ 春: データソース: タイプ:com.alibaba.druid.pool.DruidDataSource ドライバー・クラス名:はcom.mysql.jdbc.Driver URLます。jdbc:mysqlの:// localhostを:?3306 /書籍useUnicode =真&characterEncoding = UTF8 ユーザ名:ルート パスワード:123 JPA: 休止状態: DDL-オート:更新 SHOW- SQL:真 アプリケーション: 名前:microservice-学生の プロフィール:プロバイダhystrix-1005 ユーレカ: インスタンス: ホスト名:localhostの appnameの:microservice-学生の インスタンスID:microservice-学生:1005 好む-IPアドレス:真の クライアント: サービス-url: defaultZone:http://eureka2001.hmc.com:2001/eureka/,http://eureka2002.hmc.com:2002/eureka/,http://eureka2003.hmc.com:2003/eureka/ 情報: のgroupId: com.hmc.testSpringcloud たartifactId:microservice-学生プロバイダhystrix-1005 バージョン:1.0-SNAPSHOT ユーザ名:http://hmc.com 電話:123456 hystrix: コマンド: デフォルト: 実行: アイソレーション: スレッド: timeoutInMilliseconds:1500 - - サーバ: ポート:1006 コンテキストパス:/ 春: データソース: タイプ:com.alibaba.druid.pool.DruidDataSource ドライバー・クラス名:はcom.mysql.jdbc.Driver URLます。jdbc:mysqlの:// localhostを:?3306 /書籍useUnicode =真&characterEncoding = UTF8 ユーザ名:ルート パスワード:123 JPA: 休止状態: DDL-オート:更新 SHOW- SQL:真 アプリケーション: 名前:microservice-学生の プロフィール:プロバイダhystrix-1006 ユーレカ: インスタンス: ホスト名:localhostの appnameの:microservice-学生の インスタンスID:microservice-学生:1006 好む-IPアドレス:真の クライアント: サービスURL : defaultZone:http://eureka2001.hmc.com:2001/eureka/,http://eureka2002.hmc.com:2002/eureka/,http://eureka2003.hmc.com:2003/eureka/ 情報: groupId:com.hmc.testSpringcloud たartifactId:microservice-学生プロバイダhystrix-1006 バージョン:1.0-SNAPSHOT ユーザ名:http://hmc.com 電話:123456 hystrix: コマンド: デフォルト: 実行: アイソレーション: スレッド: timeoutInMilliseconds:1500
2、スタートアップクラス構成
パッケージcom.hmc.microservicestudentproviderhystrix。
輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.boot.autoconfigure.domain.EntityScan;
輸入org.springframework.cloud.client.circuitbreaker.EnableCircuitBreaker;
輸入org.springframework.cloud.netflix.eureka.EnableEurekaClient;
@EnableCircuitBreaker
@EntityScan( "com.hmc *")
@EnableEurekaClient
@SpringBootApplication
パブリッククラスMicroserviceStudentProviderHystrixApplication {
パブリック静的無効メイン(文字列[] args){
SpringApplication.run(MicroserviceStudentProviderHystrixApplication.class、引数)。
}
}
この場合、 そこ hystrixクラスタサービス。
3 、私たちの新しいプロジェクト microservice-学生消費者hystrix-タービン-91
プラス低依存のpom.xml
< 依存性> < のgroupId > org.springframework.boot </ のgroupId > < たartifactId >ばねブートスタータアクチュエータ</ たartifactId > </ 依存> < 依存性> < のgroupId > org.springframework.cloud </ のgroupId > < たartifactId >スプリングクラウドスタータータービン</ たartifactId > </ 依存>
4 、 application.yml
サーバー:
ポート:91である
コンテキストパス:/
ユーレカ:
クライアント:
-サービス-URL:
defaultzone:http://eureka2001.hmc.com:2001/eureka/,http://eureka2002.hmc.com:2002/eureka/、 http://eureka2003.hmc.com:2003/eureka/は
タービン:
アプリケーション-config設定:学生#アプリケーション名監視するためにmicroService指定
「『デフォルト』、」#がデフォルトにクラスタの名前を表しますclusterNameExpression
春:
アプリケーション:
名前:タービン
1、新しいスタートアップクラス MicroserviceStudentConsumerHystrixTurbine91Application 注釈付き: @EnableTurbine
パッケージcom.hmc.microservicestudentconsumerhystrixturbine91。
輸入org.springframework.boot.SpringApplication。
輸入org.springframework.boot.autoconfigure.SpringBootApplication。
輸入org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration。
輸入org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration。
輸入org.springframework.cloud.netflix.turbine.EnableTurbine。
@SpringBootApplication(= {DataSourceAutoConfiguration.class、HibernateJpaAutoConfiguration.class}除外する)
@EnableTurbine
パブリッククラスMicroserviceStudentConsumerHystrixTurbine91Application {
パブリック静的無効メイン(文字列[] args){
SpringApplication.run(MicroserviceStudentConsumerHystrixTurbine91Application.class、引数)を、
}
}
テスト:
最初の3起動ユーレカ、その後、10041005 でhystrixのサービスが開始されているが、
microservice-学生消費者-80 これもテストを容易にするために始めました。
ダッシュボード、タービンスタート。
この場合、 HTTP:// localhostの/学生/ hystrix サービスクラスタを呼び出すことができます。
http:// localhostを:91 / turbine.stream データを監視することができ、リアルタイムのping 返すデータ
入力90 / hystrix:// localhostを:HTTPをダッシュボードに、アドレスを入力してください
クリックして クラスタの監視計器に:
装う、Hystrix統合
前のコード、 @HystrixCommand fallbackMethodビジネスコードとの結合が高すぎるため、非常に良いですが、メンテナンスに助長されていません、それはデカップリングする必要があるが、我々は装うHystrix統合の話をしています。
1、microservice-学生プロバイダhystrixプロジェクトの変更
私たちは、元の設定されていません。書き込みに通常のロジックによると、
StudentServiceプラス新しいインターフェイスメソッド:
/** * 测试Hystrix服务降级 * @return */ public Map<String,Object> hystrix(); StudentServiceImpl写具体实现: @Override public Map<String, Object> hystrix() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 200); map.put("info","工号【"+port+"】正在为您服务"); return map; }
StudentProviderController正常调用service方法:
/** * 测试Hystrix服务降级 * @return * @throws InterruptedException */ @ResponseBody @GetMapping(value="/hystrix") // @HystrixCommand(fallbackMethod="hystrixFallback") public Map<String,Object> hystrix() throws InterruptedException{ Thread.sleep(100); // Map<String,Object> map=new HashMap<String,Object>(); // map.put("code", 200); // map.put("info","工号【"+port+"】正在为您服务"); return this.studentService.hystrix(); } // public Map<String,Object> hystrixFallback() throws InterruptedException{ // Map<String,Object> map=new HashMap<String,Object>(); // map.put("code", 500); // map.put("info", "系统【"+port+"】繁忙,稍后重试"); // return map; // }
2、microservice-common项目新建FallbackFactory类,解耦服务熔断服务降级
StudentClientService接口,新增getInfo方法;
/** * 服务熔断降级 * @return */ @GetMapping(value="/student/hystrix") public Map<String,Object> hystrix();
新建 StudentClientFallbackFactory 类,实现FallbackFactory<StudentClientService>接口;
package com.hmc.microservicecommon.service; import com.hmc.microservicecommon.entity.Student; import feign.hystrix.FallbackFactory; import org.springframework.stereotype.Component; import java.util.HashMap; import java.util.List; import java.util.Map; @Component public class StudentClientFallbackFactory implements FallbackFactory<StudentClientService> { @Override public StudentClientService create(Throwable cause) { return new StudentClientService() { @Override public boolean save(Student student) { return false; } @Override public List<Student> list() { return null; } @Override public Map<String, Object> hystrix() { Map<String,Object> map=new HashMap<String,Object>(); map.put("code", 500); map.put("info", "系统繁忙,稍后重试"); return map; } @Override public Student get(Integer id) { return null; } @Override public boolean delete(Integer id) { return false; } @Override public String ribbon() { return null; } }; } }
StudentClientService接口的@FeignClient注解加下 fallbackFactory属性
@FeignClient(value="MICROSERVICE-STUDENT",fallbackFactory=StudentClientFallbackFactory.class)
这类我们实现了 降级处理方法实现;
3、microservice-student-consumer-feign-80修改 支持Hystrix
StudentConsumerFeignController新增方法调用
/** * Feign整合Hystrix服务熔断降级 * @return * @throws InterruptedException */ @GetMapping(value="/hystrix") public Map<String,Object> hystrix() throws InterruptedException{ return studentClientService.hystrix(); }
4、microservice-student-consumer-feign-80的application.yml加上hystrix支持
feign:
hystrix:
enabled: true
1、microservice-student-consumer-feign-80的启动类上添加公共模块
@ComponentScan(basePackages = {"com.hmc.microservicecommon","com.hmc.microservicestudentconsumerfeign80"})
注意:
u 公共子项目与当前子项目的基包都要扫描到;
u 只指定公共子模块为基包会导致本子项目的springmvc功能失效;
u 只指定本子项目为基包会导致feign与Hystrix集成失败,从而导致服务熔断功能失效
package com.hmc.microservicestudentconsumerfeign80;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.cloud.netflix.feign.EnableFeignClients;
import org.springframework.context.annotation.ComponentScan;
@ComponentScan(basePackages = {"com.hmc.microservicecommon","com.hmc.microservicestudentconsumerfeign80"})//扫描公共模块
@EnableFeignClients(value = "com.hmc.*.*")
@EnableEurekaClient
@SpringBootApplication(exclude={DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
public class MicroserviceStudentConsumerFeign80Application {
public static void main(String[] args) {
SpringApplication.run(MicroserviceStudentConsumerFeign80Application.class, args);
}
}
测试开启三个eureka,以及带hystrix的provider,和带feign,hystrix的consummer。
测试的话,也是没问题的。0.9秒的话,返回正常信息;超过1秒的话,就返回错误提示;
`
集群后超时设置
上面错误是什么原因呢,咱们明明在Hystrix中的application.yml中设置了
hystrix:
command:
default:
execution:
isolation:
thread:
timeoutInMilliseconds: 1500
这里因为还有一个 feign 也有一个超时时间的设置,当然feign底层是 ribbon的封装,所以 直接配置ribbon,ribbon默认超时也是1秒。
所以这里都是强制要求,ribbon的超时时间要大于hystrix的超时时间,否则 hystrix自定义的超时时间毫无意义。
所以还得microservice-student-consumer-feign-80上加个 ribbon超时时间设置
ribbon:
ReadTimeout: 10000
ConnectTimeout: 9000
这样就完工了。可以自行测试。