一、代碼示例
說明:此處使用的SpringBoot版本為2.1.13.RELEASE宁脊,SpringCloud版本為Greenwich.SR5断国。
此處不再貼server與client代碼
1、maven依賴
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</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>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
</dependencies>
2榆苞、application.yml
server:
port: 9004
spring:
application:
name: ribbon-hystrix
eureka:
instance:
hostname: localhost
prefer-ip-address: true
instance-id: ribbon-hystrix-9004
client:
service-url:
defaultZone: http://eureka7001:7001/eureka/,http://eureka7002:7002/eureka/,http://eureka7003:7003/eureka/
#info信息
info:
app:
name: ribbon-hystrix-9004
company:
name: www.xxx.com
build:
artifactId: ${project.artifactId}
version: ${project.version}
3稳衬、啟動(dòng)類
啟動(dòng)類增加@EnableHystrix注解開啟Hystrix
package org.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.cloud.netflix.hystrix.EnableHystrix;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;
@SpringBootApplication
@EnableHystrix
public class RibbonHystrix9004Application {
public static void main(String[] args) {
SpringApplication.run(RibbonHystrix9004Application.class,args);
}
@Bean
@LoadBalanced
RestTemplate restTemplate(){
return new RestTemplate();
}
}
4、其他java類
Controller類
Controller類增加@HystrixCommand注解坐漏,通過fallbackMethod指定熔斷對(duì)應(yīng)的方法薄疚,fallbackMethod對(duì)應(yīng)的值為Controller類下的方法
package org.example.controller;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;
@RestController
public class RibbonHystrixController {
@Autowired
private RestTemplate restTemplate;
@GetMapping("/hello/{name}")
@HystrixCommand(fallbackMethod = "helloError")
public String hello(@PathVariable String name){
return restTemplate.getForObject("http://client/hello/"+name,String.class);
}
public String helloError(String name){
return "helloError,"+name;
}
}
2、測(cè)試驗(yàn)證
先后啟動(dòng)服務(wù)server赊琳、client與本服務(wù)街夭,訪問http://localhost:7001/
說明服務(wù)均啟動(dòng)成功。
再訪問http://localhost:9004/hello/zs
或
然后躏筏,再把client停了板丽,再次訪問
說明代碼生效。
github:
https://github.com/panli1988/cloud01
https://github.com/panli1988/cloud02
參考:
https://blog.csdn.net/forezp/article/details/70148833
http://www.itmuch.com/spring-cloud/spring-cloud-index/
還有尚硅谷周陽(yáng)老師的視頻