他のマイクロ サービスへの静的ルートを使用して Zuul を構成しました。他のサービスを呼び出すときに CircuitBreaker を有効にする方法はありますか?
1 に答える
Ribbonあなたが言ったように、Zuulはすべてのルートをand内に自動的にラップしHystrixます。Ribbonしかし、マイクロサービスとの統合やマイクロサービス間の統合も非常に簡単Hystrixです。Feignを使用して REST 呼び出しを処理することもできます。
と の2 つのサービスがserviceAあり、 と を使用して呼び出しserviceBたいとします。デフォルトのポート ( )でサーバーを実行しているとします。serviceAserviceBRibbonHystrixEurekalocalhost8761
サービスA
@SpringBootApplication
@EnableDiscoveryClient
public class ServiceAapplication{
public static void main(String[] args) {
SpringApplication.run(ServiceAapplication.class, args);
}
}
@RestController()
@RequestMapping("/rest")
class DummyRestController{
@RequestMapping(method = RequestMethod.GET, path = "/hello")
String hello(){
return "Hello World!";
}
}
サービスB
@SpringBootApplication
@EnableDiscoveryClient
@EnableCircuitBreaker
@EnableFeignClients
public class ServiceBapplication{
public static void main(String[] args) {
SpringApplication.run(ServiceBapplication.class, args);
}
}
@FeignClient(value = "serviceA") //must be same name as the service name in Eureka
@RibbonClient(name = "serviceA") //must be same name as the service name in Eureka
interface ServiceAClient {
@RequestMapping(method = RequestMethod.GET, value = "/rest/hello")
String helloFromServiceA();
}
@RestController()
@RequestMapping("/foo")
class DummyRestController{
private final ServiceAclient client;
@Autowired
DummyRestController(ServiceAclient client){
this.client = client;
}
@RequestMapping(method = RequestMethod.GET, path = "/bar")
String hello(){
return client.helloFromServiceA();
}
}
serviceB で GET を実行すると、以下foo/barが使用されます。
- ホストとポートを見つける Eureka
serviceA - の複数のインスタンス間で負荷を分散するためのリボン
serviceA - 全体が
Hystrixコマンドにラップされます
@EnableCircuitBreaker注釈により、ストリームserviceBが公開されます。Hystrixサーバーを実行している場合はHystrixDashboard、このストリームに接続できhelloFromServiceA、ダッシュボードにコマンドが表示されます。
通常の構成ファイルでRibbonとを構成するか、と注釈で別の構成クラスを使用できます。詳細はこちらHystrix@FeignClient@RibbonClient
重要:Ribbonタイムアウト中に別のインスタンスで再試行する場合は、Hystrixタイムアウトがタイムアウトよりも長いことを確認Ribbonしてください。この回答を参照してください